math-underflow.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Check for underflow and force underflow exceptions.
  2. Copyright (C) 2015-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _MATH_UNDERFLOW_H
  16. #define _MATH_UNDERFLOW_H 1
  17. #include <float.h>
  18. #include <math.h>
  19. #include <math-barriers.h>
  20. #define fabs_tg(x) __MATH_TG ((x), (__typeof (x)) __builtin_fabs, (x))
  21. /* These must be function-like macros because some __MATH_TG
  22. implementations macro-expand the function-name argument before
  23. concatenating a suffix to it. */
  24. #define min_of_type_f() FLT_MIN
  25. #define min_of_type_() DBL_MIN
  26. #define min_of_type_l() LDBL_MIN
  27. #define min_of_type_f128() FLT128_MIN
  28. #define min_of_type(x) __MATH_TG ((x), (__typeof (x)) min_of_type_, ())
  29. /* If X (which is not a NaN) is subnormal, force an underflow
  30. exception. */
  31. #define math_check_force_underflow(x) \
  32. do \
  33. { \
  34. __typeof (x) force_underflow_tmp = (x); \
  35. if (fabs_tg (force_underflow_tmp) \
  36. < min_of_type (force_underflow_tmp)) \
  37. { \
  38. __typeof (force_underflow_tmp) force_underflow_tmp2 \
  39. = force_underflow_tmp * force_underflow_tmp; \
  40. math_force_eval (force_underflow_tmp2); \
  41. } \
  42. } \
  43. while (0)
  44. /* Likewise, but X is also known to be nonnegative. */
  45. #define math_check_force_underflow_nonneg(x) \
  46. do \
  47. { \
  48. __typeof (x) force_underflow_tmp = (x); \
  49. if (force_underflow_tmp \
  50. < min_of_type (force_underflow_tmp)) \
  51. { \
  52. __typeof (force_underflow_tmp) force_underflow_tmp2 \
  53. = force_underflow_tmp * force_underflow_tmp; \
  54. math_force_eval (force_underflow_tmp2); \
  55. } \
  56. } \
  57. while (0)
  58. /* Likewise, for both real and imaginary parts of a complex
  59. result. */
  60. #define math_check_force_underflow_complex(x) \
  61. do \
  62. { \
  63. __typeof (x) force_underflow_complex_tmp = (x); \
  64. math_check_force_underflow (__real__ force_underflow_complex_tmp); \
  65. math_check_force_underflow (__imag__ force_underflow_complex_tmp); \
  66. } \
  67. while (0)
  68. #endif /* math-underflow.h */