fenv.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (C) 1997-2016 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library 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 GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifndef _FENV_H
  15. # error "Never use <bits/fenv.h> directly; include <fenv.h> instead."
  16. #endif
  17. /* Define bits representing the exception. We use the bit positions
  18. of the appropriate bits in the FPU control word. */
  19. enum
  20. {
  21. FE_INVALID =
  22. #define FE_INVALID 0x01
  23. FE_INVALID,
  24. __FE_DENORM = 0x02,
  25. FE_DIVBYZERO =
  26. #define FE_DIVBYZERO 0x04
  27. FE_DIVBYZERO,
  28. FE_OVERFLOW =
  29. #define FE_OVERFLOW 0x08
  30. FE_OVERFLOW,
  31. FE_UNDERFLOW =
  32. #define FE_UNDERFLOW 0x10
  33. FE_UNDERFLOW,
  34. FE_INEXACT =
  35. #define FE_INEXACT 0x20
  36. FE_INEXACT
  37. };
  38. #define FE_ALL_EXCEPT \
  39. (FE_INEXACT | FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID)
  40. /* The ix87 FPU supports all of the four defined rounding modes. We
  41. use again the bit positions in the FPU control word as the values
  42. for the appropriate macros. */
  43. enum
  44. {
  45. FE_TONEAREST =
  46. #define FE_TONEAREST 0
  47. FE_TONEAREST,
  48. FE_DOWNWARD =
  49. #define FE_DOWNWARD 0x400
  50. FE_DOWNWARD,
  51. FE_UPWARD =
  52. #define FE_UPWARD 0x800
  53. FE_UPWARD,
  54. FE_TOWARDZERO =
  55. #define FE_TOWARDZERO 0xc00
  56. FE_TOWARDZERO
  57. };
  58. /* Type representing exception flags. */
  59. typedef unsigned short int fexcept_t;
  60. /* Type representing floating-point environment. This structure
  61. corresponds to the layout of the block written by the `fstenv'
  62. instruction and has additional fields for the contents of the MXCSR
  63. register as written by the `stmxcsr' instruction. */
  64. typedef struct
  65. {
  66. unsigned short int __control_word;
  67. unsigned short int __glibc_reserved1;
  68. unsigned short int __status_word;
  69. unsigned short int __glibc_reserved2;
  70. unsigned short int __tags;
  71. unsigned short int __glibc_reserved3;
  72. unsigned int __eip;
  73. unsigned short int __cs_selector;
  74. unsigned int __opcode:11;
  75. unsigned int __glibc_reserved4:5;
  76. unsigned int __data_offset;
  77. unsigned short int __data_selector;
  78. unsigned short int __glibc_reserved5;
  79. #ifdef __x86_64__
  80. unsigned int __mxcsr;
  81. #endif
  82. }
  83. fenv_t;
  84. /* If the default argument is used we use this value. */
  85. #define FE_DFL_ENV ((const fenv_t *) -1)
  86. #ifdef __USE_GNU
  87. /* Floating-point environment where none of the exception is masked. */
  88. # define FE_NOMASK_ENV ((const fenv_t *) -2)
  89. #endif
  90. #ifdef __USE_EXTERN_INLINES
  91. __BEGIN_DECLS
  92. /* Optimized versions. */
  93. extern int __REDIRECT_NTH (__feraiseexcept_renamed, (int), feraiseexcept);
  94. __extern_always_inline void
  95. __NTH (__feraiseexcept_invalid_divbyzero (int __excepts))
  96. {
  97. if ((FE_INVALID & __excepts) != 0)
  98. {
  99. /* One example of an invalid operation is 0.0 / 0.0. */
  100. float __f = 0.0;
  101. # ifdef __SSE_MATH__
  102. __asm__ __volatile__ ("divss %0, %0 " : : "x" (__f));
  103. # else
  104. __asm__ __volatile__ ("fdiv %%st, %%st(0); fwait"
  105. : "=t" (__f) : "0" (__f));
  106. # endif
  107. (void) &__f;
  108. }
  109. if ((FE_DIVBYZERO & __excepts) != 0)
  110. {
  111. float __f = 1.0;
  112. float __g = 0.0;
  113. # ifdef __SSE_MATH__
  114. __asm__ __volatile__ ("divss %1, %0" : : "x" (__f), "x" (__g));
  115. # else
  116. __asm__ __volatile__ ("fdivp %%st, %%st(1); fwait"
  117. : "=t" (__f) : "0" (__f), "u" (__g) : "st(1)");
  118. # endif
  119. (void) &__f;
  120. }
  121. }
  122. __extern_inline int
  123. __NTH (feraiseexcept (int __excepts))
  124. {
  125. if (__builtin_constant_p (__excepts)
  126. && (__excepts & ~(FE_INVALID | FE_DIVBYZERO)) == 0)
  127. {
  128. __feraiseexcept_invalid_divbyzero (__excepts);
  129. return 0;
  130. }
  131. return __feraiseexcept_renamed (__excepts);
  132. }
  133. __END_DECLS
  134. #endif