Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / libnacl / measure-anything.c
blob93941aa5ed2ad06f681446c5e31ea62711ea54ba
1 /*
2 * measure-anything.c version 20090223
3 * D. J. Bernstein
4 * Public domain.
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #ifndef _WIN32
14 # include <sys/resource.h>
15 #endif
16 #include "cpucycles.h"
17 #include "cpuid.h"
19 typedef int uint32;
21 static uint32 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 } ;
22 static uint32 in[12];
23 static uint32 out[8];
24 static int outleft = 0;
26 #define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
27 #define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
29 static void surf(void)
31 uint32 t[12]; uint32 x; uint32 sum = 0;
32 int r; int i; int loop;
34 for (i = 0;i < 12;++i) t[i] = in[i] ^ seed[12 + i];
35 for (i = 0;i < 8;++i) out[i] = seed[24 + i];
36 x = t[11];
37 for (loop = 0;loop < 2;++loop) {
38 for (r = 0;r < 16;++r) {
39 sum += 0x9e3779b9;
40 MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
41 MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
42 MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
44 for (i = 0;i < 8;++i) out[i] ^= t[i + 4];
48 void randombytes(unsigned char *x,unsigned long long xlen)
50 while (xlen > 0) {
51 if (!outleft) {
52 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
53 surf();
54 outleft = 8;
56 *x = out[--outleft];
57 ++x;
58 --xlen;
62 extern const char *primitiveimplementation;
63 extern const char *implementationversion;
64 extern const char *sizenames[];
65 extern const long long sizes[];
66 extern void preallocate(void);
67 extern void allocate(void);
68 extern void measure(void);
70 static void printword(const char *s)
72 if (!*s) putchar('-');
73 while (*s) {
74 if (*s == ' ') putchar('_');
75 else if (*s == '\t') putchar('_');
76 else if (*s == '\r') putchar('_');
77 else if (*s == '\n') putchar('_');
78 else putchar(*s);
79 ++s;
81 putchar(' ');
84 static void printnum(long long x)
86 printf("%lld ",x);
89 static void fail(const char *why)
91 fprintf(stderr,"measure: fatal: %s\n",why);
92 exit(111);
95 unsigned char *alignedcalloc(unsigned long long len)
97 unsigned char *x = (unsigned char *) calloc(1,len + 128);
98 if (!x) fail("out of memory");
99 /* will never deallocate so shifting is ok */
100 x += 63 & (-(unsigned long) x);
101 return x;
104 static long long cyclespersecond;
106 static void printimplementations(void)
108 int i;
110 printword("implementation");
111 printword(primitiveimplementation);
112 printword(implementationversion);
113 printf("\n"); fflush(stdout);
115 for (i = 0;sizenames[i];++i) {
116 printword(sizenames[i]);
117 printnum(sizes[i]);
118 printf("\n"); fflush(stdout);
121 printword("cpuid");
122 printword(cpuid);
123 printf("\n"); fflush(stdout);
125 printword("cpucycles_persecond");
126 printnum(cyclespersecond);
127 printf("\n"); fflush(stdout);
129 printword("cpucycles_implementation");
130 printword(cpucycles_implementation);
131 printf("\n"); fflush(stdout);
133 printword("compiler");
134 printword(COMPILER);
135 #if defined(__VERSION__) && !defined(__ICC)
136 printword(__VERSION__);
137 #elif defined(__xlc__)
138 printword(__xlc__);
139 #elif defined(__ICC)
141 char buf[256];
143 sprintf(buf, "%d.%d.%d", __ICC/100, __ICC%100,
144 __INTEL_COMPILER_BUILD_DATE);
145 printword(buf);
147 #elif defined(__PGIC__)
149 char buf[256];
151 sprintf(buf, "%d.%d.%d", __PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__);
152 printword(buf);
154 #elif defined(__SUNPRO_C)
156 char buf[256];
157 int major, minor, micro;
159 micro = __SUNPRO_C & 0xf;
160 minor = (__SUNPRO_C >> 4) & 0xf;
161 major = (__SUNPRO_C >> 8) & 0xf;
163 if (micro)
164 sprintf(buf, "%d.%d.%d", major, minor, micro);
165 else
166 sprintf(buf, "%d.%d", major, minor);
167 printword(buf);
169 #else
170 printword("unknown compiler version");
171 #endif
172 printf("\n"); fflush(stdout);
175 void printentry(long long mbytes,const char *measuring,long long *m,long long mlen)
177 long long i;
178 long long j;
179 long long belowj;
180 long long abovej;
182 printword(measuring);
183 if (mbytes >= 0) printnum(mbytes); else printword("");
184 if (mlen > 0) {
185 for (j = 0;j + 1 < mlen;++j) {
186 belowj = 0;
187 for (i = 0;i < mlen;++i) if (m[i] < m[j]) ++belowj;
188 abovej = 0;
189 for (i = 0;i < mlen;++i) if (m[i] > m[j]) ++abovej;
190 if (belowj * 2 < mlen && abovej * 2 < mlen) break;
192 printnum(m[j]);
193 if (mlen > 1) {
194 for (i = 0;i < mlen;++i) printnum(m[i]);
197 printf("\n"); fflush(stdout);
200 void limits()
202 #ifdef RLIM_INFINITY
203 struct rlimit r;
204 r.rlim_cur = 0;
205 r.rlim_max = 0;
206 #ifdef RLIMIT_NOFILE
207 setrlimit(RLIMIT_NOFILE,&r);
208 #endif
209 #ifdef RLIMIT_NPROC
210 setrlimit(RLIMIT_NPROC,&r);
211 #endif
212 #ifdef RLIMIT_CORE
213 setrlimit(RLIMIT_CORE,&r);
214 #endif
215 #endif
218 int main()
220 cyclespersecond = cpucycles_persecond();
221 preallocate();
222 limits();
223 printimplementations();
224 allocate();
225 measure();
226 return 0;