-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathqtester.c
More file actions
455 lines (376 loc) · 14.4 KB
/
qtester.c
File metadata and controls
455 lines (376 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright Naoki Shibata 2010 - 2019.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// This define is needed to prevent the `execvpe` function to raise a
// warning at compile time. For more information, see
// https://linux.die.net/man/3/execvp.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <float.h>
#include <limits.h>
#include <errno.h>
#include <inttypes.h>
#if defined(POWER64_UNDEF_USE_EXTERN_INLINES)
// This is a workaround required to cross compile for PPC64 binaries
#include <features.h>
#ifdef __USE_EXTERN_INLINES
#undef __USE_EXTERN_INLINES
#endif
#endif
#include <math.h>
#include <mpfr.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <signal.h>
#include "misc.h"
#include "qtesterutil.h"
void stop(char *mes) {
fprintf(stderr, "%s\n", mes);
exit(-1);
}
int ptoc[2], ctop[2];
int pid;
FILE *fpctop;
extern char **environ;
void startChild(const char *path, char *const argv[]) {
pipe(ptoc);
pipe(ctop);
pid = fork();
assert(pid != -1);
if (pid == 0) {
// child process
char buf0[1], buf1[1];
int i;
close(ptoc[1]);
close(ctop[0]);
fflush(stdin);
fflush(stdout);
i = dup2(ptoc[0], fileno(stdin));
assert(i != -1);
i = dup2(ctop[1], fileno(stdout));
assert(i != -1);
setvbuf(stdin, buf0, _IONBF,0);
setvbuf(stdout, buf1, _IONBF,0);
fflush(stdin);
fflush(stdout);
#if !defined(__APPLE__) && !defined(__FreeBSD__)
execvpe(path, argv, environ);
#else
execvp(path, argv);
#endif
fprintf(stderr, "execvp in startChild : %s\n", strerror(errno));
exit(-1);
}
// parent process
close(ptoc[0]);
close(ctop[1]);
}
//
typedef union {
Sleef_quad q;
struct {
uint64_t l, h;
};
} cnv128;
#define child_q_q(funcStr, arg) do { \
char str[256]; \
cnv128 c; \
c.q = arg; \
sprintf(str, funcStr " %" PRIx64 ":%" PRIx64 "\n", c.h, c.l); \
write(ptoc[1], str, strlen(str)); \
if (fgets(str, 255, fpctop) == NULL) stop("child " funcStr); \
sscanf(str, "%" PRIx64 ":%" PRIx64, &c.h, &c.l); \
return c.q; \
} while(0)
#define child_q2_q(funcStr, arg) do { \
char str[256]; \
cnv128 c0, c1; \
c0.q = arg; \
sprintf(str, funcStr " %" PRIx64 ":%" PRIx64 "\n", c0.h, c0.l); \
write(ptoc[1], str, strlen(str)); \
if (fgets(str, 255, fpctop) == NULL) stop("child " funcStr); \
sscanf(str, "%" PRIx64 ":%" PRIx64 " %" PRIx64 ":%" PRIx64 , &c0.h, &c0.l, &c1.h, &c1.l); \
Sleef_quad2 ret = { c0.q, c1.q }; \
return ret; \
} while(0)
#define child_q_q_q(funcStr, arg0, arg1) do { \
char str[256]; \
cnv128 c0, c1; \
c0.q = arg0; \
c1.q = arg1; \
sprintf(str, funcStr " %" PRIx64 ":%" PRIx64 " %" PRIx64 ":%" PRIx64 "\n", c0.h, c0.l, c1.h, c1.l); \
write(ptoc[1], str, strlen(str)); \
if (fgets(str, 255, fpctop) == NULL) stop("child " funcStr); \
sscanf(str, "%" PRIx64 ":%" PRIx64, &c0.h, &c0.l); \
return c0.q; \
} while(0)
#define child_q_str(funcStr, arg) do { \
char str[256]; \
sprintf(str, funcStr " %s\n", arg); \
write(ptoc[1], str, strlen(str)); \
if (fgets(str, 255, fpctop) == NULL) stop("child " funcStr); \
cnv128 c; \
sscanf(str, "%" PRIx64 ":%" PRIx64, &c.h, &c.l); \
return c.q; \
} while(0)
#define child_str_q(funcStr, ret, arg) do { \
char str[256]; \
cnv128 c; \
c.q = arg; \
sprintf(str, funcStr " %" PRIx64 ":%" PRIx64 "\n", c.h, c.l); \
write(ptoc[1], str, strlen(str)); \
if (fgets(str, 255, fpctop) == NULL) stop("child " funcStr); \
sscanf(str, "%63s", ret); \
} while(0)
Sleef_quad child_addq_u05(Sleef_quad x, Sleef_quad y) { child_q_q_q("addq_u05", x, y); }
Sleef_quad child_subq_u05(Sleef_quad x, Sleef_quad y) { child_q_q_q("subq_u05", x, y); }
Sleef_quad child_mulq_u05(Sleef_quad x, Sleef_quad y) { child_q_q_q("mulq_u05", x, y); }
Sleef_quad child_divq_u05(Sleef_quad x, Sleef_quad y) { child_q_q_q("divq_u05", x, y); }
Sleef_quad child_sqrtq_u05(Sleef_quad x) { child_q_q("sqrtq_u05", x); }
Sleef_quad child_negq(Sleef_quad x) { child_q_q("negq", x); }
Sleef_quad child_strtoq(const char *s) { child_q_str("strtoq", s); }
void child_qtostr(char *ret, Sleef_quad x) { child_str_q("qtostr", ret, x); }
Sleef_quad child_copysignq(Sleef_quad x, Sleef_quad y) { child_q_q_q("copysignq", x, y); }
Sleef_quad child_fabsq(Sleef_quad x) { child_q_q("fabsq", x); }
Sleef_quad child_fmaxq(Sleef_quad x, Sleef_quad y) { child_q_q_q("fmaxq", x, y); }
Sleef_quad child_fminq(Sleef_quad x, Sleef_quad y) { child_q_q_q("fminq", x, y); }
//
#define cmpDenorm_q(mpfrFunc, childFunc, argx) do { \
mpfr_set_f128(frx, argx, GMP_RNDN); \
mpfrFunc(frz, frx, GMP_RNDN); \
Sleef_quad t = childFunc(argx); \
double u = countULPf128(t, frz, 1); \
if (u >= 10) { \
fprintf(stderr, "\narg = %s\ntest = %s\ncorrect = %s\nulp = %g\n", \
sprintf128(argx), sprintf128(t), sprintfr(frz), u); \
success = 0; \
break; \
} \
} while(0)
#define cmpDenorm_q_q(mpfrFunc, childFunc, argx, argy) do { \
mpfr_set_f128(frx, argx, GMP_RNDN); \
mpfr_set_f128(fry, argy, GMP_RNDN); \
mpfrFunc(frz, frx, fry, GMP_RNDN); \
Sleef_quad t = childFunc(argx, argy); \
double u = countULPf128(t, frz, 1); \
if (u >= 10) { \
Sleef_quad qz = mpfr_get_f128(frz, GMP_RNDN); \
fprintf(stderr, "\narg = %s,\n %s\ntest = %s\ncorrect = %s\nulp = %g\n", \
sprintf128(argx), sprintf128(argy), sprintf128(t), sprintf128(qz), u); \
success = 0; \
break; \
} \
} while(0)
#define checkAccuracy_q(mpfrFunc, childFunc, argx, bound) do { \
mpfr_set_f128(frx, argx, GMP_RNDN); \
mpfrFunc(frz, frx, GMP_RNDN); \
Sleef_quad t = childFunc(argx); \
double e = countULPf128(t, frz, 0); \
maxError = fmax(maxError, e); \
if (e > bound) { \
fprintf(stderr, "\narg = %s, test = %s, correct = %s, ULP = %lf\n", \
sprintf128(argx), sprintf128(childFunc(argx)), sprintfr(frz), countULPf128(t, frz, 0)); \
success = 0; \
break; \
} \
} while(0)
#define checkAccuracy_q_q(mpfrFunc, childFunc, argx, argy, bound) do { \
mpfr_set_f128(frx, argx, GMP_RNDN); \
mpfr_set_f128(fry, argy, GMP_RNDN); \
mpfrFunc(frz, frx, fry, GMP_RNDN); \
Sleef_quad t = childFunc(argx, argy); \
double e = countULPf128(t, frz, 0); \
maxError = fmax(maxError, e); \
if (e > bound) { \
fprintf(stderr, "\narg = %s, %s, test = %s, correct = %s, ULP = %lf\n", \
sprintf128(argx), sprintf128(argy), sprintf128(childFunc(argx, argy)), sprintfr(frz), countULPf128(t, frz, 0)); \
success = 0; \
break; \
} \
} while(0)
//
#define cmpDenormOuterLoop_q_q(mpfrFunc, childFunc, checkVals) do { \
for(int i=0;i<sizeof(checkVals)/sizeof(char *);i++) { \
Sleef_quad a0 = cast_q_str(checkVals[i]); \
for(int j=0;j<sizeof(checkVals)/sizeof(char *) && success;j++) { \
Sleef_quad a1 = cast_q_str(checkVals[j]); \
cmpDenorm_q_q(mpfrFunc, childFunc, a0, a1); \
} \
} \
} while(0)
#define cmpDenormOuterLoop_q(mpfrFunc, childFunc, checkVals) do { \
for(int i=0;i<sizeof(checkVals)/sizeof(char *);i++) { \
Sleef_quad a0 = cast_q_str(checkVals[i]); \
cmpDenorm_q(mpfrFunc, childFunc, a0); \
} \
} while(0)
//
#define checkAccuracyOuterLoop_q_q(mpfrFunc, childFunc, minStr, maxStr, nLoop, bound, seed) do { \
xsrand(seed); \
Sleef_quad min = cast_q_str(minStr), max = cast_q_str(maxStr); \
for(int i=0;i<nLoop && success;i++) { \
Sleef_quad x = rndf128(min, max), y = rndf128(min, max); \
checkAccuracy_q_q(mpfrFunc, childFunc, x, y, bound); \
} \
} while(0)
#define checkAccuracyOuterLoop_q(mpfrFunc, childFunc, minStr, maxStr, nLoop, bound, seed) do { \
xsrand(seed); \
Sleef_quad min = cast_q_str(minStr), max = cast_q_str(maxStr); \
for(int i=0;i<nLoop && success;i++) { \
Sleef_quad x = rndf128(min, max); \
checkAccuracy_q(mpfrFunc, childFunc, x, bound); \
} \
} while(0)
//
void checkResult(int success, double e) {
if (!success) {
fprintf(stderr, "\n\n*** Test failed\n");
exit(-1);
}
fprintf(stderr, "OK (%g ulp)\n", e);
}
#define STR_QUAD_MIN "3.36210314311209350626267781732175260e-4932"
#define STR_QUAD_MAX "1.18973149535723176508575932662800702e+4932"
#define STR_QUAD_DENORM_MIN "6.475175119438025110924438958227646552e-4966"
void do_test(int options) {
mpfr_set_default_prec(256);
mpfr_t frx, fry, frz;
mpfr_inits(frx, fry, frz, NULL);
int success = 1;
static const char *stdCheckVals[] = {
"0.0", "-0.0", "+0.5", "-0.5", "+1.0", "-1.0", "+1.5", "-1.5", "+2.0", "-2.0", "+2.5", "-2.5",
"1.234", "-1.234", "+1.234e+100", "-1.234e+100", "+1.234e-100", "-1.234e-100",
"+1.234e+3000", "-1.234e+3000", "+1.234e-3000", "-1.234e-3000",
"3.1415926535897932384626433832795028841971693993751058209749445923078164",
"+" STR_QUAD_MIN, "-" STR_QUAD_MIN,
"+" STR_QUAD_DENORM_MIN, "-" STR_QUAD_DENORM_MIN,
"NaN", "Inf", "-Inf"
};
#define NTEST 1000
double errorBound = 0.5000000001;
double maxError;
fprintf(stderr, "addq_u05 : ");
maxError = 0;
cmpDenormOuterLoop_q_q(mpfr_add, child_addq_u05, stdCheckVals);
checkAccuracyOuterLoop_q_q(mpfr_add, child_addq_u05, "1e-100", "1e+100", 5 * NTEST, errorBound, 0);
checkAccuracyOuterLoop_q_q(mpfr_add, child_addq_u05, "0", "Inf", 5 * NTEST, errorBound, 1);
checkResult(success, maxError);
fprintf(stderr, "subq_u05 : ");
maxError = 0;
cmpDenormOuterLoop_q_q(mpfr_sub, child_subq_u05, stdCheckVals);
checkAccuracyOuterLoop_q_q(mpfr_sub, child_subq_u05, "1e-100", "1e+100", 5 * NTEST, errorBound, 0);
checkAccuracyOuterLoop_q_q(mpfr_sub, child_subq_u05, "0", "Inf", 5 * NTEST, errorBound, 1);
checkResult(success, maxError);
fprintf(stderr, "mulq_u05 : ");
maxError = 0;
cmpDenormOuterLoop_q_q(mpfr_mul, child_mulq_u05, stdCheckVals);
checkAccuracyOuterLoop_q_q(mpfr_mul, child_mulq_u05, "1e-100", "1e+100", 5 * NTEST, errorBound, 0);
checkAccuracyOuterLoop_q_q(mpfr_mul, child_mulq_u05, "0", "Inf", 5 * NTEST, errorBound, 1);
checkResult(success, maxError);
fprintf(stderr, "divq_u05 : ");
maxError = 0;
cmpDenormOuterLoop_q_q(mpfr_div, child_divq_u05, stdCheckVals);
checkAccuracyOuterLoop_q_q(mpfr_div, child_divq_u05, "1e-100", "1e+100", 5 * NTEST, errorBound, 0);
checkAccuracyOuterLoop_q_q(mpfr_div, child_divq_u05, "0", "Inf", 5 * NTEST, errorBound, 1);
checkResult(success, maxError);
fprintf(stderr, "sqrtq_u05 : ");
maxError = 0;
cmpDenormOuterLoop_q(mpfr_sqrt, child_sqrtq_u05, stdCheckVals);
checkAccuracyOuterLoop_q(mpfr_sqrt, child_sqrtq_u05, "1e-100", "1e+100", 5 * NTEST, errorBound, 0);
checkAccuracyOuterLoop_q(mpfr_sqrt, child_sqrtq_u05, "0", "Inf", 5 * NTEST, errorBound, 1);
checkResult(success, maxError);
fprintf(stderr, "negq : ");
maxError = 0;
cmpDenormOuterLoop_q(mpfr_neg, child_negq, stdCheckVals);
checkAccuracyOuterLoop_q(mpfr_neg, child_negq, "1e-100", "1e+100", 5 * NTEST, errorBound, 0);
checkAccuracyOuterLoop_q(mpfr_neg, child_negq, "0", "Inf", 5 * NTEST, errorBound, 1);
checkResult(success, maxError);
if ((options & 2) != 0) {
fprintf(stderr, "strtoq : ");
for(int i=0;i<sizeof(stdCheckVals)/sizeof(char *);i++) {
Sleef_quad a0 = cast_q_str(stdCheckVals[i]);
Sleef_quad a1 = child_strtoq(stdCheckVals[i]);
if (memcmp(&a0, &a1, sizeof(Sleef_quad)) == 0) continue;
if (isnanf128(a0) && isnanf128(a1)) continue;
fprintf(stderr, "\narg = %s\ntest = %s\ncorrect = %s\n",
stdCheckVals[i], sprintf128(a1), sprintf128(a0));
success = 0;
break;
}
checkResult(success, maxError);
fprintf(stderr, "qtostr : ");
for(int i=0;i<sizeof(stdCheckVals)/sizeof(char *);i++) {
Sleef_quad a0 = cast_q_str(stdCheckVals[i]);
char s[100];
child_qtostr(s, a0);
Sleef_quad a1 = cast_q_str(s);
if (memcmp(&a0, &a1, sizeof(Sleef_quad)) == 0) continue;
if (isnanf128(a0) && isnanf128(a1)) continue;
fprintf(stderr, "\narg = %s\nteststr = %s\ntest = %s\ncorrect = %s\n",
stdCheckVals[i], s, sprintf128(a0), sprintf128(a1));
success = 0;
break;
}
checkResult(success, maxError);
}
}
int main(int argc, char **argv) {
char *argv2[argc+2], *commandSde = NULL;
int i, a2s, options;
// BUGFIX: this flush is to prevent incorrect syncing with the
// `iut*` executable that causes failures in the CPU detection on
// some CI systems.
fflush(stdout);
for(a2s=1;a2s<argc;a2s++) {
if (a2s+1 < argc && strcmp(argv[a2s], "--sde") == 0) {
commandSde = argv[a2s+1];
a2s++;
} else {
break;
}
}
for(i=a2s;i<argc;i++) argv2[i-a2s] = argv[i];
argv2[argc-a2s] = NULL;
startChild(argv2[0], argv2);
fflush(stdin);
// BUGFIX: this flush is to prevent incorrect syncing with the
// `iut*` executable that causes failures in the CPU detection on
// some CI systems.
fflush(stdin);
{
char str[256];
if (readln(ctop[0], str, 255) < 1 ||
sscanf(str, "%d", &options) != 1 ||
(options & 1) == 0) {
if (commandSde != NULL) {
close(ctop[0]);
close(ptoc[1]);
argv2[0] = commandSde;
argv2[1] = "--";
for(i=a2s;i<argc;i++) argv2[i-a2s+2] = argv[i];
argv2[argc-a2s+2] = NULL;
startChild(argv2[0], argv2);
if (readln(ctop[0], str, 255) < 1) stop("Feature detection(sde, readln)");
if (sscanf(str, "%d", &options) != 1) stop("Feature detection(sde, sscanf)");
if ((options & 1) == 0) {
fprintf(stderr, "\n\nTester : *** CPU does not support the necessary feature(SDE)\n");
return 0;
}
fprintf(stderr, "*** Using SDE\n");
} else {
fprintf(stderr, "\n\nTester : *** CPU does not support the necessary feature\n");
return 0;
}
}
}
fprintf(stderr, "\n\n*** qtester : now testing %s\n", argv2[0]);
fpctop = fdopen(ctop[0], "r");
do_test(options);
fprintf(stderr, "\n\n*** All tests passed\n");
exit(0);
}