libzahl

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

commit 73b0d33bafadb839962d5d6fbd5c1a8a18a8c56c
parent 8bbbb381017811e36c43fd08b802ec6c293e4255
Author: Mattias Andrée <maandree@kth.se>
Date:   Thu, 28 Apr 2016 21:52:47 +0200

Some documentation and cleanup for benchmark stuff

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

Diffstat:
Mbench/benchmark-func.c | 20+++++++++++++++-----
Mbench/benchmark.c | 10+++++-----
Mbench/benchmark.h | 98++++++++++++++++++++++++++++++++++++++++---------------------------------------
3 files changed, 70 insertions(+), 58 deletions(-)

diff --git a/bench/benchmark-func.c b/bench/benchmark-func.c @@ -3,6 +3,11 @@ #include <limits.h> +#if !defined(USE_MEDIAN) && !defined(USE_MID_7TH_AVERAGE) && !defined(USE_AVERAGE) && !defined(USE_LOWEST) +# define USE_MID_7TH_AVERAGE +#endif + + enum { HIGH_ONLY, HIGH_AND_LOW, @@ -27,18 +32,20 @@ struct function { #define M_MAX 200 + static char buf[1000]; static z_t temp, temp2; static unsigned long long int measurements[M_MAX]; -#if 1 + +#if defined(USE_MEDIAN) || defined(USE_MID_7TH_AVERAGE) static int measurementpcmp(const void *ap_, const void *bp_) { const unsigned long long int *ap = ap_, *bp = bp_; return *ap < *bp ? -1 : *ap > *bp; } -# if 0 +# if defined(USE_MEDIAN) static unsigned long long int gettime(size_t m) { @@ -47,7 +54,7 @@ gettime(size_t m) return measurements[m / 2]; return (measurements[m / 2] + measurements[m / 2 - 1]) / 2; } -# else +# else /* if defined(USE_MID_7TH_AVERAGE) */ static unsigned long long int gettime(size_t m) { @@ -61,7 +68,7 @@ gettime(size_t m) # undef X } # endif -#elif 0 +#elif defined(USE_AVERAGE) static unsigned long long int gettime(size_t m) { @@ -71,7 +78,7 @@ gettime(size_t m) tot += measurements[i]; return tot / m; } -#else +#else /* if defined(USE_LOWEST) */ static unsigned long long int gettime(size_t m) { @@ -84,6 +91,7 @@ gettime(size_t m) } #endif + #define FUNCTION_2D(NAME, INSTRUCTION, PREINSTRUCTION)\ static void\ NAME(z_t *as, z_t* bs, struct function *f)\ @@ -183,6 +191,7 @@ gettime(size_t m) zdivmod */ + #define X(FN, A, F1, F2) FUNCTION_2D(bench_##FN, F1, F2) LIST_2D_FUNCTIONS #undef X @@ -194,6 +203,7 @@ LIST_2D_FUNCTIONS {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; + static z_t * create_ints(size_t start, size_t end, int mode) { diff --git a/bench/benchmark.c b/bench/benchmark.c @@ -79,13 +79,13 @@ main(int argc, char *argv[]) BENCHMARK(zbset(c, a, 76, -1), 1); BENCHMARK(zbset(a, a, 76, -1), 1); BENCHMARK(zbtest(a, 76), 1); -#ifndef HEBIMATH +#ifndef HEBIMATH /* These take too long in hebimath because of inefficient division. */ BENCHMARK(zgcd(c, a, b), 0); #endif BENCHMARK(zmul(c, a, b), 0); BENCHMARK(zmul(c, a, a), 0); BENCHMARK(zsqr(c, a), 0); -#ifndef HEBIMATH +#ifndef HEBIMATH /* Ditto. */ zsets(d, "1484298084218938358480511181388394862858002249"); BENCHMARK(zmodmul(c, a, b, d), 0); BENCHMARK(zmodmul(c, a, a, d), 0); @@ -94,8 +94,8 @@ main(int argc, char *argv[]) BENCHMARK(zmodmul(c, a, a, tiny), 0); BENCHMARK(zmodsqr(c, a, tiny), 0); zsets(d, "12"); - BENCHMARK(zpow(c, a, d), 0); /* Memory corruption when using hebimath */ - BENCHMARK(zpowu(c, a, 12), 0); /* Memory corruption when using hebimath */ + BENCHMARK(zpow(c, a, d), 0); /* Memory corruption when using hebimath. */ + BENCHMARK(zpowu(c, a, 12), 0); /* Memory corruption when using hebimath. */ BENCHMARK(zmodpow(c, a, d, b), 0); BENCHMARK(zmodpowu(c, a, 12, b), 0); #endif @@ -111,7 +111,7 @@ main(int argc, char *argv[]) BENCHMARK(zdiv(c, a, b), 1); BENCHMARK(zmod(c, a, b), 1); BENCHMARK(zdivmod(c, d, a, b), 1); -#ifndef HEBIMATH +#ifndef HEBIMATH /* Ditto. */ BENCHMARK(zdiv(c, a, tiny), 0); BENCHMARK(zmod(c, a, tiny), 0); BENCHMARK(zdivmod(c, d, a, tiny), 0); diff --git a/bench/benchmark.h b/bench/benchmark.h @@ -39,60 +39,16 @@ #endif + static struct timespec dur; static char timebuf[512]; -#if defined(USE_RDTSC) -typedef unsigned long long int rdtsc_t; -static unsigned int start_high, start_low, end_high, end_low; -static unsigned long long int freq; -#elif defined(USE_CLOCK) -static clock_t start, end; -#else -static struct timespec start; -#endif - - -static void -benchmark_init(void) -{ -#if defined(__linux__) - cpu_set_t cpuset; -# if defined(USE_RDTSC) - FILE *f; - char *line = 0; - size_t size = 0; -# endif - CPU_ZERO(&cpuset); - CPU_SET(0, &cpuset); - sched_setaffinity(getpid(), sizeof(cpuset), &cpuset); -# if defined(USE_RDTSC) - f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r"); - if (getline(&line, &size, f) < 0) - abort(); - fclose(f); - freq = strtoull(line, 0, 10); - free(line); -# endif -#endif - (void) timebuf; -} #if defined(USE_RDTSC) && defined(__x86_64__) -static inline void -rdtsc(unsigned int *low, unsigned int *high) -{ - __asm__ __volatile__ ("rdtsc" : "=a"(*low), "=d"(*high)); -} -static inline rdtsc_t -rdtsc_join(unsigned int low, unsigned int high) -{ - return (rdtsc_t)low | (((rdtsc_t)high) << 32); -} -#endif - +typedef unsigned long long int rdtsc_t; +static unsigned int start_high, start_low, end_high, end_low; +static unsigned long long int freq; -#if defined(USE_RDTSC) # define TIC (rdtsc(&start_low, &start_high)) # define TOC\ do {\ @@ -106,7 +62,23 @@ rdtsc_join(unsigned int low, unsigned int high) dur_seconds -= (double)(dur.tv_sec = (int)dur_seconds);\ dur.tv_nsec = (long int)(dur_seconds * 1000000000L);\ } while (0) + +static inline void +rdtsc(unsigned int *low, unsigned int *high) +{ + __asm__ __volatile__ ("rdtsc" : "=a"(*low), "=d"(*high)); +} + +static inline rdtsc_t +rdtsc_join(unsigned int low, unsigned int high) +{ + return (rdtsc_t)low | (((rdtsc_t)high) << 32); +} + + #elif defined(USE_CLOCK) +static clock_t start, end; + # define TIC (start = clock()) # define TOC\ do {\ @@ -114,7 +86,11 @@ rdtsc_join(unsigned int low, unsigned int high) dur.tv_sec = (end - start) / 1000000ULL;\ dur.tv_nsec = ((end - start) % 1000000ULL) * 1000;\ } while (0) + + #elif defined(USE_GETTIME) +static struct timespec start; + # define TIC clock_gettime(CLOCK_MONOTONIC_RAW, &start) # define TOC\ do {\ @@ -131,3 +107,29 @@ rdtsc_join(unsigned int low, unsigned int high) #define TICKS ((unsigned long long int)(dur.tv_sec) * 1000000000ULL + (unsigned long long int)(dur.tv_nsec)) #define STIME (sprintf(timebuf, "%lli.%09li", (long long)(dur.tv_sec), dur.tv_nsec), timebuf) + + +static void +benchmark_init(void) +{ +#if defined(__linux__) + cpu_set_t cpuset; +# if defined(USE_RDTSC) + FILE *f; + char *line = 0; + size_t size = 0; +# endif + CPU_ZERO(&cpuset); + CPU_SET(0, &cpuset); + sched_setaffinity(getpid(), sizeof(cpuset), &cpuset); +# if defined(USE_RDTSC) + f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r"); + if (getline(&line, &size, f) < 0) + abort(); + fclose(f); + freq = strtoull(line, 0, 10); + free(line); +# endif +#endif + (void) timebuf; +}