Skip to content

Commit 2dcba38

Browse files
committed
Add a few ops to check out some basic arithmetic functions; seems to work.
1 parent 9491f38 commit 2dcba38

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/ops/nqp_bigint.ops

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,46 @@ BEGIN_OPS_PREAMBLE
77
#include "../6model/sixmodelobject.h"
88
#include "../6model/reprs/P6bigint.h"
99

10+
static mp_int * get_bigint(PARROT_INTERP, PMC *obj) {
11+
/* XXX sanity checks... */
12+
return &((P6bigintInstance *)PMC_data(obj))->i;
13+
}
14+
1015
END_OPS_PREAMBLE
1116

17+
/*
18+
* The ops in here mostly just delegate off to libtommath. nqp_bigint_setup must be
19+
* called before you create any types using the bigint representation. For all of the
20+
* rest ops there are two variants. The first variant simply takes two objects and
21+
* assumes they directly use the P6bigint REPR; the second one pulls out an (inlined)
22+
* P6bigint attribute and works on that; this avoids the overhead of creating a boxed
23+
* version only to throw it away again.
24+
*/
25+
1226
inline op nqp_bigint_setup() :base_core {
1327
/* Register the bigint representation. */
1428
REGISTER_DYNAMIC_REPR(interp,
1529
Parrot_str_new_constant(interp, "P6bigint"),
1630
P6bigint_initialize);
1731
}
32+
33+
inline op nqp_bigint_add(out PMC, in PMC, in PMC) :base_core {
34+
mp_int *a = get_bigint(interp, $2);
35+
mp_int *b = get_bigint(interp, $3);
36+
$1 = REPR($2)->instance_of(interp, $2);
37+
mp_add(a, b, get_bigint(interp, $1));
38+
}
39+
40+
inline op nqp_bigint_sub(out PMC, in PMC, in PMC) :base_core {
41+
mp_int *a = get_bigint(interp, $2);
42+
mp_int *b = get_bigint(interp, $3);
43+
$1 = REPR($2)->instance_of(interp, $2);
44+
mp_sub(a, b, get_bigint(interp, $1));
45+
}
46+
47+
inline op nqp_bigint_mul(out PMC, in PMC, in PMC) :base_core {
48+
mp_int *a = get_bigint(interp, $2);
49+
mp_int *b = get_bigint(interp, $3);
50+
$1 = REPR($2)->instance_of(interp, $2);
51+
mp_mul(a, b, get_bigint(interp, $1));
52+
}

0 commit comments

Comments
 (0)