Skip to content

Commit c458a08

Browse files
committed
make Rabin-Miller prime check available as nqp::isprime_I
also special-case 1 not to be a prime
1 parent e8a7719 commit c458a08

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/QAST/Operations.nqp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,7 @@ QAST::Operations.add_core_pirop_mapping('div_n', 'div', 'Nnn', :inlinable(1));
12821282
QAST::Operations.add_core_pirop_mapping('mod_i', 'mod', 'Iii', :inlinable(1));
12831283
QAST::Operations.add_core_pirop_mapping('mod_I', 'nqp_bigint_mod', 'PPPP', :inlinable(1));
12841284
QAST::Operations.add_core_pirop_mapping('expmod_I', 'nqp_bigint_exp_mod', 'PPPPP', :inlinable(1));
1285+
QAST::Operations.add_core_pirop_mapping('isprime_I', 'nqp_bigint_is_prime', 'IPi', :inlinable(1));
12851286
QAST::Operations.add_core_pirop_mapping('mod_n', 'mod', 'Nnn', :inlinable(1));
12861287
QAST::Operations.add_core_pirop_mapping('pow_n', 'pow', 'Nnn', :inlinable(1));
12871288
QAST::Operations.add_core_pirop_mapping('pow_I', 'nqp_bigint_pow', 'PPPPP', :inlinable(1));

src/ops/nqp_bigint.ops

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ inline op nqp_bigint_exp_mod(out PMC, in PMC, in PMC, in PMC, in PMC) :base_core
186186
mp_exptmod(a, b, c, get_bigint(interp, $1));
187187
}
188188

189+
inline op nqp_bigint_is_prime(out INT, invar PMC, in INT) {
190+
/* mp_prime_is_prime returns True for 1, and I think
191+
* it's worth special-casing this particular number :-)
192+
*/
193+
mp_int *a = get_bigint(interp, $2);
194+
if (mp_cmp_d(a, 1) == MP_EQ) {
195+
$1 = 0;
196+
}
197+
else {
198+
mp_prime_is_prime(a, $3, (int *) &$1);
199+
}
200+
}
201+
189202

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

0 commit comments

Comments
 (0)