Home | History | Annotate | Line # | Download | only in i387
fenv.c revision 1.9
      1  1.9  christos /* $NetBSD: fenv.c,v 1.9 2018/01/25 03:54:21 christos Exp $ */
      2  1.1     joerg 
      3  1.1     joerg /*-
      4  1.1     joerg  * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG>
      5  1.1     joerg  * All rights reserved.
      6  1.1     joerg  *
      7  1.1     joerg  * Redistribution and use in source and binary forms, with or without
      8  1.1     joerg  * modification, are permitted provided that the following conditions
      9  1.1     joerg  * are met:
     10  1.1     joerg  * 1. Redistributions of source code must retain the above copyright
     11  1.1     joerg  *    notice, this list of conditions and the following disclaimer.
     12  1.1     joerg  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1     joerg  *    notice, this list of conditions and the following disclaimer in the
     14  1.1     joerg  *    documentation and/or other materials provided with the distribution.
     15  1.1     joerg  *
     16  1.1     joerg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1     joerg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1     joerg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1     joerg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1     joerg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1     joerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1     joerg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1     joerg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1     joerg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1     joerg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1     joerg  * SUCH DAMAGE.
     27  1.1     joerg  */
     28  1.1     joerg 
     29  1.1     joerg #include <sys/cdefs.h>
     30  1.9  christos __RCSID("$NetBSD: fenv.c,v 1.9 2018/01/25 03:54:21 christos Exp $");
     31  1.8       chs 
     32  1.8       chs #include "namespace.h"
     33  1.1     joerg 
     34  1.1     joerg #include <sys/param.h>
     35  1.1     joerg #include <sys/sysctl.h>
     36  1.1     joerg #include <assert.h>
     37  1.1     joerg #include <fenv.h>
     38  1.1     joerg #include <stddef.h>
     39  1.1     joerg #include <string.h>
     40  1.1     joerg 
     41  1.8       chs #ifdef __weak_alias
     42  1.8       chs __weak_alias(feclearexcept,_feclearexcept)
     43  1.8       chs __weak_alias(fedisableexcept,_fedisableexcept)
     44  1.8       chs __weak_alias(feenableexcept,_feenableexcept)
     45  1.8       chs __weak_alias(fegetenv,_fegetenv)
     46  1.8       chs __weak_alias(fegetexcept,_fegetexcept)
     47  1.8       chs __weak_alias(fegetexceptflag,_fegetexceptflag)
     48  1.8       chs __weak_alias(fegetround,_fegetround)
     49  1.8       chs __weak_alias(feholdexcept,_feholdexcept)
     50  1.8       chs __weak_alias(feraiseexcept,_feraiseexcept)
     51  1.8       chs __weak_alias(fesetenv,_fesetenv)
     52  1.8       chs __weak_alias(fesetexceptflag,_fesetexceptflag)
     53  1.8       chs __weak_alias(fesetround,_fesetround)
     54  1.8       chs __weak_alias(fetestexcept,_fetestexcept)
     55  1.8       chs __weak_alias(feupdateenv,_feupdateenv)
     56  1.8       chs #endif
     57  1.8       chs 
     58  1.1     joerg /* Load x87 Control Word */
     59  1.1     joerg #define	__fldcw(__cw)		__asm__ __volatile__	\
     60  1.1     joerg 	("fldcw %0" : : "m" (__cw))
     61  1.1     joerg 
     62  1.1     joerg /* No-Wait Store Control Word */
     63  1.1     joerg #define	__fnstcw(__cw)		__asm__ __volatile__	\
     64  1.1     joerg 	("fnstcw %0" : "=m" (*(__cw)))
     65  1.1     joerg 
     66  1.1     joerg /* No-Wait Store Status Word */
     67  1.1     joerg #define	__fnstsw(__sw)		__asm__ __volatile__	\
     68  1.1     joerg 	("fnstsw %0" : "=am" (*(__sw)))
     69  1.1     joerg 
     70  1.1     joerg /* No-Wait Clear Exception Flags */
     71  1.1     joerg #define	__fnclex()		__asm__ __volatile__	\
     72  1.1     joerg 	("fnclex")
     73  1.1     joerg 
     74  1.1     joerg /* Load x87 Environment */
     75  1.1     joerg #define	__fldenv(__env)		__asm__ __volatile__	\
     76  1.1     joerg 	("fldenv %0" : : "m" (__env))
     77  1.1     joerg 
     78  1.1     joerg /* No-Wait Store x87 environment */
     79  1.1     joerg #define	__fnstenv(__env)	__asm__ __volatile__	\
     80  1.1     joerg 	("fnstenv %0" : "=m" (*(__env)))
     81  1.1     joerg 
     82  1.1     joerg /* Check for and handle pending unmasked x87 pending FPU exceptions */
     83  1.1     joerg #define	__fwait(__env)		__asm__	__volatile__	\
     84  1.1     joerg 	("fwait")
     85  1.1     joerg 
     86  1.1     joerg /* Load the MXCSR register */
     87  1.1     joerg #define	__ldmxcsr(__mxcsr)	__asm__ __volatile__	\
     88  1.1     joerg 	("ldmxcsr %0" : : "m" (__mxcsr))
     89  1.1     joerg 
     90  1.1     joerg /* Store the MXCSR register state */
     91  1.1     joerg #define	__stmxcsr(__mxcsr)	__asm__ __volatile__	\
     92  1.1     joerg 	("stmxcsr %0" : "=m" (*(__mxcsr)))
     93  1.1     joerg 
     94  1.1     joerg /*
     95  1.1     joerg  * The following constant represents the default floating-point environment
     96  1.1     joerg  * (that is, the one installed at program startup) and has type pointer to
     97  1.1     joerg  * const-qualified fenv_t.
     98  1.1     joerg  *
     99  1.1     joerg  * It can be used as an argument to the functions within the <fenv.h> header
    100  1.1     joerg  * that manage the floating-point environment, namely fesetenv() and
    101  1.1     joerg  * feupdateenv().
    102  1.1     joerg  *
    103  1.1     joerg  * x87 fpu registers are 16bit wide. The upper bits, 31-16, are marked as
    104  1.1     joerg  * RESERVED. We provide a partial floating-point environment, where we
    105  1.1     joerg  * define only the lower bits. The reserved bits are extracted and set by the
    106  1.1     joerg  * consumers of FE_DFL_ENV, during runtime.
    107  1.1     joerg  */
    108  1.1     joerg fenv_t __fe_dfl_env = {
    109  1.9  christos 	.x87 = {
    110  1.9  christos 		.control = __NetBSD_NPXCW__,    /* Control word register */
    111  1.9  christos 		.unused1 = 0,			/* Unused */
    112  1.9  christos 		.status = 0,  		     	/* Status word register */
    113  1.9  christos 		.unused2 = 0,			/* Unused */
    114  1.9  christos 		.tag = 0xffff,          	/* Tag word register */
    115  1.9  christos 		.unused3 = 0,			/* Unused */
    116  1.9  christos 		.others = {
    117  1.9  christos 			0, 0, 0, 0x0000ffff,
    118  1.1     joerg 		}
    119  1.1     joerg 	},
    120  1.9  christos 	.mxcsr = __INITIAL_MXCSR__		/* MXCSR register */
    121  1.1     joerg };
    122  1.1     joerg 
    123  1.1     joerg /*
    124  1.1     joerg  * Test for SSE support on this processor.
    125  1.1     joerg  *
    126  1.1     joerg  * We need to use ldmxcsr/stmxcsr to get correct results if any part
    127  1.1     joerg  * of the program was compiled to use SSE floating-point, but we can't
    128  1.1     joerg  * use SSE on older processors.
    129  1.1     joerg  *
    130  1.1     joerg  * In order to do so, we need to query the processor capabilities via the CPUID
    131  1.1     joerg  * instruction. We can make it even simpler though, by querying the machdep.sse
    132  1.1     joerg  * sysctl.
    133  1.1     joerg  */
    134  1.1     joerg static int __HAS_SSE = 0;
    135  1.1     joerg 
    136  1.6     joerg static void __init_libm(void) __attribute__ ((constructor, used));
    137  1.1     joerg 
    138  1.6     joerg static void __init_libm(void)
    139  1.1     joerg {
    140  1.3      taca 	size_t oldlen = sizeof(__HAS_SSE);
    141  1.1     joerg 	int rv;
    142  1.6     joerg 	uint16_t control;
    143  1.1     joerg 
    144  1.1     joerg 	rv = sysctlbyname("machdep.sse", &__HAS_SSE, &oldlen, NULL, 0);
    145  1.1     joerg 	if (rv == -1)
    146  1.1     joerg 		__HAS_SSE = 0;
    147  1.6     joerg 
    148  1.6     joerg 	__fnstcw(&control);
    149  1.6     joerg 	__fe_dfl_env.x87.control = control;
    150  1.1     joerg }
    151  1.1     joerg 
    152  1.1     joerg /*
    153  1.1     joerg  * The feclearexcept() function clears the supported floating-point exceptions
    154  1.1     joerg  * represented by `excepts'.
    155  1.1     joerg  */
    156  1.1     joerg int
    157  1.1     joerg feclearexcept(int excepts)
    158  1.1     joerg {
    159  1.1     joerg 	fenv_t env;
    160  1.1     joerg 	uint32_t mxcsr;
    161  1.1     joerg 	int ex;
    162  1.1     joerg 
    163  1.1     joerg 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    164  1.1     joerg 
    165  1.1     joerg 	ex = excepts & FE_ALL_EXCEPT;
    166  1.1     joerg 
    167  1.1     joerg 	/* It's ~3x faster to call fnclex, than store/load fp env */
    168  1.1     joerg 	if (ex == FE_ALL_EXCEPT) {
    169  1.1     joerg 		__fnclex();
    170  1.1     joerg 	} else {
    171  1.1     joerg 		__fnstenv(&env);
    172  1.1     joerg 		env.x87.status &= ~ex;
    173  1.1     joerg 		__fldenv(env);
    174  1.1     joerg 	}
    175  1.1     joerg 
    176  1.1     joerg 	if (__HAS_SSE) {
    177  1.1     joerg 		__stmxcsr(&mxcsr);
    178  1.1     joerg 		mxcsr &= ~ex;
    179  1.1     joerg 		__ldmxcsr(mxcsr);
    180  1.1     joerg 	}
    181  1.1     joerg 
    182  1.1     joerg 	/* Success */
    183  1.1     joerg 	return (0);
    184  1.1     joerg }
    185  1.1     joerg 
    186  1.1     joerg /*
    187  1.1     joerg  * The fegetexceptflag() function stores an implementation-defined
    188  1.1     joerg  * representation of the states of the floating-point status flags indicated by
    189  1.1     joerg  * the argument excepts in the object pointed to by the argument flagp.
    190  1.1     joerg  */
    191  1.1     joerg int
    192  1.1     joerg fegetexceptflag(fexcept_t *flagp, int excepts)
    193  1.1     joerg {
    194  1.1     joerg 	uint32_t mxcsr;
    195  1.1     joerg 	uint16_t status;
    196  1.1     joerg 	int ex;
    197  1.1     joerg 
    198  1.1     joerg 	_DIAGASSERT(flagp != NULL);
    199  1.1     joerg 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    200  1.1     joerg 
    201  1.1     joerg 	ex = excepts & FE_ALL_EXCEPT;
    202  1.1     joerg 
    203  1.1     joerg 	__fnstsw(&status);
    204  1.1     joerg 	if (__HAS_SSE)
    205  1.1     joerg 		__stmxcsr(&mxcsr);
    206  1.1     joerg 	else
    207  1.1     joerg 		mxcsr = 0;
    208  1.1     joerg 
    209  1.1     joerg 	*flagp = (mxcsr | status) & ex;
    210  1.1     joerg 
    211  1.1     joerg 	/* Success */
    212  1.1     joerg 	return (0);
    213  1.1     joerg }
    214  1.1     joerg 
    215  1.1     joerg /*
    216  1.1     joerg  * The feraiseexcept() function raises the supported floating-point exceptions
    217  1.1     joerg  * represented by the argument `excepts'.
    218  1.1     joerg  *
    219  1.1     joerg  * The standard explicitly allows us to execute an instruction that has the
    220  1.1     joerg  * exception as a side effect, but we choose to manipulate the status register
    221  1.1     joerg  * directly.
    222  1.1     joerg  *
    223  1.1     joerg  * The validation of input is being deferred to fesetexceptflag().
    224  1.1     joerg  */
    225  1.1     joerg int
    226  1.1     joerg feraiseexcept(int excepts)
    227  1.1     joerg {
    228  1.1     joerg 	fexcept_t ex;
    229  1.1     joerg 
    230  1.1     joerg 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    231  1.1     joerg 
    232  1.1     joerg 	ex = excepts & FE_ALL_EXCEPT;
    233  1.1     joerg 	fesetexceptflag(&ex, excepts);
    234  1.1     joerg 	__fwait();
    235  1.1     joerg 
    236  1.1     joerg 	/* Success */
    237  1.1     joerg 	return (0);
    238  1.1     joerg }
    239  1.1     joerg 
    240  1.1     joerg /*
    241  1.1     joerg  * This function sets the floating-point status flags indicated by the argument
    242  1.1     joerg  * `excepts' to the states stored in the object pointed to by `flagp'. It does
    243  1.1     joerg  * NOT raise any floating-point exceptions, but only sets the state of the flags.
    244  1.1     joerg  */
    245  1.1     joerg int
    246  1.1     joerg fesetexceptflag(const fexcept_t *flagp, int excepts)
    247  1.1     joerg {
    248  1.1     joerg 	fenv_t env;
    249  1.1     joerg 	uint32_t mxcsr;
    250  1.1     joerg 	int ex;
    251  1.1     joerg 
    252  1.1     joerg 	_DIAGASSERT(flagp != NULL);
    253  1.1     joerg 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    254  1.1     joerg 
    255  1.1     joerg 	ex = excepts & FE_ALL_EXCEPT;
    256  1.1     joerg 
    257  1.1     joerg 	__fnstenv(&env);
    258  1.1     joerg 	env.x87.status &= ~ex;
    259  1.1     joerg 	env.x87.status |= *flagp & ex;
    260  1.1     joerg 	__fldenv(env);
    261  1.1     joerg 
    262  1.1     joerg 	if (__HAS_SSE) {
    263  1.1     joerg 		__stmxcsr(&mxcsr);
    264  1.1     joerg 		mxcsr &= ~ex;
    265  1.1     joerg 		mxcsr |= *flagp & ex;
    266  1.1     joerg 		__ldmxcsr(mxcsr);
    267  1.1     joerg 	}
    268  1.1     joerg 
    269  1.1     joerg 	/* Success */
    270  1.1     joerg 	return (0);
    271  1.1     joerg }
    272  1.1     joerg 
    273  1.1     joerg /*
    274  1.1     joerg  * The fetestexcept() function determines which of a specified subset of the
    275  1.1     joerg  * floating-point exception flags are currently set. The `excepts' argument
    276  1.1     joerg  * specifies the floating-point status flags to be queried.
    277  1.1     joerg  */
    278  1.1     joerg int
    279  1.1     joerg fetestexcept(int excepts)
    280  1.1     joerg {
    281  1.1     joerg 	uint32_t mxcsr;
    282  1.1     joerg 	uint16_t status;
    283  1.1     joerg 	int ex;
    284  1.1     joerg 
    285  1.1     joerg 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    286  1.1     joerg 
    287  1.1     joerg 	ex = excepts & FE_ALL_EXCEPT;
    288  1.1     joerg 
    289  1.1     joerg 	__fnstsw(&status);
    290  1.1     joerg 	if (__HAS_SSE)
    291  1.1     joerg 		__stmxcsr(&mxcsr);
    292  1.1     joerg 	else
    293  1.1     joerg 		mxcsr = 0;
    294  1.1     joerg 
    295  1.1     joerg 	return ((status | mxcsr) & ex);
    296  1.1     joerg }
    297  1.1     joerg 
    298  1.1     joerg int
    299  1.1     joerg fegetround(void)
    300  1.1     joerg {
    301  1.1     joerg 	uint16_t control;
    302  1.1     joerg 
    303  1.1     joerg 	/*
    304  1.1     joerg 	 * We assume that the x87 and the SSE unit agree on the
    305  1.1     joerg 	 * rounding mode.  Reading the control word on the x87 turns
    306  1.1     joerg 	 * out to be about 5 times faster than reading it on the SSE
    307  1.1     joerg 	 * unit on an Opteron 244.
    308  1.1     joerg 	 */
    309  1.1     joerg 	__fnstcw(&control);
    310  1.1     joerg 
    311  1.1     joerg 	return (control & __X87_ROUND_MASK);
    312  1.1     joerg }
    313  1.1     joerg 
    314  1.1     joerg /*
    315  1.1     joerg  * The fesetround() function shall establish the rounding direction represented
    316  1.1     joerg  * by its argument round. If the argument is not equal to the value of a
    317  1.1     joerg  * rounding direction macro, the rounding direction is not changed.
    318  1.1     joerg  */
    319  1.1     joerg int
    320  1.1     joerg fesetround(int round)
    321  1.1     joerg {
    322  1.1     joerg 	uint32_t mxcsr;
    323  1.1     joerg 	uint16_t control;
    324  1.1     joerg 
    325  1.1     joerg 	if (round & ~__X87_ROUND_MASK) {
    326  1.1     joerg 		/* Failure */
    327  1.1     joerg 		return (-1);
    328  1.1     joerg 	}
    329  1.1     joerg 
    330  1.1     joerg 	__fnstcw(&control);
    331  1.1     joerg 	control &= ~__X87_ROUND_MASK;
    332  1.1     joerg 	control |= round;
    333  1.1     joerg 	__fldcw(control);
    334  1.1     joerg 
    335  1.1     joerg 	if (__HAS_SSE) {
    336  1.1     joerg 		__stmxcsr(&mxcsr);
    337  1.1     joerg 		mxcsr &= ~(__X87_ROUND_MASK << __SSE_ROUND_SHIFT);
    338  1.1     joerg 		mxcsr |= round << __SSE_ROUND_SHIFT;
    339  1.1     joerg 		__ldmxcsr(mxcsr);
    340  1.1     joerg 	}
    341  1.1     joerg 
    342  1.1     joerg 	/* Success */
    343  1.1     joerg 	return (0);
    344  1.1     joerg }
    345  1.1     joerg 
    346  1.1     joerg /*
    347  1.1     joerg  * The fegetenv() function attempts to store the current floating-point
    348  1.1     joerg  * environment in the object pointed to by envp.
    349  1.1     joerg  */
    350  1.1     joerg int
    351  1.1     joerg fegetenv(fenv_t *envp)
    352  1.1     joerg {
    353  1.1     joerg 	uint32_t mxcsr;
    354  1.1     joerg 
    355  1.1     joerg 	_DIAGASSERT(flagp != NULL);
    356  1.1     joerg 
    357  1.1     joerg 	/*
    358  1.1     joerg 	 * fnstenv masks all exceptions, so we need to restore the old control
    359  1.1     joerg 	 * word to avoid this side effect.
    360  1.1     joerg 	 */
    361  1.1     joerg 	__fnstenv(envp);
    362  1.1     joerg 	__fldcw(envp->x87.control);
    363  1.1     joerg 	if (__HAS_SSE) {
    364  1.1     joerg 		__stmxcsr(&mxcsr);
    365  1.1     joerg 		envp->mxcsr = mxcsr;
    366  1.1     joerg 	}
    367  1.1     joerg 
    368  1.1     joerg 	/* Success */
    369  1.1     joerg 	return (0);
    370  1.1     joerg }
    371  1.1     joerg 
    372  1.1     joerg /*
    373  1.1     joerg  * The feholdexcept() function saves the current floating-point environment in
    374  1.1     joerg  * the object pointed to by envp, clears the floating-point status flags, and
    375  1.1     joerg  * then installs a non-stop (continue on floating-point exceptions) mode, if
    376  1.1     joerg  * available, for all floating-point exceptions.
    377  1.1     joerg  */
    378  1.1     joerg int
    379  1.1     joerg feholdexcept(fenv_t *envp)
    380  1.1     joerg {
    381  1.1     joerg 	uint32_t mxcsr;
    382  1.1     joerg 
    383  1.1     joerg 	_DIAGASSERT(envp != NULL);
    384  1.1     joerg 
    385  1.1     joerg 	__fnstenv(envp);
    386  1.1     joerg 	__fnclex();
    387  1.1     joerg 	if (__HAS_SSE) {
    388  1.1     joerg 		__stmxcsr(&mxcsr);
    389  1.1     joerg 		envp->mxcsr = mxcsr;
    390  1.1     joerg 		mxcsr &= ~FE_ALL_EXCEPT;
    391  1.1     joerg 		mxcsr |= FE_ALL_EXCEPT << __SSE_EMASK_SHIFT;
    392  1.1     joerg 		__ldmxcsr(mxcsr);
    393  1.1     joerg 	}
    394  1.1     joerg 
    395  1.1     joerg 	/* Success */
    396  1.1     joerg 	return (0);
    397  1.1     joerg }
    398  1.1     joerg 
    399  1.1     joerg /*
    400  1.1     joerg  * The fesetenv() function attempts to establish the floating-point environment
    401  1.1     joerg  * represented by the object pointed to by envp. The argument `envp' points
    402  1.1     joerg  * to an object set by a call to fegetenv() or feholdexcept(), or equal a
    403  1.1     joerg  * floating-point environment macro. The fesetenv() function does not raise
    404  1.1     joerg  * floating-point exceptions, but only installs the state of the floating-point
    405  1.1     joerg  * status flags represented through its argument.
    406  1.1     joerg  */
    407  1.1     joerg int
    408  1.1     joerg fesetenv(const fenv_t *envp)
    409  1.1     joerg {
    410  1.1     joerg 	fenv_t env;
    411  1.1     joerg 
    412  1.1     joerg 	_DIAGASSERT(envp != NULL);
    413  1.1     joerg 
    414  1.1     joerg 	/* Store the x87 floating-point environment */
    415  1.1     joerg 	memset(&env, 0, sizeof(env));
    416  1.1     joerg 	__fnstenv(&env);
    417  1.1     joerg 
    418  1.1     joerg 	__fe_dfl_env.x87.unused1 = env.x87.unused1;
    419  1.1     joerg 	__fe_dfl_env.x87.unused2 = env.x87.unused2;
    420  1.1     joerg 	__fe_dfl_env.x87.unused3 = env.x87.unused3;
    421  1.7  christos 	memcpy(__fe_dfl_env.x87.others, env.x87.others,
    422  1.7  christos 	    sizeof(__fe_dfl_env.x87.others));
    423  1.1     joerg 
    424  1.1     joerg 	__fldenv(envp->x87);
    425  1.1     joerg 	if (__HAS_SSE)
    426  1.1     joerg 		__ldmxcsr(envp->mxcsr);
    427  1.1     joerg 
    428  1.1     joerg 	/* Success */
    429  1.1     joerg 	return (0);
    430  1.1     joerg }
    431  1.1     joerg 
    432  1.1     joerg /*
    433  1.1     joerg  * The feupdateenv() function saves the currently raised floating-point
    434  1.1     joerg  * exceptions in its automatic storage, installs the floating-point environment
    435  1.1     joerg  * represented by the object pointed to by `envp', and then raises the saved
    436  1.1     joerg  * floating-point exceptions. The argument `envp' shall point to an object set
    437  1.1     joerg  * by a call to feholdexcept() or fegetenv(), or equal a floating-point
    438  1.1     joerg  * environment macro.
    439  1.1     joerg  */
    440  1.1     joerg int
    441  1.1     joerg feupdateenv(const fenv_t *envp)
    442  1.1     joerg {
    443  1.1     joerg 	fenv_t env;
    444  1.1     joerg 	uint32_t mxcsr;
    445  1.1     joerg 	uint16_t status;
    446  1.1     joerg 
    447  1.1     joerg 	_DIAGASSERT(envp != NULL);
    448  1.1     joerg 
    449  1.1     joerg 	/* Store the x87 floating-point environment */
    450  1.1     joerg 	memset(&env, 0, sizeof(env));
    451  1.1     joerg 	__fnstenv(&env);
    452  1.1     joerg 
    453  1.1     joerg 	__fe_dfl_env.x87.unused1 = env.x87.unused1;
    454  1.1     joerg 	__fe_dfl_env.x87.unused2 = env.x87.unused2;
    455  1.1     joerg 	__fe_dfl_env.x87.unused3 = env.x87.unused3;
    456  1.7  christos 	memcpy(__fe_dfl_env.x87.others, env.x87.others,
    457  1.7  christos 	    sizeof(__fe_dfl_env.x87.others));
    458  1.1     joerg 
    459  1.1     joerg 	__fnstsw(&status);
    460  1.1     joerg 	if (__HAS_SSE)
    461  1.1     joerg 		__stmxcsr(&mxcsr);
    462  1.1     joerg 	else
    463  1.1     joerg 		mxcsr = 0;
    464  1.1     joerg 	fesetenv(envp);
    465  1.1     joerg 	feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
    466  1.1     joerg 
    467  1.1     joerg 	/* Success */
    468  1.1     joerg 	return (0);
    469  1.1     joerg }
    470  1.1     joerg 
    471  1.1     joerg /*
    472  1.1     joerg  * The following functions are extentions to the standard
    473  1.1     joerg  */
    474  1.1     joerg int
    475  1.1     joerg feenableexcept(int mask)
    476  1.1     joerg {
    477  1.1     joerg 	uint32_t mxcsr, omask;
    478  1.1     joerg 	uint16_t control;
    479  1.1     joerg 
    480  1.1     joerg 	mask &= FE_ALL_EXCEPT;
    481  1.1     joerg 	__fnstcw(&control);
    482  1.1     joerg 	if (__HAS_SSE)
    483  1.1     joerg 		__stmxcsr(&mxcsr);
    484  1.1     joerg 	else
    485  1.1     joerg 		mxcsr = 0;
    486  1.1     joerg 
    487  1.1     joerg 	omask = (control | mxcsr >> __SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
    488  1.1     joerg 	control &= ~mask;
    489  1.1     joerg 	__fldcw(control);
    490  1.1     joerg 	if (__HAS_SSE) {
    491  1.1     joerg 		mxcsr &= ~(mask << __SSE_EMASK_SHIFT);
    492  1.1     joerg 		__ldmxcsr(mxcsr);
    493  1.1     joerg 	}
    494  1.1     joerg 
    495  1.4  riastrad 	return (FE_ALL_EXCEPT & ~omask);
    496  1.1     joerg }
    497  1.1     joerg 
    498  1.1     joerg int
    499  1.1     joerg fedisableexcept(int mask)
    500  1.1     joerg {
    501  1.1     joerg 	uint32_t mxcsr, omask;
    502  1.1     joerg 	uint16_t control;
    503  1.1     joerg 
    504  1.1     joerg 	mask &= FE_ALL_EXCEPT;
    505  1.1     joerg 	__fnstcw(&control);
    506  1.1     joerg 	if (__HAS_SSE)
    507  1.1     joerg 		__stmxcsr(&mxcsr);
    508  1.1     joerg 	else
    509  1.1     joerg 		mxcsr = 0;
    510  1.1     joerg 
    511  1.1     joerg 	omask = (control | mxcsr >> __SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
    512  1.1     joerg 	control |= mask;
    513  1.1     joerg 	__fldcw(control);
    514  1.1     joerg 	if (__HAS_SSE) {
    515  1.1     joerg 		mxcsr |= mask << __SSE_EMASK_SHIFT;
    516  1.1     joerg 		__ldmxcsr(mxcsr);
    517  1.1     joerg 	}
    518  1.1     joerg 
    519  1.4  riastrad 	return (FE_ALL_EXCEPT & ~omask);
    520  1.1     joerg }
    521  1.1     joerg 
    522  1.1     joerg int
    523  1.1     joerg fegetexcept(void)
    524  1.1     joerg {
    525  1.1     joerg 	uint16_t control;
    526  1.1     joerg 
    527  1.1     joerg 	/*
    528  1.1     joerg 	 * We assume that the masks for the x87 and the SSE unit are
    529  1.1     joerg 	 * the same.
    530  1.1     joerg 	 */
    531  1.1     joerg 	__fnstcw(&control);
    532  1.1     joerg 
    533  1.5  riastrad 	return (~control & FE_ALL_EXCEPT);
    534  1.1     joerg }
    535