fenv.h (3407B)
1 /*- 2 * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG> 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 27 #ifndef _FENV_H_ 28 #define _FENV_H_ 29 30 31 // The floating-point environment for VX32 is just the 32-bit MXCSR register. 32 typedef unsigned fenv_t; 33 typedef unsigned fexcept_t; 34 35 /* Exception flags */ 36 #define FE_INVALID 0x01 37 #define FE_DENORMAL 0x02 38 #define FE_DIVBYZERO 0x04 39 #define FE_OVERFLOW 0x08 40 #define FE_UNDERFLOW 0x10 41 #define FE_INEXACT 0x20 42 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \ 43 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW) 44 45 /* Rounding modes */ 46 #define FE_TONEAREST 0x0000 47 #define FE_DOWNWARD 0x2000 48 #define FE_UPWARD 0x4000 49 #define FE_TOWARDZERO 0x6000 50 #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \ 51 FE_UPWARD | FE_TOWARDZERO) 52 53 /* Default floating-point environment for MXCSR */ 54 #define FE_DFL_ENV 0x0x1F80 55 56 57 #define __ldmxcsr(__csr) __asm __volatile("ldmxcsr %0" : : "m" (__csr)) 58 #define __stmxcsr(__csr) __asm __volatile("stmxcsr %0" : "=m" (*(__csr))) 59 60 static __inline int 61 feclearexcept(int __excepts) 62 { 63 fenv_t __env; 64 65 __stmxcsr(&__env); 66 __env &= ~__excepts; 67 __ldmxcsr(__env); 68 return (0); 69 } 70 71 static __inline int 72 fegetexceptflag(fexcept_t *__flagp, int __excepts) 73 { 74 unsigned __mxcsr; 75 76 __stmxcsr(&__mxcsr); 77 *__flagp = __mxcsr & __excepts; 78 return (0); 79 } 80 81 int fesetexceptflag(const fexcept_t *__flagp, int __excepts); 82 int feraiseexcept(int __excepts); 83 84 static __inline int 85 fetestexcept(int __excepts) 86 { 87 unsigned __mxcsr; 88 89 __stmxcsr(&__mxcsr); 90 return (__mxcsr & __excepts); 91 } 92 93 static __inline int 94 fegetround(void) 95 { 96 unsigned __mxcsr; 97 98 __stmxcsr(&__mxcsr); 99 return (__mxcsr & _ROUND_MASK); 100 } 101 102 static __inline int 103 fesetround(int __round) 104 { 105 unsigned __mxcsr; 106 107 if (__round & ~_ROUND_MASK) 108 return (-1); 109 110 __stmxcsr(&__mxcsr); 111 __mxcsr &= ~_ROUND_MASK; 112 __mxcsr |= __round; 113 __ldmxcsr(__mxcsr); 114 115 return (0); 116 } 117 118 int feholdexcept(fenv_t *__envp); 119 120 static __inline int 121 fegetenv(fenv_t *__envp) 122 { 123 __stmxcsr(__envp); 124 return (0); 125 } 126 127 static __inline int 128 fesetenv(const fenv_t *__envp) 129 { 130 __ldmxcsr(*__envp); 131 return (0); 132 } 133 134 int feupdateenv(const fenv_t *__envp); 135 136 137 #endif /* !_FENV_H_ */