fenv.h revision 1.4
1/* $NetBSD: fenv.h,v 1.4 2016/01/05 00:47:08 ozaki-r 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#if !defined(__mc68010__) && !defined(__mcoldfire__) 63 64typedef uint32_t fexcept_t; 65 66/* same layout as fmovem */ 67typedef struct { 68 uint32_t fpcr; 69 uint32_t fpsr; 70 uint32_t fppc; 71} fenv_t; 72 73#define FE_DFL_ENV ((fenv_t *) -1) 74 75#define __get_fpcr(__fpcr) \ 76 __asm__ __volatile__ ("fmove%.l %!,%0" : "=dm" (__fpcr)) 77#define __set_fpcr(__fpcr) \ 78 __asm__ __volatile__ ("fmove%.l %0,%!" : : "dm" (__fpcr)) 79 80#define __get_fpsr(__fpsr) \ 81 __asm__ __volatile__ ("fmove%.l %/fpsr,%0" : "=dm" (__fpsr)) 82#define __set_fpsr(__fpsr) \ 83 __asm__ __volatile__ ("fmove%.l %0,%/fpsr" : : "dm" (__fpsr)) 84 85#define __fmul(__s, __t, __d) \ 86 do { \ 87 __t d = __d; \ 88 __asm__ __volatile__ ("fmul" __s "; fnop" : "=f" (d) : "0" (d)); \ 89 } while (/*CONSTCOND*/0) 90 91#define __fdiv(__s, __t, __d) \ 92 do { \ 93 __t d = __d; \ 94 __asm__ __volatile__ ("fdiv" __s "; fnop" : "=f" (d) : "0" (d)); \ 95 } while (/*CONSTCOND*/0) 96 97#define __fetox(__s, __t, __d) \ 98 do { \ 99 __t d = __d; \ 100 __asm__ __volatile__ ("fetox" __s "; fnop" : "=f" (d) : "0" (d)); \ 101 } while (/*CONSTCOND*/0) 102 103#define __fgetenv(__envp) \ 104 __asm__ __volatile__ ("fmovem%.l %/fpcr/%/fpsr/%/fpiar,%0" : "=m" (__envp)) 105 106#define __fsetenv(__envp) \ 107 __asm__ __volatile__ ("fmovem%.l %0,%/fpcr/%/fpsr/%/fpiar" : : "m" (__envp)) 108 109__BEGIN_DECLS 110 111__fenv_static inline int 112feclearexcept(int __excepts) 113{ 114 fexcept_t __fpsr; 115 116 __excepts &= FE_ALL_EXCEPT; 117 118 __get_fpsr(__fpsr); 119 __fpsr &= ~__excepts; 120 __set_fpsr(__fpsr); 121 122 return 0; 123} 124 125__fenv_static inline int 126fegetexceptflag(fexcept_t *__flagp, int __excepts) 127{ 128 fexcept_t __fpsr; 129 130 __get_fpsr(__fpsr); 131 132 *__flagp = __fpsr & __excepts & FE_ALL_EXCEPT; 133 134 return 0; 135} 136 137__fenv_static inline int 138fesetexceptflag(const fexcept_t *__flagp, int __excepts) 139{ 140 fexcept_t __fpsr; 141 142 __get_fpsr(__fpsr); 143 144 __fpsr &= ~(__excepts & FE_ALL_EXCEPT); 145 __fpsr |= *__flagp & __excepts & FE_ALL_EXCEPT; 146 147 __set_fpsr(__fpsr); 148 149 return 0; 150} 151 152__fenv_static inline int 153feraiseexcept(int __excepts) 154{ 155 if (__excepts & FE_INVALID) /* Inf * 0 */ 156 __fmul("%.s %#0r0,%0", double, __builtin_huge_val()); 157 158 if (__excepts & FE_DIVBYZERO) /* 1.0 / 0 */ 159 __fdiv("%.s %#0r0,%0", double, 1.0); 160 161 if (__excepts & FE_OVERFLOW) /* MAX * MAX */ 162 __fmul("%.x %0,%0", long double, LDBL_MAX); 163 164 if (__excepts & FE_UNDERFLOW) /* e ^ -MAX */ 165 __fetox("%.x %0", long double, -LDBL_MAX); 166 167 if (__excepts & FE_INEXACT) /* 1 / 3 */ 168 __fdiv("%.s %#0r3,%0", long double, 1.0); 169 170 return 0; 171} 172 173__fenv_static inline int 174fetestexcept(int __excepts) 175{ 176 fexcept_t __fpsr; 177 178 __get_fpsr(__fpsr); 179 180 return __fpsr & __excepts & FE_ALL_EXCEPT; 181} 182 183__fenv_static inline int 184fegetround(void) 185{ 186 fexcept_t __fpcr; 187 188 __get_fpcr(__fpcr); 189 return __fpcr & _ROUND_MASK; 190} 191 192__fenv_static inline int 193fesetround(int __round) 194{ 195 fexcept_t __fpcr; 196 197 if (__round & ~_ROUND_MASK) 198 return -1; 199 200 __get_fpcr(__fpcr); 201 202 __fpcr &= ~_ROUND_MASK; 203 __fpcr |= __round; 204 205 __set_fpcr(__fpcr); 206 207 return 0; 208} 209 210__fenv_static inline int 211fegetenv(fenv_t *__envp) 212{ 213 __fgetenv(__envp); 214 215 return 0; 216} 217 218__fenv_static inline int 219feholdexcept(fenv_t *__envp) 220{ 221 fexcept_t __fpcr, __fpsr; 222 223 __fgetenv(__envp); 224 __fpsr = __envp->fpsr & ~FE_ALL_EXCEPT; 225 __set_fpsr(__fpsr); /* clear all */ 226 __fpcr = __envp->fpcr & ~(FE_ALL_EXCEPT << 6); 227 __set_fpcr(__fpcr); /* set non/stop */ 228 229 return 0; 230} 231 232__fenv_static inline int 233fesetenv(const fenv_t *__envp) 234{ 235 fenv_t __tenv; 236 237 __fgetenv(__tenv); 238 239 if (__envp == FE_DFL_ENV) { 240 __tenv.fpcr |= 241 __envp->fpcr & ((FE_ALL_EXCEPT << 6) | FE_UPWARD); 242 __tenv.fpsr |= __envp->fpsr & FE_ALL_EXCEPT; 243 } 244 245 __fsetenv(__tenv); 246 247 return 0; 248} 249 250__fenv_static inline int 251feupdateenv(const fenv_t *__envp) 252{ 253 fexcept_t __fpsr; 254 255 __get_fpsr(__fpsr); 256 __fpsr &= FE_ALL_EXCEPT; 257 fesetenv(__envp); 258 feraiseexcept((int)__fpsr); 259 return 0; 260} 261 262#if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE) 263 264/* We currently provide no external definitions of the functions below. */ 265 266static inline int 267feenableexcept(int __mask) 268{ 269 fexcept_t __fpsr, __oldmask; 270 271 __get_fpsr(__fpsr); 272 __oldmask = __fpsr & FE_ALL_EXCEPT; 273 __fpsr |= __mask & FE_ALL_EXCEPT; 274 __set_fpsr(__fpsr); 275 276 return __oldmask; 277} 278 279static inline int 280fedisableexcept(int __mask) 281{ 282 fexcept_t __fpsr, __oldmask; 283 284 __get_fpsr(__fpsr); 285 __oldmask = __fpsr & FE_ALL_EXCEPT; 286 __fpsr &= ~(__mask & FE_ALL_EXCEPT); 287 __set_fpsr(__fpsr); 288 289 return __oldmask; 290} 291 292static inline int 293fegetexcept(void) 294{ 295 fexcept_t __fpsr; 296 297 __get_fpsr(__fpsr); 298 299 return __fpsr & FE_ALL_EXCEPT; 300} 301 302#endif /* _NETBSD_SOURCE || _GNU_SOURCE */ 303 304__END_DECLS 305 306#endif /* !__m68010__ && !__mcoldfire__ */ 307 308#endif /* _M68K_FENV_H_ */ 309