Bug 1259052 - Remove MP_MACRO from libmpi r=franziskus,mt
authorTim Taubert <ttaubert@mozilla.com>
Wed, 04 May 2016 12:27:23 +0200
changeset 12147 9f350ea29cc8da413d9a0ce0fae65a3d6c09877a
parent 12146 9115ef8116f47cef0d748fd4917a19d51b6e2f09
child 12148 f69a7e4926119cac1cad93c1fb38bec3939d8a51
push id1200
push userttaubert@mozilla.com
push dateWed, 04 May 2016 10:29:19 +0000
reviewersfranziskus, mt
bugs1259052
Bug 1259052 - Remove MP_MACRO from libmpi r=franziskus,mt
lib/freebl/mpi/mpi-config.h
lib/freebl/mpi/mpi-priv.h
lib/freebl/mpi/mpi.c
--- a/lib/freebl/mpi/mpi-config.h
+++ b/lib/freebl/mpi/mpi-config.h
@@ -56,19 +56,15 @@
 #ifndef MP_DEBUG
 #define MP_DEBUG      0  /* print diagnostic output?            */
 #endif
 
 #ifndef MP_DEFPREC
 #define MP_DEFPREC    64 /* default precision, in digits        */
 #endif
 
-#ifndef MP_MACRO
-#define MP_MACRO      1  /* use macros for frequent calls?      */
-#endif
-
 #ifndef MP_SQUARE
 #define MP_SQUARE     1  /* use separate squaring code?         */
 #endif
 
 #endif /* ifndef MPI_CONFIG_H_ */
 
 
--- a/lib/freebl/mpi/mpi-priv.h
+++ b/lib/freebl/mpi/mpi-priv.h
@@ -90,71 +90,29 @@ extern const float s_logv_2[];
 #define  MP_LT       -1
 #define  MP_EQ        0
 #define  MP_GT        1
 
 /* }}} */
 
 /* {{{ private function declarations */
 
-/* 
-   If MP_MACRO is false, these will be defined as actual functions;
-   otherwise, suitable macro definitions will be used.  This works
-   around the fact that ANSI C89 doesn't support an 'inline' keyword
-   (although I hear C9x will ... about bloody time).  At present, the
-   macro definitions are identical to the function bodies, but they'll
-   expand in place, instead of generating a function call.
+void     s_mp_setz(mp_digit *dp, mp_size count); /* zero digits           */
+void     s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count); /* copy */
+void    *s_mp_alloc(size_t nb, size_t ni);       /* general allocator     */
+void     s_mp_free(void *ptr);                   /* general free function */
 
-   I chose these particular functions to be made into macros because
-   some profiling showed they are called a lot on a typical workload,
-   and yet they are primarily housekeeping.
- */
-#if MP_MACRO == 0
- void     s_mp_setz(mp_digit *dp, mp_size count); /* zero digits           */
- void     s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count); /* copy */
- void    *s_mp_alloc(size_t nb, size_t ni);       /* general allocator     */
- void     s_mp_free(void *ptr);                   /* general free function */
 extern unsigned long mp_allocs;
 extern unsigned long mp_frees;
 extern unsigned long mp_copies;
-#else
-
- /* Even if these are defined as macros, we need to respect the settings
-    of the MP_MEMSET and MP_MEMCPY configuration options...
-  */
- #if MP_MEMSET == 0
-  #define  s_mp_setz(dp, count) \
-       {int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=0;}
- #else
-  #define  s_mp_setz(dp, count) memset(dp, 0, (count) * sizeof(mp_digit))
- #endif /* MP_MEMSET */
-
- #if MP_MEMCPY == 0
-  #define  s_mp_copy(sp, dp, count) \
-       {int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=(sp)[ix];}
- #else
-  #define  s_mp_copy(sp, dp, count) memcpy(dp, sp, (count) * sizeof(mp_digit))
- #endif /* MP_MEMCPY */
-
- #define  s_mp_alloc(nb, ni)  calloc(nb, ni)
- #define  s_mp_free(ptr) {if(ptr) free(ptr);}
-#endif /* MP_MACRO */
 
 mp_err   s_mp_grow(mp_int *mp, mp_size min);   /* increase allocated size */
 mp_err   s_mp_pad(mp_int *mp, mp_size min);    /* left pad with zeroes    */
 
