1 1.7 chs /* $NetBSD: ieeefp.h,v 1.7 2017/03/22 23:11:10 chs Exp $ */ 2 1.1 itojun 3 1.1 itojun /* 4 1.1 itojun * Written by J.T. Conklin, Apr 6, 1995 5 1.1 itojun * Public domain. 6 1.1 itojun */ 7 1.1 itojun 8 1.1 itojun #ifndef _SH3_IEEEFP_H_ 9 1.3 uch #define _SH3_IEEEFP_H_ 10 1.1 itojun 11 1.4 matt #include <sys/featuretest.h> 12 1.4 matt 13 1.4 matt #if defined(_NETBSD_SOURCE) || defined(_ISOC99_SOURCE) 14 1.4 matt 15 1.5 christos #include <fenv.h> 16 1.4 matt 17 1.4 matt #if defined(_NETBSD_SOURCE) 18 1.4 matt 19 1.6 christos typedef int fp_except; 20 1.6 christos 21 1.7 chs #ifdef __SH_FPU_ANY__ 22 1.7 chs 23 1.7 chs /* hardfloat */ 24 1.7 chs 25 1.4 matt #define FP_X_INV FE_INVALID /* invalid operation exception */ 26 1.4 matt #define FP_X_DZ FE_DIVBYZERO /* divide-by-zero exception */ 27 1.4 matt #define FP_X_OFL FE_OVERFLOW /* overflow exception */ 28 1.4 matt #define FP_X_UFL FE_UNDERFLOW /* underflow exception */ 29 1.4 matt #define FP_X_IMP FE_INEXACT /* imprecise (loss of precision) */ 30 1.1 itojun 31 1.1 itojun typedef enum { 32 1.4 matt FP_RN=FE_TONEAREST, /* round to nearest representable number */ 33 1.6 christos FP_RM=FE_DOWNWARD, /* round toward negative infinity */ 34 1.6 christos FP_RP=FE_UPWARD, /* round toward positive infinity */ 35 1.4 matt FP_RZ=FE_TOWARDZERO /* round to zero (truncate) */ 36 1.1 itojun } fp_rnd; 37 1.1 itojun 38 1.7 chs #else /* __SH_FPU_ANY__ */ 39 1.7 chs 40 1.7 chs /* softfloat */ 41 1.7 chs 42 1.7 chs #define FP_X_INV 0x01 /* invalid operation exception */ 43 1.7 chs #define FP_X_DZ 0x04 /* divide-by-zero exception */ 44 1.7 chs #define FP_X_OFL 0x08 /* overflow exception */ 45 1.7 chs #define FP_X_UFL 0x10 /* underflow exception */ 46 1.7 chs #define FP_X_IMP 0x20 /* imprecise (loss of precision) */ 47 1.7 chs 48 1.7 chs typedef enum { 49 1.7 chs FP_RN=0, /* round to nearest representable number */ 50 1.7 chs FP_RM=1, /* round toward negative infinity */ 51 1.7 chs FP_RP=2, /* round toward positive infinity */ 52 1.7 chs FP_RZ=3 /* round to zero (truncate) */ 53 1.7 chs } fp_rnd; 54 1.7 chs 55 1.7 chs /* adjust for FP_* and FE_* value differences */ 56 1.7 chs 57 1.7 chs static inline fp_except 58 1.7 chs __FPE(int __fe) 59 1.7 chs { 60 1.7 chs int __fp = 0; 61 1.7 chs 62 1.7 chs if (__fe & FE_DIVBYZERO) 63 1.7 chs __fp |= FP_X_DZ; 64 1.7 chs if (__fe & FE_INEXACT) 65 1.7 chs __fp |= FP_X_IMP; 66 1.7 chs if (__fe & FE_INVALID) 67 1.7 chs __fp |= FP_X_INV; 68 1.7 chs if (__fe & FE_OVERFLOW) 69 1.7 chs __fp |= FP_X_OFL; 70 1.7 chs if (__fe & FE_UNDERFLOW) 71 1.7 chs __fp |= FP_X_UFL; 72 1.7 chs return __fp; 73 1.7 chs } 74 1.7 chs 75 1.7 chs static inline int 76 1.7 chs __FEE(fp_except __fp) 77 1.7 chs { 78 1.7 chs int __fe = 0; 79 1.7 chs 80 1.7 chs if (__fp & FP_X_DZ) 81 1.7 chs __fe |= FE_DIVBYZERO; 82 1.7 chs if (__fp & FP_X_IMP) 83 1.7 chs __fe |= FE_INEXACT; 84 1.7 chs if (__fp & FP_X_INV) 85 1.7 chs __fe |= FE_INVALID; 86 1.7 chs if (__fp & FP_X_OFL) 87 1.7 chs __fe |= FE_OVERFLOW; 88 1.7 chs if (__fp & FP_X_UFL) 89 1.7 chs __fe |= FE_UNDERFLOW; 90 1.7 chs return __fe; 91 1.7 chs } 92 1.7 chs 93 1.7 chs static inline fp_rnd 94 1.7 chs __FPR(int __fe) 95 1.7 chs { 96 1.7 chs 97 1.7 chs switch (__fe) { 98 1.7 chs case FE_TONEAREST: 99 1.7 chs return FP_RN; 100 1.7 chs case FE_DOWNWARD: 101 1.7 chs return FP_RM; 102 1.7 chs case FE_UPWARD: 103 1.7 chs return FP_RP; 104 1.7 chs case FE_TOWARDZERO: 105 1.7 chs return FP_RZ; 106 1.7 chs default: 107 1.7 chs return FP_RN; 108 1.7 chs } 109 1.7 chs } 110 1.7 chs 111 1.7 chs static inline int 112 1.7 chs __FER(fp_rnd __fp) 113 1.7 chs { 114 1.7 chs 115 1.7 chs switch (__fp) { 116 1.7 chs case FP_RN: 117 1.7 chs return FE_TONEAREST; 118 1.7 chs case FP_RM: 119 1.7 chs return FE_DOWNWARD; 120 1.7 chs case FP_RP: 121 1.7 chs return FE_UPWARD; 122 1.7 chs case FP_RZ: 123 1.7 chs return FE_TOWARDZERO; 124 1.7 chs default: 125 1.7 chs return FE_TONEAREST; 126 1.7 chs } 127 1.7 chs } 128 1.7 chs 129 1.7 chs #endif /* __SH_FPU_ANY__ */ 130 1.7 chs 131 1.4 matt #endif /* !_ISOC99_SOURCE */ 132 1.4 matt 133 1.4 matt #endif /* _NETBSD_SOURCE || _ISOC99_SOURCE */ 134 1.4 matt 135 1.2 uch #endif /* !_SH3_IEEEFP_H_ */ 136