math-narrow-eval.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* Narrow floating-point values to their semantic type.
  2. Copyright (C) 2015-2026 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. <https://www.gnu.org/licenses/>. */
  15. #ifndef _MATH_NARROW_EVAL_H
  16. #define _MATH_NARROW_EVAL_H 1
  17. #include <float.h>
  18. /* math_narrow_eval reduces its floating-point argument to the range
  19. and precision of its semantic type. (The original evaluation may
  20. still occur with excess range and precision, so the result may be
  21. affected by double rounding.) */
  22. #if FLT_EVAL_METHOD == 0
  23. # define math_narrow_eval(x) (x)
  24. #else
  25. # if FLT_EVAL_METHOD == 1
  26. # define excess_precision(type) __builtin_types_compatible_p (type, float)
  27. # else
  28. # define excess_precision(type) (__builtin_types_compatible_p (type, float) \
  29. || __builtin_types_compatible_p (type, \
  30. double))
  31. # endif
  32. # define math_narrow_eval(x) \
  33. ({ \
  34. __typeof (x) math_narrow_eval_tmp = (x); \
  35. if (excess_precision (__typeof (math_narrow_eval_tmp))) \
  36. __asm__ ("" : "+m" (math_narrow_eval_tmp)); \
  37. math_narrow_eval_tmp; \
  38. })
  39. #endif
  40. #endif /* math-narrow-eval.h */