fenv-softfloat.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*-
  2. * Copyright (c) 2004-2011 David Schultz <[email protected]>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * $FreeBSD$
  27. */
  28. #ifndef _FENV_H_
  29. #error "This file is meant to be included only by <fenv.h>."
  30. #endif
  31. /*
  32. * This file implements the functionality of <fenv.h> on platforms that
  33. * lack an FPU and use softfloat in libc for floating point. To use it,
  34. * you must write an <fenv.h> that provides the following:
  35. *
  36. * - a typedef for fenv_t, which may be an integer or struct type
  37. * - a typedef for fexcept_t (XXX This file assumes fexcept_t is a
  38. * simple integer type containing the exception mask.)
  39. * - definitions of FE_* constants for the five exceptions and four
  40. * rounding modes in IEEE 754, as described in fenv(3)
  41. * - a definition, and the corresponding external symbol, for FE_DFL_ENV
  42. * - a macro __set_env(env, flags, mask, rnd), which sets the given fenv_t
  43. * from the exception flags, mask, and rounding mode
  44. * - macros __env_flags(env), __env_mask(env), and __env_round(env), which
  45. * extract fields from an fenv_t
  46. * - a definition of __fenv_static
  47. *
  48. * If the architecture supports an optional FPU, it's recommended that you
  49. * define fenv_t and fexcept_t to match the hardware ABI. Otherwise, it
  50. * doesn't matter how you define them.
  51. */
  52. extern int __softfloat_float_exception_flags;
  53. extern int __softfloat_float_exception_mask;
  54. extern int __softfloat_float_rounding_mode;
  55. void __softfloat_float_raise(int);
  56. __fenv_static inline int
  57. feclearexcept(int __excepts)
  58. {
  59. __softfloat_float_exception_flags &= ~__excepts;
  60. return (0);
  61. }
  62. __fenv_static inline int
  63. fegetexceptflag(fexcept_t *__flagp, int __excepts)
  64. {
  65. *__flagp = __softfloat_float_exception_flags & __excepts;
  66. return (0);
  67. }
  68. __fenv_static inline int
  69. fesetexceptflag(const fexcept_t *__flagp, int __excepts)
  70. {
  71. __softfloat_float_exception_flags &= ~__excepts;
  72. __softfloat_float_exception_flags |= *__flagp & __excepts;
  73. return (0);
  74. }
  75. __fenv_static inline int
  76. feraiseexcept(int __excepts)
  77. {
  78. __softfloat_float_raise(__excepts);
  79. return (0);
  80. }
  81. __fenv_static inline int
  82. fetestexcept(int __excepts)
  83. {
  84. return (__softfloat_float_exception_flags & __excepts);
  85. }
  86. __fenv_static inline int
  87. fegetround(void)
  88. {
  89. return (__softfloat_float_rounding_mode);
  90. }
  91. __fenv_static inline int
  92. fesetround(int __round)
  93. {
  94. __softfloat_float_rounding_mode = __round;
  95. return (0);
  96. }
  97. __fenv_static inline int
  98. fegetenv(fenv_t *__envp)
  99. {
  100. __set_env(*__envp, __softfloat_float_exception_flags,
  101. __softfloat_float_exception_mask, __softfloat_float_rounding_mode);
  102. return (0);
  103. }
  104. __fenv_static inline int
  105. feholdexcept(fenv_t *__envp)
  106. {
  107. fenv_t __env;
  108. fegetenv(__envp);
  109. __softfloat_float_exception_flags = 0;
  110. __softfloat_float_exception_mask = 0;
  111. return (0);
  112. }
  113. __fenv_static inline int
  114. fesetenv(const fenv_t *__envp)
  115. {
  116. __softfloat_float_exception_flags = __env_flags(*__envp);
  117. __softfloat_float_exception_mask = __env_mask(*__envp);
  118. __softfloat_float_rounding_mode = __env_round(*__envp);
  119. return (0);
  120. }
  121. __fenv_static inline int
  122. feupdateenv(const fenv_t *__envp)
  123. {
  124. int __oflags = __softfloat_float_exception_flags;
  125. fesetenv(__envp);
  126. feraiseexcept(__oflags);
  127. return (0);
  128. }
  129. #if __BSD_VISIBLE
  130. /* We currently provide no external definitions of the functions below. */
  131. __fenv_static inline int
  132. feenableexcept(int __mask)
  133. {
  134. int __omask = __softfloat_float_exception_mask;
  135. __softfloat_float_exception_mask |= __mask;
  136. return (__omask);
  137. }
  138. __fenv_static inline int
  139. fedisableexcept(int __mask)
  140. {
  141. int __omask = __softfloat_float_exception_mask;
  142. __softfloat_float_exception_mask &= ~__mask;
  143. return (__omask);
  144. }
  145. __fenv_static inline int
  146. fegetexcept(void)
  147. {
  148. return (__softfloat_float_exception_mask);
  149. }
  150. #endif /* __BSD_VISIBLE */