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 nan_value = 0.0; |
| 37 | int errors = 0; |
| 38 | |
| 39 | int main(void) |
| 40 | { |
| 41 | nan_value /= nan_value; |
| 42 | |
| 43 | check_i1(ilogb, 0.0, FP_ILOGB0); |
| 44 | check_i1(ilogb, HUGE_VAL, INT_MAX); |
| 45 | check_i1(ilogb, nan_value, FP_ILOGBNAN); |
| 46 | check_i1(ilogbf, 0.0, FP_ILOGB0); |
| 47 | check_i1(ilogbf, HUGE_VALF, INT_MAX); |
| 48 | check_i1(ilogbf, nan_value, FP_ILOGBNAN); |
| 49 | |
| 50 | printf("Errors: %d\n", errors); |
| 51 | return errors; |
| 52 | } |