Home | History | Annotate | Line # | Download | only in include
fenv.h revision 1.2
      1 /*	$NetBSD: fenv.h,v 1.2 2017/01/13 19:10:14 christos 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/mips/fenv.h 226218 2011-10-10 15:43:09Z das $
     29  */
     30 
     31 #ifndef	_MIPS_FENV_H_
     32 #define	_MIPS_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 	fpu_control_t __attribute__((__mode__(__SI__)));
     41 typedef	fpu_control_t	fenv_t;
     42 typedef	fpu_control_t	fexcept_t;
     43 
     44 /* Exception flags */
     45 #define	FE_INEXACT	0x0004
     46 #define	FE_UNDERFLOW	0x0008
     47 #define	FE_OVERFLOW	0x0010
     48 #define	FE_DIVBYZERO	0x0020
     49 #define	FE_INVALID	0x0040
     50 #define	FE_ALL_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | \
     51 			 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
     52 
     53 /* Rounding modes */
     54 #define	FE_TONEAREST	0x0000
     55 #define	FE_TOWARDZERO	0x0001
     56 #define	FE_UPWARD	0x0002
     57 #define	FE_DOWNWARD	0x0003
     58 #define	_ROUND_MASK	(FE_TONEAREST | FE_DOWNWARD | \
     59 			 FE_UPWARD | FE_TOWARDZERO)
     60 __BEGIN_DECLS
     61 
     62 /* Default floating-point environment */
     63 extern const fenv_t	__fe_dfl_env;
     64 #define	FE_DFL_ENV	(&__fe_dfl_env)
     65 
     66 /* We need to be able to map status flag positions to mask flag positions */
     67 #define	_ENABLE_MASK	(FE_ALL_EXCEPT << _ENABLE_SHIFT)
     68 #define _ENABLE_SHIFT    5
     69 
     70 
     71 #define	__rfs(__fpsr)	__asm __volatile("cfc1 %0,$31" : "=r" ((*__fpsr)))
     72 #define	__wfs(__fpsr)	__asm __volatile("ctc1 %0,$31" : : "r" (__fpsr))
     73 
     74 __fenv_static inline int
     75 feclearexcept(int __excepts)
     76 {
     77 	fexcept_t __fpsr;
     78 
     79 	__excepts &= FE_ALL_EXCEPT;
     80 	__rfs(&__fpsr);
     81 	__fpsr &= ~(__excepts | (__excepts << _ENABLE_SHIFT));
     82 	__wfs(__fpsr);
     83 	return 0;
     84 }
     85 
     86 __fenv_static inline int
     87 fegetexceptflag(fexcept_t *__flagp, int __excepts)
     88 {
     89 	fexcept_t __fpsr;
     90 
     91 	__rfs(&__fpsr);
     92 	*__flagp = __fpsr & __excepts;
     93 	return (0);
     94 }
     95 
     96 __fenv_static inline int
     97 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
     98 {
     99 	fexcept_t __fpsr;
    100 
    101 	__rfs(&__fpsr);
    102 	__fpsr &= ~__excepts;
    103 	__fpsr |= *__flagp & __excepts;
    104 	__wfs(__fpsr);
    105 	return (0);
    106 }
    107 
    108 __fenv_static inline int
    109 feraiseexcept(int __excepts)
    110 {
    111 	fexcept_t __ex = __excepts;
    112 
    113 	fesetexceptflag(&__ex, __excepts);	/* XXX */
    114 	return (0);
    115 }
    116 
    117 __fenv_static inline int
    118 fetestexcept(int __excepts)
    119 {
    120 	fexcept_t __fpsr;
    121 
    122 	__rfs(&__fpsr);
    123 	return (__fpsr & __excepts);
    124 }
    125 
    126 __fenv_static inline int
    127 fegetround(void)
    128 {
    129 	fexcept_t __fpsr;
    130 
    131 	__rfs(&__fpsr);
    132 	return __fpsr & _ROUND_MASK;
    133 }
    134 
    135 __fenv_static inline int
    136 fesetround(int __round)
    137 {
    138 	fexcept_t __fpsr;
    139 
    140 	if (__round & ~_ROUND_MASK)
    141 		return 1;
    142 	__rfs(&__fpsr);
    143 	__fpsr &= ~_ROUND_MASK;
    144 	__fpsr |= __round;
    145 	__wfs(&__fpsr);
    146 
    147 	return 0;
    148 }
    149 
    150 __fenv_static inline int
    151 fegetenv(fenv_t *__envp)
    152 {
    153 
    154 	__rfs(__envp);
    155 	return (0);
    156 }
    157 
    158 __fenv_static inline int
    159 feholdexcept(fenv_t *__envp)
    160 {
    161 	fenv_t __env;
    162 
    163 	__rfs(&__env);
    164 	*__envp = __env;
    165 	__env &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
    166 	__wfs(__env);
    167 	return (0);
    168 }
    169 
    170 __fenv_static inline int
    171 fesetenv(const fenv_t *__envp)
    172 {
    173 
    174 	__wfs(*__envp);
    175 	return (0);
    176 }
    177 
    178 __fenv_static inline int
    179 feupdateenv(const fenv_t *__envp)
    180 {
    181 	fexcept_t __fpsr;
    182 
    183 	__rfs(&__fpsr);
    184 	__wfs(*__envp);
    185 	feraiseexcept(__fpsr & FE_ALL_EXCEPT);
    186 	return (0);
    187 }
    188 
    189 #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE)
    190 
    191 /* We currently provide no external definitions of the functions below. */
    192 
    193 static inline int
    194 feenableexcept(int __excepts)
    195 {
    196 	fenv_t __old_fpsr, __new_fpsr;
    197 
    198 	__rfs(&__new_fpsr);
    199 	__old_fpsr = (__new_fpsr & _ENABLE_MASK) >> _ENABLE_SHIFT;
    200 	__excepts &= FE_ALL_EXCEPT;
    201 	__new_fpsr |= __excepts << _ENABLE_SHIFT;
    202 	__wfs(__new_fpsr);
    203 	return __old_fpsr;
    204 }
    205 
    206 static inline int
    207 fedisableexcept(int __excepts)
    208 {
    209 	fenv_t __old_fpsr, __new_fpsr;
    210 
    211 	__rfs(&__new_fpsr);
    212 	__old_fpsr = (__new_fpsr & _ENABLE_MASK) >> _ENABLE_SHIFT;
    213 	__excepts &= FE_ALL_EXCEPT;
    214 	__new_fpsr &= ~(__excepts << _ENABLE_SHIFT);
    215 	__wfs(__new_fpsr);
    216 	return __old_fpsr;
    217 }
    218 
    219 static inline int
    220 fegetexcept(void)
    221 {
    222 	fenv_t __fpsr;
    223 
    224 	__rfs(&__fpsr);
    225 	return ((__fpsr & _ENABLE_MASK) >> _ENABLE_SHIFT);
    226 }
    227 
    228 #endif /* _NETBSD_SOURCE || _GNU_SOURCE */
    229 
    230 __END_DECLS
    231 
    232 #endif	/* !_FENV_H_ */
    233