📄 timing.c
字号:
#include <tommath.h>#include <time.h>ulong64 _tt;#ifdef IOWNANATHLON#include <unistd.h>#define SLEEP sleep(4)#else#define SLEEP#endifvoid ndraw(mp_int * a, char *name){ char buf[4096]; printf("%s: ", name); mp_toradix(a, buf, 64); printf("%s\n", buf);}static void draw(mp_int * a){ ndraw(a, "");}unsigned long lfsr = 0xAAAAAAAAUL;int lbit(void){ if (lfsr & 0x80000000UL) { lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL; return 1; } else { lfsr <<= 1; return 0; }}/* RDTSC from Scott Duplichan */static ulong64 TIMFUNC(void){#if defined __GNUC__#if defined(__i386__) || defined(__x86_64__) unsigned long long a; __asm__ __volatile__("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n":: "m"(a):"%eax", "%edx"); return a;#else /* gcc-IA64 version */ unsigned long result; __asm__ __volatile__("mov %0=ar.itc":"=r"(result)::"memory"); while (__builtin_expect((int) result == -1, 0)) __asm__ __volatile__("mov %0=ar.itc":"=r"(result)::"memory"); return result;#endif // Microsoft and Intel Windows compilers#elif defined _M_IX86 __asm rdtsc#elif defined _M_AMD64 return __rdtsc();#elif defined _M_IA64#if defined __INTEL_COMPILER#include <ia64intrin.h>#endif return __getReg(3116);#else#error need rdtsc function for this build#endif}#define DO(x) x; x;//#define DO4(x) DO2(x); DO2(x);//#define DO8(x) DO4(x); DO4(x);//#define DO(x) DO8(x); DO8(x);int main(void){ ulong64 tt, gg, CLK_PER_SEC; FILE *log, *logb, *logc, *logd; mp_int a, b, c, d, e, f; int n, cnt, ix, old_kara_m, old_kara_s; unsigned rr; mp_init(&a); mp_init(&b); mp_init(&c); mp_init(&d); mp_init(&e); mp_init(&f); srand(time(NULL)); /* temp. turn off TOOM */ TOOM_MUL_CUTOFF = TOOM_SQR_CUTOFF = 100000; CLK_PER_SEC = TIMFUNC(); sleep(1); CLK_PER_SEC = TIMFUNC() - CLK_PER_SEC; printf("CLK_PER_SEC == %llu\n", CLK_PER_SEC); goto exptmod; log = fopen("logs/add.log", "w"); for (cnt = 8; cnt <= 128; cnt += 8) { SLEEP; mp_rand(&a, cnt); mp_rand(&b, cnt); rr = 0; tt = -1; do { gg = TIMFUNC(); DO(mp_add(&a, &b, &c)); gg = (TIMFUNC() - gg) >> 1; if (tt > gg) tt = gg; } while (++rr < 100000); printf("Adding\t\t%4d-bit => %9llu/sec, %9llu cycles\n", mp_count_bits(&a), CLK_PER_SEC / tt, tt); fprintf(log, "%d %9llu\n", cnt * DIGIT_BIT, tt); fflush(log); } fclose(log); log = fopen("logs/sub.log", "w"); for (cnt = 8; cnt <= 128; cnt += 8) { SLEEP; mp_rand(&a, cnt); mp_rand(&b, cnt); rr = 0; tt = -1; do { gg = TIMFUNC(); DO(mp_sub(&a, &b, &c)); gg = (TIMFUNC() - gg) >> 1; if (tt > gg) tt = gg; } while (++rr < 100000); printf("Subtracting\t\t%4d-bit => %9llu/sec, %9llu cycles\n", mp_count_bits(&a), CLK_PER_SEC / tt, tt); fprintf(log, "%d %9llu\n", cnt * DIGIT_BIT, tt); fflush(log); } fclose(log); /* do mult/square twice, first without karatsuba and second with */ multtest: old_kara_m = KARATSUBA_MUL_CUTOFF; old_kara_s = KARATSUBA_SQR_CUTOFF; for (ix = 0; ix < 2; ix++) { printf("With%s Karatsuba\n", (ix == 0) ? "out" : ""); KARATSUBA_MUL_CUTOFF = (ix == 0) ? 9999 : old_kara_m;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -