fenv.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Copyright (C) 2008 HighTec EDV-Systeme GmbH.
  2. This file is part of GCC.
  3. GCC is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. GCC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #ifndef _FENV_H
  19. #define _FENV_H
  20. /* IEEE754 rounding modes */
  21. /* Macros according to ISO/IEC 9899:1999 */
  22. #define FE_TONEAREST 0x0UL /* Rounding toward nearest */
  23. #define FE_UPWARD 0x1UL /* Rounding toward +INFINITY */
  24. #define FE_DOWNWARD 0x2UL /* Rounding toward -INFINITY */
  25. #define FE_TOWARDZERO 0x3UL /* Rounding toward zero */
  26. extern inline int fesetround (int) __attribute__ ((always_inline,gnu_inline));
  27. extern inline int fegetround (void) __attribute__ ((always_inline,gnu_inline));
  28. /*
  29. A CALL saves the context and a RET restores the context.
  30. Therefore, a RET invalidates the changes in PSW.
  31. => Functions must be inlined and cannot be part of a lib.
  32. */
  33. /* Get current IEEE754 rounding mode */
  34. extern inline __attribute__ ((always_inline,gnu_inline))
  35. int fegetround (void)
  36. {
  37. /* PSW bitfield RM, bits [25:24] contains rounding mode */
  38. int res;
  39. __asm__ volatile ("mfcr %0, $psw " : "=d" (res) : : "memory");
  40. return (res & 0x03000000UL) >> 24uL;
  41. }
  42. /* Set IEEE754 rounding mode */
  43. extern inline __attribute__ ((always_inline,gnu_inline))
  44. int fesetround (int round)
  45. {
  46. /* Set rounding mode useing updfl */
  47. #if defined (ERRATA_CPU114)
  48. __asm__ volatile ("updfl %0" :: "d" (0xf00 | (round & 3)) : "memory");
  49. #else
  50. __asm__ volatile ("updfl %0" :: "d" (0x300 | (round & 3)) : "memory");
  51. #endif
  52. return 0;
  53. }
  54. #endif /* _FENV_H */