ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
#ifndef sw_Math_hpp
#define sw_Math_hpp
 
#include "Types.hpp"
#include "Half.hpp"
 
#include "Vulkan/VkDebug.hpp"
 
#include <cmath>
#if defined(_MSC_VER)
   #include <intrin.h>
#endif
 
namespace sw
{
   using std::abs;
 
   #undef min
   #undef max
 
   template<class T>
   inline T max(T a, T b)
   {
       return a > b ? a : b;
   }
 
   template<class T>
   inline T min(T a, T b)
   {
       return a < b ? a : b;
   }
 
   template<class T>
   inline T max(T a, T b, T c)
   {
       return max(max(a, b), c);
   }
 
   template<class T>
   inline T min(T a, T b, T c)
   {
       return min(min(a, b), c);
   }
 
   template<class T>
   inline T max(T a, T b, T c, T d)
   {
       return max(max(a, b), max(c, d));
   }
 
   template<class T>
   inline T min(T a, T b, T c, T d)
   {
       return min(min(a, b), min(c, d));
   }
 
   template <typename destType, typename sourceType>
   destType bit_cast(const sourceType &source)
   {
       union
       {
           sourceType s;
           destType d;
       } sd;
       sd.s = source;
       return sd.d;
   }
 
   inline int iround(float x)
   {
       return (int)floor(x + 0.5f);
   //    return _mm_cvtss_si32(_mm_load_ss(&x));   // FIXME: Demands SSE support
   }
 
   inline int ifloor(float x)
   {
       return (int)floor(x);
   }
 
   inline int ceilFix4(int x)
   {
       return (x + 0xF) & 0xFFFFFFF0;
   }
 
   inline int ceilInt4(int x)
   {
       return (x + 0xF) >> 4;
   }
 