-#if MP_MACRO == 0
- void     s_mp_clamp(mp_int *mp);               /* clip leading zeroes     */
-#else
- #define  s_mp_clamp(mp)\
-  { mp_size used = MP_USED(mp); \
-    while (used > 1 && DIGIT(mp, used - 1) == 0) --used; \
-    MP_USED(mp) = used; \
-  } 
-#endif /* MP_MACRO */
+void     s_mp_clamp(mp_int *mp);               /* clip leading zeroes     */
 
 void     s_mp_exch(mp_int *a, mp_int *b);      /* swap a and b in place   */
 
 mp_err   s_mp_lshd(mp_int *mp, mp_size p);     /* left-shift by p digits  */
 void     s_mp_rshd(mp_int *mp, mp_size p);     /* right-shift by p digits */
 mp_err   s_mp_mul_2d(mp_int *mp, mp_digit d);  /* multiply by 2^d in place */
 void     s_mp_div_2d(mp_int *mp, mp_digit d);  /* divide by 2^d in place  */
 void     s_mp_mod_2d(mp_int *mp, mp_digit d);  /* modulo 2^d in place     */
--- a/lib/freebl/mpi/mpi.c
+++ b/lib/freebl/mpi/mpi.c
@@ -14,16 +14,20 @@
 
 #if defined(__arm__) && \
     ((defined(__thumb__) && !defined(__thumb2__)) || defined(__ARM_ARCH_3__))
 /* 16-bit thumb or ARM v3 doesn't work inlined assember version */
 #undef MP_ASSEMBLY_MULTIPLY
 #undef MP_ASSEMBLY_SQUARE
 #endif
 
+#if defined(_MSC_VER) && _MSC_VER < 1900
+#define inline __inline
+#endif
+
 #if MP_LOGTAB
 /*
   A table of the logs of 2 for various bases (the 0 and 1 entries of
   this table are meaningless and should not be referenced).  
 
   This table is used to compute output lengths for the mp_toradix()
   function.  Since a number n in radix r takes up about log_r(n)
   digits, we estimate the output size by taking the least integer
@@ -2771,96 +2775,86 @@ mp_err   s_mp_pad(mp_int *mp, mp_size mi
   return MP_OKAY;
 
 } /* end s_mp_pad() */
 
 /* }}} */
 
 /* {{{ s_mp_setz(dp, count) */
 
-#if MP_MACRO == 0
 /* Set 'count' digits pointed to by dp to be zeroes                       */
-void s_mp_setz(mp_digit *dp, mp_size count)
+inline void s_mp_setz(mp_digit *dp, mp_size count)
 {
 #if MP_MEMSET == 0
   int  ix;
 
   for(ix = 0; ix < count; ix++)
     dp[ix] = 0;
 #else
   memset(dp, 0, count * sizeof(mp_digit));
 #endif
 
 } /* end s_mp_setz() */
-#endif
 
 /* }}} */
 
 /* {{{ s_mp_copy(sp, dp, count) */
 
-#if MP_MACRO == 0
 /* Copy 'count' digits from sp to dp                                      */
-void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count)
+inline void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count)
 {
 #if MP_MEMCPY == 0
   int  ix;
 
   for(ix = 0; ix < count; ix++)
     dp[ix] = sp[ix];
 #else
   memcpy(dp, sp, count * sizeof(mp_digit));
 #endif
   ++mp_copies;
 
 } /* end s_mp_copy() */
-#endif
 
 /* }}} */
 
 /* {{{ s_mp_alloc(nb, ni) */
 
-#if MP_MACRO == 0
 /* Allocate ni records of nb bytes each, and return a pointer to that     */
-void    *s_mp_alloc(size_t nb, size_t ni)
+inline void *s_mp_alloc(size_t nb, size_t ni)
 {
   ++mp_allocs;
   return calloc(nb, ni);
 
 } /* end s_mp_alloc() */
-#endif
 
 /* }}} */
 
 /* {{{ s_mp_free(ptr) */
 
-#if MP_MACRO == 0
 /* Free the memory pointed to by ptr                                      */
-void     s_mp_free(void *ptr)
+inline void s_mp_free(void *ptr)
 {
   if(ptr) {
     ++mp_frees;
     free(ptr);
   }
 } /* end s_mp_free() */
-#endif
 
 /* }}} */
 
 /* {{{ s_mp_clamp(mp) */
 
-#if MP_MACRO == 0
 /* Remove leading zeroes from the given value                             */
-void     s_mp_clamp(mp_int *mp)
+inline void s_mp_clamp(mp_int *mp)
 {
   mp_size used = MP_USED(mp);
   while (used > 1 && DIGIT(mp, used - 1) == 0)
     --used;
   MP_USED(mp) = used;
 } /* end s_mp_clamp() */
-#endif
 
 /* }}} */
 
 /* {{{ s_mp_exch(a, b) */
 
 /* Exchange the data for a and b; (b, a) = (a, b)                         */
 void     s_mp_exch(mp_int *a, mp_int *b)
 {