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