1 /*- 2 * Copyright (c) 2004-2005 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 * $FreeBSD: src/lib/msun/i387/fenv.c,v 1.8 2011/10/21 06:25:31 das Exp $ 27 */ 28 29 #include <cdefs-compat.h> 30 #include <types-compat.h> 31 #include <math_private.h> 32 #include <i387/bsd_npx.h> 33 34 #define __fenv_static 35 #include "fenv.h" 36 37 #ifdef __GNUC_GNU_INLINE__ 38 #error "This file must be compiled with C99 'inline' semantics" 39 #endif 40 41 const fenv_t __fe_dfl_env = { 42 __INITIAL_NPXCW__, 43 0x0000, 44 0x0000, 45 0x1f80, 46 0xffffffff, 47 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff } 49 }; 50 51 enum __sse_support __has_sse = 52 #ifdef __SSE__ 53 __SSE_YES; 54 #else 55 __SSE_UNK; 56 #endif 57 58 #define getfl(x) __asm __volatile("pushfl\n\tpopl %0" : "=mr" (*(x))) 59 #define setfl(x) __asm __volatile("pushl %0\n\tpopfl" : : "g" (x)) 60 #define cpuid_dx(x) __asm __volatile("pushl %%ebx\n\tmovl $1, %%eax\n\t" \ 61 "cpuid\n\tpopl %%ebx" \ 62 : "=d" (*(x)) : : "eax", "ecx") 63 64 /* 65 * Test for SSE support on this processor. We need to do this because 66 * we need to use ldmxcsr/stmxcsr to get correct results if any part 67 * of the program was compiled to use SSE floating-point, but we can't 68 * use SSE on older processors. 69 */ 70 int 71 __test_sse(void) 72 { 73 int flag, nflag; 74 int dx_features; 75 76 /* Am I a 486? */ 77 getfl(&flag); 78 nflag = flag ^ 0x200000; 79 setfl(nflag); 80 getfl(&nflag); 81 if (flag != nflag) { 82 /* Not a 486, so CPUID should work. */ 83 cpuid_dx(&dx_features); 84 if (dx_features & 0x2000000) { 85 __has_sse = __SSE_YES; 86 return (1); 87 } 88 } 89 __has_sse = __SSE_NO; 90 return (0); 91 } 92 93 extern inline DLLEXPORT int feclearexcept(int __excepts); 94 extern inline DLLEXPORT int fegetexceptflag(fexcept_t *__flagp, int __excepts); 95 96 DLLEXPORT int 97 fesetexceptflag(const fexcept_t *flagp, int excepts) 98 { 99 fenv_t env; 100 uint32_t mxcsr; 101 102 __fnstenv(&env); 103 env.__status &= ~excepts; 104 env.__status |= *flagp & excepts; 105 __fldenv(env); 106 107 if (__HAS_SSE()) { 108 __stmxcsr(&mxcsr); 109 mxcsr &= ~excepts; 110 mxcsr |= *flagp & excepts; 111 __ldmxcsr(mxcsr); 112 } 113 114 return (0); 115 } 116 117 DLLEXPORT int 118 feraiseexcept(int excepts) 119 { 120 fexcept_t ex = excepts; 121 122 fesetexceptflag(&ex, excepts); 123 __fwait(); 124 return (0); 125 } 126 127 extern inline DLLEXPORT int fetestexcept(int __excepts); 128 extern inline DLLEXPORT int fegetround(void); 129 extern inline DLLEXPORT int fesetround(int __round); 130 131 int 132 fegetenv(fenv_t *envp) 133 { 134 uint32_t mxcsr; 135 136 __fnstenv(envp); 137 /* 138 * fnstenv masks all exceptions, so we need to restore 139 * the old control word to avoid this side effect. 140 */ 141 __fldcw(envp->__control); 142 if (__HAS_SSE()) { 143 __stmxcsr(&mxcsr); 144 __set_mxcsr(*envp, mxcsr); 145 } 146 return (0); 147 } 148 149 int 150 feholdexcept(fenv_t *envp) 151 { 152 uint32_t mxcsr; 153 154 __fnstenv(envp); 155 __fnclex(); 156 if (__HAS_SSE()) { 157 __stmxcsr(&mxcsr); 158 __set_mxcsr(*envp, mxcsr); 159 mxcsr &= ~FE_ALL_EXCEPT; 160 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT; 161 __ldmxcsr(mxcsr); 162 } 163 return (0); 164 } 165 166 extern inline DLLEXPORT int fesetenv(const fenv_t *__envp); 167 168 DLLEXPORT int 169 feupdateenv(const fenv_t *envp) 170 { 171 uint32_t mxcsr; 172 uint16_t status; 173 174 __fnstsw(&status); 175 if (__HAS_SSE()) 176 __stmxcsr(&mxcsr); 177 else 178 mxcsr = 0; 179 fesetenv(envp); 180 feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT); 181 return (0); 182 } 183 184 int 185 __feenableexcept(int mask) 186 { 187 uint32_t mxcsr, omask; 188 uint16_t control; 189 190 mask &= FE_ALL_EXCEPT; 191 __fnstcw(&control); 192 if (__HAS_SSE()) 193 __stmxcsr(&mxcsr); 194 else 195 mxcsr = 0; 196 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT; 197 control &= ~mask; 198 __fldcw(control); 199 if (__HAS_SSE()) { 200 mxcsr &= ~(mask << _SSE_EMASK_SHIFT); 201 __ldmxcsr(mxcsr); 202 } 203 return (omask); 204 } 205 206 int 207 __fedisableexcept(int mask) 208 { 209 uint32_t mxcsr, omask; 210 uint16_t control; 211 212 mask &= FE_ALL_EXCEPT; 213 __fnstcw(&control); 214 if (__HAS_SSE()) 215 __stmxcsr(&mxcsr); 216 else 217 mxcsr = 0; 218 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT; 219 control |= mask; 220 __fldcw(control); 221 if (__HAS_SSE()) { 222 mxcsr |= mask << _SSE_EMASK_SHIFT; 223 __ldmxcsr(mxcsr); 224 } 225 return (omask); 226 } 227 228 __weak_reference(__feenableexcept, feenableexcept); 229 __weak_reference(__fedisableexcept, fedisableexcept); 230