Home | History | Annotate | Line # | Download | only in x86_64
fenv.c revision 1.6
      1 /* $NetBSD: fenv.c,v 1.6 2013/11/11 00:31:51 joerg Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2004-2005 David Schultz <das (at) 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 #include <sys/cdefs.h>
     30 __RCSID("$NetBSD: fenv.c,v 1.6 2013/11/11 00:31:51 joerg Exp $");
     31 
     32 #include <assert.h>
     33 #include <fenv.h>
     34 #include <stddef.h>
     35 #include <string.h>
     36 
     37 /* Load x87 Control Word */
     38 #define	__fldcw(__cw)		__asm__ __volatile__ \
     39 	("fldcw %0" : : "m" (__cw))
     40 
     41 /* No-Wait Store Control Word */
     42 #define	__fnstcw(__cw)		__asm__ __volatile__ \
     43 	("fnstcw %0" : "=m" (*(__cw)))
     44 
     45 /* No-Wait Store Status Word */
     46 #define	__fnstsw(__sw)		__asm__ __volatile__ \
     47 	("fnstsw %0" : "=am" (*(__sw)))
     48 
     49 /* No-Wait Clear Exception Flags */
     50 #define	__fnclex()		__asm__ __volatile__ \
     51 	("fnclex")
     52 
     53 /* Load x87 Environment */
     54 #define	__fldenv(__env)		__asm__ __volatile__ \
     55 	("fldenv %0" : : "m" (__env))
     56 
     57 /* No-Wait Store x87 environment */
     58 #define	__fnstenv(__env)	__asm__ __volatile__ \
     59 	("fnstenv %0" : "=m" (*(__env)))
     60 
     61 /* Check for and handle pending unmasked x87 pending FPU exceptions */
     62 #define	__fwait(__env)		__asm__	__volatile__	\
     63 	("fwait")
     64 
     65 /* Load the MXCSR register */
     66 #define	__ldmxcsr(__mxcsr)	__asm__ __volatile__ \
     67 	("ldmxcsr %0" : : "m" (__mxcsr))
     68 
     69 /* Store the MXCSR register state */
     70 #define	__stmxcsr(__mxcsr)	__asm__ __volatile__ \
     71 	("stmxcsr %0" : "=m" (*(__mxcsr)))
     72 
     73 /*
     74  * The following constant represents the default floating-point environment
     75  * (that is, the one installed at program startup) and has type pointer to
     76  * const-qualified fenv_t.
     77  *
     78  * It can be used as an argument to the functions within the <fenv.h> header
     79  * that manage the floating-point environment, namely fesetenv() and
     80  * feupdateenv().
     81  *
     82  * x87 fpu registers are 16bit wide. The upper bits, 31-16, are marked as
     83  * RESERVED. We provide a partial floating-point environment, where we
     84  * define only the lower bits. The reserved bits are extracted and set by
     85  * the consumers of FE_DFL_ENV, during runtime.
     86  */
     87 fenv_t __fe_dfl_env = {
     88 	{
     89 		__NetBSD_NPXCW__,	/* Control word register */
     90 		0x00000000,		/* Status word register */
     91 		0x0000ffff,		/* Tag word register */
     92 		{
     93 			0x00000000,
     94 			0x00000000,
     95 			0x00000000,
     96 			0x00000000,
     97 		},
     98 	},
     99 	__INITIAL_MXCSR__       /* MXCSR register */
    100 };
    101 #define FE_DFL_ENV      ((const fenv_t *) &__fe_dfl_env)
    102 
    103 static void __init_libm(void) __attribute__ ((constructor, used));
    104 
    105 static void __init_libm(void)
    106 {
    107 	uint16_t control;
    108 
    109 	__fnstcw(&control);
    110 	__fe_dfl_env.x87.control = control;
    111 }
    112 
    113 
    114 /*
    115  * The feclearexcept() function clears the supported floating-point exceptions
    116  * represented by `excepts'.
    117  */
    118 int
    119 feclearexcept(int excepts)
    120 {
    121 	fenv_t fenv;
    122 	int ex;
    123 
    124 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    125 
    126 	ex = excepts & FE_ALL_EXCEPT;
    127 
    128 	/* Store the current x87 floating-point environment */
    129 	__fnstenv(&fenv);
    130 
    131 	/* Clear the requested floating-point exceptions */
    132 	fenv.x87.status &= ~ex;
    133 
    134 	/* Load the x87 floating-point environent */
    135 	__fldenv(fenv);
    136 
    137 	/* Same for SSE environment */
    138 	__stmxcsr(&fenv.mxcsr);
    139 	fenv.mxcsr &= ~ex;
    140 	__ldmxcsr(fenv.mxcsr);
    141 
    142 	/* Success */
    143 	return (0);
    144 }
    145 
    146 /*
    147  * The fegetexceptflag() function stores an implementation-defined
    148  * representation of the states of the floating-point status flags indicated by
    149  * the argument excepts in the object pointed to by the argument flagp.
    150  */
    151 int
    152 fegetexceptflag(fexcept_t *flagp, int excepts)
    153 {
    154 	uint32_t mxcsr;
    155 	uint16_t x87_status;
    156 	int ex;
    157 
    158 	_DIAGASSERT(flagp != NULL);
    159 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    160 
    161 	ex = excepts & FE_ALL_EXCEPT;
    162 
    163 	/* Store the current x87 status register */
    164 	__fnstsw(&x87_status);
    165 
    166 	/* Store the MXCSR register */
    167 	__stmxcsr(&mxcsr);
    168 
    169 	/* Store the results in flagp */
    170 	*flagp = (x87_status | mxcsr) & ex;
    171 
    172 	/* Success */
    173 	return (0);
    174 }
    175 
    176 /*
    177  * The feraiseexcept() function raises the supported floating-point exceptions
    178  * represented by the argument `excepts'.
    179  *
    180  * The standard explicitly allows us to execute an instruction that has the
    181  * exception as a side effect, but we choose to manipulate the status register
    182  * directly.
    183  *
    184  * The validation of input is being deferred to fesetexceptflag().
    185  */
    186 int
    187 feraiseexcept(int excepts)
    188 {
    189 	int ex;
    190 
    191 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    192 
    193 	ex = excepts & FE_ALL_EXCEPT;
    194 	fesetexceptflag((unsigned int *)&ex, ex);
    195 	__fwait();
    196 
    197 	/* Success */
    198 	return (0);
    199 }
    200 
    201 /*
    202  * This function sets the floating-point status flags indicated by the argument
    203  * `excepts' to the states stored in the object pointed to by `flagp'. It does
    204  * NOT raise any floating-point exceptions, but only sets the state of the flags.
    205  */
    206 int
    207 fesetexceptflag(const fexcept_t *flagp, int excepts)
    208 {
    209 	fenv_t fenv;
    210 	int ex;
    211 
    212 	_DIAGASSERT(flagp != NULL);
    213 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    214 
    215 	ex = excepts & FE_ALL_EXCEPT;
    216 
    217 	/* Store the current x87 floating-point environment */
    218 	__fnstenv(&fenv);
    219 
    220 	/* Set the requested status flags */
    221 	fenv.x87.status |= *flagp & ex;
    222 
    223 	/* Load the x87 floating-point environent */
    224 	__fldenv(fenv);
    225 
    226 	/* Same for SSE environment */
    227 	__stmxcsr(&fenv.mxcsr);
    228 	fenv.mxcsr |= *flagp & ex;
    229 	__ldmxcsr(fenv.mxcsr);
    230 
    231 	/* Success */
    232 	return (0);
    233 }
    234 
    235 /*
    236  * The fetestexcept() function determines which of a specified subset of the
    237  * floating-point exception flags are currently set. The `excepts' argument
    238  * specifies the floating-point status flags to be queried.
    239  */
    240 int
    241 fetestexcept(int excepts)
    242 {
    243 	fenv_t fenv;
    244 	uint32_t mxcsr;
    245 	uint16_t status;
    246 	int ex;
    247 
    248 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    249 
    250 	ex = excepts & FE_ALL_EXCEPT;
    251 
    252 	/* Store the current x87 floating-point environment */
    253 	memset(&fenv, 0, sizeof(fenv));
    254 
    255 	__fnstenv(&fenv);
    256 	__fnstsw(&status);
    257 
    258 	/* Store the MXCSR register state */
    259 	__stmxcsr(&fenv.mxcsr);
    260 	__stmxcsr(&mxcsr);
    261 
    262 	return ((fenv.x87.status | fenv.mxcsr) & ex);
    263 }
    264 
    265 /*
    266  * The fegetround() function gets the current rounding direction.
    267  */
    268 int
    269 fegetround(void)
    270 {
    271 	uint32_t mxcsr;
    272 	uint16_t control;
    273 
    274 	/*
    275 	 * We check both the x87 floating-point unit _and_ the SSE unit.
    276 	 * Normally, those two must agree with respect to each other. If they
    277 	 * don't, it's not our fault and the result is non-determinable, in
    278 	 * which case POSIX says that a negative value should be returned.
    279 	 */
    280 	__fnstcw(&control);
    281 	__stmxcsr(&mxcsr);
    282 
    283 	if ((control & _X87_ROUNDING_MASK)
    284 	    != ((mxcsr & _SSE_ROUNDING_MASK) >> 3)) {
    285 		return (-1);
    286 	}
    287 
    288 	return (control & _X87_ROUNDING_MASK);
    289 }
    290 
    291 /*
    292  * The fesetround() function establishes the rounding direction represented by
    293  * its argument `round'. If the argument is not equal to the value of a rounding
    294  * direction macro, the rounding direction is not changed.
    295  */
    296 int
    297 fesetround(int round)
    298 {
    299 	uint32_t  mxcsr;
    300 	uint16_t control;
    301 
    302 	/* Check whether requested rounding direction is supported */
    303 	if (round & (~_X87_ROUNDING_MASK))
    304 		return (-1);
    305 
    306 	/* Store the current x87 control word register  */
    307 	__fnstcw(&control);
    308 
    309 	/*
    310 	 * Set the rounding direction
    311 	 * Rounding Control is bits 10-11, so shift appropriately
    312 	 */
    313 	control &= ~_X87_ROUNDING_MASK;
    314 	control |= round;
    315 
    316 	/* Load the x87 control word register */
    317 	__fldcw(control);
    318 
    319 	/*
    320 	 * Same for the SSE environment
    321 	 * Rounding Control is bits 13-14, so shift appropriately
    322 	 */
    323 	__stmxcsr(&mxcsr);
    324 	mxcsr &= ~_SSE_ROUNDING_MASK;
    325 	mxcsr |= (round << _SSE_ROUND_SHIFT);
    326 	__ldmxcsr(mxcsr);
    327 
    328 	/* Success */
    329 	return (0);
    330 }
    331 
    332 /*
    333  * The fegetenv() function attempts to store the current floating-point
    334  * environment in the object pointed to by envp.
    335  */
    336 int
    337 fegetenv(fenv_t *envp)
    338 {
    339 	_DIAGASSERT(envp != NULL);
    340 
    341 	/* Store the current x87 floating-point environment */
    342 	__fnstenv(envp);
    343 
    344 	/* Store the MXCSR register state */
    345 	__stmxcsr(&envp->mxcsr);
    346 
    347      /*
    348       * When an FNSTENV instruction is executed, all pending exceptions are
    349       * essentially lost (either the x87 FPU status register is cleared or all
    350       * exceptions are masked).
    351       *
    352       * 8.6 X87 FPU EXCEPTION SYNCHRONIZATION -
    353       * Intel(R) 64 and IA-32 Architectures Softare Developer's Manual - Vol 1
    354       *
    355       */
    356 	__fldcw(envp->x87.control);
    357 
    358 	/* Success */
    359 	return (0);
    360 }
    361 
    362 /*
    363  * The feholdexcept() function saves the current floating-point environment
    364  * in the object pointed to by envp, clears the floating-point status flags, and
    365  * then installs a non-stop (continue on floating-point exceptions) mode, if
    366  * available, for all floating-point exceptions.
    367  */
    368 int
    369 feholdexcept(fenv_t *envp)
    370 {
    371 	uint32_t mxcsr;
    372 
    373 	_DIAGASSERT(envp != NULL);
    374 
    375 	/* Store the current x87 floating-point environment */
    376 	__fnstenv(envp);
    377 
    378 	/* Clear all exception flags in FPU */
    379 	__fnclex();
    380 
    381 	/* Store the MXCSR register state */
    382 	__stmxcsr(&envp->mxcsr);
    383 
    384 	/* Clear exception flags in MXCSR XXX */
    385 	mxcsr = envp->mxcsr;
    386 	mxcsr &= ~FE_ALL_EXCEPT;
    387 
    388 	/* Mask all exceptions */
    389 	mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
    390 
    391 	__ldmxcsr(mxcsr);
    392 
    393 	/* Success */
    394 	return (0);
    395 }
    396 
    397 /*
    398  * The fesetenv() function attempts to establish the floating-point environment
    399  * represented by the object pointed to by envp. The argument `envp' points
    400  * to an object set by a call to fegetenv() or feholdexcept(), or equal a
    401  * floating-point environment macro. The fesetenv() function does not raise
    402  * floating-point exceptions, but only installs the state of the floating-point
    403  * status flags represented through its argument.
    404  */
    405 int
    406 fesetenv(const fenv_t *envp)
    407 {
    408 	fenv_t fenv;
    409 
    410 	_DIAGASSERT(envp != NULL);
    411 
    412 	/* Store the x87 floating-point environment */
    413 	memset(&fenv, 0, sizeof fenv);
    414 	__fnstenv(&fenv);
    415 
    416 	__fe_dfl_env.x87.control = (fenv.x87.control & 0xffff0000)
    417 	    | (__fe_dfl_env.x87.control & 0x0000ffff);
    418 	__fe_dfl_env.x87.status = (fenv.x87.status & 0xffff0000)
    419 	    | (__fe_dfl_env.x87.status & 0x0000ffff);
    420 	__fe_dfl_env.x87.tag = (fenv.x87.tag & 0xffff0000)
    421 	    | (__fe_dfl_env.x87.tag & 0x0000ffff);
    422 	__fe_dfl_env.x87.others[3] = (fenv.x87.others[3] & 0xffff0000)
    423 	    | (__fe_dfl_env.x87.others[3] & 0x0000ffff);
    424 	__fldenv(*envp);
    425 
    426 	/* Store the MXCSR register */
    427 	__ldmxcsr(envp->mxcsr);
    428 
    429 	/* Success */
    430 	return (0);
    431 }
    432 
    433 /*
    434  * The feupdateenv() function saves the currently raised floating-point
    435  * exceptions in its automatic storage, installs the floating-point environment
    436  * represented by the object pointed to by `envp', and then raises the saved
    437  * floating-point exceptions. The argument `envp' shall point to an object set
    438  * by a call to feholdexcept() or fegetenv(), or equal a floating-point
    439  * environment macro.
    440  */
    441 int
    442 feupdateenv(const fenv_t *envp)
    443 {
    444 	fenv_t fenv;
    445 	uint32_t mxcsr;
    446 	uint16_t sw;
    447 
    448 	_DIAGASSERT(envp != NULL);
    449 
    450 	/* Store the x87 floating-point environment */
    451 	memset(&fenv, 0, sizeof(fenv));
    452 	__fnstenv(&fenv);
    453 
    454 	__fe_dfl_env.x87.control = (fenv.x87.control & 0xffff0000)
    455 	    | (__fe_dfl_env.x87.control & 0x0000ffff);
    456 	__fe_dfl_env.x87.status = (fenv.x87.status & 0xffff0000)
    457 	    | (__fe_dfl_env.x87.status & 0x0000ffff);
    458 	__fe_dfl_env.x87.tag = (fenv.x87.tag & 0xffff0000)
    459 	    | (__fe_dfl_env.x87.tag & 0x0000ffff);
    460 	__fe_dfl_env.x87.others[3] = (fenv.x87.others[3] & 0xffff0000)
    461 	    | (__fe_dfl_env.x87.others[3] & 0x0000ffff);
    462 
    463 	/* Store the x87 status register */
    464 	__fnstsw(&sw);
    465 
    466 	/* Store the MXCSR register */
    467 	__stmxcsr(&mxcsr);
    468 
    469 	/* Install new floating-point environment */
    470 	fesetenv(envp);
    471 
    472 	/* Raise any previously accumulated exceptions */
    473 	feraiseexcept((sw | mxcsr) & FE_ALL_EXCEPT);
    474 
    475 	/* Success */
    476 	return (0);
    477 }
    478 
    479 /*
    480  * The following functions are extentions to the standard
    481  */
    482 int
    483 feenableexcept(int mask)
    484 {
    485 	uint32_t mxcsr, omask;
    486 	uint16_t control;
    487 
    488 	_DIAGASSERT((mask & ~FE_ALL_EXCEPT) == 0);
    489 	mask &= FE_ALL_EXCEPT;
    490 
    491 	__fnstcw(&control);
    492 	__stmxcsr(&mxcsr);
    493 
    494 	omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
    495 	control &= ~mask;
    496 	__fldcw(control);
    497 
    498 	mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
    499 	__ldmxcsr(mxcsr);
    500 
    501 	return (FE_ALL_EXCEPT & ~omask);
    502 
    503 }
    504 
    505 int
    506 fedisableexcept(int mask)
    507 {
    508 	uint32_t mxcsr, omask;
    509 	uint16_t control;
    510 
    511 	_DIAGASSERT((mask & ~FE_ALL_EXCEPT) == 0);
    512 
    513 	__fnstcw(&control);
    514 	__stmxcsr(&mxcsr);
    515 
    516 	omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
    517 	control |= mask;
    518 	__fldcw(control);
    519 
    520 	mxcsr |= mask << _SSE_EMASK_SHIFT;
    521 	__ldmxcsr(mxcsr);
    522 
    523 	return (FE_ALL_EXCEPT & ~omask);
    524 }
    525 
    526 int
    527 fegetexcept(void)
    528 {
    529 	uint16_t control;
    530 
    531 	/*
    532 	 * We assume that the masks for the x87 and the SSE unit are
    533 	 * the same.
    534 	 */
    535 	__fnstcw(&control);
    536 
    537 	return (~control & FE_ALL_EXCEPT);
    538 }
    539 
    540