   #define BITS(x)    ( \
   !!((x) & 0x80000000) + \
   !!((x) & 0xC0000000) + \
   !!((x) & 0xE0000000) + \
   !!((x) & 0xF0000000) + \
   !!((x) & 0xF8000000) + \
   !!((x) & 0xFC000000) + \
   !!((x) & 0xFE000000) + \
   !!((x) & 0xFF000000) + \
   !!((x) & 0xFF800000) + \
   !!((x) & 0xFFC00000) + \
   !!((x) & 0xFFE00000) + \
   !!((x) & 0xFFF00000) + \
   !!((x) & 0xFFF80000) + \
   !!((x) & 0xFFFC0000) + \
   !!((x) & 0xFFFE0000) + \
   !!((x) & 0xFFFF0000) + \
   !!((x) & 0xFFFF8000) + \
   !!((x) & 0xFFFFC000) + \
   !!((x) & 0xFFFFE000) + \
   !!((x) & 0xFFFFF000) + \
   !!((x) & 0xFFFFF800) + \
   !!((x) & 0xFFFFFC00) + \
   !!((x) & 0xFFFFFE00) + \
   !!((x) & 0xFFFFFF00) + \
   !!((x) & 0xFFFFFF80) + \
   !!((x) & 0xFFFFFFC0) + \
   !!((x) & 0xFFFFFFE0) + \
   !!((x) & 0xFFFFFFF0) + \
   !!((x) & 0xFFFFFFF8) + \
   !!((x) & 0xFFFFFFFC) + \
   !!((x) & 0xFFFFFFFE) + \
   !!((x) & 0xFFFFFFFF))
 
   #define MAX(x, y) ((x) > (y) ? (x) : (y))
   #define MIN(x, y) ((x) < (y) ? (x) : (y))
 
   inline float exp2(float x)
   {
       return exp2f(x);
   }
 
   inline int exp2(int x)
   {
       return 1 << x;
   }
 
   inline unsigned long log2(int x)
   {
       #if defined(_MSC_VER)
           unsigned long y;
           _BitScanReverse(&y, x);
           return y;
       #else
           return 31 - __builtin_clz(x);
       #endif
   }
 
   inline int ilog2(float x)
   {
       unsigned int y = *(unsigned int*)&x;
 
       return ((y & 0x7F800000) >> 23) - 127;
   }
 
   inline float log2(float x)
   {
       return logf(x) * 1.44269504f;   // 1.0 / log[e](2)
   }
 
   inline bool isPow2(int x)
   {
       return (x & -x) == x;
   }
 
   template<class T>
   inline T clamp(T x, T a, T b)
   {
       ASSERT(a <= b);
       if(x < a) x = a;
       if(x > b) x = b;
 
       return x;
   }
 
   inline float clamp01(float x)
   {
       return clamp(x, 0.0f, 1.0f);
   }
 
   // Bit-cast of a floating-point value into a two's complement integer representation.
   // This makes floating-point values comparable as integers.
   inline int32_t float_as_twos_complement(float f)
   {
       // IEEE-754 floating-point numbers are sorted by magnitude in the same way as integers,
       // except negative values are like one's complement integers. Convert them to two's complement.
       int32_t i = bit_cast<int32_t>(f);
       return (i < 0) ? (0x7FFFFFFFu - i) : i;
   }
 
   // 'Safe' clamping operation which always returns a value between min and max (inclusive).
   inline float clamp_s(float x, float min, float max)
   {
       // NaN values can't be compared directly
       if(float_as_twos_complement(x) < float_as_twos_complement(min)) x = min;
       if(float_as_twos_complement(x) > float_as_twos_complement(max)) x = max;
 
       return x;
   }
 
   inline int ceilPow2(int x)
   {
       int i = 1;
 
       while(i < x)
       {
           i <<= 1;
       }
 
       return i;
   }
 
   inline int floorDiv(int a, int b)
   {
       return a / b + ((a % b) >> 31);
   }
 
   inline int floorMod(int a, int b)
   {
       int r = a % b;
       return r + ((r >> 31) & b);
   }
 
   inline int ceilDiv(int a, int b)
   {
       return a / b - (-(a % b) >> 31);
   }
 
   inline int ceilMod(int a, int b)
   {
       int r = a % b;
       return r - ((-r >> 31) & b);
   }
 
   template<const int n>
   inline unsigned int unorm(float x)
   {
       static const unsigned int max = 0xFFFFFFFF >> (32 - n);
       static const float maxf = static_cast<float>(max);
 
       if(x >= 1.0f)
       {
           return max;
       }
       else if(x <= 0.0f)
       {
           return 0;
       }
       else
       {
           return static_cast<unsigned int>(maxf * x + 0.5f);
       }
   }
 
   template<const int n>
   inline int snorm(float x)
   {
       static const unsigned int min = 0x80000000 >> (32 - n);
       static const unsigned int max = 0xFFFFFFFF >> (32 - n + 1);
       static const float maxf = static_cast<float>(max);
       static const unsigned int range = 0xFFFFFFFF >> (32 - n);
 
       if(x >= 0.0f)
       {
           if(x >= 1.0f)
           {
               return max;
           }
           else
           {
               return static_cast<int>(maxf * x + 0.5f);
           }
       }
       else
       {
           if(x <= -1.0f)
           {
               return min;
           }
           else
           {
               return static_cast<int>(maxf * x - 0.5f) & range;
           }
       }
   }
 
   template<const int n>
   inline unsigned int ucast(float x)
   {
       static const unsigned int max = 0xFFFFFFFF >> (32 - n);
       static const float maxf = static_cast<float>(max);
 
       if(x >= maxf)
       {
           return max;
       }
       else if(x <= 0.0f)
       {
           return 0;
       }
       else
       {
           return static_cast<unsigned int>(x + 0.5f);
       }
   }
 
   template<const int n>
   inline int scast(float x)
   {
       static const unsigned int min = 0x80000000 >> (32 - n);
       static const unsigned int max = 0xFFFFFFFF >> (32 - n + 1);
       static const float maxf = static_cast<float>(max);
       static const float minf = static_cast<float>(min);
       static const unsigned int range = 0xFFFFFFFF >> (32 - n);
 
       if(x > 0.0f)
       {
           if(x >= maxf)
           {
               return max;
           }
           else
           {
               return static_cast<int>(x + 0.5f);
           }
       }
       else
       {
           if(x <= -minf)
           {
               return min;
           }
           else
           {
               return static_cast<int>(x - 0.5f) & range;
           }
       }
   }
 
   inline float sRGBtoLinear(float c)
   {
       if(c <= 0.04045f)
       {
           return c * 0.07739938f;   // 1.0f / 12.92f;
       }
       else
       {
           return powf((c + 0.055f) * 0.9478673f, 2.4f);   // 1.0f / 1.055f
       }
   }
 
   inline float linearToSRGB(float c)
   {
       if(c <= 0.0031308f)
       {
           return c * 12.92f;
       }
       else
       {
           return 1.055f * powf(c, 0.4166667f) - 0.055f;   // 1.0f / 2.4f
       }
   }
 
   unsigned char sRGB8toLinear8(unsigned char value);
 
   uint64_t FNV_1a(const unsigned char *data, int size);   // Fowler-Noll-Vo hash function
 
   // Round up to the next multiple of alignment
   template<typename T>
   inline T align(T value, unsigned int alignment)
   {
       return ((value + alignment - 1) / alignment) * alignment;
   }
 
   template<unsigned int alignment, typename T>
   inline T align(T value)
   {
       return ((value + alignment - 1) / alignment) * alignment;
   }
 
   inline int clampToSignedInt(unsigned int x)
   {
       return static_cast<int>(min(x, 0x7FFFFFFFu));
   }
}
 
#endif   // sw_Math_hpp