Home | History | Annotate | Line # | Download | only in include
fenv.h revision 1.1.4.1
      1 /*	$NetBSD: fenv.h,v 1.1.4.1 2017/03/20 06:57:18 pgoyette 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  * $FreeBSD: head/lib/msun/powerpc/fenv.h 226218 2011-10-10 15:43:09Z das $
     29  */
     30 
     31 #ifndef	_POWERPC_FENV_H_
     32 #define	_POWERPC_FENV_H_
     33 
     34 #include <sys/stdint.h>
     35 
     36 #ifndef	__fenv_static
     37 #define	__fenv_static	static
     38 #endif
     39 
     40 typedef	uint32_t	fenv_t;
     41 typedef	uint32_t	fexcept_t;
     42 
     43 /* Exception flags */
     44 #define	FE_INEXACT	0x02000000
     45 #define	FE_DIVBYZERO	0x04000000
     46 #define	FE_UNDERFLOW	0x08000000
     47 #define	FE_OVERFLOW	0x10000000
     48 #define	FE_INVALID	0x20000000	/* all types of invalid FP ops */
     49 
     50 /*
     51  * The PowerPC architecture has extra invalid flags that indicate the
     52  * specific type of invalid operation occurred.  These flags may be
     53  * tested, set, and cleared---but not masked---separately.  All of
     54  * these bits are cleared when FE_INVALID is cleared, but only
     55  * FE_VXSOFT is set when FE_INVALID is explicitly set in software.
     56  */
     57 #define	FE_VXCVI	0x00000100	/* invalid integer convert */
     58 #define	FE_VXSQRT	0x00000200	/* square root of a negative */
     59 #define	FE_VXSOFT	0x00000400	/* software-requested exception */
     60 #define	FE_VXVC		0x00080000	/* ordered comparison involving NaN */
     61 #define	FE_VXIMZ	0x00100000	/* inf * 0 */
     62 #define	FE_VXZDZ	0x00200000	/* 0 / 0 */
     63 #define	FE_VXIDI	0x00400000	/* inf / inf */
     64 #define	FE_VXISI	0x00800000	/* inf - inf */
     65 #define	FE_VXSNAN	0x01000000	/* operation on a signalling NaN */
     66 #define	FE_ALL_INVALID	(FE_VXCVI | FE_VXSQRT | FE_VXSOFT | FE_VXVC | \
     67 			 FE_VXIMZ | FE_VXZDZ | FE_VXIDI | FE_VXISI | \
     68 			 FE_VXSNAN | FE_INVALID)
     69 #define	FE_ALL_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | \
     70 			 FE_ALL_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
     71 
     72 /* Rounding modes */
     73 #define	FE_TONEAREST	0x0000
     74 #define	FE_TOWARDZERO	0x0001
     75 #define	FE_UPWARD	0x0002
     76 #define	FE_DOWNWARD	0x0003
     77 #define	_ROUND_MASK	(FE_TONEAREST | FE_DOWNWARD | \
     78 			 FE_UPWARD | FE_TOWARDZERO)
     79 
     80 #ifndef _KERNEL
     81 __BEGIN_DECLS
     82 
     83 /* Default floating-point environment */
     84 extern const fenv_t	__fe_dfl_env;
     85 #define	FE_DFL_ENV	(&__fe_dfl_env)
     86 
     87 /* We need to be able to map status flag positions to mask flag positions */
     88 #define	_FPUSW_SHIFT	22
     89 #define	_ENABLE_MASK	((FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
     90 			 FE_OVERFLOW | FE_UNDERFLOW) >> _FPUSW_SHIFT)
     91 
     92 #ifndef _SOFT_FLOAT
     93 #define	__mffs(__env)	__asm __volatile("mffs %0" : "=f" (*(__env)))
     94 #define	__mtfsf(__env)	__asm __volatile("mtfsf 255,%0" : : "f" (__env))
     95 
     96 static inline uint32_t
     97 __mfmsr(void)
     98 {
     99 	uint32_t __msr;
    100 
    101 	__asm volatile ("mfmsr %0" : "=r"(__msr));
    102 	return __msr;
    103 }
    104 
    105 static inline void
    106 __mtmsr(uint32_t __msr)
    107 {
    108 
    109 	__asm volatile ("mtmsr %0" : : "r"(__msr));
    110 }
    111 
    112 #define __MSR_FE_MASK	(0x00000800 | 0x00000100)
    113 #define __MSR_FE_DIS	(0)
    114 #define __MSR_FE_PREC	(0x00000800 | 0x00000100)
    115 
    116 static inline void
    117 __updatemsr(uint32_t __reg)
    118 {
    119 	uint32_t __msr;
    120 
    121 	__msr = __mfmsr() & ~__MSR_FE_MASK;
    122 	if (__reg != 0) {
    123 		__msr |= __MSR_FE_PREC;
    124 	} else {
    125 		__msr |= __MSR_FE_DIS;
    126 	}
    127 	__mtmsr(__msr);
    128 }
    129 
    130 #else
    131 #define	__mffs(__env)
    132 #define	__mtfsf(__env)
    133 #define __updatemsr(__reg)
    134 #endif
    135 
    136 union __fpscr {
    137 	double __d;
    138 	struct {
    139 		uint32_t __junk;
    140 		fenv_t __reg;
    141 	} __bits;
    142 };
    143 
    144 __fenv_static inline int
    145 feclearexcept(int __excepts)
    146 {
    147 	union __fpscr __r;
    148 
    149 	if (__excepts & FE_INVALID)
    150 		__excepts |= FE_ALL_INVALID;
    151 	__mffs(&__r.__d);
    152 	__r.__bits.__reg &= ~__excepts;
    153 	__mtfsf(__r.__d);
    154 	return (0);
    155 }
    156 
    157 __fenv_static inline int
    158 fegetexceptflag(fexcept_t *__flagp, int __excepts)
    159 {
    160 	union __fpscr __r;
    161 
    162 	__mffs(&__r.__d);
    163 	*__flagp = __r.__bits.__reg & __excepts;
    164 	return (0);
    165 }
    166 
    167 __fenv_static inline int
    168 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
    169 {
    170 	union __fpscr __r;
    171 
    172 	if (__excepts & FE_INVALID)
    173 		__excepts |= FE_ALL_EXCEPT;
    174 	__mffs(&__r.__d);
    175 	__r.__bits.__reg &= ~__excepts;
    176 	__r.__bits.__reg |= *__flagp & __excepts;
    177 	__mtfsf(__r.__d);
    178 	return (0);
    179 }
    180 
    181 __fenv_static inline int
    182 feraiseexcept(int __excepts)
    183 {
    184 	union __fpscr __r;
    185 
    186 	if (__excepts & FE_INVALID)
    187 		__excepts |= FE_VXSOFT;
    188 	__mffs(&__r.__d);
    189 	__r.__bits.__reg |= __excepts;
    190 	__mtfsf(__r.__d);
    191 	return (0);
    192 }
    193 
    194 __fenv_static inline int
    195 fetestexcept(int __excepts)
    196 {
    197 	union __fpscr __r;
    198 
    199 	__mffs(&__r.__d);
    200 	return (__r.__bits.__reg & __excepts);
    201 }
    202 
    203 __fenv_static inline int
    204 fegetround(void)
    205 {
    206 	union __fpscr __r;
    207 
    208 	__mffs(&__r.__d);
    209 	return (__r.__bits.__reg & _ROUND_MASK);
    210 }
    211 
    212 __fenv_static inline int
    213 fesetround(int __round)
    214 {
    215 	union __fpscr __r;
    216 
    217 	if (__round & ~_ROUND_MASK)
    218 		return (-1);
    219 	__mffs(&__r.__d);
    220 	__r.__bits.__reg &= ~_ROUND_MASK;
    221 	__r.__bits.__reg |= __round;
    222 	__mtfsf(__r.__d);
    223 	return (0);
    224 }
    225 
    226 __fenv_static inline int
    227 fegetenv(fenv_t *__envp)
    228 {
    229 	union __fpscr __r;
    230 
    231 	__mffs(&__r.__d);
    232 	*__envp = __r.__bits.__reg;
    233 	return (0);
    234 }
    235 
    236 __fenv_static inline int
    237 feholdexcept(fenv_t *__envp)
    238 {
    239 	union __fpscr __r;
    240 	uint32_t msr;
    241 
    242 	__mffs(&__r.__d);
    243 	*__envp = __r.__d;
    244 	__r.__bits.__reg &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
    245 	__mtfsf(__r.__d);
    246 	__updatemsr(__r.__bits.__reg);
    247 	return (0);
    248 }
    249 
    250 __fenv_static inline int
    251 fesetenv(const fenv_t *__envp)
    252 {
    253 	union __fpscr __r;
    254 
    255 	__r.__bits.__reg = *__envp;
    256 	__mtfsf(__r.__d);
    257 	__updatemsr(__r.__bits.__reg);
    258 	return (0);
    259 }
    260 
    261 __fenv_static inline int
    262 feupdateenv(const fenv_t *__envp)
    263 {
    264 	union __fpscr __r;
    265 
    266 	__mffs(&__r.__d);
    267 	__r.__bits.__reg &= FE_ALL_EXCEPT;
    268 	__r.__bits.__reg |= *__envp;
    269 	__mtfsf(__r.__d);
    270 	__updatemsr(__r.__bits.__reg);
    271 	return (0);
    272 }
    273 
    274 #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE)
    275 
    276 /* We currently provide no external definitions of the functions below. */
    277 
    278 static inline int
    279 feenableexcept(int __mask)
    280 {
    281 	union __fpscr __r;
    282 	fenv_t __oldmask;
    283 
    284 	__mffs(&__r.__d);
    285 	__oldmask = __r.__bits.__reg;
    286 	__r.__bits.__reg |= (__mask & FE_ALL_EXCEPT) >> _FPUSW_SHIFT;
    287 	__mtfsf(__r.__d);
    288 	__updatemsr(__r.__bits.__reg);
    289 	return ((__oldmask & _ENABLE_MASK) << _FPUSW_SHIFT);
    290 }
    291 
    292 static inline int
    293 fedisableexcept(int __mask)
    294 {
    295 	union __fpscr __r;
    296 	fenv_t __oldmask;
    297 
    298 	__mffs(&__r.__d);
    299 	__oldmask = __r.__bits.__reg;
    300 	__r.__bits.__reg &= ~((__mask & FE_ALL_EXCEPT) >> _FPUSW_SHIFT);
    301 	__mtfsf(__r.__d);
    302 	__updatemsr(__r.__bits.__reg);
    303 	return ((__oldmask & _ENABLE_MASK) << _FPUSW_SHIFT);
    304 }
    305 
    306 static inline int
    307 fegetexcept(void)
    308 {
    309 	union __fpscr __r;
    310 
    311 	__mffs(&__r.__d);
    312 	return ((__r.__bits.__reg & _ENABLE_MASK) << _FPUSW_SHIFT);
    313 }
    314 
    315 #endif /* _NETBSD_SOURCE || _GNU_SOURCE */
    316 
    317 __END_DECLS
    318 #endif
    319 
    320 #endif	/* !_POWERPC_FENV_H_ */
    321