Debian Codegraph @

etc/measure.c

raw symbols SCIP
1 /*
2 * Benchmarking utilities extracted from SUPERCOP by Andres Erbsen
3 * based on measure-anything.c version 20120328 and measure.c
4 * by D. J. Bernstein
5 * Public domain.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <string.h>
12 #include <time.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/resource.h>
16
17 // cpucycles_amd64cpuinfo
18 long long cpucycles(void)
19 {
20 unsigned long long result;
21 asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
22 : "=a" (result) :: "%rdx");
23 return result;
24 }
25
26 // SUPERCOP randombytes
27 static uint32_t seed[32] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5 } ;
28 static uint32_t in[12];
29 static uint32_t out[8];
30 static int outleft = 0;
31 static void surf(void)
32 {
33 #define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
34 #define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
35 uint32_t t[12]; uint32_t x; uint32_t sum = 0;
36 int r; int i; int loop;
37
38 for (i = 0;i < 12;++i) t[i] = in[i] ^ seed[12 + i];
39 for (i = 0;i < 8;++i) out[i] = seed[24 + i];
40 x = t[11];
41 for (loop = 0;loop < 2;++loop) {
42 for (r = 0;r < 16;++r) {
43 sum += 0x9e3779b9;
44 MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
45 MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
46 MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
47 }
48 for (i = 0;i < 8;++i) out[i] ^= t[i + 4];
49 }
50 #undef ROTATE
51 #undef MUSH
52 }
53 void randombytes(unsigned char *x,unsigned long long xlen)
54 {
55 while (xlen > 0) {
56 if (!outleft) {
57 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
58 surf();
59 outleft = 8;
60 }
61 *x = out[--outleft];
62 ++x;
63 --xlen;
64 }
65 }
66
67 // SUPERCOP limits
68 void limits()
69 {
70 #ifdef RLIM_INFINITY
71 struct rlimit r;
72 r.rlim_cur = 0;
73 r.rlim_max = 0;
74 #ifdef RLIMIT_NOFILE
75 setrlimit(RLIMIT_NOFILE,&r);
76 #endif
77 #ifdef RLIMIT_NPROC
78 setrlimit(RLIMIT_NPROC,&r);
79 #endif
80 #ifdef RLIMIT_CORE
81 setrlimit(RLIMIT_CORE,&r);
82 #endif
83 #endif
84 }
85
86 void UUT(unsigned char*);
87
88 // place the call to UUT in a separate function, because gcc prohibits
89 // use of bp in inline assembly if you have a variable-length array or
90 // alloca in the code; see
91 // https://stackoverflow.com/questions/46248430/how-can-i-determine-preferably-at-compile-time-whether-gcc-is-using-rbp-based?noredirect=1#comment79462384_46248430
92 void __attribute__ ((noinline)) measure_helper(int n, unsigned char *buf, long long* cycles)
93 {
94 for (int i = 0;i <= n;++i) {
95 cycles[i] = cpucycles();
96 UUT(buf);
97 }
98 }
99
100 void measure(int n)
101 {
102 unsigned char *buf = aligned_alloc(64, 1024);
103 long long* cycles = calloc(n + 1, sizeof(long long));
104
105 measure_helper(n, buf, cycles);
106 for (int i = 0;i < n;++i) cycles[i] = cycles[i + 1] - cycles[i];
107 for (int i = 0;i < n;++i) printf("%lld\n", cycles[i]);
108
109 __asm__ __volatile__("" :: "m" (buf)); // do not optimize buf away
110 free(cycles);
111 }
112
113 int main(int argc, char** argv)
114 {
115 int n = 0;
116 if (argc != 2) return 111;
117 sscanf(argv[1], "%d", &n);
118 limits();
119 measure(n);
120 return 0;
121 }