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