2 #ifndef __BSE_IEEE754_H__
3 #define __BSE_IEEE754_H__
10 #define PI (3.141592653589793238462643383279502884197) // pi
30 #define BSE_FLOAT_BIAS (127)
31 #define BSE_FLOAT_MAX_NORMAL (3.40282347e+38)
32 #define BSE_FLOAT_MIN_NORMAL (1.17549435e-38)
33 #define BSE_FLOAT_MAX_SUBNORMAL (1.17549421e-38)
34 #define BSE_FLOAT_MIN_SUBNORMAL (1.40129846e-45)
35 #define BSE_FLOAT_EPSILON (5.9604644775390625e-08)
36 #define BSE_DOUBLE_BIAS (1023)
37 #define BSE_DOUBLE_MAX_NORMAL (1.7976931348623157e+308)
38 #define BSE_DOUBLE_MIN_NORMAL (2.2250738585072014e-308)
39 #define BSE_DOUBLE_MAX_SUBNORMAL (2.2250738585072009e-308)
40 #define BSE_DOUBLE_MIN_SUBNORMAL (4.9406564584124654e-324)
41 #define BSE_DOUBLE_EPSILON (1.1102230246251565404236316680908203125e-16)
42 #define BSE_DOUBLE_INF (_bse_dinf_union.d)
43 #define BSE_DOUBLE_NAN (_bse_dnan_union.d)
44 #define BSE_FLOAT_INF (_bse_finf_union.f)
45 #define BSE_FLOAT_NAN (_bse_fnan_union.f)
48 #define BSE_LOG_2_BASE_10 (0.30102999566398119521)
55 #define BSE_FLOAT_IS_ZERO(f) ((f) == 0.0)
56 #define BSE_FLOAT_IS_NORMAL(f) (BSE_FLOAT_PARTS (f).mpn.biased_exponent > 0 && \
57 BSE_FLOAT_PARTS (f).mpn.biased_exponent < 255)
58 #define BSE_FLOAT_IS_SUBNORMAL(f) (BSE_FLOAT_PARTS (f).mpn.biased_exponent == 0 && \
59 BSE_FLOAT_PARTS (f).mpn.mantissa != 0)
60 #define BSE_FLOAT_IS_NANINF(f) (BSE_FLOAT_PARTS (f).mpn.biased_exponent == 255)
61 #define BSE_FLOAT_IS_NAN(f) (BSE_FLOAT_IS_NANINF (f) && BSE_FLOAT_PARTS (f).mpn.mantissa != 0)
62 #define BSE_FLOAT_IS_INF(f) (BSE_FLOAT_IS_NANINF (f) && BSE_FLOAT_PARTS (f).mpn.mantissa == 0)
63 #define BSE_FLOAT_IS_INF_POSITIVE(f) (BSE_FLOAT_IS_INF (f) && BSE_FLOAT_PARTS (f).mpn.sign == 0)
64 #define BSE_FLOAT_IS_INF_NEGATIVE(f) (BSE_FLOAT_IS_INF (f) && BSE_FLOAT_PARTS (f).mpn.sign == 1)
66 #define BSE_FLOAT_SIGN(f) (signbit (f))
68 #define BSE_FLOAT_SIGN(f) (BSE_FLOAT_PARTS (f).mpn.sign)
72 #define BSE_DOUBLE_IS_ZERO(d) ((d) == 0.0)
73 #define BSE_DOUBLE_IS_NORMAL(d) (BSE_DOUBLE_PARTS (d).mpn.biased_exponent > 0 && \
74 BSE_DOUBLE_PARTS (d).mpn.biased_exponent < 2047)
75 #define BSE_DOUBLE_IS_SUBNORMAL(d) (BSE_DOUBLE_PARTS (d).mpn.biased_exponent == 0 && \
76 (BSE_DOUBLE_PARTS (d).mpn.mantissa_low != 0 || \
77 BSE_DOUBLE_PARTS (d).mpn.mantissa_high != 0))
78 #define BSE_DOUBLE_IS_NANINF(d) (BSE_DOUBLE_PARTS (d).mpn.biased_exponent == 2047)
79 #define BSE_DOUBLE_IS_NAN(d) (BSE_DOUBLE_IS_NANINF (d) && \
80 (BSE_DOUBLE_PARTS (d).mpn.mantissa_low != 0 || \
81 BSE_DOUBLE_PARTS (d).mpn.mantissa_high != 0))
82 #define BSE_DOUBLE_IS_INF(d) (BSE_DOUBLE_IS_NANINF (d) && \
83 BSE_DOUBLE_PARTS (d).mpn.mantissa_low == 0 && \
84 BSE_DOUBLE_PARTS (d).mpn.mantissa_high == 0)
85 #define BSE_DOUBLE_IS_INF_POSITIVE(d) (BSE_DOUBLE_IS_INF (d) && BSE_DOUBLE_PARTS (d).mpn.sign == 0)
86 #define BSE_DOUBLE_IS_INF_NEGATIVE(d) (BSE_DOUBLE_IS_INF (d) && BSE_DOUBLE_PARTS (d).mpn.sign == 1)
88 #define BSE_DOUBLE_SIGN(d) (signbit (d))
90 #define BSE_DOUBLE_SIGN(d) (BSE_DOUBLE_PARTS (d).mpn.sign)
94 static inline float bse_float_zap_denormal (
register float fval);
95 static inline double bse_double_zap_denormal (
register double dval);
99 #define BSE_FLOAT_FLUSH(mutable_float) BSE_FLOAT_FLUSH_with_threshold (mutable_float)
100 #define BSE_DOUBLE_FLUSH(mutable_double) BSE_DOUBLE_FLUSH_with_threshold (mutable_double)
102 #define BSE_FLOAT_FLUSH(mutable_float) BSE_FLOAT_FLUSH_with_cond (mutable_float)
103 #define BSE_DOUBLE_FLUSH(mutable_double) BSE_DOUBLE_FLUSH_with_cond (mutable_double)
106 #define BSE_FLOAT_FLUSH(mutable_float) BSE_FLOAT_FLUSH_with_if (mutable_float)
107 #define BSE_DOUBLE_FLUSH(mutable_double) BSE_DOUBLE_FLUSH_with_if (mutable_double)
111 typedef unsigned short int BseFpuState;
112 #if defined (__i386__) && defined (__GNUC__)
117 static inline void bse_fpu_setround (BseFpuState *cw);
118 static inline int bse_fpu_okround (
void);
119 static inline void bse_fpu_restore (BseFpuState cv);
120 static inline int bse_ftoi (
register float f) G_GNUC_CONST;
121 static inline int bse_dtoi (
register double f) G_GNUC_CONST;
124 static inline guint64 bse_dtoull (
const double v);
125 static inline gint64 bse_dtoll (
const double v);
128 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
134 uint biased_exponent : 8;
142 uint mantissa_low : 32;
143 uint mantissa_high : 20;
144 uint biased_exponent : 11;
148 #define _BSE_DOUBLE_INF_BYTES { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x7f }
149 #define _BSE_DOUBLE_NAN_BYTES { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x7f }
150 #define _BSE_FLOAT_INF_BYTES { 0x00, 0x00, 0x80, 0x7f }
151 #define _BSE_FLOAT_NAN_BYTES { 0x00, 0x00, 0xc0, 0x7f }
152 #elif G_BYTE_ORDER == G_BIG_ENDIAN
158 uint biased_exponent : 8;
167 uint biased_exponent : 11;
168 uint mantissa_high : 20;
169 uint mantissa_low : 32;
172 #define _BSE_DOUBLE_INF_BYTES { 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
173 #define _BSE_DOUBLE_NAN_BYTES { 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
174 #define _BSE_FLOAT_INF_BYTES { 0x7f, 0x80, 0x00, 0x00 }
175 #define _BSE_FLOAT_NAN_BYTES { 0x7f, 0xc0, 0x00, 0x00 }
177 #error unknown ENDIAN type
180 static const union {
unsigned char c[8];
double d; } _bse_dnan_union = { _BSE_DOUBLE_NAN_BYTES };
181 static const union {
unsigned char c[8];
double d; } _bse_dinf_union = { _BSE_DOUBLE_INF_BYTES };
182 static const union {
unsigned char c[4];
float f; } _bse_fnan_union = { _BSE_FLOAT_NAN_BYTES };
183 static const union {
unsigned char c[4];
float f; } _bse_finf_union = { _BSE_FLOAT_INF_BYTES };
190 #define BSE_FLOAT_PARTS(f) (((BseFloatIEEE754) (f)))
191 #define BSE_DOUBLE_PARTS(d) (((BseDoubleIEEE754) (d)))
196 bse_float_zap_denormal (
register float fval)
198 if (G_UNLIKELY (BSE_FLOAT_IS_SUBNORMAL (fval)))
205 bse_double_zap_denormal (
register double dval)
207 if (G_UNLIKELY (BSE_DOUBLE_IS_SUBNORMAL (dval)))
214 #define BSE_FLOAT_FLUSH_with_threshold(mutable_float) do { \
215 volatile float __forced_float = 1e-29 + mutable_float; \
216 mutable_float = __forced_float - 1e-29; \
218 #define BSE_DOUBLE_FLUSH_with_threshold(mutable_double) do { \
219 volatile double __forced_double = 1e-288 + mutable_double; \
220 mutable_double = __forced_double - 1e-288; \
223 #define BSE_FLOAT_FLUSH_with_cond(mutable_float) do { \
224 mutable_float = G_UNLIKELY (fabs (mutable_float) < 1e-32) ? 0 : mutable_float; \
226 #define BSE_DOUBLE_FLUSH_with_cond(mutable_double) do { \
227 mutable_double = G_UNLIKELY (fabs (mutable_double) < 1e-290) ? 0 : mutable_double; \
230 #define BSE_FLOAT_FLUSH_with_if(mutable_float) do { \
231 if (G_UNLIKELY (fabs (mutable_float) < 1e-32)) \
234 #define BSE_DOUBLE_FLUSH_with_if(mutable_double) do { \
235 if (G_UNLIKELY (fabs (mutable_double) < 1e-290)) \
236 mutable_double = 0; \
239 #if defined (__i386__) && defined (__GNUC__)
241 bse_fpu_setround (BseFpuState *cw)
254 bse_fpu_okround (
void)
260 return !(cv & 0x0c00);
263 bse_fpu_restore (BseFpuState cv)
269 static inline int G_GNUC_CONST
270 bse_ftoi (
register float f)
279 static inline int G_GNUC_CONST
280 bse_dtoi (
register double f)
290 # define bse_fpu_setround(p) ((void) (p));
291 # define bse_fpu_okround() (1)
292 # define bse_fpu_restore(x)
293 static inline int G_GNUC_CONST
294 bse_ftoi (
register float v)
296 return (
int) (v < -0.0 ? v - 0.5 : v + 0.5);
298 static inline int G_GNUC_CONST
299 bse_dtoi (
register double v)
301 return (
int) (v < -0.0 ? v - 0.5 : v + 0.5);
304 static inline guint64
305 bse_dtoull (
const double v)
307 return v < -0.0 ? (guint64) (v - 0.5) : (guint64) (v + 0.5);
310 bse_dtoll (
const double v)
312 return v < -0.0 ? (gint64) (v - 0.5) : (gint64) (v + 0.5);
Definition: bseieee754.hh:138
Definition: bseieee754.hh:129