Home | History | Annotate | Line # | Download | only in include
fenv.h revision 1.6
      1  1.6    simonb /*	$NetBSD: fenv.h,v 1.6 2020/07/26 08:08:41 simonb 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  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  christos  * SUCH DAMAGE.
     27  1.1  christos  *
     28  1.1  christos  * $FreeBSD: head/lib/msun/mips/fenv.h 226218 2011-10-10 15:43:09Z das $
     29  1.1  christos  */
     30  1.1  christos 
     31  1.1  christos #ifndef	_MIPS_FENV_H_
     32  1.1  christos #define	_MIPS_FENV_H_
     33  1.1  christos 
     34  1.1  christos #include <sys/stdint.h>
     35  1.1  christos 
     36  1.1  christos /* Exception flags */
     37  1.2  christos #define	FE_INEXACT	0x0004
     38  1.1  christos #define	FE_UNDERFLOW	0x0008
     39  1.2  christos #define	FE_OVERFLOW	0x0010
     40  1.2  christos #define	FE_DIVBYZERO	0x0020
     41  1.2  christos #define	FE_INVALID	0x0040
     42  1.1  christos #define	FE_ALL_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | \
     43  1.1  christos 			 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
     44  1.1  christos 
     45  1.1  christos /* Rounding modes */
     46  1.1  christos #define	FE_TONEAREST	0x0000
     47  1.1  christos #define	FE_TOWARDZERO	0x0001
     48  1.1  christos #define	FE_UPWARD	0x0002
     49  1.1  christos #define	FE_DOWNWARD	0x0003
     50  1.1  christos #define	_ROUND_MASK	(FE_TONEAREST | FE_DOWNWARD | \
     51  1.1  christos 			 FE_UPWARD | FE_TOWARDZERO)
     52  1.4       chs 
     53  1.4       chs #ifndef __mips_soft_float
     54  1.4       chs 
     55  1.4       chs #ifndef	__fenv_static
     56  1.4       chs #define	__fenv_static	static
     57  1.4       chs #endif
     58  1.4       chs 
     59  1.4       chs typedef	uint32_t 	fpu_control_t __attribute__((__mode__(__SI__)));
     60  1.4       chs typedef	fpu_control_t	fenv_t;
     61  1.4       chs typedef	fpu_control_t	fexcept_t;
     62  1.4       chs 
     63  1.1  christos __BEGIN_DECLS
     64  1.1  christos 
     65  1.1  christos /* Default floating-point environment */
     66  1.1  christos extern const fenv_t	__fe_dfl_env;
     67  1.1  christos #define	FE_DFL_ENV	(&__fe_dfl_env)
     68  1.1  christos 
     69  1.1  christos /* We need to be able to map status flag positions to mask flag positions */
     70  1.2  christos #define	_ENABLE_MASK	(FE_ALL_EXCEPT << _ENABLE_SHIFT)
     71  1.6    simonb #define	_ENABLE_SHIFT    5
     72  1.1  christos 
     73  1.3       chs static inline fpu_control_t
     74  1.3       chs __rfs(void)
     75  1.3       chs {
     76  1.3       chs 	fpu_control_t __fpsr;
     77  1.3       chs 
     78  1.3       chs 	__asm __volatile("cfc1 %0,$31" : "=r" (__fpsr));
     79  1.3       chs 	return __fpsr;
     80  1.3       chs }
     81  1.2  christos 
     82  1.3       chs static inline void
     83  1.3       chs __wfs(fpu_control_t __fpsr)
     84  1.3       chs {
     85  1.3       chs 
     86  1.3       chs 	__asm __volatile("ctc1 %0,$31" : : "r" (__fpsr));
     87  1.3       chs }
     88  1.1  christos 
     89  1.5  christos #if __GNUC_PREREQ__(8, 0)
     90  1.5  christos #pragma GCC diagnostic push
     91  1.5  christos #pragma GCC diagnostic ignored "-Wshadow"
     92  1.5  christos #endif
     93  1.5  christos 
     94  1.1  christos __fenv_static inline int
     95  1.1  christos feclearexcept(int __excepts)
     96  1.1  christos {
     97  1.1  christos 	fexcept_t __fpsr;
     98  1.1  christos 
     99  1.2  christos 	__excepts &= FE_ALL_EXCEPT;
    100  1.3       chs 	__fpsr = __rfs();
    101  1.2  christos 	__fpsr &= ~(__excepts | (__excepts << _ENABLE_SHIFT));
    102  1.1  christos 	__wfs(__fpsr);
    103  1.2  christos 	return 0;
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos __fenv_static inline int
    107  1.1  christos fegetexceptflag(fexcept_t *__flagp, int __excepts)
    108  1.1  christos {
    109  1.1  christos 	fexcept_t __fpsr;
    110  1.1  christos 
    111  1.3       chs 	__fpsr = __rfs();
    112  1.1  christos 	*__flagp = __fpsr & __excepts;
    113  1.1  christos 	return (0);
    114  1.1  christos }
    115  1.1  christos 
    116  1.1  christos __fenv_static inline int
    117  1.1  christos fesetexceptflag(const fexcept_t *__flagp, int __excepts)
    118  1.1  christos {
    119  1.1  christos 	fexcept_t __fpsr;
    120  1.1  christos 
    121  1.3       chs 	__fpsr = __rfs();
    122  1.1  christos 	__fpsr &= ~__excepts;
    123  1.1  christos 	__fpsr |= *__flagp & __excepts;
    124  1.1  christos 	__wfs(__fpsr);
    125  1.1  christos 	return (0);
    126  1.1  christos }
    127  1.1  christos 
    128  1.1  christos __fenv_static inline int
    129  1.1  christos feraiseexcept(int __excepts)
    130  1.1  christos {
    131  1.1  christos 	fexcept_t __ex = __excepts;
    132  1.1  christos 
    133  1.1  christos 	fesetexceptflag(&__ex, __excepts);	/* XXX */
    134  1.1  christos 	return (0);
    135  1.1  christos }
    136  1.1  christos 
    137  1.1  christos __fenv_static inline int
    138  1.1  christos fetestexcept(int __excepts)
    139  1.1  christos {
    140  1.1  christos 	fexcept_t __fpsr;
    141  1.1  christos 
    142  1.3       chs 	__fpsr = __rfs();
    143  1.1  christos 	return (__fpsr & __excepts);
    144  1.1  christos }
    145  1.1  christos 
    146  1.1  christos __fenv_static inline int
    147  1.1  christos fegetround(void)
    148  1.1  christos {
    149  1.2  christos 	fexcept_t __fpsr;
    150  1.1  christos 
    151  1.3       chs 	__fpsr = __rfs();
    152  1.2  christos 	return __fpsr & _ROUND_MASK;
    153  1.1  christos }
    154  1.1  christos 
    155  1.1  christos __fenv_static inline int
    156  1.1  christos fesetround(int __round)
    157  1.1  christos {
    158  1.2  christos 	fexcept_t __fpsr;
    159  1.2  christos 
    160  1.2  christos 	if (__round & ~_ROUND_MASK)
    161  1.2  christos 		return 1;
    162  1.3       chs 	__fpsr = __rfs();
    163  1.2  christos 	__fpsr &= ~_ROUND_MASK;
    164  1.2  christos 	__fpsr |= __round;
    165  1.3       chs 	__wfs(__fpsr);
    166  1.1  christos 
    167  1.2  christos 	return 0;
    168  1.1  christos }
    169  1.1  christos 
    170  1.1  christos __fenv_static inline int
    171  1.1  christos fegetenv(fenv_t *__envp)
    172  1.1  christos {
    173  1.1  christos 
    174  1.3       chs 	*__envp = __rfs();
    175  1.1  christos 	return (0);
    176  1.1  christos }
    177  1.1  christos 
    178  1.1  christos __fenv_static inline int
    179  1.1  christos feholdexcept(fenv_t *__envp)
    180  1.1  christos {
    181  1.1  christos 	fenv_t __env;
    182  1.1  christos 
    183  1.3       chs 	__env = __rfs();
    184  1.1  christos 	*__envp = __env;
    185  1.1  christos 	__env &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
    186  1.1  christos 	__wfs(__env);
    187  1.1  christos 	return (0);
    188  1.1  christos }
    189  1.1  christos 
    190  1.1  christos __fenv_static inline int
    191  1.1  christos fesetenv(const fenv_t *__envp)
    192  1.1  christos {
    193  1.1  christos 
    194  1.1  christos 	__wfs(*__envp);
    195  1.1  christos 	return (0);
    196  1.1  christos }
    197  1.1  christos 
    198  1.1  christos __fenv_static inline int
    199  1.1  christos feupdateenv(const fenv_t *__envp)
    200  1.1  christos {
    201  1.1  christos 	fexcept_t __fpsr;
    202  1.1  christos 
    203  1.3       chs 	__fpsr = __rfs();
    204  1.1  christos 	__wfs(*__envp);
    205  1.1  christos 	feraiseexcept(__fpsr & FE_ALL_EXCEPT);
    206  1.1  christos 	return (0);
    207  1.1  christos }
    208  1.1  christos 
    209  1.5  christos #if __GNUC_PREREQ__(8, 0)
    210  1.5  christos #pragma GCC diagnostic pop
    211  1.5  christos #endif
    212  1.5  christos 
    213  1.1  christos #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE)
    214  1.1  christos 
    215  1.4       chs __fenv_static inline int
    216  1.2  christos feenableexcept(int __excepts)
    217  1.1  christos {
    218  1.1  christos 	fenv_t __old_fpsr, __new_fpsr;
    219  1.1  christos 
    220  1.3       chs 	__new_fpsr = __rfs();
    221  1.2  christos 	__old_fpsr = (__new_fpsr & _ENABLE_MASK) >> _ENABLE_SHIFT;
    222  1.2  christos 	__excepts &= FE_ALL_EXCEPT;
    223  1.2  christos 	__new_fpsr |= __excepts << _ENABLE_SHIFT;
    224  1.1  christos 	__wfs(__new_fpsr);
    225  1.2  christos 	return __old_fpsr;
    226  1.1  christos }
    227  1.1  christos 
    228  1.4       chs __fenv_static inline int
    229  1.2  christos fedisableexcept(int __excepts)
    230  1.1  christos {
    231  1.1  christos 	fenv_t __old_fpsr, __new_fpsr;
    232  1.1  christos 
    233  1.3       chs 	__new_fpsr = __rfs();
    234  1.2  christos 	__old_fpsr = (__new_fpsr & _ENABLE_MASK) >> _ENABLE_SHIFT;
    235  1.2  christos 	__excepts &= FE_ALL_EXCEPT;
    236  1.2  christos 	__new_fpsr &= ~(__excepts << _ENABLE_SHIFT);
    237  1.1  christos 	__wfs(__new_fpsr);
    238  1.2  christos 	return __old_fpsr;
    239  1.1  christos }
    240  1.1  christos 
    241  1.4       chs __fenv_static inline int
    242  1.1  christos fegetexcept(void)
    243  1.1  christos {
    244  1.1  christos 	fenv_t __fpsr;
    245  1.1  christos 
    246  1.3       chs 	__fpsr = __rfs();
    247  1.2  christos 	return ((__fpsr & _ENABLE_MASK) >> _ENABLE_SHIFT);
    248  1.1  christos }
    249  1.1  christos 
    250  1.1  christos #endif /* _NETBSD_SOURCE || _GNU_SOURCE */
    251  1.1  christos 
    252  1.1  christos __END_DECLS
    253  1.1  christos 
    254  1.4       chs #endif /* __mips_soft_float */
    255  1.4       chs 
    256  1.1  christos #endif	/* !_FENV_H_ */
    257