Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bigint bit shift ops
  • Loading branch information
moritz committed Nov 6, 2011
1 parent 45d5eed commit 4715e25
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/PAST/NQP.pir
Expand Up @@ -498,7 +498,9 @@ entry to produce the node to be returned.
maphash['bitand_i'] = 'band__II'
maphash['bitneg_i'] = 'bnot__II'
maphash['bitshiftl_i'] = 'shl__III'
maphash['bitshiftl_I'] = 'nqp_bigint_shl__PPI'
maphash['bitshiftr_i'] = 'shr__III'
maphash['bitshiftr_I'] = 'nqp_bigint_shr__PPI'

maphash['bitor_s'] = 'bors__SS'
maphash['bitxor_s'] = 'bxors__SS'
Expand Down
16 changes: 16 additions & 0 deletions src/ops/nqp_bigint.ops
Expand Up @@ -162,3 +162,19 @@ inline op nqp_bigint_to_str(out STR, in PMC) :base_core {
$1 = Parrot_str_new(interp, buf, len - 1);
mem_sys_free(buf);
}

inline op nqp_bigint_shr(out PMC, in PMC, in INT) :base_core {
mp_int *a = get_bigint(interp, $2);
$1 = REPR($2)->allocate(interp, STABLE($2));
REPR($1)->initialize(interp, STABLE($1), OBJECT_BODY($1));
mp_div_2d(a, $3, get_bigint(interp, $1), NULL);
}

inline op nqp_bigint_shl(out PMC, in PMC, in INT) :base_core {
mp_int *b;
mp_int *a = get_bigint(interp, $2);
$1 = REPR($2)->allocate(interp, STABLE($2));
REPR($1)->initialize(interp, STABLE($1), OBJECT_BODY($1));
b = get_bigint(interp, $1);
mp_mul_2d(a, $3, b);
}
5 changes: 4 additions & 1 deletion t/nqp/60-bigint.t
@@ -1,5 +1,5 @@
#! nqp
plan(9);
plan(12);

pir::nqp_bigint_setup__v();

Expand All @@ -23,3 +23,6 @@ ok(iseq(nqp::mul_I($b, $b), 15129,), 'multiplication');
ok(iseq(nqp::add_I($b, $b), -246,), 'addition');
ok(nqp::iseq_I(nqp::sub_I($b, $b), nqp::box_i(0, $bi_type)), 'subtraction');
ok(nqp::iseq_I(nqp::div_I($b, $b), $one), 'division');
ok(iseq(nqp::bitshiftl_I($one, 3), 8), 'bitshift left');
ok(iseq($one, 1), 'original not modified by bitshift left');
ok(iseq(nqp::bitshiftr_I(nqp::box_i(16, $bi_type), 4), 1), 'bitshift right');

0 comments on commit 4715e25

Please sign in to comment.