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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
#ifndef SLANG_CORE_MATH_H
#define SLANG_CORE_MATH_H
#include <math.h>
namespace Slang
{
class Math
{
public:
// Use to fix type punning issues with strict aliasing
union FloatIntUnion
{
float fvalue;
int ivalue;
inline static FloatIntUnion makeFromInt(int i) { FloatIntUnion cast; cast.ivalue = i; return cast; }
inline static FloatIntUnion makeFromFloat(float f) { FloatIntUnion cast; cast.fvalue = f; return cast; }
};
static const float Pi;
template<typename T>
static T Min(const T& v1, const T&v2)
{
return v1<v2?v1:v2;
}
template<typename T>
static T Max(const T& v1, const T&v2)
{
return v1>v2?v1:v2;
}
template<typename T>
static T Min(const T& v1, const T&v2, const T&v3)
{
return Min(v1, Min(v2, v3));
}
template<typename T>
static T Max(const T& v1, const T&v2, const T&v3)
{
return Max(v1, Max(v2, v3));
}
template<typename T>
static T Clamp(const T& val, const T& vmin, const T&vmax)
{
if (val < vmin) return vmin;
else if (val > vmax) return vmax;
else return val;
}
static inline int FastFloor(float x)
{
int i = (int)x;
return i - (i > x);
}
static inline int FastFloor(double x)
{
int i = (int)x;
return i - (i > x);
}
static inline int IsNaN(float x)
{
#ifdef _M_X64
return _isnanf(x);
#else
return isnan(x);
#endif
}
static inline int IsInf(float x)
{
return isinf(x);
}
static inline unsigned int Ones32(unsigned int x)
{
/* 32-bit recursive reduction using SWAR...
but first step is mapping 2-bit values
into sum of 2 1-bit values in sneaky way
*/
x -= ((x >> 1) & 0x55555555);
x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
x = (((x >> 4) + x) & 0x0f0f0f0f);
x += (x >> 8);
x += (x >> 16);
return(x & 0x0000003f);
}
static inline unsigned int Log2Floor(unsigned int x)
{
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
return(Ones32(x >> 1));
}
static inline unsigned int Log2Ceil(unsigned int x)
{
int y = (x & (x - 1));
y |= -y;
y >>= (32 - 1);
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
return(Ones32(x >> 1) - y);
}
/*
static inline int Log2(float x)
{
unsigned int ix = (unsigned int&)x;
unsigned int exp = (ix >> 23) & 0xFF;
int log2 = (unsigned int)(exp) - 127;
return log2;
}
*/
};
inline int FloatAsInt(float val)
{
return Math::FloatIntUnion::makeFromFloat(val).ivalue;
}
inline float IntAsFloat(int val)
{
return Math::FloatIntUnion::makeFromInt(val).fvalue;
}
inline unsigned short FloatToHalf(float val)
{
const auto x = FloatAsInt(val);
unsigned short bits = (x >> 16) & 0x8000;
unsigned short m = (x >> 12) & 0x07ff;
unsigned int e = (x >> 23) & 0xff;
if (e < 103)
return bits;
if (e > 142)
{
bits |= 0x7c00u;
bits |= e == 255 && (x & 0x007fffffu);
return bits;
}
if (e < 113)
{
m |= 0x0800u;
bits |= (m >> (114 - e)) + ((m >> (113 - e)) & 1);
return bits;
}
bits |= ((e - 112) << 10) | (m >> 1);
bits += m & 1;
return bits;
}
inline float HalfToFloat(unsigned short input)
{
static const auto magic = Math::FloatIntUnion::makeFromInt((127 + (127 - 15)) << 23);
static const auto was_infnan = Math::FloatIntUnion::makeFromInt((127 + 16) << 23);
Math::FloatIntUnion o;
o.ivalue = (input & 0x7fff) << 13; // exponent/mantissa bits
o.fvalue *= magic.fvalue; // exponent adjust
if (o.fvalue >= was_infnan.fvalue) // make sure Inf/NaN survive
o.ivalue |= 255 << 23;
o.ivalue |= (input & 0x8000) << 16; // sign bit
return o.fvalue;
}
class Random
{
private:
unsigned int seed;
public:
Random(int seed)
{
this->seed = seed;
}
int Next() // random between 0 and RandMax (currently 0x7fff)
{
return ((seed = ((seed << 12) + 150889L) % 714025) & 0x7fff);
}
int Next(int min, int max) // inclusive min, exclusive max
{
unsigned int a = ((seed = ((seed << 12) + 150889L) % 714025) & 0xFFFF);
unsigned int b = ((seed = ((seed << 12) + 150889L) % 714025) & 0xFFFF);
unsigned int r = (a << 16) + b;
return min + r % (max - min);
}
float NextFloat()
{
return ((Next() << 15) + Next()) / ((float)(1 << 30));
}
float NextFloat(float valMin, float valMax)
{
return valMin + (valMax - valMin) * NextFloat();
}
static int RandMax()
{
return 0x7fff;
}
};
}
#endif
|