quadmath-rounding-mode.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* GCC Quad-Precision Math Library
  2. Copyright (C) 2012 Free Software Foundation, Inc.
  3. This file is part of the libquadmath library.
  4. Libquadmath is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. Libquadmath 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. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with libquadmath; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #ifndef QUADMATH_ROUNDING_MODE_H
  17. #define QUADMATH_ROUNDING_MODE_H
  18. #include <stdbool.h>
  19. #if defined(HAVE_FENV_H)
  20. # include <fenv.h>
  21. #endif /* HAVE_FENV_H */
  22. static inline int
  23. get_rounding_mode (void)
  24. {
  25. #if defined(HAVE_FENV_H) && (defined(FE_DOWNWARD) || defined(FE_TONEAREST) \
  26. || defined(FE_TOWARDZERO) || defined(FE_UPWARD))
  27. return fegetround ();
  28. #else
  29. return 0;
  30. #endif
  31. }
  32. static inline bool
  33. round_away (bool negative, bool last_digit_odd, bool half_bit, bool more_bits,
  34. int mode)
  35. {
  36. switch (mode)
  37. {
  38. #ifdef FE_DOWNWARD
  39. case FE_DOWNWARD:
  40. return negative && (half_bit || more_bits);
  41. #endif
  42. #ifdef FE_DOWNWARD
  43. case FE_TONEAREST:
  44. return half_bit && (last_digit_odd || more_bits);
  45. #endif
  46. #ifdef FE_TOWARDZERO
  47. case FE_TOWARDZERO:
  48. return false;
  49. #endif
  50. #ifdef FE_UPWARD
  51. case FE_UPWARD:
  52. return !negative && (half_bit || more_bits);
  53. #endif
  54. default:
  55. return false;
  56. }
  57. }
  58. #endif /* QUADMATH_ROUNDING_MODE_H */