9 #include <fpu_control.h>
11 /*======================================================================*/
12 /* declarations for the functions we are testing */
14 double __adddf3(double, double);
15 double __subdf3(double, double);
16 double __muldf3(double, double);
17 double __divdf3(double, double);
18 double __negdf2(double);
19 double __sqrtdf2(double);
20 double __negdf3(double a
, double dummy
) { return __negdf2(a
); }
21 double __sqrtdf3(double a
, double dummy
) { return __sqrtdf2(a
); }
23 float __addsf3(float, float);
24 float __subsf3(float, float);
25 float __mulsf3(float, float);
26 float __divsf3(float, float);
27 float __negsf2(float);
28 float __sqrtsf2(float);
29 float __negsf3(float a
, float dummy
) { return __negsf2(a
); }
30 float __sqrtsf3(float a
, float dummy
) { return __sqrtsf2(a
); }
32 int __fixdfsi(double);
34 double __floatsidf(int);
35 float __floatsisf(int);
36 double __extendsfdf2(float);
37 float __truncdfsf2(double);
39 int __eqdf2(double, double);
40 int __nedf2(double, double);
41 int __gtdf2(double, double);
42 int __gedf2(double, double);
43 int __ltdf2(double, double);
44 int __ledf2(double, double);
46 int __eqsf2(float, float);
47 int __nesf2(float, float);
48 int __gtsf2(float, float);
49 int __gesf2(float, float);
50 int __ltsf2(float, float);
51 int __lesf2(float, float);
53 /*======================================================================*/
54 /* definitions for functions we are checking against */
56 double r_adddf3(double a
, double b
) { return a
+ b
; }
57 double r_subdf3(double a
, double b
) { return a
- b
; }
58 double r_muldf3(double a
, double b
) { return a
* b
; }
59 double r_divdf3(double a
, double b
) { return a
/ b
; }
60 double r_negdf3(double a
, double b
) { return -a
; }
61 double sqrt(double x
);
62 double r_sqrtdf3(double a
, double b
) { return sqrt(a
); }
64 float r_addsf3(float a
, float b
) { return a
+ b
; }
65 float r_subsf3(float a
, float b
) { return a
- b
; }
66 float r_mulsf3(float a
, float b
) { return a
* b
; }
67 float r_divsf3(float a
, float b
) { return a
/ b
; }
68 float r_negsf3(float a
, float b
) { return -a
; }
70 float r_sqrtsf3(float a
, float b
) { return sqrtf(a
); }
72 int r_fixdfsi(double a
) { return (int)a
; }
73 int r_fixsfsi(float a
) { return (int)a
; }
74 double r_floatsidf(int a
) { return (double)a
; }
75 float r_floatsisf(int a
) { return (float)a
; }
76 double r_extendsfdf2(float a
) { return (double)a
; }
77 float r_truncdfsf2(double a
) { return (float)a
; }
79 int r_eqdf2(double a
, double b
) { return !(a
== b
); }
80 int r_nedf2(double a
, double b
) { return a
!= b
; }
81 int r_gtdf2(double a
, double b
) { return a
> b
; }
82 int r_gedf2(double a
, double b
) { return (a
>= b
) - 1; }
83 int r_ltdf2(double a
, double b
) { return -(a
< b
); }
84 int r_ledf2(double a
, double b
) { return 1 - (a
<= b
); }
86 int r_eqsf2(float a
, float b
) { return !(a
== b
); }
87 int r_nesf2(float a
, float b
) { return a
!= b
; }
88 int r_gtsf2(float a
, float b
) { return a
> b
; }
89 int r_gesf2(float a
, float b
) { return (a
>= b
) - 1; }
90 int r_ltsf2(float a
, float b
) { return -(a
< b
); }
91 int r_lesf2(float a
, float b
) { return 1 - (a
<= b
); }
93 /*======================================================================*/
95 void print_float(float x
)
99 printf("%-20.8e %X %02X %06lX",
100 x
, ux
.bits
.sign
, ux
.bits
.exp
, (unsigned long)ux
.bits
.frac
);
103 void print_double(double x
)
105 union _FP_UNION_D ux
;
107 #if _FP_W_TYPE_SIZE < _FP_FRACBITS_D
108 printf("%-30.18e %X %04X %06lX%08lX",
109 x
, ux
.bits
.sign
, ux
.bits
.exp
,
110 (unsigned long)ux
.bits
.frac1
, (unsigned long)ux
.bits
.frac0
);
112 printf("%-30.18e %X %04X %014lX",
113 x
, ux
.bits
.sign
, ux
.bits
.exp
,
114 (unsigned long)ux
.bits
.frac
);
118 float rand_float(void)
125 u
.i
= lrand48() << 1;
127 if (u
.u
.bits
.exp
== _FP_EXPMAX_S
)
129 else if (u
.u
.bits
.exp
== 0 && u
.u
.bits
.frac
!= 0)
136 double rand_double(void)
143 u
.i
[0] = lrand48() << 1;
144 u
.i
[1] = lrand48() << 1;
146 if (u
.u
.bits
.exp
== _FP_EXPMAX_D
)
148 #if _FP_W_TYPE_SIZE < _FP_FRACBITS_D
149 else if (u
.u
.bits
.exp
== 0 && !(u
.u
.bits
.frac0
== 0 && u
.u
.bits
.frac1
== 0))
152 else if (u
.u
.bits
.exp
== 0 && u
.u
.bits
.frac
!= 0)
161 float gen_special_float(int i
)
170 X_c
= FP_CLS_NAN
; X_f
= 0x1234;
173 X_c
= FP_CLS_NAN
; X_f
= 0x1;
182 X_c
= FP_CLS_NORMAL
; X_e
= 0;
192 double gen_special_double(int i
)
202 #if _FP_W_TYPE_SIZE < _FP_FRACBITS_D
203 __FP_FRAC_SET_2(X
, _FP_QNANNEGATEDP
? 0 : _FP_QNANBIT_D
, 0x1234);
205 _FP_FRAC_SET_1(X
, (_FP_QNANNEGATEDP
? 0 : _FP_QNANBIT_D
) | 0x1234);
210 #if _FP_W_TYPE_SIZE < _FP_FRACBITS_D
211 __FP_FRAC_SET_2(X
, _FP_QNANNEGATEDP
? 0 : _FP_QNANBIT_D
, 0x1);
213 _FP_FRAC_SET_1(X
, (_FP_QNANNEGATEDP
? 0 : _FP_QNANBIT_D
) | 0x1);
223 X_c
= FP_CLS_NORMAL
; X_e
= 0;
224 #if _FP_W_TYPE_SIZE < _FP_FRACBITS_D
225 __FP_FRAC_SET_2(X
, 0, 0x87654321);
227 _FP_FRAC_SET_1(X
, 0x87654321);
237 float build_float(const char *s
, const char *e
, const char *f
)
241 u
.bits
.sign
= strtoul(s
, 0, 16);
242 u
.bits
.exp
= strtoul(e
, 0, 16);
243 u
.bits
.frac
= strtoul(f
, 0, 16);
248 double build_double(const char *s
, const char *e
, const char *f
)
252 u
.bits
.sign
= strtoul(s
, 0, 16);
253 u
.bits
.exp
= strtoul(e
, 0, 16);
254 #if _FP_W_TYPE_SIZE < _FP_FRACBITS_D
256 size_t len
= strlen(f
)+1;
257 char *dup
= memcpy(alloca(len
), f
, len
);
258 char *low
= dup
+ len
- _FP_W_TYPE_SIZE
/4 - 1;
260 u
.bits
.frac0
= strtoul(low
, 0, 16);
262 u
.bits
.frac1
= strtoul(dup
, 0, 16);
265 u
.bits
.frac
= strtoul(f
, 0, 16);
271 /*======================================================================*/
273 fpu_control_t fcw0
, fcw1
;
275 void test_float_arith(float (*tf
)(float, float),
276 float (*rf
)(float, float),
282 if (memcmp(&tr
, &rr
, sizeof(float)) != 0)
284 fputs("error:\n\tx = ", stdout
); print_float(x
);
285 fputs("\n\ty = ", stdout
); print_float(y
);
286 fputs("\n\ttrue = ", stdout
); print_float(rr
);
287 fputs("\n\tfalse = ", stdout
); print_float(tr
);
292 void test_double_arith(double (*tf
)(double, double),
293 double (*rf
)(double, double),
298 /* Don't worry. Even this does not make it error free
299 on ia32. If the result is denormal, it will not
300 honour the double precision and generate bad results
301 anyway. On the other side, who wants to use ia32
302 for IEEE math? I don't. */
304 fcw1
= ((fcw0
& ~_FPU_EXTENDED
) | _FPU_DOUBLE
);
312 if (memcmp(&tr
, &rr
, sizeof(double)) != 0)
314 fputs("error:\n\tx = ", stdout
); print_double(x
);
315 fputs("\n\ty = ", stdout
); print_double(y
);
316 fputs("\n\ttrue = ", stdout
); print_double(rr
);
317 fputs("\n\tfalse = ", stdout
); print_double(tr
);
322 void test_float_double_conv(float x
)
325 rr
= r_extendsfdf2(x
);
326 tr
= __extendsfdf2(x
);
327 if (memcmp(&tr
, &rr
, sizeof(double)) != 0)
329 fputs("error:\n\tx = ", stdout
); print_float(x
);
330 fputs("\n\ttrue = ", stdout
); print_double(rr
);
331 fputs("\n\tfalse = ", stdout
); print_double(tr
);
336 void test_double_float_conv(double x
)
339 rr
= r_truncdfsf2(x
);
340 tr
= __truncdfsf2(x
);
341 if (memcmp(&tr
, &rr
, sizeof(float)) != 0)
343 fputs("error:\n\tx = ", stdout
); print_double(x
);
344 fputs("\n\ttrue = ", stdout
); print_float(rr
);
345 fputs("\n\tfalse = ", stdout
); print_float(tr
);
350 void test_int_float_conv(int x
)
355 if (memcmp(&tr
, &rr
, sizeof(float)) != 0)
357 printf("error\n\tx = %d", x
);
358 fputs("\n\ttrue = ", stdout
); print_float(rr
);
359 fputs("\n\tfalse = ", stdout
); print_float(tr
);
364 void test_int_double_conv(int x
)
369 if (memcmp(&tr
, &rr
, sizeof(double)) != 0)
371 printf("error\n\tx = %d", x
);
372 fputs("\n\ttrue = ", stdout
); print_double(rr
);
373 fputs("\n\tfalse = ", stdout
); print_double(tr
);
378 void test_float_int_conv(float x
)
385 fputs("error:\n\tx = ", stdout
); print_float(x
);
386 printf("\n\ttrue = %d\n\tfalse = %d\n", rr
, tr
);
390 void test_double_int_conv(double x
)
397 fputs("error:\n\tx = ", stdout
); print_double(x
);
398 printf("\n\ttrue = %d\n\tfalse = %d\n", rr
, tr
);
402 int eq0(int x
) { return x
== 0; }
403 int ne0(int x
) { return x
!= 0; }
404 int le0(int x
) { return x
<= 0; }
405 int lt0(int x
) { return x
< 0; }
406 int ge0(int x
) { return x
>= 0; }
407 int gt0(int x
) { return x
> 0; }
409 void test_float_cmp(int (*tf
)(float, float),
410 int (*rf
)(float, float),
417 if (cmp0(rr
) != cmp0(tr
))
419 fputs("error:\n\tx = ", stdout
); print_float(x
);
420 fputs("\n\ty = ", stdout
); print_float(y
);
421 printf("\n\ttrue = %d\n\tfalse = %d\n", rr
, tr
);
425 void test_double_cmp(int (*tf
)(double, double),
426 int (*rf
)(double, double),
433 if (cmp0(rr
) != cmp0(tr
))
435 fputs("error:\n\tx = ", stdout
); print_double(x
);
436 fputs("\n\ty = ", stdout
); print_double(y
);
437 printf("\n\ttrue = %d\n\tfalse = %d\n", rr
, tr
);
442 /*======================================================================*/
445 int main(int ac
, char **av
)
448 __ieee_set_fp_control(0);
454 float (*r
)(float, float);
455 float (*t
)(float, float);
458 case 'a': r
= r_addsf3
; t
= __addsf3
; break;
459 case 's': r
= r_subsf3
; t
= __subsf3
; break;
460 case 'm': r
= r_mulsf3
; t
= __mulsf3
; break;
461 case 'd': r
= r_divsf3
; t
= __divsf3
; break;
462 case 'r': r
= r_sqrtsf3
; t
= __sqrtsf3
; break;
463 case 'j': r
= r_negsf3
; t
= __negsf3
; break;
470 int count
= (ac
> 1 ? atoi(av
[1]) : 100);
472 test_float_arith(t
, r
, rand_float(), rand_float());
479 for (i
= 0; i
< NSPECIALS
; i
++)
480 for (j
= 0; j
< NSPECIALS
; j
++)
481 test_float_arith(t
, r
, gen_special_float(i
),
482 gen_special_float(j
));
488 test_float_arith(t
, r
, build_float(av
[1], av
[2], av
[3]),
489 build_float(av
[4], av
[5], av
[6]));
496 double (*r
)(double, double);
497 double (*t
)(double, double);
500 case 'A': r
= r_adddf3
; t
= __adddf3
; break;
501 case 'S': r
= r_subdf3
; t
= __subdf3
; break;
502 case 'M': r
= r_muldf3
; t
= __muldf3
; break;
503 case 'D': r
= r_divdf3
; t
= __divdf3
; break;
504 case 'R': r
= r_sqrtdf3
; t
= __sqrtdf3
; break;
505 case 'J': r
= r_negdf3
; t
= __negdf3
; break;
512 int count
= (ac
> 1 ? atoi(av
[1]) : 100);
514 test_double_arith(t
, r
, rand_double(), rand_double());
521 for (i
= 0; i
< NSPECIALS
; i
++)
522 for (j
= 0; j
< NSPECIALS
; j
++)
523 test_double_arith(t
, r
, gen_special_double(i
),
524 gen_special_double(j
));
530 test_double_arith(t
, r
, build_double(av
[1], av
[2], av
[3]),
531 build_double(av
[4], av
[5], av
[6]));
542 int count
= (ac
> 1 ? atoi(av
[1]) : 100);
544 test_float_double_conv(rand_float());
551 for (i
= 0; i
< NSPECIALS
; i
++)
552 test_float_double_conv(gen_special_float(i
));
558 test_float_double_conv(build_float(av
[1], av
[2], av
[3]));
568 int count
= (ac
> 1 ? atoi(av
[1]) : 100);
570 test_double_float_conv(rand_double());
577 for (i
= 0; i
< NSPECIALS
; i
++)
578 test_double_float_conv(gen_special_double(i
));
584 test_double_float_conv(build_double(av
[1], av
[2], av
[3]));
594 int count
= (ac
> 1 ? atoi(av
[1]) : 100);
596 test_int_float_conv(lrand48() << 1);
602 test_int_float_conv(strtol(av
[1], 0, 0));
612 int count
= (ac
> 1 ? atoi(av
[1]) : 100);
614 test_int_double_conv(lrand48() << 1);
620 test_int_double_conv(strtol(av
[1], 0, 0));
630 int count
= (ac
> 1 ? atoi(av
[1]) : 100);
632 test_float_int_conv(rand_float());
639 for (i
= 0; i
< NSPECIALS
; i
++)
640 test_float_int_conv(gen_special_float(i
));
646 test_float_int_conv(build_float(av
[1], av
[2], av
[3]));
656 int count
= (ac
> 1 ? atoi(av
[1]) : 100);
658 test_double_int_conv(rand_double());
665 for (i
= 0; i
< NSPECIALS
; i
++)
666 test_double_int_conv(gen_special_double(i
));
672 test_double_int_conv(build_double(av
[1], av
[2], av
[3]));
678 int (*r
)(float, float);
679 int (*t
)(float, float);
683 case 'e': r
= r_eqsf2
; t
= __eqsf2
; c
= eq0
; break;
684 case 'n': r
= r_nesf2
; t
= __nesf2
; c
= ne0
; break;
688 case 'e': r
= r_lesf2
; t
= __lesf2
; c
= le0
; break;
689 case 't': r
= r_ltsf2
; t
= __ltsf2
; c
= lt0
; break;
695 case 'e': r
= r_gesf2
; t
= __gesf2
; c
= ge0
; break;
696 case 't': r
= r_gtsf2
; t
= __gtsf2
; c
= gt0
; break;
705 int count
= (ac
> 1 ? atoi(av
[1]) : 100);
707 test_float_cmp(t
, r
, c
, rand_float(), rand_float());
714 for (i
= 0; i
< NSPECIALS
; i
++)
715 for (j
= 0; j
< NSPECIALS
; j
++)
716 test_float_cmp(t
, r
, c
, gen_special_float(i
),
717 gen_special_float(j
));
723 test_float_cmp(t
, r
, c
, build_float(av
[1], av
[2], av
[3]),
724 build_float(av
[4], av
[5], av
[6]));
731 int (*r
)(double, double);
732 int (*t
)(double, double);
736 case 'E': r
= r_eqdf2
; t
= __eqdf2
; c
= eq0
; break;
737 case 'N': r
= r_nedf2
; t
= __nedf2
; c
= ne0
; break;
741 case 'E': r
= r_ledf2
; t
= __ledf2
; c
= le0
; break;
742 case 'T': r
= r_ltdf2
; t
= __ltdf2
; c
= lt0
; break;
748 case 'E': r
= r_gedf2
; t
= __gedf2
; c
= ge0
; break;
749 case 'T': r
= r_gtdf2
; t
= __gtdf2
; c
= gt0
; break;
758 int count
= (ac
> 1 ? atoi(av
[1]) : 100);
760 test_double_cmp(t
, r
, c
, rand_double(), rand_double());
767 for (i
= 0; i
< NSPECIALS
; i
++)
768 for (j
= 0; j
< NSPECIALS
; j
++)
769 test_double_cmp(t
, r
, c
, gen_special_double(i
),
770 gen_special_double(j
));
776 test_double_cmp(t
, r
, c
, build_double(av
[1], av
[2], av
[3]),
777 build_double(av
[4], av
[5], av
[6]));