(wrong string) ée

From: <git_AT_suckless.org>
Date: Wed, 27 Apr 2016 02:17:19 +0200 (CEST)

commit d2ec8006ca45753b6a404cd7ccbf7092d876c660
Author: Mattias Andrée <maandree_AT_kth.se>
AuthorDate: Wed Apr 27 02:16:48 2016 +0200
Commit: Mattias Andrée <maandree_AT_kth.se>
CommitDate: Wed Apr 27 02:16:48 2016 +0200

    Make benchmark against libtommath more fair: enable error checking
    
    Signed-off-by: Mattias Andrée <maandree_AT_kth.se>

diff --git a/bench/libtommath.h b/bench/libtommath.h
index ddc34ac..af38977 100644
--- a/bench/libtommath.h
+++ b/bench/libtommath.h
_AT_@ -1,25 +1,43 @@
 #include <tommath.h>
 
-#include <stddef.h>
 #include <setjmp.h>
+#include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
 
 #define BIGINT_LIBRARY "libtommath"
 
+#define FAST_RANDOM 0
+#define SECURE_RANDOM 0
+#define DEFAULT_RANDOM 0
+#define FASTEST_RANDOM 0
+#define LIBC_RAND_RANDOM 0
+#define LIBC_RANDOM_RANDOM 0
+#define LIBC_RAND48_RANDOM 0
+#define QUASIUNIFORM 0
+#define UNIFORM 1
+#define MODUNIFORM 2
+
 typedef mp_int z_t[1];
 
 static z_t _0, _1, _a, _b;
-static int _tmp;
+static int _tmp, error;
+static jmp_buf jbuf;
+
+#ifdef UNSAFE
+# define try(expr) (expr)
+#else
+# define try(expr) do if ((error = (expr))) longjmp(jbuf, 1); while (0)
+#endif
 
 static void
 zsetup(jmp_buf env)
 {
- (void) env;
- mp_init_set_int(_0, 0);
- mp_init_set_int(_1, 1);
- mp_init(_a);
- mp_init(_b);
+ *jbuf = *env;
+ try(mp_init_set_int(_0, 0));
+ try(mp_init_set_int(_1, 1));
+ try(mp_init(_a));
+ try(mp_init(_b));
 }
 
 static void
_AT_@ -31,117 +49,138 @@ zunsetup(void)
         mp_clear(_b);
 }
 
