From c699cb29db99a7f1d043a402b7749001973cee25 Mon Sep 17 00:00:00 2001 From: David Leon Gil Date: Sat, 6 Sep 2014 22:05:41 -0400 Subject: [PATCH] Minor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit src/include/barrett_field.h: - Requires review: corrected failure to cast to (mask_t) prior to negation. (Or, if this is wrong; should cast to needed bitwidth explicitly.) - Changed type of nwords_out to uint32_t to agree with header. src/include/intrinsics.h: - Fixed up various preprocessor statements to check for definition rather than value of built-ins. - Added macro to use Clang’s __builtin_readcyclecounter on platforms on which it’s available. (Which is most platforms these days.) src/include/magic.h: Preprocessor “if” versus “if defined”. src/include/word.h: Fixed ifdefs; enabled support for memset_s on Darwin. Added explicit cast to mask_t. Added void to function definitions and declarations in the following files (not including void is okay in modern C++, but not modern C, IIRC): include/goldilocks.h, src/crandom.c, src/goldilocks.c, src/include/api.h, src/include/intrinsics.h, test/bench.c, test/test.c, test/test.h, test/test_arithmetic.c, test/test_goldilocks.c, test/test_pointops.c, test/test_scalarmul.c, test/test_sha512.c --- include/goldilocks.h | 2 +- src/crandom.c | 2 +- src/goldilocks.c | 4 ++-- src/include/api.h | 6 +++--- src/include/barrett_field.h | 6 +++--- src/include/intrinsics.h | 13 ++++++++++--- src/include/magic.h | 2 +- src/include/word.h | 29 ++++++++++++++++++++++------- test/bench.c | 2 +- test/test.c | 4 ++-- test/test.h | 16 ++++++++-------- test/test_arithmetic.c | 2 +- test/test_goldilocks.c | 2 +- test/test_pointops.c | 2 +- test/test_scalarmul.c | 6 +++--- test/test_sha512.c | 2 +- 16 files changed, 61 insertions(+), 39 deletions(-) diff --git a/include/goldilocks.h b/include/goldilocks.h index f012adb..a62fb6a 100644 --- a/include/goldilocks.h +++ b/include/goldilocks.h @@ -100,7 +100,7 @@ static const int GOLDI_EALREADYINIT = 44805; * @retval Nonzero An error occurred. */ int -goldilocks_init () +goldilocks_init (void) __attribute__((warn_unused_result,visibility ("default"))); diff --git a/src/crandom.c b/src/crandom.c index b9c1eb0..da0c3c9 100644 --- a/src/crandom.c +++ b/src/crandom.c @@ -14,7 +14,7 @@ volatile unsigned int crandom_features = 0; -unsigned int crandom_detect_features() { +unsigned int crandom_detect_features(void) { unsigned int out = GEN; # if (defined(__i386__) || defined(__x86_64__)) diff --git a/src/goldilocks.c b/src/goldilocks.c index 31e38d1..3016e1f 100644 --- a/src/goldilocks.c +++ b/src/goldilocks.c @@ -57,7 +57,7 @@ static struct { } goldilocks_global; static inline mask_t -goldilocks_check_init() { +goldilocks_check_init(void) { if (likely(goldilocks_global.state == G_INITED)) { return MASK_SUCCESS; } else { @@ -66,7 +66,7 @@ goldilocks_check_init() { } int -goldilocks_init () { +goldilocks_init (void) { const char *res = compare_and_swap(&goldilocks_global.state, NULL, G_INITING); if (res == G_INITED) return GOLDI_EALREADYINIT; else if (res) { diff --git a/src/include/api.h b/src/include/api.h index 7ee5e1e..2c8975a 100644 --- a/src/include/api.h +++ b/src/include/api.h @@ -43,9 +43,9 @@ #endif */ -static inline int timingattacks() { return 0; } -static inline int copyrightclaims() { return 0; } -static inline int patentclaims() { +static inline int timingattacks(void) { return 0; } +static inline int copyrightclaims(void) { return 0; } +static inline int patentclaims(void) { /* Until the end of July 2014, point compression * is patented. */ return 20; diff --git a/src/include/barrett_field.h b/src/include/barrett_field.h index 1187138..0331f72 100644 --- a/src/include/barrett_field.h +++ b/src/include/barrett_field.h @@ -37,7 +37,7 @@ extern const struct barrett_prime_t curve_prime_order; /** * Reduce a number (with optional high carry word) mod p. * - * @param [inout] a The value to be reduced. + * @param [in,out] a The value to be reduced. * @param [in] nwords_a The number of words in a. * @param [in] a_carry A high word to be carried into the computation. * @param [in] prime The Barrett prime. @@ -132,7 +132,7 @@ barrett_mul_or_mac( static inline void barrett_mul( word_t *out, - int nwords_out, + uint32_t nwords_out, const word_t *a, uint32_t nwords_a, @@ -158,7 +158,7 @@ barrett_mac( const struct barrett_prime_t *prime ) { - barrett_mul_or_mac(out,nwords_out,a,nwords_a,b,nwords_b,prime,-1); + barrett_mul_or_mac(out,nwords_out,a,nwords_a,b,nwords_b,prime,-(mask_t)1); } mask_t diff --git a/src/include/intrinsics.h b/src/include/intrinsics.h index 1b39eb5..46df9da 100644 --- a/src/include/intrinsics.h +++ b/src/include/intrinsics.h @@ -13,13 +13,13 @@ #include #include "config.h" -#if __i386__ || __x86_64__ +#if defined(__i386__) || defined(__x86_64__) #include #endif /** @brief Macro to make a function static, forcibly inlined and possibly unused. */ #define INTRINSIC \ - static __inline__ __attribute__((__gnu_inline__, __always_inline__, unused)) + static inline __attribute__((__gnu_inline__, __always_inline__)) #define GEN 1 /**< @brief Intrinsics field has been generated. */ #define SSE2 2 /**< @brief Machine supports SSE2 */ @@ -33,13 +33,20 @@ /** * @brief If on x86, read the timestamp counter. Otherwise, return 0. */ -INTRINSIC u_int64_t rdtsc() { +#ifndef __has_builtin +#define __has_builtin(X) 0 +#endif +#if defined(__clang__) && __has_builtin(__builtin_readcyclecounter) +#define rdtsc __builtin_readcyclecounter +#else +INTRINSIC u_int64_t rdtsc(void) { u_int64_t out = 0; # if (defined(__i386__) || defined(__x86_64__)) __asm__ __volatile__ ("rdtsc" : "=A"(out)); # endif return out; } +#endif /** * Return x unchanged, but confuse the compiler. diff --git a/src/include/magic.h b/src/include/magic.h index 1aac4ce..9246e40 100644 --- a/src/include/magic.h +++ b/src/include/magic.h @@ -76,7 +76,7 @@ extern const word_t SCALARMUL_FIXED_WINDOW_ADJUSTMENT[2*SCALAR_WORDS]; * @brief If true, use wider tables for the precomputed combs. */ #ifndef USE_BIG_COMBS -#if __ARM_NEON__ +#if defined(__ARM_NEON__) #define USE_BIG_COMBS 1 #else #define USE_BIG_COMBS (WORD_BITS==64) diff --git a/src/include/word.h b/src/include/word.h index 2826ee7..2015775 100644 --- a/src/include/word.h +++ b/src/include/word.h @@ -20,13 +20,16 @@ #include #include -#if __ARM_NEON__ +#if defined(__ARM_NEON__) #include -#elif __SSE2__ +#elif defined(__SSE2__) #include #endif -#if (__SIZEOF_INT128__ == 16 && __SIZEOF_SIZE_T__ == 8 && (__SIZEOF_LONG__==8 || __POINTER_WIDTH__==64) && !GOLDI_FORCE_32_BIT) +#if (__SIZEOF_INT128__ == 16 \ + && __SIZEOF_SIZE_T__ == 8 \ + && (__SIZEOF_LONG__==8 || __POINTER_WIDTH__==64) \ + && !defined(GOLDI_FORCE_32_BIT)) /* It's a 64-bit machine if: * __uint128_t exists * size_t is 64 bits @@ -67,7 +70,7 @@ typedef int64_t dsword_t; #define WORDS_FOR_BITS(_x) (DIV_CEIL((_x),WORD_BITS)) typedef word_t mask_t; -static const mask_t MASK_FAILURE = 0, MASK_SUCCESS = -1; +static const mask_t MASK_FAILURE = 0, MASK_SUCCESS = -(mask_t)1; @@ -106,7 +109,7 @@ typedef word_t vecmask_t __attribute__((vector_size(32))); static __inline__ big_register_t br_set_to_mask(mask_t x) { - uint32_t y = x; + uint32_t y = (uint32_t)x; big_register_t ret = {y,y,y,y,y,y,y,y}; return ret; } @@ -193,10 +196,22 @@ letoh64 (uint64_t x) { return x; } * @param c The char to set it to (probably zero). * @param s The size of the object. */ -#ifdef __STDC_LIB_EXT1__ /* which it won't be, because we're -std=c99 */ +#if (defined(__DARWIN_C_LEVEL) \ + || (defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1)) +#define HAS_MEMSET_S +#endif + +#if !defined(__STDC_WANT_LIB_EXT1__) || __STDC_WANT_LIB_EXT1__ != 1 +#define NEED_MEMSET_S_EXTERN +#endif + +#ifdef HAS_MEMSET_S +#ifdef NEED_MEMSET_S_EXTERN +extern int memset_s(void *, size_t, int, size_t); +#endif static __inline__ void really_memset(void *p, char c, size_t s) { - memset_s(p,s,c,s); + memset_s(p, s, c, s); } #else static __inline__ void __attribute__((always_inline,unused)) diff --git a/test/bench.c b/test/bench.c index dfa433b..beff4b4 100644 --- a/test/bench.c +++ b/test/bench.c @@ -22,7 +22,7 @@ ignore_result ( int result ) { (void)result; } -static double now() { +static double now(void) { struct timeval tv; gettimeofday(&tv, NULL); diff --git a/test/test.c b/test/test.c index f905f83..322ef95 100644 --- a/test/test.c +++ b/test/test.c @@ -9,7 +9,7 @@ int failed_tests, n_tests, failed_this_test, running_a_test; -static void end_test() { +static void end_test(void) { if (!failed_this_test) { printf("[PASS]\n"); } @@ -25,7 +25,7 @@ static void begin_test(const char *name) { running_a_test = 1; } -void youfail() { +void youfail(void) { if (failed_this_test) return; failed_this_test = 1; failed_tests ++; diff --git a/test/test.h b/test/test.h index 91d6c3f..e41f578 100644 --- a/test/test.h +++ b/test/test.h @@ -29,20 +29,20 @@ void scalar_print ( int nwords ); -void youfail(); +void youfail(void); -int test_sha512_monte_carlo(); +int test_sha512_monte_carlo(void); -int test_linear_combo (); +int test_linear_combo (void); -int test_scalarmul_compatibility (); +int test_scalarmul_compatibility (void); -int test_scalarmul_commutativity (); +int test_scalarmul_commutativity (void); -int test_arithmetic (); +int test_arithmetic (void); -int test_goldilocks (); +int test_goldilocks (void); -int test_pointops (); +int test_pointops (void); #endif // __GOLDILOCKS_TEST_H__ diff --git a/test/test_arithmetic.c b/test/test_arithmetic.c index 51a646c..b2064c2 100644 --- a/test/test_arithmetic.c +++ b/test/test_arithmetic.c @@ -148,7 +148,7 @@ static mask_t test_mul_sqr ( return succ; } -int test_arithmetic () { +int test_arithmetic (void) { int j, ntests = 100000; gmp_randstate_t state; diff --git a/test/test_goldilocks.c b/test/test_goldilocks.c index 460a26d..d017fe7 100644 --- a/test/test_goldilocks.c +++ b/test/test_goldilocks.c @@ -4,7 +4,7 @@ #include #include -int test_goldilocks () { +int test_goldilocks (void) { const char *message1 = "hello world"; const char *message2 = "Jello world"; diff --git a/test/test_pointops.c b/test/test_pointops.c index 6dfdab7..d19bf58 100644 --- a/test/test_pointops.c +++ b/test/test_pointops.c @@ -249,7 +249,7 @@ single_twisting_test ( return succ ? 0 : -1; } -int test_pointops () { +int test_pointops (void) { struct affine_t base, pbase; struct p448_t ser448; diff --git a/test/test_scalarmul.c b/test/test_scalarmul.c index 5627b64..661a1d7 100644 --- a/test/test_scalarmul.c +++ b/test/test_scalarmul.c @@ -274,7 +274,7 @@ single_scalarmul_commutativity_test ( } } -int test_scalarmul_commutativity () { +int test_scalarmul_commutativity (void) { int i,j,k,got; struct crandom_state_t crand; @@ -312,7 +312,7 @@ int test_scalarmul_commutativity () { return 0; } -int test_linear_combo () { +int test_linear_combo (void) { int i,j,k,got; struct crandom_state_t crand; @@ -355,7 +355,7 @@ int test_linear_combo () { return 0; } -int test_scalarmul_compatibility () { +int test_scalarmul_compatibility (void) { int i,j,k,got; struct crandom_state_t crand; diff --git a/test/test_sha512.c b/test/test_sha512.c index e5c409f..081ddf3 100644 --- a/test/test_sha512.c +++ b/test/test_sha512.c @@ -59,7 +59,7 @@ static int sha512_monte_carlo_core ( return 0; } -int test_sha512_monte_carlo() { +int test_sha512_monte_carlo(void) { const char *seed = "5c337de5caf35d18ed90b5cddfce001ca1b8ee8602f367e7c24ccca6f893802f" "b1aca7a3dae32dcd60800a59959bc540d63237876b799229ae71a2526fbc52cd";