Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a few ops to check out some basic arithmetic functions; seems to …
…work.
  • Loading branch information
jnthn committed Oct 28, 2011
1 parent 9491f38 commit 2dcba38
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/ops/nqp_bigint.ops
Expand Up @@ -7,11 +7,46 @@ BEGIN_OPS_PREAMBLE
#include "../6model/sixmodelobject.h"
#include "../6model/reprs/P6bigint.h"

static mp_int * get_bigint(PARROT_INTERP, PMC *obj) {
/* XXX sanity checks... */
return &((P6bigintInstance *)PMC_data(obj))->i;
}

END_OPS_PREAMBLE

/*
* The ops in here mostly just delegate off to libtommath. nqp_bigint_setup must be
* called before you create any types using the bigint representation. For all of the
* rest ops there are two variants. The first variant simply takes two objects and
* assumes they directly use the P6bigint REPR; the second one pulls out an (inlined)
* P6bigint attribute and works on that; this avoids the overhead of creating a boxed
* version only to throw it away again.
*/

inline op nqp_bigint_setup() :base_core {
/* Register the bigint representation. */
REGISTER_DYNAMIC_REPR(interp,
Parrot_str_new_constant(interp, "P6bigint"),
P6bigint_initialize);
}

inline op nqp_bigint_add(out PMC, in PMC, in PMC) :base_core {
mp_int *a = get_bigint(interp, $2);
mp_int *b = get_bigint(interp, $3);
$1 = REPR($2)->instance_of(interp, $2);
mp_add(a, b, get_bigint(interp, $1));
}

inline op nqp_bigint_sub(out PMC, in PMC, in PMC) :base_core {
mp_int *a = get_bigint(interp, $2);
mp_int *b = get_bigint(interp, $3);
$1 = REPR($2)->instance_of(interp, $2);
mp_sub(a, b, get_bigint(interp, $1));
}

inline op nqp_bigint_mul(out PMC, in PMC, in PMC) :base_core {
mp_int *a = get_bigint(interp, $2);
mp_int *b = get_bigint(interp, $3);
$1 = REPR($2)->instance_of(interp, $2);
mp_mul(a, b, get_bigint(interp, $1));
}

0 comments on commit 2dcba38

Please sign in to comment.