libzahl

big integer library
git clone git://git.suckless.org/libzahl
Log | Files | Refs | README | LICENSE

commit 52f91804ff67246deab3d98c790f8b6b845afcde
parent d453ba2f3bb35c3ab12cee18b1c2ab4df478db54
Author: Mattias Andrée <maandree@kth.se>
Date:   Fri,  8 Apr 2016 15:53:31 +0200

Some improvements:

-  Improve .gitignore

-  Improve portability of the makefile

-  Bring the translations, for gmp and tommath,
   used by the benchmark up to date

Signed-off-by: Mattias Andrée <maandree@kth.se>

Diffstat:
M.gitignore | 7++++++-
MMakefile | 57++++++++++++++++++++++++++++++++++++++++-----------------
Mbench/benchmark.c | 2+-
Mbench/libgmp.h | 14++++++++++++++
Mbench/libtommath.h | 23+++++++++++++++++++++--
5 files changed, 82 insertions(+), 21 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,5 +1,10 @@ -_/ *~ +\#*\# +.\#* +*.swo +*.swp +*.orig +*.bak *.o *.su *.a diff --git a/Makefile b/Makefile @@ -67,14 +67,42 @@ INLINE_FUN =\ zcmpu\ zbtest -HDR = $(HDR_PUBLIC) $(HDR_PRIVATE) -OBJ = $(FUN:=.o) allocator.o +HDR = $(HDR_PUBLIC) $(HDR_PRIVATE) +OBJ = $(FUN:=.o) allocator.o MAN3 = $(FUN:=.3) $(INLINE_FUN:=.3) MAN7 = libzahl.7 +VPATH = src + +BENCHMARK_LIB_ = libzahl.a +BENCHMARK_LIB_zahl = libzahl.a +BENCHMARK_LIB_libzahl = libzahl.a +BENCHMARK_LIB_tommath = -ltommath +BENCHMARK_LIB_libtommath = -ltommath +BENCHMARK_LIB_gmp = -lgmp +BENCHMARK_LIB_libgmp = -lgmp + +BENCHMARK_DEP_ = libzahl.a +BENCHMARK_DEP_zahl = libzahl.a +BENCHMARK_DEP_libzahl = libzahl.a +BENCHMARK_DEP_tommath = bench/libtommath.h +BENCHMARK_DEP_libtommath = bench/libtommath.h +BENCHMARK_DEP_gmp = bench/libgmp.h +BENCHMARK_DEP_libgmp = bench/libgmp.h + +BENCHMARK_CPP_tommath = '-DBENCHMARK_LIB="libtommath.h"' +BENCHMARK_CPP_libtommath = '-DBENCHMARK_LIB="libtommath.h"' +BENCHMARK_CPP_gmp = '-DBENCHMARK_LIB="libgmp.h"' +BENCHMARK_CPP_libgmp = '-DBENCHMARK_LIB="libgmp.h"' + +CPPFLAGS += $(BENCHMARK_CPP_$(BENCHMARK_LIB)) + +CFLAGS_WITHOUT_O = $$(printf '%s\n' $(CFLAGS) | sed '/^-O.*$$/d') + + all: libzahl.a -%.o: src/%.c $(HDR) config.mk +.o: .c $(HDR) config.mk $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $< libzahl.a: $(OBJ) @@ -85,19 +113,10 @@ test-random.c: test-generate.py ./test-generate.py > test-random.c test: test.c libzahl.a test-random.c - $(CC) $(LDFLAGS) $(CFLAGS:--O*) -O0 $(CPPFLAGS) -o $@ test.c libzahl.a + $(CC) $(LDFLAGS) $(CFLAGS_WITHOUT_O) -O0 $(CPPFLAGS) -o $@ test.c libzahl.a -ifndef BENCHMARK_LIB -benchmark: bench/benchmark.c libzahl.a - $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ $^ -endif -ifdef BENCHMARK_LIB -CPPFLAGS += -DBENCHMARK_LIB='"$(BENCHMARK_LIB).h"' -BENCHMARK_libtommath = -ltommath -BENCHMARK_libgmp = -lgmp -benchmark: bench/benchmark.c bench/$(BENCHMARK_LIB).h - $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ bench/benchmark.c $(BENCHMARK_$(BENCHMARK_LIB)) -endif +benchmark: bench/benchmark.c $(BENCHMARK_DEP_$(BENCHMARK_LIB)) + $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ bench/benchmark.c $(BENCHMARK_LIB_$(BENCHMARK_LIB)) benchmark-zrand: bench/benchmark-zrand.c libzahl.a $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ $^ @@ -110,10 +129,14 @@ install: libzahl.a mkdir -p -- "$(DESTDIR)$(PREFIX)/include" mkdir -p -- "$(DESTDIR)$(MANPREFIX)/man3" mkdir -p -- "$(DESTDIR)$(MANPREFIX)/man7" + @if test -n "$(DESTDIR)"; then \ + cd man && test -d "$(DESTDIR)$(MANPREFIX)/man7" || \ + (printf '\n\n!! DESTDIR must be an absolute path. !!\n\n\n' ; exit 1) \ + fi cp -- libzahl.a "$(DESTDIR)$(EXECPREFIX)/lib" cp -- $(HDR_PUBLIC) "$(DESTDIR)$(PREFIX)/include" - cp -- $(foreach M,$(MAN3),man/$(M)) "$(DESTDIR)$(MANPREFIX)/man3" - cp -- $(foreach M,$(MAN7),man/$(M)) "$(DESTDIR)$(MANPREFIX)/man7" + cd man && cp -- $(MAN3) "$(DESTDIR)$(MANPREFIX)/man3" + cd man && cp -- $(MAN7) "$(DESTDIR)$(MANPREFIX)/man7" uninstall: -rm -- "$(DESTDIR)$(EXECPREFIX)/lib/libzahl.a" diff --git a/bench/benchmark.c b/bench/benchmark.c @@ -18,7 +18,7 @@ i = FAST ? 1000000L : 1000L;\ clock_gettime(CLOCK_MONOTONIC_RAW, &start);\ while (i--) {\ - INSTRUCTION;\ + (void)INSTRUCTION;\ }\ clock_gettime(CLOCK_MONOTONIC_RAW, &end);\ end.tv_sec -= start.tv_sec;\ diff --git a/bench/libgmp.h b/bench/libgmp.h @@ -37,8 +37,15 @@ zunsetup(void) } #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 mpz_init @@ -197,6 +204,13 @@ zrand(z_t r, int dev, int dist, z_t n) mpz_urandomm(r, _randstate, n); break; + case MODUNIFORM: + bits = zbits(n); + mpz_urandomb(r, _randstate, bits); + if (zcmp(r, n) > 0) + zsub(r, r, n); + break; + default: abort(); } diff --git a/bench/libtommath.h b/bench/libtommath.h @@ -30,8 +30,15 @@ zunsetup(void) } #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) @@ -76,12 +83,18 @@ zunsetup(void) #define zstr_length(a, b) (mp_radix_size(a, b, &_tmp), _tmp) #define zstr(a, s) 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 zsave(a, s) ((_tmp = ((s) ? mp_signed_bin_size(a) : mp_to_signed_bin(a, s)))) -#define zload(a, s) mp_read_signed_bin(a, s, _tmp) +#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 int +zsave(z_t a, char *buf) +{ + _tmp = buf ? mp_signed_bin_size(a) : mp_to_signed_bin(a, (unsigned char *)buf); + return _tmp; +} + static void zsetu(z_t r, unsigned long long int val) { @@ -183,6 +196,12 @@ zrand(z_t r, int dev, int dist, z_t n) } break; + case MODUNIFORM: + mp_rand(r, bits); + if (zcmp(r, n) > 0) + zsub(r, r, n); + break; + default: abort(); }