Home | History | Annotate | Line # | Download | only in include
fenv.h revision 1.2
      1 /*	$NetBSD: fenv.h,v 1.2 2015/12/25 16:19:38 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _M68K_FENV_H_
     33 #define _M68K_FENV_H_
     34 
     35 #include <sys/stdint.h>
     36 #include <m68k/float.h>
     37 #include <m68k/fpreg.h>
     38 
     39 #ifndef __fenv_static
     40 #define __fenv_static   static
     41 #endif
     42 
     43 /* Exception bits, from FPSR */
     44 #define	FE_INEXACT	FPSR_AINEX
     45 #define	FE_DIVBYZERO	FPSR_ADZ
     46 #define	FE_UNDERFLOW	FPSR_AUNFL
     47 #define	FE_OVERFLOW	FPSR_AOVFL
     48 #define	FE_INVALID	FPSR_AIOP
     49 
     50 #define FE_ALL_EXCEPT \
     51     (FE_INEXACT | FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID)
     52 
     53 /* Rounding modes, from FPSR */
     54 #define FE_TONEAREST	FPCR_NEAR
     55 #define	FE_TOWARDZERO	FPCR_ZERO
     56 #define	FE_DOWNWARD	FPCR_MINF
     57 #define	FE_UPWARD	FPCR_PINF
     58 
     59 #define _ROUND_MASK	\
     60     (FE_TONEAREST | FE_TOWARDZERO | FE_DOWNWARD | FE_UPWARD)
     61 
     62 typedef uint32_t fexcept_t;
     63 
     64 /* same layout as fmovem */
     65 typedef struct {
     66 	uint32_t fpcr;
     67 	uint32_t fpsr;
     68 	uint32_t fppc;
     69 } fenv_t;
     70 
     71 #define FE_DFL_ENV	((fenv_t *) -1)
     72 
     73 #define __get_fpcr(__fpcr) \
     74     __asm__ __volatile__ ("fmove%.l %!,%0" : "=dm" (__fpcr))
     75 #define __set_fpcr(__fpcr) \
     76     __asm__ __volatile__ ("fmove%.l %0,%!" : : "dm" (__fpcr))
     77 
     78 #define __get_fpsr(__fpsr) \
     79     __asm__ __volatile__ ("fmove%.l %/fpsr,%0" : "=dm" (__fpsr))
     80 #define __set_fpsr(__fpsr) \
     81     __asm__ __volatile__ ("fmove%.l %0,%/fpsr" : : "dm" (__fpsr))
     82 
     83 #define __fmul(__s, __t, __d) \
     84     do { \
     85 	    __t d = __d; \
     86 	    __asm__ __volatile__ ("fmul" __s "; fnop" : "=f" (d) : "0" (d)); \
     87     } while (/*CONSTCOND*/0)
     88 
     89 #define __fdiv(__s, __t, __d) \
     90     do { \
     91 	    __t d = __d; \
     92 	    __asm__ __volatile__ ("fdiv" __s "; fnop" : "=f" (d) : "0" (d)); \
     93     } while (/*CONSTCOND*/0)
     94 
     95 #define __fetox(__s, __t, __d) \
     96     do { \
     97 	    __t d = __d; \
     98 	    __asm__ __volatile__ ("fetox" __s "; fnop" : "=f" (d) : "0" (d)); \
     99     } while (/*CONSTCOND*/0)
    100 
    101 #define __fgetenv(__envp) \
    102     __asm__ __volatile__ ("fmovem%.l %/fpcr/%/fpsr/%/fpiar,%0" : "=m" (__envp))
    103 
    104 #define __fsetenv(__envp) \
    105     __asm__ __volatile__ ("fmovem%.l %0,%/fpcr/%/fpsr/%/fpiar" : : "m" (__envp))
    106 
    107 __BEGIN_DECLS
    108 
    109 __fenv_static inline int
    110 feclearexcept(int __excepts)
    111 {
    112 	fexcept_t __fpsr;
    113 
    114 	__excepts &= FE_ALL_EXCEPT;
    115 
    116 	__get_fpsr(__fpsr);
    117 	__fpsr &= ~__excepts;
    118 	__set_fpsr(__fpsr);
    119 
    120 	return 0;
    121 }
    122 
    123 __fenv_static inline int
    124 fegetexceptflag(fexcept_t *__flagp, int __excepts)
    125 {
    126 	fexcept_t __fpsr;
    127 
    128 	__get_fpsr(__fpsr);
    129 
    130 	*__flagp = __fpsr & __excepts & FE_ALL_EXCEPT;
    131 
    132 	return 0;
    133 }
    134 
    135 __fenv_static inline int
    136 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
    137 {
    138 	fexcept_t __fpsr;
    139 
    140 	__get_fpsr(__fpsr);
    141 
    142 	__fpsr &= ~(__excepts & FE_ALL_EXCEPT);
    143 	__fpsr |= *__flagp & __excepts & FE_ALL_EXCEPT;
    144 
    145 	__set_fpsr(__fpsr);
    146 
    147 	return 0;
    148 }
    149 
    150 __fenv_static inline int
    151 feraiseexcept(int __excepts)
    152 {
    153 	if (__excepts & FE_INVALID)	/* Inf * 0 */
    154 		__fmul("%.s %#0r0,%0", double, __builtin_huge_val());
    155 
    156 	if (__excepts & FE_DIVBYZERO)	/* 1.0 / 0 */
    157 		__fdiv("%.s %#0r0,%0", double, 1.0);
    158 
    159 	if (__excepts & FE_OVERFLOW)	/* MAX * MAX */
    160 		__fmul("%.x %0,%0", long double, LDBL_MAX);
    161 
    162 	if (__excepts & FE_UNDERFLOW)	/* e ^ -MAX */
    163 		__fetox("%.x %0", long double, -LDBL_MAX);
    164 
    165 	if (__excepts & FE_INEXACT)	/* 1 / 3 */
    166 		__fdiv("%.s %#0r3,%0", long double, 1.0);
    167 
    168 	return 0;
    169 }
    170 
    171 __fenv_static inline int
    172 fetestexcept(int __excepts)
    173 {
    174 	fexcept_t __fpsr;
    175 
    176 	__get_fpsr(__fpsr);
    177 
    178 	return __fpsr & __excepts & FE_ALL_EXCEPT;
    179 }
    180 
    181 __fenv_static inline int
    182 fegetround(void)
    183 {
    184 	fexcept_t __fpcr;
    185 
    186 	__get_fpcr(__fpcr);
    187 	return __fpcr & _ROUND_MASK;
    188 }
    189 
    190 __fenv_static inline int
    191 fesetround(int __round)
    192 {
    193 	fexcept_t __fpcr;
    194 
    195 	if (__round & ~_ROUND_MASK)
    196 		return -1;
    197 
    198 	__get_fpcr(__fpcr);
    199 
    200 	__fpcr &= ~_ROUND_MASK;
    201 	__fpcr |= __round;
    202 
    203 	__set_fpcr(__fpcr);
    204 
    205 	return 0;
    206 }
    207 
    208 __fenv_static inline int
    209 fegetenv(fenv_t *__envp)
    210 {
    211 	__fgetenv(__envp);
    212 
    213 	return 0;
    214 }
    215 
    216 __fenv_static inline int
    217 feholdexcept(fenv_t *__envp)
    218 {
    219 	fexcept_t __fpcr, __fpsr;
    220 
    221 	__fgetenv(__envp);
    222 	__fpsr = __envp->fpsr & ~FE_ALL_EXCEPT;
    223 	__set_fpsr(__fpsr);	/* clear all */
    224 	__fpcr = __envp->fpcr & ~(FE_ALL_EXCEPT << 6);
    225 	__set_fpcr(__fpcr);	/* set non/stop */
    226 
    227 	return 0;
    228 }
    229 
    230 __fenv_static inline int
    231 fesetenv(const fenv_t *__envp)
    232 {
    233 	fenv_t __tenv;
    234 
    235 	__fgetenv(__tenv);
    236 
    237 	if (__envp == FE_DFL_ENV) {
    238 		__tenv.fpcr |=
    239 		    __envp->fpcr & ((FE_ALL_EXCEPT << 6) | FE_UPWARD);
    240 		__tenv.fpsr |= __envp->fpsr & FE_ALL_EXCEPT;
    241 	}
    242 
    243 	__fsetenv(__tenv);
    244 
    245 	return 0;
    246 }
    247 
    248 __fenv_static inline int
    249 feupdateenv(const fenv_t *__envp)
    250 {
    251 	fexcept_t __fpsr;
    252 
    253 	__get_fpsr(__fpsr);
    254 	__fpsr &= FE_ALL_EXCEPT;
    255 	fesetenv(__envp);
    256 	feraiseexcept((int)__fpsr);
    257 	return 0;
    258 }
    259 
    260 #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE)
    261 
    262 /* We currently provide no external definitions of the functions below. */
    263 
    264 static inline int
    265 feenableexcept(int __mask)
    266 {
    267 	fexcept_t __fpsr, __oldmask;
    268 
    269 	__get_fpsr(__fpsr);
    270 	__oldmask = __fpsr & FE_ALL_EXCEPT;
    271 	__fpsr |= __mask & FE_ALL_EXCEPT;
    272 	__set_fpsr(__fpsr);
    273 
    274 	return __oldmask;
    275 }
    276 
    277 static inline int
    278 fedisableexcept(int __mask)
    279 {
    280 	fexcept_t __fpsr, __oldmask;
    281 
    282 	__get_fpsr(__fpsr);
    283 	__oldmask = __fpsr & FE_ALL_EXCEPT;
    284 	__fpsr &= ~(__mask & FE_ALL_EXCEPT);
    285 	__set_fpsr(__fpsr);
    286 
    287 	return __oldmask;
    288 }
    289 
    290 static inline int
    291 fegetexcept(void)
    292 {
    293 	fexcept_t __fpsr;
    294 
    295 	__get_fpsr(__fpsr);
    296 
    297 	return __fpsr & FE_ALL_EXCEPT;
    298 }
    299 
    300 #endif /* _NETBSD_SOURCE || _GNU_SOURCE */
    301 
    302 __END_DECLS
    303 
    304 #endif /* _M68K_FENV_H_ */
    305