lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <math.h> |
| 2 | #include <float.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
| 5 | #include <limits.h> |
| 6 | #include <stdio.h> |
| 7 | |
| 8 | #define check_d1(func, param, expected) \ |
| 9 | do { \ |
| 10 | int err; hex_union ur; hex_union up; \ |
| 11 | double result = func(param); up.f = param; ur.f = result; \ |
| 12 | errors += (err = (result != (expected))); \ |
| 13 | err \ |
| 14 | ? printf("FAIL: %s(%g/"HEXFMT")=%g/"HEXFMT" (expected %g)\n", \ |
| 15 | #func, (double)(param), (long long)up.hex, result, (long long)ur.hex, (double)(expected)) \ |
| 16 | : printf("PASS: %s(%g)=%g\n", #func, (double)(param), result); \ |
| 17 | } while (0) |
| 18 | |
| 19 | #define check_i1(func, param, expected) \ |
| 20 | do { \ |
| 21 | int err; hex_union up; \ |
| 22 | long long result = func(param); up.f = param; \ |
| 23 | errors += (err = (result != (expected))); \ |
| 24 | err \ |
| 25 | ? printf("FAIL: %s(%g/"HEXFMT")=%lld/%llu (expected %llu)\n", \ |
| 26 | #func, (double)(param), (long long)up.hex, result, result, (long long)(expected)) \ |
| 27 | : printf("PASS: %s(%g)=%lld/%llu\n", #func, (double)(param), result, result); \ |
| 28 | } while (0) |
| 29 | |
| 30 | #define HEXFMT "%08llx" |
| 31 | typedef union { |
| 32 | double f; |
| 33 | uint64_t hex; |
| 34 | } hex_union; |
| 35 | |
| 36 | double zero = 0.0; |
| 37 | double minus_zero = 0.0; |
| 38 | double nan_value = 0.0; |
| 39 | int errors = 0; |
| 40 | |
| 41 | int main(void) |
| 42 | { |
| 43 | nan_value /= nan_value; |
| 44 | minus_zero = copysign(zero, -1.0); |
| 45 | |
| 46 | check_i1(isfinite, 1.0, 1); |
| 47 | check_i1(isfinite, 2.0, 1); |
| 48 | check_i1(isfinite, 3.0, 1); |
| 49 | check_i1(isfinite, DBL_MAX, 1); |
| 50 | check_i1(isfinite, FLT_MAX, 1); |
| 51 | check_i1(isfinite, HUGE_VAL, 0); |
| 52 | check_i1(isfinite, HUGE_VALF, 0); |
| 53 | check_i1(isfinite, HUGE_VALL, 0); |
| 54 | check_i1(isfinite, nan_value, 0); |
| 55 | check_i1(isfinite, nan_value, 0); |
| 56 | check_i1(isfinite, nan_value, 0); |
| 57 | |
| 58 | check_i1(isnan, 1.0, 0); |
| 59 | check_i1(isnan, 2.0, 0); |
| 60 | check_i1(isnan, 3.0, 0); |
| 61 | check_i1(isnan, DBL_MAX, 0); |
| 62 | check_i1(isnan, FLT_MAX, 0); |
| 63 | check_i1(isnan, HUGE_VAL, 0); |
| 64 | check_i1(isnan, HUGE_VALF, 0); |
| 65 | check_i1(isnan, HUGE_VALL, 0); |
| 66 | check_i1(isnan, (float)HUGE_VALL, 0); |
| 67 | check_i1(isnan, nan_value, 1); |
| 68 | check_i1(isnan, nan_value, 1); |
| 69 | check_i1(isnan, nan_value, 1); |
| 70 | |
| 71 | check_i1(isinf, 1.0, 0); |
| 72 | check_i1(isinf, 2.0, 0); |
| 73 | check_i1(isinf, 3.0, 0); |
| 74 | check_i1(isinf, DBL_MAX, 0); |
| 75 | check_i1(isinf, FLT_MAX, 0); |
| 76 | check_i1(isinf, (float)DBL_MAX, 1); |
| 77 | check_i1(isinf, HUGE_VAL, 1); |
| 78 | check_i1(isinf, HUGE_VALF, 1); |
| 79 | check_i1(isinf, HUGE_VALL, 1); |
| 80 | check_i1(isinf, (float)HUGE_VALL, 1); |
| 81 | check_i1(isinf, nan_value, 0); |
| 82 | check_i1(isinf, nan_value, 0); |
| 83 | check_i1(isinf, nan_value, 0); |
| 84 | |
| 85 | check_i1(fpclassify, minus_zero, FP_ZERO); |
| 86 | check_i1(fpclassify, 0.0, FP_ZERO); |
| 87 | check_i1(fpclassify, 1.0, FP_NORMAL); |
| 88 | check_i1(fpclassify, 2.0, FP_NORMAL); |
| 89 | check_i1(fpclassify, 3.0, FP_NORMAL); |
| 90 | check_i1(fpclassify, DBL_MIN/1.01, FP_SUBNORMAL); |
| 91 | check_i1(fpclassify, DBL_MIN, FP_NORMAL); |
| 92 | check_i1(fpclassify, DBL_MAX, FP_NORMAL); |
| 93 | check_i1(fpclassify, FLT_MAX, FP_NORMAL); |
| 94 | check_i1(fpclassify, DBL_MAX, FP_NORMAL); |
| 95 | check_i1(fpclassify, DBL_MAX*1.01, FP_INFINITE); |
| 96 | check_i1(fpclassify, HUGE_VAL, FP_INFINITE); |
| 97 | check_i1(fpclassify, HUGE_VALF, FP_INFINITE); |
| 98 | check_i1(fpclassify, HUGE_VALL, FP_INFINITE); |
| 99 | check_i1(fpclassify, (float)HUGE_VALL, FP_INFINITE); |
| 100 | check_i1(fpclassify, nan_value, FP_NAN); |
| 101 | check_i1(fpclassify, nan_value, FP_NAN); |
| 102 | check_i1(fpclassify, nan_value, FP_NAN); |
| 103 | |
| 104 | check_i1(!!signbit, -1.0, 1); |
| 105 | check_i1(!!signbit, minus_zero, 1); |
| 106 | check_i1(!!signbit, 0.0, 0); |
| 107 | check_i1(!!signbit, HUGE_VAL, 0); |
| 108 | check_i1(!!signbit, HUGE_VALF, 0); |
| 109 | check_i1(!!signbit, HUGE_VALL, 0); |
| 110 | check_i1(!!signbit, -HUGE_VAL, 1); |
| 111 | check_i1(!!signbit, -HUGE_VALF, 1); |
| 112 | check_i1(!!signbit, -HUGE_VALL, 1); |
| 113 | |
| 114 | printf("Errors: %d\n", errors); |
| 115 | return errors; |
| 116 | } |