Home | History | Annotate | Line # | Download | only in include
      1  1.4  riastrad /*	$NetBSD: fenv.h,v 1.4 2024/10/30 15:56:10 riastradh 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: src/lib/msun/alpha/fenv.h,v 1.3 2005/03/16 19:03:44 das Exp $
     29  1.1  christos  */
     30  1.1  christos 
     31  1.1  christos #ifndef	_ALPHA_FENV_H_
     32  1.1  christos #define	_ALPHA_FENV_H_
     33  1.1  christos 
     34  1.4  riastrad #include <sys/featuretest.h>
     35  1.1  christos #include <sys/stdint.h>
     36  1.1  christos 
     37  1.1  christos typedef	__uint64_t	fenv_t;
     38  1.1  christos typedef	__uint16_t	fexcept_t;
     39  1.1  christos 
     40  1.1  christos /* Exception flags */
     41  1.2  christos #define	FE_INVALID	0x01
     42  1.2  christos #define	FE_DIVBYZERO	0x02
     43  1.2  christos #define	FE_OVERFLOW	0x04
     44  1.2  christos #define	FE_UNDERFLOW	0x08
     45  1.2  christos #define	FE_INEXACT	0x10
     46  1.2  christos #define	FE_INTOVF	0x20	/* not maskable */
     47  1.1  christos #define	FE_ALL_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | FE_INTOVF | \
     48  1.1  christos 			 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
     49  1.1  christos 
     50  1.1  christos /* Rounding modes */
     51  1.1  christos #define	FE_TOWARDZERO	0x00
     52  1.1  christos #define	FE_DOWNWARD	0x01
     53  1.1  christos #define	FE_TONEAREST	0x02
     54  1.1  christos #define	FE_UPWARD	0x03
     55  1.1  christos #define	_ROUND_MASK	(FE_TONEAREST | FE_DOWNWARD | \
     56  1.1  christos 			 FE_UPWARD | FE_TOWARDZERO)
     57  1.1  christos #define	_ROUND_SHIFT	58
     58  1.1  christos 
     59  1.2  christos #define	_FPUSW_SHIFT	52
     60  1.1  christos 
     61  1.1  christos #define	__excb()	__asm __volatile("excb")
     62  1.1  christos #define	__mf_fpcr(__cw)	__asm __volatile("mf_fpcr %0" : "=f" (*(__cw)))
     63  1.1  christos #define	__mt_fpcr(__cw)	__asm __volatile("mt_fpcr %0" : : "f" (__cw))
     64  1.1  christos 
     65  1.1  christos union __fpcr {
     66  1.1  christos 	double __d;
     67  1.1  christos 	fenv_t __bits;
     68  1.1  christos };
     69  1.1  christos 
     70  1.1  christos __BEGIN_DECLS
     71  1.1  christos 
     72  1.1  christos /* Default floating-point environment */
     73  1.1  christos extern const fenv_t	__fe_dfl_env;
     74  1.1  christos #define	FE_DFL_ENV	(&__fe_dfl_env)
     75  1.1  christos 
     76  1.3  christos #if __GNUC_PREREQ__(8, 0)
     77  1.3  christos #pragma GCC diagnostic push
     78  1.3  christos #pragma GCC diagnostic ignored "-Wshadow"
     79  1.3  christos #endif
     80  1.3  christos 
     81  1.1  christos static __inline int
     82  1.1  christos feclearexcept(int __excepts)
     83  1.1  christos {
     84  1.1  christos 	union __fpcr __r;
     85  1.1  christos 
     86  1.1  christos 	__excb();
     87  1.1  christos 	__mf_fpcr(&__r.__d);
     88  1.1  christos 	__r.__bits &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
     89  1.1  christos 	__mt_fpcr(__r.__d);
     90  1.1  christos 	__excb();
     91  1.1  christos 	return 0;
     92  1.1  christos }
     93  1.1  christos 
     94  1.1  christos static __inline int
     95  1.1  christos fegetexceptflag(fexcept_t *__flagp, int __excepts)
     96  1.1  christos {
     97  1.1  christos 	union __fpcr __r;
     98  1.1  christos 
     99  1.1  christos 	__excb();
    100  1.1  christos 	__mf_fpcr(&__r.__d);
    101  1.1  christos 	__excb();
    102  1.1  christos 	*__flagp = (__r.__bits >> _FPUSW_SHIFT) & __excepts;
    103  1.1  christos 	return 0;
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos static __inline int
    107  1.1  christos fesetexceptflag(const fexcept_t *__flagp, int __excepts)
    108  1.1  christos {
    109  1.1  christos 	union __fpcr __r;
    110  1.1  christos 	fenv_t __xflag, __xexcepts;
    111  1.1  christos 
    112  1.1  christos 	__xflag = (fenv_t)*__flagp << _FPUSW_SHIFT;
    113  1.1  christos 	__xexcepts = (fenv_t)__excepts << _FPUSW_SHIFT;
    114  1.1  christos 	__excb();
    115  1.1  christos 	__mf_fpcr(&__r.__d);
    116  1.1  christos 	__r.__bits &= ~__xexcepts;
    117  1.1  christos 	__r.__bits |= __xflag & __xexcepts;
    118  1.1  christos 	__mt_fpcr(__r.__d);
    119  1.1  christos 	__excb();
    120  1.1  christos 	return 0;
    121  1.1  christos }
    122  1.1  christos 
    123  1.1  christos static __inline int
    124  1.1  christos feraiseexcept(int __excepts)
    125  1.1  christos {
    126  1.1  christos 
    127  1.1  christos 	/*
    128  1.1  christos 	 * XXX Generating exceptions this way does not actually invoke
    129  1.1  christos 	 * a userland trap handler when enabled, but neither do
    130  1.1  christos 	 * arithmetic operations as far as I can tell.  Perhaps there
    131  1.1  christos 	 * are more bugs in the kernel trap handler.
    132  1.1  christos 	 */
    133  1.1  christos 	fexcept_t __ex = __excepts;
    134  1.1  christos 	fesetexceptflag(&__ex, __excepts);
    135  1.1  christos 	return 0;
    136  1.1  christos }
    137  1.1  christos 
    138  1.1  christos static __inline int
    139  1.1  christos fetestexcept(int __excepts)
    140  1.1  christos {
    141  1.1  christos 	union __fpcr __r;
    142  1.1  christos 
    143  1.1  christos 	__excb();
    144  1.1  christos 	__mf_fpcr(&__r.__d);
    145  1.1  christos 	__excb();
    146  1.1  christos 	return (__r.__bits >> _FPUSW_SHIFT) & __excepts;
    147  1.1  christos }
    148  1.1  christos 
    149  1.1  christos static __inline int
    150  1.1  christos fegetround(void)
    151  1.1  christos {
    152  1.1  christos 	union __fpcr __r;
    153  1.1  christos 
    154  1.1  christos 	/*
    155  1.1  christos 	 * No exception barriers should be required here if we assume
    156  1.1  christos 	 * that only fesetround() can change the rounding mode.
    157  1.1  christos 	 */
    158  1.1  christos 	__mf_fpcr(&__r.__d);
    159  1.1  christos 	return (int)(__r.__bits >> _ROUND_SHIFT) & _ROUND_MASK;
    160  1.1  christos }
    161  1.1  christos 
    162  1.1  christos static __inline int
    163  1.1  christos fesetround(int __round)
    164  1.1  christos {
    165  1.1  christos 	union __fpcr __r;
    166  1.1  christos 
    167  1.1  christos 	if (__round & ~_ROUND_MASK)
    168  1.1  christos 		return (-1);
    169  1.1  christos 	__excb();
    170  1.1  christos 	__mf_fpcr(&__r.__d);
    171  1.1  christos 	__r.__bits &= ~((fenv_t)_ROUND_MASK << _ROUND_SHIFT);
    172  1.1  christos 	__r.__bits |= (fenv_t)__round << _ROUND_SHIFT;
    173  1.1  christos 	__mt_fpcr(__r.__d);
    174  1.1  christos 	__excb();
    175  1.1  christos 	return 0;
    176  1.1  christos }
    177  1.1  christos 
    178  1.3  christos #if __GNUC_PREREQ__(8, 0)
    179  1.3  christos #pragma GCC diagnostic pop
    180  1.3  christos #endif
    181  1.3  christos 
    182  1.1  christos int	fegetenv(fenv_t *);
    183  1.1  christos int	feholdexcept(fenv_t *);
    184  1.1  christos int	fesetenv(const fenv_t *);
    185  1.1  christos int	feupdateenv(const fenv_t *);
    186  1.1  christos 
    187  1.1  christos #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE)
    188  1.1  christos int	feenableexcept(int);
    189  1.1  christos int	fedisableexcept(int);
    190  1.1  christos int	fegetexcept(void);
    191  1.1  christos #endif /* _NETBSD_SOURCE || _GNU_SOURCE */
    192  1.1  christos 
    193  1.1  christos 
    194  1.1  christos __END_DECLS
    195  1.1  christos 
    196  1.1  christos #endif	/* !_ALPHA_FENV_H_ */
    197