diff options
author | Franziskus Kiefer <franziskuskiefer@gmail.com> | 2016-04-30 15:57:44 +0200 |
---|---|---|
committer | Franziskus Kiefer <franziskuskiefer@gmail.com> | 2016-04-30 15:57:44 +0200 |
commit | 489f6acbd9042a17e155ee7350d48248b83d8b3a (patch) | |
tree | 9bff87bd5b6c97092ff757dae47438647ebcb08a | |
parent | 330e3680e79c75496bf1dde38a237beb5224299b (diff) | |
download | nss-hg-489f6acbd9042a17e155ee7350d48248b83d8b3a.tar.gz |
Backed out changeset 794b7836c7f6 and 88a15710dd49
-rw-r--r-- | lib/freebl/mpi/mpi-config.h | 4 | ||||
-rw-r--r-- | lib/freebl/mpi/mpi-priv.h | 54 | ||||
-rw-r--r-- | lib/freebl/mpi/mpi.c | 20 |
3 files changed, 67 insertions, 11 deletions
diff --git a/lib/freebl/mpi/mpi-config.h b/lib/freebl/mpi/mpi-config.h index 171dacc7c..04a9d95b8 100644 --- a/lib/freebl/mpi/mpi-config.h +++ b/lib/freebl/mpi/mpi-config.h @@ -61,6 +61,10 @@ #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 diff --git a/lib/freebl/mpi/mpi-priv.h b/lib/freebl/mpi/mpi-priv.h index 1fd628a8d..7a0725f46 100644 --- a/lib/freebl/mpi/mpi-priv.h +++ b/lib/freebl/mpi/mpi-priv.h @@ -95,19 +95,61 @@ extern const float s_logv_2[]; /* {{{ private function declarations */ -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 */ - +/* + 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. + + 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 */ -void s_mp_clamp(mp_int *mp); /* clip leading 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_exch(mp_int *a, mp_int *b); /* swap a and b in place */ diff --git a/lib/freebl/mpi/mpi.c b/lib/freebl/mpi/mpi.c index 06b10d203..a269636a2 100644 --- a/lib/freebl/mpi/mpi.c +++ b/lib/freebl/mpi/mpi.c @@ -2776,8 +2776,9 @@ mp_err s_mp_pad(mp_int *mp, mp_size min) /* {{{ s_mp_setz(dp, count) */ +#if MP_MACRO == 0 /* Set 'count' digits pointed to by dp to be zeroes */ -__inline__ void s_mp_setz(mp_digit *dp, mp_size count) +void s_mp_setz(mp_digit *dp, mp_size count) { #if MP_MEMSET == 0 int ix; @@ -2789,13 +2790,15 @@ __inline__ void s_mp_setz(mp_digit *dp, mp_size count) #endif } /* end s_mp_setz() */ +#endif /* }}} */ /* {{{ s_mp_copy(sp, dp, count) */ +#if MP_MACRO == 0 /* Copy 'count' digits from sp to dp */ -__inline__ void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count) +void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count) { #if MP_MEMCPY == 0 int ix; @@ -2808,44 +2811,51 @@ __inline__ void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count) ++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 */ -__inline__ void *s_mp_alloc(size_t nb, size_t ni) +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 */ -__inline__ void s_mp_free(void *ptr) +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 */ -inline void s_mp_clamp(mp_int *mp) +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 /* }}} */ |