-#define FAST_RANDOM 0
-#define SECURE_RANDOM 0
-#define DEFAULT_RANDOM 0
-#define FASTEST_RANDOM 0
-#define LIBC_RAND_RANDOM 0
-#define LIBC_RANDOM_RANDOM 0
-#define LIBC_RAND48_RANDOM 0
-#define QUASIUNIFORM 0
-#define UNIFORM 1
-#define MODUNIFORM 2
-
-#define zperror(x) ((void)0)
-#define zinit(a) mp_init(a)
-#define zfree(a) mp_clear(a)
-
-#define zset(r, a) mp_copy(a, r)
-#define zneg(r, a) mp_neg(a, r)
-#define zabs(r, a) mp_abs(a, r)
-#define zadd_unsigned(r, a, b) (mp_abs(a, _a), mp_abs(b, _b), mp_add(_a, _b, r))
-#define zsub_unsigned(r, a, b) (mp_abs(a, _a), mp_abs(b, _b), mp_sub(_a, _b, r))
-#define zadd(r, a, b) mp_add(a, b, r)
-#define zsub(r, a, b) mp_sub(a, b, r)
-#define zand(r, a, b) mp_and(a, b, r)
-#define zor(r, a, b) mp_or(a, b, r)
-#define zxor(r, a, b) mp_xor(a, b, r)
-#define zeven(a) mp_iseven(a)
-#define zodd(a) mp_isodd(a)
-#define zeven_nonzero(a) mp_iseven(a)
-#define zodd_nonzero(a) mp_isodd(a)
-#define zzero(a) mp_iszero(a)
-#define zsignum(a) mp_cmp(a, _0)
-#define zbits(a) mp_count_bits(a)
-#define zlsb(a) mp_cnt_lsb(a)
-#define zswap(a, b) mp_exch(a, b)
-#define zlsh(r, a, b) mp_mul_2d(a, b, r)
-#define zrsh(r, a, b) mp_div_2d(a, b, r, 0)
-#define ztrunc(r, a, b) mp_mod_2d(a, b, r)
-#define zcmpmag(a, b) mp_cmp_mag(a, b)
-#define zcmp(a, b) mp_cmp(a, b)
-#define zcmpi(a, b) (zseti(_b, b), mp_cmp(a, _b))
-#define zcmpu(a, b) (zsetu(_b, b), mp_cmp(a, _b))
-#define zgcd(r, a, b) mp_gcd(a, b, r)
-#define zmul(r, a, b) mp_mul(a, b, r)
-#define zsqr(r, a) mp_sqr(a, r)
-#define zmodmul(r, a, b, m) mp_mulmod(a, b, m, r)
-#define zmodsqr(r, a, m) mp_sqrmod(a, m, r)
-#define zpow(r, a, b) mp_expt_d(a, (mp_digit)mp_get_int(b), r)
-#define zpowu(r, a, b) mp_expt_d(a, b, r)
-#define zmodpow(r, a, b, m) mp_exptmod(a, b, m, r)
-#define zmodpowu(r, a, b, m) (mp_set_int(_b, b), mp_exptmod(a, _b, m, r))
-#define zsets(a, s) mp_read_radix(a, s, 10)
-#define zstr_length(a, b) (mp_radix_size(a, b, &_tmp), _tmp)
-#define zstr(a, s, n) (mp_toradix(a, s, 10))
-#define zptest(w, a, t) (mp_prime_is_prime(a, t, &_tmp), _tmp) /* Note, the witness is not returned. */
-#define zload(a, s) mp_read_signed_bin(a, (unsigned char *)s, _tmp)
-#define zdiv(r, a, b) mp_div(a, b, r, 0)
-#define zmod(r, a, b) mp_mod(a, b, r)
-#define zdivmod(q, r, a, b) mp_div(a, b, q, r)
+static inline void
+zperror(const char *str)
+{
+ if (str && *str)
+ fprintf(stderr, "%s: %s\n", str, mp_error_to_string(error));
+ else
+ fprintf(stderr, "%s\n", mp_error_to_string(error));
+}
 
-static int
-zsave(z_t a, char *buf)
+static inline void
+zinit(z_t a)
 {
- _tmp = buf ? mp_signed_bin_size(a) : mp_to_signed_bin(a, (unsigned char *)buf);
- return _tmp;
+ try(mp_init(a));
 }
 
-static void
-zsetu(z_t r, unsigned long long int val)
+static inline void
+zfree(z_t a)
 {
- uint32_t high = (uint32_t)(val >> 32);
- uint32_t low = (uint32_t)val;
+ mp_clear(a);
+}
 
- if (high) {
- mp_set_int(r, high);
- mp_set_int(_a, low);
- mp_lshd(r, 32);
- zadd(r, r, _a);
- } else {
- mp_set_int(r, low);
- }
-
+static inline void
+zset(z_t r, z_t a)
+{
+ try(mp_copy(a, r));
 }
 
-static void
-zseti(z_t r, long long int val)
+static inline void
+zneg(z_t r, z_t a)
 {
- if (val >= 0) {
- zsetu(r, (unsigned long long int)val);
- } else {
- zsetu(r, (unsigned long long int)-val);
- zneg(r, r);
- }
+ try(mp_neg(a, r));
 }
 
