Home | History | Annotate | Line # | Download | only in sparc64
fenv.c revision 1.1
      1  1.1  christos /*	$NetBSD: fenv.c,v 1.1 2011/01/31 00:19:33 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG>
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  christos  */
     26  1.1  christos #include <sys/cdefs.h>
     27  1.1  christos __RCSID("$NetBSD: fenv.c,v 1.1 2011/01/31 00:19:33 christos Exp $");
     28  1.1  christos 
     29  1.1  christos #include <assert.h>
     30  1.1  christos #include <fenv.h>
     31  1.1  christos 
     32  1.1  christos /* Load floating-point state register (all 64bits) */
     33  1.1  christos #define	__ldxfsr(__r)	__asm__	__volatile__		\
     34  1.1  christos 	("ldx %0, %%fsr" : : "m" (__r))
     35  1.1  christos 
     36  1.1  christos /* Save floating-point state register (all 64bits) */
     37  1.1  christos #define	__stxfsr(__r)	__asm__	__volatile__		\
     38  1.1  christos 	("stx %%fsr, %0" : "=m" (*(__r)))
     39  1.1  christos 
     40  1.1  christos /*
     41  1.1  christos  * The feclearexcept() function clears the supported floating-point exceptions
     42  1.1  christos  * represented by `excepts'.
     43  1.1  christos  */
     44  1.1  christos int
     45  1.1  christos feclearexcept(int excepts)
     46  1.1  christos {
     47  1.1  christos 	fexcept_t r;
     48  1.1  christos 	int ex;
     49  1.1  christos 
     50  1.1  christos 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
     51  1.1  christos 
     52  1.1  christos 	ex = excepts & FE_ALL_EXCEPT;
     53  1.1  christos 
     54  1.1  christos 	__stxfsr(&r);
     55  1.1  christos 	r &= ~ex;
     56  1.1  christos 	__ldxfsr(r);
     57  1.1  christos 
     58  1.1  christos 	/* Success */
     59  1.1  christos 	return 0;
     60  1.1  christos }
     61  1.1  christos 
     62  1.1  christos /*
     63  1.1  christos  * The fegetexceptflag() function stores an implementation-defined
     64  1.1  christos  * representation of the states of the floating-point status flags indicated
     65  1.1  christos  * by the argument excepts in the object pointed to by the argument flagp.
     66  1.1  christos  */
     67  1.1  christos int
     68  1.1  christos fegetexceptflag(fexcept_t *flagp, int excepts)
     69  1.1  christos {
     70  1.1  christos 	fexcept_t r;
     71  1.1  christos 	int ex;
     72  1.1  christos 
     73  1.1  christos 	_DIAGASSERT(flagp != NULL);
     74  1.1  christos 	_DIAGASSERT((excepts & ~_FE_ALL_EXCEPT) == 0);
     75  1.1  christos 
     76  1.1  christos 	ex = excepts & FE_ALL_EXCEPT;
     77  1.1  christos 
     78  1.1  christos 	__stxfsr(&r);
     79  1.1  christos 	*flagp = r & ex;
     80  1.1  christos 
     81  1.1  christos 	/* Success */
     82  1.1  christos 	return 0;
     83  1.1  christos }
     84  1.1  christos 
     85  1.1  christos 
     86  1.1  christos /*
     87  1.1  christos  * This function sets the floating-point status flags indicated by the argument
     88  1.1  christos  * `excepts' to the states stored in the object pointed to by `flagp'. It does
     89  1.1  christos  * NOT raise any floating-point exceptions, but only sets the state of the flags.
     90  1.1  christos  */
     91  1.1  christos int
     92  1.1  christos fesetexceptflag(const fexcept_t *flagp, int excepts)
     93  1.1  christos {
     94  1.1  christos 	fexcept_t r;
     95  1.1  christos 	int ex;
     96  1.1  christos 
     97  1.1  christos 	_DIAGASSERT(flagp != NULL);
     98  1.1  christos 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
     99  1.1  christos 
    100  1.1  christos 	ex = excepts & FE_ALL_EXCEPT;
    101  1.1  christos 
    102  1.1  christos 	__stxfsr(&r);
    103  1.1  christos 	r &= ~ex;
    104  1.1  christos 	r |= *flagp & ex;
    105  1.1  christos 	__ldxfsr(r);
    106  1.1  christos 
    107  1.1  christos 	/* Success */
    108  1.1  christos 	return 0;
    109  1.1  christos }
    110  1.1  christos 
    111  1.1  christos /*
    112  1.1  christos  * The feraiseexcept() function raises the supported floating-point exceptions
    113  1.1  christos  * represented by the argument `excepts'.
    114  1.1  christos  *
    115  1.1  christos  * The order in which these floating-point exceptions are raised is unspecified
    116  1.1  christos  * (by the standard).
    117  1.1  christos  */
    118  1.1  christos int
    119  1.1  christos feraiseexcept(int excepts)
    120  1.1  christos {
    121  1.1  christos 	volatile double d;
    122  1.1  christos 	int ex;
    123  1.1  christos 
    124  1.1  christos 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    125  1.1  christos 
    126  1.1  christos 	ex = excepts & FE_ALL_EXCEPT;
    127  1.1  christos 
    128  1.1  christos 	/*
    129  1.1  christos 	 * With a compiler that supports the FENV_ACCESS pragma properly, simple
    130  1.1  christos 	 * expressions like '0.0 / 0.0' should be sufficient to generate traps.
    131  1.1  christos 	 * Unfortunately, we need to bring a volatile variable into the equation
    132  1.1  christos 	 * to prevent incorrect optimizations.
    133  1.1  christos 	 */
    134  1.1  christos 	if (ex & FE_INVALID) {
    135  1.1  christos 		d = 0.0;
    136  1.1  christos 		d = 0.0 / d;
    137  1.1  christos 	}
    138  1.1  christos 	if (ex & FE_DIVBYZERO) {
    139  1.1  christos 		d = 0.0;
    140  1.1  christos 		d = 1.0 / d;
    141  1.1  christos 	}
    142  1.1  christos 	if (ex & FE_OVERFLOW) {
    143  1.1  christos 		d = 0x1.ffp1023;
    144  1.1  christos 		d *= 2.0;
    145  1.1  christos 	}
    146  1.1  christos 	if (ex & FE_UNDERFLOW) {
    147  1.1  christos 		d = 0x1p-1022;
    148  1.1  christos 		d /= 0x1p1023;
    149  1.1  christos 	}
    150  1.1  christos 	if (ex & FE_INEXACT) {
    151  1.1  christos 		d = 0x1p-1022;
    152  1.1  christos 		d += 1.0;
    153  1.1  christos 	}
    154  1.1  christos 
    155  1.1  christos 	/* Success */
    156  1.1  christos 	return 0;
    157  1.1  christos }
    158  1.1  christos 
    159  1.1  christos /*
    160  1.1  christos  * The fetestexcept() function determines which of a specified subset of the
    161  1.1  christos  * floating-point exception flags are currently set. The `excepts' argument
    162  1.1  christos  * specifies the floating-point status flags to be queried.
    163  1.1  christos  */
    164  1.1  christos int
    165  1.1  christos fetestexcept(int excepts)
    166  1.1  christos {
    167  1.1  christos 	fexcept_t r;
    168  1.1  christos 
    169  1.1  christos 	_DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
    170  1.1  christos 
    171  1.1  christos 	__stxfsr(&r);
    172  1.1  christos 
    173  1.1  christos 	return r & (excepts & FE_ALL_EXCEPT);
    174  1.1  christos }
    175  1.1  christos 
    176  1.1  christos /*
    177  1.1  christos  * The fegetround() function gets the current rounding direction.
    178  1.1  christos  */
    179  1.1  christos int
    180  1.1  christos fegetround(void)
    181  1.1  christos {
    182  1.1  christos 	fenv_t r;
    183  1.1  christos 
    184  1.1  christos 	__stxfsr(&r);
    185  1.1  christos 
    186  1.1  christos 	return (r >> _ROUND_SHIFT) & _ROUND_MASK;
    187  1.1  christos }
    188  1.1  christos 
    189  1.1  christos /*
    190  1.1  christos  * The fesetround() function establishes the rounding direction represented by
    191  1.1  christos  * its argument `round'. If the argument is not equal to the value of a rounding
    192  1.1  christos  * direction macro, the rounding direction is not changed.
    193  1.1  christos  */
    194  1.1  christos int
    195  1.1  christos fesetround(int round)
    196  1.1  christos {
    197  1.1  christos 	fenv_t r;
    198  1.1  christos 
    199  1.1  christos 	_DIAGASSERT((round & ~_ROUND_MASK) == 0);
    200  1.1  christos 	if (round & ~_ROUND_MASK)
    201  1.1  christos 		return -1;
    202  1.1  christos 
    203  1.1  christos 	__stxfsr(&r);
    204  1.1  christos 	r &= ~(_ROUND_MASK << _ROUND_SHIFT);
    205  1.1  christos 	r |= round << _ROUND_SHIFT;
    206  1.1  christos 	__ldxfsr(r);
    207  1.1  christos 
    208  1.1  christos 	/* Success */
    209  1.1  christos 	return 0;
    210  1.1  christos }
    211  1.1  christos 
    212  1.1  christos /*
    213  1.1  christos  * The fegetenv() function attempts to store the current floating-point
    214  1.1  christos  * environment in the object pointed to by envp.
    215  1.1  christos  */
    216  1.1  christos int
    217  1.1  christos fegetenv(fenv_t *envp)
    218  1.1  christos {
    219  1.1  christos 	_DIAGASSERT(envp != NULL);
    220  1.1  christos 
    221  1.1  christos 	__stxfsr(envp);
    222  1.1  christos 
    223  1.1  christos 	/* Success */
    224  1.1  christos 	return 0;
    225  1.1  christos }
    226  1.1  christos 
    227  1.1  christos 
    228  1.1  christos /*
    229  1.1  christos  * The feholdexcept() function saves the current floating-point environment
    230  1.1  christos  * in the object pointed to by envp, clears the floating-point status flags, and
    231  1.1  christos  * then installs a non-stop (continue on floating-point exceptions) mode, if
    232  1.1  christos  * available, for all floating-point exceptions.
    233  1.1  christos  */
    234  1.1  christos int
    235  1.1  christos feholdexcept(fenv_t *envp)
    236  1.1  christos {
    237  1.1  christos 	fenv_t r;
    238  1.1  christos 
    239  1.1  christos 	_DIAGASSERT(envp != NULL);
    240  1.1  christos 
    241  1.1  christos 	__stxfsr(&r);
    242  1.1  christos 	*envp = r;
    243  1.1  christos 	r &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
    244  1.1  christos 	__ldxfsr(r);
    245  1.1  christos 
    246  1.1  christos 	/* Success */
    247  1.1  christos 	return 0;
    248  1.1  christos }
    249  1.1  christos 
    250  1.1  christos /*
    251  1.1  christos  * The fesetenv() function attempts to establish the floating-point environment
    252  1.1  christos  * represented by the object pointed to by envp. The argument `envp' points
    253  1.1  christos  * to an object set by a call to fegetenv() or feholdexcept(), or equal a
    254  1.1  christos  * floating-point environment macro. The fesetenv() function does not raise
    255  1.1  christos  * floating-point exceptions, but only installs the state of the floating-point
    256  1.1  christos  * status flags represented through its argument.
    257  1.1  christos  */
    258  1.1  christos int
    259  1.1  christos fesetenv(const fenv_t *envp)
    260  1.1  christos {
    261  1.1  christos 	_DIAGASSERT(envp != NULL);
    262  1.1  christos 
    263  1.1  christos 	__ldxfsr(*envp);
    264  1.1  christos 
    265  1.1  christos 	/* Success */
    266  1.1  christos 	return 0;
    267  1.1  christos }
    268  1.1  christos 
    269  1.1  christos 
    270  1.1  christos /*
    271  1.1  christos  * The feupdateenv() function saves the currently raised floating-point
    272  1.1  christos  * exceptions in its automatic storage, installs the floating-point environment
    273  1.1  christos  * represented by the object pointed to by `envp', and then raises the saved
    274  1.1  christos  * floating-point exceptions. The argument `envp' shall point to an object set
    275  1.1  christos  * by a call to feholdexcept() or fegetenv(), or equal a floating-point
    276  1.1  christos  * environment macro.
    277  1.1  christos  */
    278  1.1  christos int
    279  1.1  christos feupdateenv(const fenv_t *envp)
    280  1.1  christos {
    281  1.1  christos 	fexcept_t r;
    282  1.1  christos 
    283  1.1  christos 	_DIAGASSERT(envp != NULL);
    284  1.1  christos 
    285  1.1  christos 	__stxfsr(&r);
    286  1.1  christos 	__ldxfsr(*envp);
    287  1.1  christos 
    288  1.1  christos 	_DIAGASSERT((r & ~FE_ALL_EXCEPT) == 0);
    289  1.1  christos 	feraiseexcept(r & FE_ALL_EXCEPT);
    290  1.1  christos 
    291  1.1  christos 	/* Success */
    292  1.1  christos 	return 0;
    293  1.1  christos }
    294  1.1  christos 
    295  1.1  christos /*
    296  1.1  christos  * The following functions are extentions to the standard
    297  1.1  christos  */
    298  1.1  christos int
    299  1.1  christos feenableexcept(int mask)
    300  1.1  christos {
    301  1.1  christos 	fenv_t old_r, new_r;
    302  1.1  christos 
    303  1.1  christos 	__stxfsr(&old_r);
    304  1.1  christos 	new_r = old_r | ((mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
    305  1.1  christos 	__ldxfsr(new_r);
    306  1.1  christos 
    307  1.1  christos 	return (old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT;
    308  1.1  christos }
    309  1.1  christos 
    310  1.1  christos int
    311  1.1  christos fedisableexcept(int mask)
    312  1.1  christos {
    313  1.1  christos 	fenv_t old_r, new_r;
    314  1.1  christos 
    315  1.1  christos 	__stxfsr(&old_r);
    316  1.1  christos 	new_r = old_r & ~((mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
    317  1.1  christos 	__ldxfsr(new_r);
    318  1.1  christos 
    319  1.1  christos 	return (old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT;
    320  1.1  christos }
    321  1.1  christos 
    322  1.1  christos int
    323  1.1  christos fegetexcept(void)
    324  1.1  christos {
    325  1.1  christos 	fenv_t r;
    326  1.1  christos 
    327  1.1  christos 	__stxfsr(&r);
    328  1.1  christos 	return (r & _ENABLE_MASK) >> _FPUSW_SHIFT;
    329  1.1  christos }
    330