#define _BSD_VISIBILE #include #include #include #include #include #include static inline int64_t mynow(void) { int x; struct timespec tp; x = clock_gettime(CLOCK_VIRTUAL, &tp); assert(x == 0); return (tstosbt(tp)); } int main(int argc, char **argv) { size_t iterations = 1, i; int64_t a, b, t; size_t sz; unsigned sec, usec; for (t = 0; t < mstosbt(100); iterations *= 2) { a = mynow(); for (i = 0; i < iterations; i++) { int retry = 10; unsigned long rndval; __asm __volatile( "1:\n\t" "rdrand %1\n\t" "jc 2f\n\t" "dec %0\n\t" "jne 1b\n\t" "2:" : "+r" (retry), "=r" (rndval) : : "cc"); if (retry == 0) errx(1, "failed 10 times"); } b = mynow(); t = b - a; } sz = sizeof(unsigned long); sec = t >> 32; usec = sbttous(t & 0xffffffff); printf("%zu iterations of %zu bytes each took %u.%06u seconds.ms, or\n", iterations, sz, sec, usec); double x = (double)sz * iterations / (sec + (((double)usec) / 1000000)); printf("%.3f B/s %.3f kB/s %.3f MiB/s\n", x, x/1024, x/1024/1024); }