-
Notifications
You must be signed in to change notification settings - Fork 0
/
denormal.h
193 lines (150 loc) · 6.47 KB
/
denormal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef _WDL_DENORMAL_H_
#define _WDL_DENORMAL_H_
typedef struct
{
#ifdef __ppc__ // todo: other big endian platforms...
unsigned int hw;
unsigned int lw;
#else
unsigned int lw;
unsigned int hw;
#endif
} WDL_DenormalTwoInts;
typedef union { double fl; WDL_DenormalTwoInts w; } WDL_DenormalDoubleAccess;
typedef union { float fl; unsigned int w; } WDL_DenormalFloatAccess;
// note: the _aggressive versions filter out anything less than around 1.0e-16 or so (approximately) to 0.0, including -0.0 (becomes 0.0)
// note: new! the _aggressive versions also filter inf and NaN to 0.0
#ifdef __cplusplus
#define WDL_DENORMAL_INLINE inline
#elif defined(_MSC_VER)
#define WDL_DENORMAL_INLINE __inline
#else
#define WDL_DENORMAL_INLINE
#endif
#define WDL_DENORMAL_DOUBLE_HW(a) (((const WDL_DenormalDoubleAccess*)(a))->w.hw)
#define WDL_DENORMAL_DOUBLE_LW(a) (((const WDL_DenormalDoubleAccess*)(a))->w.lw)
#define WDL_DENORMAL_FLOAT_W(a) (((const WDL_DenormalFloatAccess*)(a))->w)
#define WDL_DENORMAL_DOUBLE_HW_NC(a) (((WDL_DenormalDoubleAccess*)(a))->w.hw)
#define WDL_DENORMAL_DOUBLE_LW_NC(a) (((WDL_DenormalDoubleAccess*)(a))->w.lw)
#define WDL_DENORMAL_FLOAT_W_NC(a) (((WDL_DenormalFloatAccess*)(a))->w)
#define WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF 0x3cA00000 // 0x3B8000000 maybe instead? that's 10^-5 smaller or so
#define WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF 0x25000000
#define WDL_NOT_DENORMAL_DOUBLE(a) (WDL_DENORMAL_DOUBLE_HW(a)&0x7ff00000)
#define WDL_NOT_DENORMAL_FLOAT(a) (WDL_DENORMAL_FLOAT_W(a)&0x7f800000)
#define WDL_DENORMAL_OR_ZERO_DOUBLE(a) (!WDL_NOT_DENORMAL_DOUBLE(a))
#define WDL_DENORMAL_OR_ZERO_FLOAT(a) (!WDL_NOT_DENORMAL_FLOAT(a))
#define WDL_DENORMAL_OR_ZERO_DOUBLE_AGGRESSIVE(a) (((WDL_DENORMAL_DOUBLE_HW(a)+0x100000)&0x7ff00000) < WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF)
#define WDL_DENORMAL_OR_ZERO_FLOAT_AGGRESSIVE(a) (((WDL_DENORMAL_FLOAT_W(a)+0x800000)&0x7f800000) < WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF)
static double WDL_DENORMAL_INLINE denormal_filter_double(double a)
{
return (WDL_DENORMAL_DOUBLE_HW(&a)&0x7ff00000) ? a : 0.0;
}
static double WDL_DENORMAL_INLINE denormal_filter_double2(double a)
{
return ((WDL_DENORMAL_DOUBLE_HW(&a)+0x100000)&0x7ff00000) > 0x100000 ? a : 0.0;
}
static double WDL_DENORMAL_INLINE denormal_filter_double_aggressive(double a)
{
return ((WDL_DENORMAL_DOUBLE_HW(&a)+0x100000)&0x7ff00000) >= WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF ? a : 0.0;
}
static float WDL_DENORMAL_INLINE denormal_filter_float(float a)
{
return (WDL_DENORMAL_FLOAT_W(&a)&0x7f800000) ? a : 0.0f;
}
static float WDL_DENORMAL_INLINE denormal_filter_float2(float a)
{
return ((WDL_DENORMAL_FLOAT_W(&a)+0x800000)&0x7f800000) > 0x800000 ? a : 0.0f;
}
static float WDL_DENORMAL_INLINE denormal_filter_float_aggressive(float a)
{
return ((WDL_DENORMAL_FLOAT_W(&a)+0x800000)&0x7f800000) >= WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF ? a : 0.0f;
}
static void WDL_DENORMAL_INLINE denormal_fix_double(double *a)
{
if (!(WDL_DENORMAL_DOUBLE_HW(a)&0x7ff00000)) *a=0.0;
}
static void WDL_DENORMAL_INLINE denormal_fix_double_aggressive(double *a)
{
if (((WDL_DENORMAL_DOUBLE_HW(a)+0x100000)&0x7ff00000) < WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF) *a=0.0;
}
static void WDL_DENORMAL_INLINE denormal_fix_float(float *a)
{
if (!(WDL_DENORMAL_FLOAT_W(a)&0x7f800000)) *a=0.0f;
}
static void WDL_DENORMAL_INLINE denormal_fix_float_aggressive(float *a)
{
if (((WDL_DENORMAL_FLOAT_W(a)+0x800000)&0x7f800000) < WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF) *a=0.0f;
}
#ifdef __cplusplus // automatic typed versions (though one should probably use the explicit versions...
static double WDL_DENORMAL_INLINE denormal_filter(double a)
{
return (WDL_DENORMAL_DOUBLE_HW(&a)&0x7ff00000) ? a : 0.0;
}
static double WDL_DENORMAL_INLINE denormal_filter_aggressive(double a)
{
return ((WDL_DENORMAL_DOUBLE_HW(&a)+0x100000)&0x7ff00000) >= WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF ? a : 0.0;
}
static float WDL_DENORMAL_INLINE denormal_filter(float a)
{
return (WDL_DENORMAL_FLOAT_W(&a)&0x7f800000) ? a : 0.0f;
}
static float WDL_DENORMAL_INLINE denormal_filter_aggressive(float a)
{
return ((WDL_DENORMAL_FLOAT_W(&a)+0x800000)&0x7f800000) >= WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF ? a : 0.0f;
}
static void WDL_DENORMAL_INLINE denormal_fix(double *a)
{
if (!(WDL_DENORMAL_DOUBLE_HW(a)&0x7ff00000)) *a=0.0;
}
static void WDL_DENORMAL_INLINE denormal_fix_aggressive(double *a)
{
if (((WDL_DENORMAL_DOUBLE_HW(a)+0x100000)&0x7ff00000) < WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF) *a=0.0;
}
static void WDL_DENORMAL_INLINE denormal_fix(float *a)
{
if (!(WDL_DENORMAL_FLOAT_W(a)&0x7f800000)) *a=0.0f;
}
static void WDL_DENORMAL_INLINE denormal_fix_aggressive(float *a)
{
if (((WDL_DENORMAL_FLOAT_W(a)+0x800000)&0x7f800000) < WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF) *a=0.0f;
}
static bool WDL_DENORMAL_INLINE WDL_DENORMAL_OR_ZERO(double *a)
{
return WDL_DENORMAL_OR_ZERO_DOUBLE(a);
}
static bool WDL_DENORMAL_INLINE WDL_DENORMAL_OR_ZERO(float *a)
{
return WDL_DENORMAL_OR_ZERO_FLOAT(a);
}
static bool WDL_DENORMAL_INLINE WDL_DENORMAL_OR_ZERO_AGGRESSIVE(double *a)
{
return WDL_DENORMAL_OR_ZERO_DOUBLE_AGGRESSIVE(a);
}
static bool WDL_DENORMAL_INLINE WDL_DENORMAL_OR_ZERO_AGGRESSIVE(float *a)
{
return WDL_DENORMAL_OR_ZERO_FLOAT_AGGRESSIVE(a);
}
#endif // cplusplus versions
////////////////////
// this isnt a denormal function but it is similar, so we'll put it here as a bonus
static void WDL_DENORMAL_INLINE GetDoubleMaxAbsValue(double *out, const double *in) // note: the value pointed to by "out" must be >=0.0, __NOT__ <= -0.0
{
unsigned int hw = WDL_DENORMAL_DOUBLE_HW(in)&0x7fffffff;
if (hw >= WDL_DENORMAL_DOUBLE_HW(out) && (hw>WDL_DENORMAL_DOUBLE_HW(out) || WDL_DENORMAL_DOUBLE_LW(in) > WDL_DENORMAL_DOUBLE_LW(out)))
{
WDL_DENORMAL_DOUBLE_LW_NC(out) = WDL_DENORMAL_DOUBLE_LW(in);
WDL_DENORMAL_DOUBLE_HW_NC(out) = hw;
}
}
static void WDL_DENORMAL_INLINE GetFloatMaxAbsValue(float *out, const float *in) // note: the value pointed to by "out" must be >=0.0, __NOT__ <= -0.0
{
unsigned int hw = WDL_DENORMAL_FLOAT_W(in)&0x7fffffff;
if (hw > WDL_DENORMAL_FLOAT_W(out)) WDL_DENORMAL_FLOAT_W_NC(out)=hw;
}
#ifdef __cplusplus
static void WDL_DENORMAL_INLINE GetFloatMaxAbsValue(double *out, const double *in) // note: the value pointed to by "out" must be >=0.0, __NOT__ <= -0.0
{
GetDoubleMaxAbsValue(out,in);
}
#endif
#endif