-static void
-zsplit(z_t high, z_t low, z_t a, size_t brk)
+static inline void
+zabs(z_t r, z_t a)
 {
- if (low == a) {
- zrsh(high, a, brk);
- ztrunc(low, a, brk);
- } else {
- ztrunc(low, a, brk);
- zrsh(high, a, brk);
- }
+ try(mp_abs(a, r));
+}
+
+static inline void
+zadd(z_t r, z_t a, z_t b)
+{
+ try(mp_add(a, b, r));
+}
+
+static inline void
+zsub(z_t r, z_t a, z_t b)
+{
+ try(mp_sub(a, b, r));
+}
+
+static inline void
+zadd_unsigned(z_t r, z_t a, z_t b)
+{
+ zabs(_a, a);
+ zabs(_b, b);
+ zadd(r, _a, _b);
+}
+
+static inline void
+zsub_unsigned(z_t r, z_t a, z_t b)
+{
+ zabs(_a, a);
+ zabs(_b, b);
+ zsub(r, _a, _b);
+}
+
+static inline size_t
+zbits(z_t a)
+{
+ return mp_count_bits(a);
+}
+
+static inline size_t
+zlsb(z_t a)
+{
+ return mp_cnt_lsb(a);
+}
+
+static inline int
+zeven(z_t a)
+{
+ return mp_iseven(a);
+}
+
+static inline int
+zodd(z_t a)
+{
+ return mp_isodd(a);
+}
+
+static inline int
+zeven_nonzero(z_t a)
+{
+ return zeven(a);
+}
+
+static inline int
+zodd_nonzero(z_t a)
+{
+ return zodd(a);
+}
+
+static inline int
+zzero(z_t a)
+{
+ return mp_iszero(a);
+}
+
+static inline void
+zand(z_t r, z_t a, z_t b)
+{
+ try(mp_and(a, b, r));
+}
+
+static inline void
+zor(z_t r, z_t a, z_t b)
+{
+ try(mp_or(a, b, r));
+}
+
+static inline void
+zxor(z_t r, z_t a, z_t b)
+{
+ try(mp_xor(a, b, r));
 }
 
 static void
 znot(z_t r, z_t a)
 {
- mp_2expt(_a, (int)zbits(a));
- mp_sub_d(_a, 1, _a);
+ try(mp_2expt(_a, (int)zbits(a)));
+ try(mp_sub_d(_a, 1, _a));
         zand(r, a, _a);
         zneg(r, r);
 }
_AT_@ -149,7 +188,7 @@ znot(z_t r, z_t a)
 static int
 zbtest(z_t a, size_t bit)
 {
- mp_2expt(_b, (int)bit);
+ try(mp_2expt(_b, (int)bit));
         zand(_b, a, _b);
         return !zzero(_b);
 }
_AT_@ -158,14 +197,214 @@ static void
 zbset(z_t r, z_t a, size_t bit, int mode)
 {
         if (mode > 0) {
- mp_2expt(_b, (int)bit);
+ try(mp_2expt(_b, (int)bit));
                 zor(r, a, _b);
         } else if (mode < 0 || zbtest(a, bit)) {
- mp_2expt(_b, (int)bit);
+ try(mp_2expt(_b, (int)bit));
                 zxor(r, a, _b);
         }
 }
 
