fenv.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Copyright (C) 1997-2019 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. #if __GLIBC_USE (IEC_60559_BFP_EXT)
  91. /* Type representing floating-point control modes. */
  92. typedef struct
  93. {
  94. unsigned short int __control_word;
  95. unsigned short int __glibc_reserved;
  96. unsigned int __mxcsr;
  97. }
  98. femode_t;
  99. /* Default floating-point control modes. */
  100. # define FE_DFL_MODE ((const femode_t *) -1L)
  101. #endif
  102. #ifdef __USE_EXTERN_INLINES
  103. __BEGIN_DECLS
  104. /* Optimized versions. */
  105. #ifndef _LIBC
  106. extern int __REDIRECT_NTH (__feraiseexcept_renamed, (int), feraiseexcept);
  107. #endif
  108. __extern_always_inline void
  109. __NTH (__feraiseexcept_invalid_divbyzero (int __excepts))
  110. {
  111. if ((FE_INVALID & __excepts) != 0)
  112. {
  113. /* One example of an invalid operation is 0.0 / 0.0. */
  114. float __f = 0.0;
  115. # ifdef __SSE_MATH__
  116. __asm__ __volatile__ ("divss %0, %0 " : : "x" (__f));
  117. # else
  118. __asm__ __volatile__ ("fdiv %%st, %%st(0); fwait"
  119. : "=t" (__f) : "0" (__f));
  120. # endif
  121. (void) &__f;
  122. }
  123. if ((FE_DIVBYZERO & __excepts) != 0)
  124. {
  125. float __f = 1.0;
  126. float __g = 0.0;
  127. # ifdef __SSE_MATH__
  128. __asm__ __volatile__ ("divss %1, %0" : : "x" (__f), "x" (__g));
  129. # else
  130. __asm__ __volatile__ ("fdivp %%st, %%st(1); fwait"
  131. : "=t" (__f) : "0" (__f), "u" (__g) : "st(1)");
  132. # endif
  133. (void) &__f;
  134. }
  135. }
  136. __extern_inline int
  137. __NTH (feraiseexcept (int __excepts))
  138. {
  139. if (__builtin_constant_p (__excepts)
  140. && (__excepts & ~(FE_INVALID | FE_DIVBYZERO)) == 0)
  141. {
  142. __feraiseexcept_invalid_divbyzero (__excepts);
  143. return 0;
  144. }
  145. return __feraiseexcept_renamed (__excepts);
  146. }
  147. __END_DECLS
  148. #endif