Skip to content

Commit d78307d

Browse files
committed
bigint bitwise and, or, xor
1 parent 4715e25 commit d78307d

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/PAST/NQP.pir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,11 @@ entry to produce the node to be returned.
494494

495495
# bitwise ops
496496
maphash['bitor_i'] = 'bor__II'
497+
maphash['bitor_I'] = 'nqp_bigint_bor__PPP'
497498
maphash['bitxor_i'] = 'bxor__II'
499+
maphash['bitxor_I'] = 'nqp_bigint_bxor__PPP'
498500
maphash['bitand_i'] = 'band__II'
501+
maphash['bitand_I'] = 'nqp_bigint_band__PPP'
499502
maphash['bitneg_i'] = 'bnot__II'
500503
maphash['bitshiftl_i'] = 'shl__III'
501504
maphash['bitshiftl_I'] = 'nqp_bigint_shl__PPI'

src/ops/nqp_bigint.ops

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,25 @@ inline op nqp_bigint_shl(out PMC, in PMC, in INT) :base_core {
178178
b = get_bigint(interp, $1);
179179
mp_mul_2d(a, $3, b);
180180
}
181+
182+
inline op nqp_bigint_band(out PMC, in PMC, in PMC) :base_cor {
183+
mp_int *a = get_bigint(interp, $2);
184+
mp_int *b = get_bigint(interp, $3);
185+
$1 = REPR($2)->allocate(interp, STABLE($2));
186+
REPR($1)->initialize(interp, STABLE($1), OBJECT_BODY($1));
187+
mp_and(a, b, get_bigint(interp, $1));
188+
}
189+
inline op nqp_bigint_bor(out PMC, in PMC, in PMC) :base_cor {
190+
mp_int *a = get_bigint(interp, $2);
191+
mp_int *b = get_bigint(interp, $3);
192+
$1 = REPR($2)->allocate(interp, STABLE($2));
193+
REPR($1)->initialize(interp, STABLE($1), OBJECT_BODY($1));
194+
mp_or(a, b, get_bigint(interp, $1));
195+
}
196+
inline op nqp_bigint_bxor(out PMC, in PMC, in PMC) :base_cor {
197+
mp_int *a = get_bigint(interp, $2);
198+
mp_int *b = get_bigint(interp, $3);
199+
$1 = REPR($2)->allocate(interp, STABLE($2));
200+
REPR($1)->initialize(interp, STABLE($1), OBJECT_BODY($1));
201+
mp_xor(a, b, get_bigint(interp, $1));
202+
}

t/nqp/60-bigint.t

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#! nqp
2-
plan(12);
1+
#! nq5
2+
plan(15);
33

44
pir::nqp_bigint_setup__v();
55

@@ -8,11 +8,12 @@ my $bi_type := $knowhow.new_type(:name('TestBigInt'), :repr('P6bigint'));
88
$bi_type.HOW.compose($bi_type);
99
sub s($x) { pir::nqp_bigint_to_str__SP($x) };
1010
sub iseq($x, $y) { nqp::iseq_I($x, nqp::box_i($y, $bi_type)) }
11+
sub box($x) { nqp::box_i($x, $bi_type) }
1112

12-
my $one := nqp::box_i(1, $bi_type);
13+
my $one := box(1);
1314

1415
my $b := pir::nqp_bigint_from_str__PPS($one, '-123');
15-
my $c := nqp::box_i(-123, $bi_type);
16+
my $c := box(-123);
1617

1718
ok(s($b) eq '-123', 'can round-trip negative number (string)');
1819
ok(s($c) eq '-123', 'can round-trip negative number (string) by boxing');
@@ -23,6 +24,11 @@ ok(iseq(nqp::mul_I($b, $b), 15129,), 'multiplication');
2324
ok(iseq(nqp::add_I($b, $b), -246,), 'addition');
2425
ok(nqp::iseq_I(nqp::sub_I($b, $b), nqp::box_i(0, $bi_type)), 'subtraction');
2526
ok(nqp::iseq_I(nqp::div_I($b, $b), $one), 'division');
27+
2628
ok(iseq(nqp::bitshiftl_I($one, 3), 8), 'bitshift left');
2729
ok(iseq($one, 1), 'original not modified by bitshift left');
28-
ok(iseq(nqp::bitshiftr_I(nqp::box_i(16, $bi_type), 4), 1), 'bitshift right');
30+
ok(iseq(nqp::bitshiftr_I(box(16), 4), 1), 'bitshift right');
31+
32+
ok(iseq(nqp::bitand_I(box(0xdead), box(0xbeef)), 0x9ead), 'bit and');
33+
ok(iseq(nqp::bitor_I( box(0xdead), box(0xbeef)), 0xfeef), 'bit and');
34+
ok(iseq(nqp::bitxor_I(box(0xdead), box(0xbeef)), 0x6042), 'bit and');

0 commit comments

Comments
 (0)