Skip to content

Commit

Permalink
implement bigint rand op
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Sep 22, 2012
1 parent c458a08 commit 8ae6f20
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/QAST/Operations.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ QAST::Operations.add_core_pirop_mapping('mod_i', 'mod', 'Iii', :inlinable(1));
QAST::Operations.add_core_pirop_mapping('mod_I', 'nqp_bigint_mod', 'PPPP', :inlinable(1));
QAST::Operations.add_core_pirop_mapping('expmod_I', 'nqp_bigint_exp_mod', 'PPPPP', :inlinable(1));
QAST::Operations.add_core_pirop_mapping('isprime_I', 'nqp_bigint_is_prime', 'IPi', :inlinable(1));
QAST::Operations.add_core_pirop_mapping('rand_I', 'nqp_bigint_rand', 'PPP', :inlinable(1));
QAST::Operations.add_core_pirop_mapping('mod_n', 'mod', 'Nnn', :inlinable(1));
QAST::Operations.add_core_pirop_mapping('pow_n', 'pow', 'Nnn', :inlinable(1));
QAST::Operations.add_core_pirop_mapping('pow_I', 'nqp_bigint_pow', 'PPPPP', :inlinable(1));
Expand Down
13 changes: 13 additions & 0 deletions src/ops/nqp_bigint.ops
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ inline op nqp_bigint_is_prime(out INT, invar PMC, in INT) {
}
}

/* generates a pseudo random number up to $2 of type $3 */
inline op nqp_bigint_rand(out PMC, invar PMC, invar PMC) :base_core {
mp_int *a = get_bigint(interp, $2);
mp_int *b = NULL;

$1 = REPR($3)->allocate(interp, STABLE($3));
REPR($1)->initialize(interp, STABLE($1), OBJECT_BODY($1));

b = get_bigint(interp, $1);
mp_rand(b, USED(a) + 1);
mp_mod(b, a, b);
}


inline op nqp_bigint_neg(out PMC, in PMC, in PMC) :base_core {
mp_int *a = get_bigint(interp, $2);
Expand Down

0 comments on commit 8ae6f20

Please sign in to comment.