[libmath-prime-util-perl] 164/181: Push some constants into a shared header file

Partha P. Mukherjee ppm-guest at moszumanska.debian.org
Thu May 21 18:51:17 UTC 2015


This is an automated email from the git hooks/post-receive script.

ppm-guest pushed a commit to annotated tag v0.36
in repository libmath-prime-util-perl.

commit 7e26ad426b55f8d7171f3ecfcbc7dfbd874735ae
Author: Dana Jacobsen <dana at acm.org>
Date:   Sat Jan 11 15:39:10 2014 -0800

    Push some constants into a shared header file
---
 MANIFEST    |  1 +
 XS.xs       | 11 ++++-------
 cache.c     | 15 ++++++---------
 constants.h | 24 ++++++++++++++++++++++++
 4 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/MANIFEST b/MANIFEST
index a88eb7c..69b4c4a 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -22,6 +22,7 @@ aks.h
 aks.c
 cache.h
 cache.c
+constants.h
 factor.h
 factor.c
 lehmer.h
diff --git a/XS.xs b/XS.xs
index 531e415..7e170d8 100644
--- a/XS.xs
+++ b/XS.xs
@@ -19,6 +19,7 @@
 #include "lehmer.h"
 #include "lmo.h"
 #include "aks.h"
+#include "constants.h"
 
 #if BITS_PER_WORD == 64
   #if defined(_MSC_VER)
@@ -77,15 +78,11 @@
   static const unsigned int ivmax_maxlen = 10;
   static const char uvmax_str[] = "4294967295";
   static const char ivmax_str[] = "2147483648";
-  static const UV _max_prime    = UVCONST(4294967291);
-  static const UV _max_primeidx = UVCONST(203280221);
 #else
   static const unsigned int uvmax_maxlen = 20;
   static const unsigned int ivmax_maxlen = 19;
   static const char uvmax_str[] = "18446744073709551615";
   static const char ivmax_str[] =  "9223372036854775808";
-  static const UV _max_prime    = UVCONST(18446744073709551557);
-  static const UV _max_primeidx = UVCONST(425656284035217743);
 #endif
 
 #define MY_CXT_KEY "Math::Prime::Util::API_guts"
@@ -593,8 +590,8 @@ next_prime(IN SV* svn)
   PPCODE:
     if (_validate_int(aTHX_ svn, 0)) {
       UV n = my_svuv(svn);
-      if ( ((ix == 0) && (n >= _max_prime))    ||
-           ((ix == 2) && (n >= _max_primeidx)) ) {
+      if ( ((ix == 0) && (n >= MPU_MAX_PRIME))    ||
+           ((ix == 2) && (n >= MPU_MAX_PRIME_IDX)) ) {
         /* Out of range.  Fall through to Perl. */
       } else {
         UV ret;
@@ -1042,7 +1039,7 @@ forcomposites (SV* block, IN SV* svbeg, IN SV* svend = 0)
         beg = 6;
       }
       /* Find the two primes that bound their interval. */
-      /* If beg or end are >= _max_prime, then this will die. */
+      /* If beg or end are >= max_prime, then this will die. */
       prevprime = _XS_prev_prime(beg);
       nextprime = _XS_next_prime(end);
       ctx = start_segment_primes(beg, nextprime, &segment);
diff --git a/cache.c b/cache.c
index 10a1753..365a066 100644
--- a/cache.c
+++ b/cache.c
@@ -5,6 +5,7 @@
 #include "ptypes.h"
 #include "cache.h"
 #include "sieve.h"
+#include "constants.h"   /* _MPU_FILL_EXTRA_N and _MPU_INITIAL_CACHE_SIZE */
 
 #include "EXTERN.h"
 #include "perl.h"
@@ -82,18 +83,15 @@ static int mutex_init = 0;
 static unsigned char* prime_cache_sieve = 0;
 static UV             prime_cache_size = 0;
 
-/* To avoid thrashing, sieve a little farther than we absolutely need to. */
-#define FILL_EXTRA_N (128*30)
-
 /* Erase the primary cache and fill up to n. */
 /* Note: You must have a write lock before calling this! */
 static void _erase_and_fill_prime_cache(UV n) {
   UV padded_n;
 
-  if (n >= (UV_MAX-FILL_EXTRA_N))
+  if (n >= (UV_MAX-_MPU_FILL_EXTRA_N))
     padded_n = UV_MAX;
   else
-    padded_n = ((n + FILL_EXTRA_N)/30)*30;
+    padded_n = ((n + _MPU_FILL_EXTRA_N)/30)*30;
 
   /* If new size isn't larger or smaller, then we're done. */
   if (prime_cache_size == padded_n)
@@ -215,7 +213,6 @@ void release_prime_segment(unsigned char* mem) {
 
 
 
-#define INITIAL_CACHE_SIZE ((1024-16)*30 - FILL_EXTRA_N)
 void prime_precalc(UV n)
 {
   if (!mutex_init) {
@@ -225,9 +222,9 @@ void prime_precalc(UV n)
     mutex_init = 1;
   }
 
-  /* On initialization, make a few primes (2-30k using 1k memory) */
+  /* On initialization, make a few primes (30k per 1k memory) */
   if (n == 0)
-    n = INITIAL_CACHE_SIZE;
+    n = _MPU_INITIAL_CACHE_SIZE;
   get_prime_cache(n, 0);   /* Sieve to n */
 
   /* TODO: should we prealloc the segment here? */
@@ -251,7 +248,7 @@ void prime_memfree(void)
 
   WRITE_LOCK_START;
     /* Put primary cache back to initial state */
-    _erase_and_fill_prime_cache(INITIAL_CACHE_SIZE);
+    _erase_and_fill_prime_cache(_MPU_INITIAL_CACHE_SIZE);
   WRITE_LOCK_END;
 }
 
diff --git a/constants.h b/constants.h
new file mode 100644
index 0000000..0fc1e5a
--- /dev/null
+++ b/constants.h
@@ -0,0 +1,24 @@
+#ifndef MPU_CONSTANTS_H
+#define MPU_CONSTANTS_H
+
+#include "EXTERN.h"
+#include "perl.h"
+
+#if BITS_PER_WORD == 32
+ #define MPU_MAX_PRIME      UVCONST(4294967291)
+ #define MPU_MAX_PRIME_IDX  UVCONST(203280221)
+#else
+ #define MPU_MAX_PRIME      UVCONST(18446744073709551557)
+ #define MPU_MAX_PRIME_IDX  UVCONST(425656284035217743)
+#endif
+
+
+/****************************************************************************/
+/* Configuration */
+
+/* To avoid thrashing, sieve a little farther than needed */
+#define _MPU_FILL_EXTRA_N (128*30)
+/* The initial cache size.  30k primes per 1k of cache. */
+#define _MPU_INITIAL_CACHE_SIZE ((4096-16)*30 - _MPU_FILL_EXTRA_N)
+
+#endif

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git



More information about the Pkg-perl-cvs-commits mailing list