diff libinterp/corefcn/__isprimelarge__.cc @ 31055:3a93a77dd4cf stable

Minor performance tweaks to isprime.m and __isprimelarge__.cc __isprimelarge__.cc: change an if-else ladder to a single equivalent return statement, about 1% faster and less code. isprime.m: periodic check and retune of internal threshold parameter used to select one technique over the other.
author Arun Giridhar <arungiridhar@gmail.com>
date Wed, 01 Jun 2022 17:19:30 -0400
parents cfade47fc4fc
children 5f015f2829b7
line wrap: on
line diff
--- a/libinterp/corefcn/__isprimelarge__.cc	Tue May 31 23:30:40 2022 -0400
+++ b/libinterp/corefcn/__isprimelarge__.cc	Wed Jun 01 17:19:30 2022 -0400
@@ -115,20 +115,12 @@
   // If the number passes all 12 tests, then it is prime.
   // If it fails any, then it is composite.
   // The first 12 primes suffice to test all 64-bit integers.
-  if (! millerrabin ( 2, d, r, n))  return false;
-  if (! millerrabin ( 3, d, r, n))  return false;
-  if (! millerrabin ( 5, d, r, n))  return false;
-  if (! millerrabin ( 7, d, r, n))  return false;
-  if (! millerrabin (11, d, r, n))  return false;
-  if (! millerrabin (13, d, r, n))  return false;
-  if (! millerrabin (17, d, r, n))  return false;
-  if (! millerrabin (19, d, r, n))  return false;
-  if (! millerrabin (23, d, r, n))  return false;
-  if (! millerrabin (29, d, r, n))  return false;
-  if (! millerrabin (31, d, r, n))  return false;
-  if (! millerrabin (37, d, r, n))  return false;
-  // If we are all the way here, then it is prime.
-  return true;
+  return millerrabin ( 2, d, r, n) && millerrabin ( 3, d, r, n)
+      && millerrabin ( 5, d, r, n) && millerrabin ( 7, d, r, n)
+      && millerrabin (11, d, r, n) && millerrabin (13, d, r, n)
+      && millerrabin (17, d, r, n) && millerrabin (19, d, r, n)
+      && millerrabin (23, d, r, n) && millerrabin (29, d, r, n)
+      && millerrabin (31, d, r, n) && millerrabin (37, d, r, n);
 
   /*
   Mathematical references for the curious as to why we need only