ilogb.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #define check_d1(func, param, expected) \
  8. do { \
  9. int err; hex_union ur; hex_union up; \
  10. double result = func(param); up.f = param; ur.f = result; \
  11. errors += (err = (result != (expected))); \
  12. err \
  13. ? printf("FAIL: %s(%g/"HEXFMT")=%g/"HEXFMT" (expected %g)\n", \
  14. #func, (double)(param), (long long)up.hex, result, (long long)ur.hex, (double)(expected)) \
  15. : printf("PASS: %s(%g)=%g\n", #func, (double)(param), result); \
  16. } while (0)
  17. #define check_i1(func, param, expected) \
  18. do { \
  19. int err; hex_union up; \
  20. long long result = func(param); up.f = param; \
  21. errors += (err = (result != (expected))); \
  22. err \
  23. ? printf("FAIL: %s(%g/"HEXFMT")=%lld/%llu (expected %llu)\n", \
  24. #func, (double)(param), (long long)up.hex, result, result, (long long)(expected)) \
  25. : printf("PASS: %s(%g)=%lld/%llu\n", #func, (double)(param), result, result); \
  26. } while (0)
  27. #define HEXFMT "%08llx"
  28. typedef union {
  29. double f;
  30. uint64_t hex;
  31. } hex_union;
  32. double nan_value = 0.0;
  33. int errors = 0;
  34. int main(void)
  35. {
  36. nan_value /= nan_value;
  37. check_i1(ilogb, 0.0, FP_ILOGB0);
  38. check_i1(ilogb, HUGE_VAL, INT_MAX);
  39. check_i1(ilogb, nan_value, FP_ILOGBNAN);
  40. check_i1(ilogbf, 0.0, FP_ILOGB0);
  41. check_i1(ilogbf, HUGE_VALF, INT_MAX);
  42. check_i1(ilogbf, nan_value, FP_ILOGBNAN);
  43. printf("Errors: %d\n", errors);
  44. return errors;
  45. }