+static inline void
+zswap(z_t a, z_t b)
+{
+ mp_exch(a, b);
+}
+
+static inline void
+zlsh(z_t r, z_t a, size_t b)
+{
+ try(mp_mul_2d(a, (int)b, r));
+}
+
+static inline void
+zrsh(z_t r, z_t a, size_t b)
+{
+ try(mp_div_2d(a, (int)b, r, 0));
+}
+
+static inline void
+ztrunc(z_t r, z_t a, size_t b)
+{
+ try(mp_mod_2d(a, (int)b, r));
+}
+
+static void
+zsplit(z_t high, z_t low, z_t a, size_t brk)
+{
+ if (low == a) {
+ zrsh(high, a, brk);
+ ztrunc(low, a, brk);
+ } else {
+ ztrunc(low, a, brk);
+ zrsh(high, a, brk);
+ }
+}
+
+static void
+zsetu(z_t r, unsigned long long int val)
+{
+ try(mp_set_long_long(r, val));
+}
+
+static void
+zseti(z_t r, long long int val)
+{
+ if (val >= 0) {
+ zsetu(r, (unsigned long long int)val);
+ } else {
+ zsetu(r, (unsigned long long int)-val);
+ zneg(r, r);
+ }
+}
+
+static inline int
+zcmpmag(z_t a, z_t b)
+{
+ return mp_cmp_mag(a, b);
+}
+
+static inline int
+zcmp(z_t a, z_t b)
+{
+ return mp_cmp(a, b);
+}
+
+static inline int
+zcmpi(z_t a, long long int b)
+{
+ zseti(_b, b);
+ return zcmp(a, _b);
+}
+
+static inline int
+zcmpu(z_t a, unsigned long long int b)
+{
+ zsetu(_b, b);
+ return zcmp(a, _b);
+}
+
+static inline int
+zsignum(z_t a)
+{
+ return zcmp(a, _0);
+}
+
+static inline void
+zgcd(z_t r, z_t a, z_t b)
+{
+ try(mp_gcd(a, b, r));
+}
+
+static inline void
+zmul(z_t r, z_t a, z_t b)
+{
+ try(mp_mul(a, b, r));
+}
+
+static inline void
+zsqr(z_t r, z_t a)
+{
+ try(mp_sqr(a, r));
+}
+
+static inline void
+zmodmul(z_t r, z_t a, z_t b, z_t m)
+{
+ try(mp_mulmod(a, b, m, r));
+}
+
+static inline void
+zmodsqr(z_t r, z_t a, z_t m)
+{
+ try(mp_sqrmod(a, m, r));
+}
+
+static inline void
+zpow(z_t r, z_t a, z_t b)
+{
+ try(mp_expt_d(a, (mp_digit)mp_get_int(b), r));
+}
+
+static inline void
+zpowu(z_t r, z_t a, unsigned long long int b)
+{
+ try(mp_expt_d(a, (mp_digit)b, r));
+}
+
+static inline void
+zmodpow(z_t r, z_t a, z_t b, z_t m)
+{
+ try(mp_exptmod(a, b, m, r));
+}
+
+static inline void
+zmodpowu(z_t r, z_t a, unsigned long long int b, z_t m)
+{
+ try(mp_set_int(_b, b));
+ try(mp_exptmod(a, _b, m, r));
+}
+
+static inline void
+zsets(z_t a, char *s)
+{
+ try(mp_read_radix(a, s, 10));
+}
+
+static inline size_t
+zstr_length(z_t a, size_t b)
+{
+ try(mp_radix_size(a, b, &_tmp));
+ return _tmp;
+}
+
+static inline char *
+zstr(z_t a, char *s, size_t n)
+{
+ try(mp_toradix(a, s, 10));
+ return s;
+ (void) n;
+}
+
+static inline int
+zptest(z_t w, z_t a, int t)
+{
+ try(mp_prime_is_prime(a, t, &_tmp));
+ return _tmp;
+ (void) w; /* Note, the witness is not returned. */
+}
+
+static inline size_t
+zsave(z_t a, char *b)
+{
+ _tmp = b ? mp_signed_bin_size(a) : mp_to_signed_bin(a, (unsigned char *)b);
+ return _tmp;
+}
+
+static inline size_t
+zload(z_t a, char *b) /* Note, requires that zsave was called directly prior. */
+{
+ return mp_read_signed_bin(a, (unsigned char *)b, _tmp);
+}
+
+static inline void
+zdiv(z_t r, z_t a, z_t b)
+{
+ try(mp_div(a, b, r, 0));
+}
+
+static inline void
+zmod(z_t r, z_t a, z_t b)
+{
+ try(mp_mod(a, b, r));
+}
+
+static inline void
+zdivmod(z_t q, z_t r, z_t a, z_t b)
+{
+ try(mp_div(a, b, q, r));
+}
+
 static void
 zrand(z_t r, int dev, int dist, z_t n)
 {
_AT_@ -185,7 +424,7 @@ zrand(z_t r, int dev, int dist, z_t n)
 
         switch (dist) {
         case QUASIUNIFORM:
- mp_rand(r, bits);
+ try(mp_rand(r, bits));
                 zadd(r, r, _1);
                 zmul(r, r, n);
                 zrsh(r, r, bits);
_AT_@ -199,7 +438,7 @@ zrand(z_t r, int dev, int dist, z_t n)
                 break;
 
         case MODUNIFORM:
- mp_rand(r, bits);
+ try(mp_rand(r, bits));
                 if (zcmp(r, n) > 0)
                         zsub(r, r, n);
                 break;
Received on Wed Apr 27 2016 - 02:17:19 CEST

This archive was generated by hypermail 2.3.0 : Wed Apr 27 2016 - 02:24:15 CEST