Home | History | Annotate | Line # | Download | only in include
fenv.h revision 1.1
      1 /*-
      2  * Copyright (c) 2004-2005 David Schultz <das (at) 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: releng/10.1/lib/msun/ia64/fenv.h 226218 2011-10-10 15:43:09Z das $
     27  */
     28 
     29 #ifndef	_IA64_FENV_H_
     30 #define	_IA64_FENV_H_
     31 
     32 #include <sys/stdint.h>
     33 
     34 #ifndef	__fenv_static
     35 #define	__fenv_static	static
     36 #endif
     37 
     38 typedef	__uint64_t	fenv_t;
     39 typedef	__uint16_t	fexcept_t;
     40 
     41 /* Exception flags */
     42 #define	FE_INVALID	0x01
     43 #define	FE_DENORMAL	0x02
     44 #define	FE_DIVBYZERO	0x04
     45 #define	FE_OVERFLOW	0x08
     46 #define	FE_UNDERFLOW	0x10
     47 #define	FE_INEXACT	0x20
     48 #define	FE_ALL_EXCEPT	(FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
     49 			 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
     50 
     51 /* Rounding modes */
     52 #define	FE_TONEAREST	0x0000
     53 #define	FE_DOWNWARD	0x0400
     54 #define	FE_UPWARD	0x0800
     55 #define	FE_TOWARDZERO	0x0c00
     56 #define	_ROUND_MASK	(FE_TONEAREST | FE_DOWNWARD | \
     57 			 FE_UPWARD | FE_TOWARDZERO)
     58 
     59 __BEGIN_DECLS
     60 
     61 /* Default floating-point environment */
     62 extern const fenv_t	__fe_dfl_env;
     63 #define	FE_DFL_ENV	(&__fe_dfl_env)
     64 
     65 #define	_FPUSW_SHIFT	13
     66 
     67 #define	__stfpsr(__r)	__asm __volatile("mov %0=ar.fpsr" : "=r" (*(__r)))
     68 #define	__ldfpsr(__r)	__asm __volatile("mov ar.fpsr=%0;;" : : "r" (__r))
     69 
     70 __fenv_static inline int
     71 feclearexcept(int __excepts)
     72 {
     73 	fenv_t __fpsr;
     74 
     75 	__stfpsr(&__fpsr);
     76 	__fpsr &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
     77 	__ldfpsr(__fpsr);
     78 	return (0);
     79 }
     80 
     81 __fenv_static inline int
     82 fegetexceptflag(fexcept_t *__flagp, int __excepts)
     83 {
     84 	fenv_t __fpsr;
     85 
     86 	__stfpsr(&__fpsr);
     87 	*__flagp = (fexcept_t)(__fpsr >> _FPUSW_SHIFT) & __excepts;
     88 	return (0);
     89 }
     90 
     91 __fenv_static inline int
     92 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
     93 {
     94 	fenv_t __fpsr;
     95 
     96 	__stfpsr(&__fpsr);
     97 	__fpsr &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
     98 	__fpsr |= (fenv_t)(__excepts & *__flagp) << _FPUSW_SHIFT;
     99 	__ldfpsr(__fpsr);
    100 	return (0);
    101 }
    102 
    103 /*
    104  * It is worthwhile to use the inline version of this function iff it
    105  * is called with arguments that are compile-time constants (due to
    106  * dead code elimination).  Unfortunately, gcc isn't smart enough to
    107  * figure this out automatically, and there's no way to tell it.
    108  * We assume that constant arguments will be the common case.
    109  */
    110 __fenv_static inline int
    111 feraiseexcept(int __excepts)
    112 {
    113 	volatile double d;
    114 
    115 	/*
    116 	 * With a compiler that supports the FENV_ACCESS pragma
    117 	 * properly, simple expressions like '0.0 / 0.0' should
    118 	 * be sufficient to generate traps.  Unfortunately, we
    119 	 * need to bring a volatile variable into the equation
    120 	 * to prevent incorrect optimizations.
    121 	 */
    122 	if (__excepts & FE_INVALID) {
    123 		d = 0.0;
    124 		d = 0.0 / d;
    125 	}
    126 	if (__excepts & FE_DIVBYZERO) {
    127 		d = 0.0;
    128 		d = 1.0 / d;
    129 	}
    130 	if (__excepts & FE_OVERFLOW) {
    131 		d = 0x1.ffp1023;
    132 		d *= 2.0;
    133 	}
    134 	if (__excepts & FE_UNDERFLOW) {
    135 		d = 0x1p-1022;
    136 		d /= 0x1p1023;
    137 	}
    138 	if (__excepts & FE_INEXACT) {
    139 		d = 0x1p-1022;
    140 		d += 1.0;
    141 	}
    142 	return (0);
    143 }
    144 
    145 __fenv_static inline int
    146 fetestexcept(int __excepts)
    147 {
    148 	fenv_t __fpsr;
    149 
    150 	__stfpsr(&__fpsr);
    151 	return ((__fpsr >> _FPUSW_SHIFT) & __excepts);
    152 }
    153 
    154 
    155 __fenv_static inline int
    156 fegetround(void)
    157 {
    158 	fenv_t __fpsr;
    159 
    160 	__stfpsr(&__fpsr);
    161 	return (__fpsr & _ROUND_MASK);
    162 }
    163 
    164 __fenv_static inline int
    165 fesetround(int __round)
    166 {
    167 	fenv_t __fpsr;
    168 
    169 	if (__round & ~_ROUND_MASK)
    170 		return (-1);
    171 	__stfpsr(&__fpsr);
    172 	__fpsr &= ~_ROUND_MASK;
    173 	__fpsr |= __round;
    174 	__ldfpsr(__fpsr);
    175 	return (0);
    176 }
    177 
    178 __fenv_static inline int
    179 fegetenv(fenv_t *__envp)
    180 {
    181 
    182 	__stfpsr(__envp);
    183 	return (0);
    184 }
    185 
    186 __fenv_static inline int
    187 feholdexcept(fenv_t *__envp)
    188 {
    189 	fenv_t __fpsr;
    190 
    191 	__stfpsr(&__fpsr);
    192 	*__envp = __fpsr;
    193 	__fpsr &= ~((fenv_t)FE_ALL_EXCEPT << _FPUSW_SHIFT);
    194 	__fpsr |= FE_ALL_EXCEPT;
    195 	__ldfpsr(__fpsr);
    196 	return (0);
    197 }
    198 
    199 __fenv_static inline int
    200 fesetenv(const fenv_t *__envp)
    201 {
    202 
    203 	__ldfpsr(*__envp);
    204 	return (0);
    205 }
    206 
    207 int feupdateenv(const fenv_t *__envp);
    208 
    209 #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE)
    210 
    211 /* We currently provide no external definitions of the functions below. */
    212 
    213 static inline int
    214 feenableexcept(int __mask)
    215 {
    216 	fenv_t __newfpsr, __oldfpsr;
    217 
    218 	__stfpsr(&__oldfpsr);
    219 	__newfpsr = __oldfpsr & ~(__mask & FE_ALL_EXCEPT);
    220 	__ldfpsr(__newfpsr);
    221 	return (~__oldfpsr & FE_ALL_EXCEPT);
    222 }
    223 
    224 static inline int
    225 fedisableexcept(int __mask)
    226 {
    227 	fenv_t __newfpsr, __oldfpsr;
    228 
    229 	__stfpsr(&__oldfpsr);
    230 	__newfpsr = __oldfpsr | (__mask & FE_ALL_EXCEPT);
    231 	__ldfpsr(__newfpsr);
    232 	return (~__oldfpsr & FE_ALL_EXCEPT);
    233 }
    234 
    235 static inline int
    236 fegetexcept(void)
    237 {
    238 	fenv_t __fpsr;
    239 
    240 	__stfpsr(&__fpsr);
    241 	return (~__fpsr & FE_ALL_EXCEPT);
    242 }
    243 
    244 #endif /* _NETBSD_SOURCE || _GNU_SOURCE */
    245 
    246 __END_DECLS
    247 
    248 #endif	/* !_IA64_FENV_H_ */
    249