fenv-softfloat.h revision 266133
1168404Spjd/*- 2168404Spjd * Copyright (c) 2004-2011 David Schultz <das@FreeBSD.ORG> 3168404Spjd * All rights reserved. 4168404Spjd * 5185029Spjd * Redistribution and use in source and binary forms, with or without 6185029Spjd * modification, are permitted provided that the following conditions 7168404Spjd * are met: 8168404Spjd * 1. Redistributions of source code must retain the above copyright 9168404Spjd * notice, this list of conditions and the following disclaimer. 10168404Spjd * 2. Redistributions in binary form must reproduce the above copyright 11168404Spjd * notice, this list of conditions and the following disclaimer in the 12168404Spjd * documentation and/or other materials provided with the distribution. 13168404Spjd * 14168404Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15168404Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16168404Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17168404Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18168404Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19168404Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20168404Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21168404Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22219089Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23224174Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24168404Spjd * SUCH DAMAGE. 25168404Spjd * 26185029Spjd * $FreeBSD: stable/10/lib/msun/src/fenv-softfloat.h 266133 2014-05-15 15:02:48Z ian $ 27185029Spjd */ 28168404Spjd 29168404Spjd#ifndef _FENV_H_ 30168404Spjd#error "This file is meant to be included only by <fenv.h>." 31168404Spjd#endif 32168404Spjd 33185029Spjd/* 34168404Spjd * This file implements the functionality of <fenv.h> on platforms that 35168404Spjd * lack an FPU and use softfloat in libc for floating point. To use it, 36168404Spjd * you must write an <fenv.h> that provides the following: 37224174Smm * 38224174Smm * - a typedef for fenv_t, which may be an integer or struct type 39224174Smm * - a typedef for fexcept_t (XXX This file assumes fexcept_t is a 40224174Smm * simple integer type containing the exception mask.) 41224174Smm * - definitions of FE_* constants for the five exceptions and four 42224174Smm * rounding modes in IEEE 754, as described in fenv(3) 43224174Smm * - a definition, and the corresponding external symbol, for FE_DFL_ENV 44224174Smm * - a macro __set_env(env, flags, mask, rnd), which sets the given fenv_t 45168404Spjd * from the exception flags, mask, and rounding mode 46168404Spjd * - macros __env_flags(env), __env_mask(env), and __env_round(env), which 47168404Spjd * extract fields from an fenv_t 48185029Spjd * - a definition of __fenv_static 49168404Spjd * 50185029Spjd * If the architecture supports an optional FPU, it's recommended that you 51185029Spjd * define fenv_t and fexcept_t to match the hardware ABI. Otherwise, it 52185029Spjd * doesn't matter how you define them. 53192800Strasz */ 54185029Spjd 55185029Spjdextern int __softfloat_float_exception_flags; 56224174Smmextern int __softfloat_float_exception_mask; 57224174Smmextern int __softfloat_float_rounding_mode; 58219089Spjdvoid __softfloat_float_raise(int); 59168404Spjd 60168404Spjd__fenv_static inline int 61224174Smmfeclearexcept(int __excepts) 62224174Smm{ 63224174Smm 64168404Spjd __softfloat_float_exception_flags &= ~__excepts; 65185029Spjd return (0); 66168404Spjd} 67168404Spjd 68168404Spjd__fenv_static inline int 69185029Spjdfegetexceptflag(fexcept_t *__flagp, int __excepts) 70{ 71 72 *__flagp = __softfloat_float_exception_flags & __excepts; 73 return (0); 74} 75 76__fenv_static inline int 77fesetexceptflag(const fexcept_t *__flagp, int __excepts) 78{ 79 80 __softfloat_float_exception_flags &= ~__excepts; 81 __softfloat_float_exception_flags |= *__flagp & __excepts; 82 return (0); 83} 84 85__fenv_static inline int 86feraiseexcept(int __excepts) 87{ 88 89 __softfloat_float_raise(__excepts); 90 return (0); 91} 92 93__fenv_static inline int 94fetestexcept(int __excepts) 95{ 96 97 return (__softfloat_float_exception_flags & __excepts); 98} 99 100__fenv_static inline int 101fegetround(void) 102{ 103 104 return (__softfloat_float_rounding_mode); 105} 106 107__fenv_static inline int 108fesetround(int __round) 109{ 110 111 __softfloat_float_rounding_mode = __round; 112 return (0); 113} 114 115__fenv_static inline int 116fegetenv(fenv_t *__envp) 117{ 118 119 __set_env(*__envp, __softfloat_float_exception_flags, 120 __softfloat_float_exception_mask, __softfloat_float_rounding_mode); 121 return (0); 122} 123 124__fenv_static inline int 125feholdexcept(fenv_t *__envp) 126{ 127 fenv_t __env; 128 129 fegetenv(__envp); 130 __softfloat_float_exception_flags = 0; 131 __softfloat_float_exception_mask = 0; 132 return (0); 133} 134 135__fenv_static inline int 136fesetenv(const fenv_t *__envp) 137{ 138 139 __softfloat_float_exception_flags = __env_flags(*__envp); 140 __softfloat_float_exception_mask = __env_mask(*__envp); 141 __softfloat_float_rounding_mode = __env_round(*__envp); 142 return (0); 143} 144 145__fenv_static inline int 146feupdateenv(const fenv_t *__envp) 147{ 148 int __oflags = __softfloat_float_exception_flags; 149 150 fesetenv(__envp); 151 feraiseexcept(__oflags); 152 return (0); 153} 154 155#if __BSD_VISIBLE 156 157/* We currently provide no external definitions of the functions below. */ 158 159__fenv_static inline int 160feenableexcept(int __mask) 161{ 162 int __omask = __softfloat_float_exception_mask; 163 164 __softfloat_float_exception_mask |= __mask; 165 return (__omask); 166} 167 168__fenv_static inline int 169fedisableexcept(int __mask) 170{ 171 int __omask = __softfloat_float_exception_mask; 172 173 __softfloat_float_exception_mask &= ~__mask; 174 return (__omask); 175} 176 177__fenv_static inline int 178fegetexcept(void) 179{ 180 181 return (__softfloat_float_exception_mask); 182} 183 184#endif /* __BSD_VISIBLE */ 185