gallium: Add c99_compat.h to u_bitcast.h
[mesa.git] / src / gallium / auxiliary / util / u_math.h
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Math utilities and approximations for common math functions.
31 * Reduced precision is usually acceptable in shaders...
32 *
33 * "fast" is used in the names of functions which are low-precision,
34 * or at least lower-precision than the normal C lib functions.
35 */
36
37
38 #ifndef U_MATH_H
39 #define U_MATH_H
40
41
42 #include "pipe/p_compiler.h"
43
44 #include "c99_math.h"
45 #include <assert.h>
46 #include <float.h>
47 #include <stdarg.h>
48
49 #include "util/bitscan.h"
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55
56 #ifndef M_SQRT2
57 #define M_SQRT2 1.41421356237309504880
58 #endif
59
60 #define POW2_TABLE_SIZE_LOG2 9
61 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
62 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
63 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
64 extern float pow2_table[POW2_TABLE_SIZE];
65
66
67 /**
68 * Initialize math module. This should be called before using any
69 * other functions in this module.
70 */
71 extern void
72 util_init_math(void);
73
74
75 union fi {
76 float f;
77 int32_t i;
78 uint32_t ui;
79 };
80
81
82 union di {
83 double d;
84 int64_t i;
85 uint64_t ui;
86 };
87
88
89 /**
90 * Extract the IEEE float32 exponent.
91 */
92 static inline signed
93 util_get_float32_exponent(float x)
94 {
95 union fi f;
96
97 f.f = x;
98
99 return ((f.ui >> 23) & 0xff) - 127;
100 }
101
102
103 /**
104 * Fast version of 2^x
105 * Identity: exp2(a + b) = exp2(a) * exp2(b)
106 * Let ipart = int(x)
107 * Let fpart = x - ipart;
108 * So, exp2(x) = exp2(ipart) * exp2(fpart)
109 * Compute exp2(ipart) with i << ipart
110 * Compute exp2(fpart) with lookup table.
111 */
112 static inline float
113 util_fast_exp2(float x)
114 {
115 int32_t ipart;
116 float fpart, mpart;
117 union fi epart;
118
119 if(x > 129.00000f)
120 return 3.402823466e+38f;
121
122 if (x < -126.99999f)
123 return 0.0f;
124
125 ipart = (int32_t) x;
126 fpart = x - (float) ipart;
127
128 /* same as
129 * epart.f = (float) (1 << ipart)
130 * but faster and without integer overflow for ipart > 31
131 */
132 epart.i = (ipart + 127 ) << 23;
133
134 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
135
136 return epart.f * mpart;
137 }
138
139
140 /**
141 * Fast approximation to exp(x).
142 */
143 static inline float
144 util_fast_exp(float x)
145 {
146 const float k = 1.44269f; /* = log2(e) */
147 return util_fast_exp2(k * x);
148 }
149
150
151 #define LOG2_TABLE_SIZE_LOG2 16
152 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
153 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
154 extern float log2_table[LOG2_TABLE_SIZE];
155
156
157 /**
158 * Fast approximation to log2(x).
159 */
160 static inline float
161 util_fast_log2(float x)
162 {
163 union fi num;
164 float epart, mpart;
165 num.f = x;
166 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
167 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
168 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
169 return epart + mpart;
170 }
171
172
173 /**
174 * Fast approximation to x^y.
175 */
176 static inline float
177 util_fast_pow(float x, float y)
178 {
179 return util_fast_exp2(util_fast_log2(x) * y);
180 }
181
182 /* Note that this counts zero as a power of two.
183 */
184 static inline boolean
185 util_is_power_of_two( unsigned v )
186 {
187 return (v & (v-1)) == 0;
188 }
189
190
191 /**
192 * Floor(x), returned as int.
193 */
194 static inline int
195 util_ifloor(float f)
196 {
197 int ai, bi;
198 double af, bf;
199 union fi u;
200 af = (3 << 22) + 0.5 + (double) f;
201 bf = (3 << 22) + 0.5 - (double) f;
202 u.f = (float) af; ai = u.i;
203 u.f = (float) bf; bi = u.i;
204 return (ai - bi) >> 1;
205 }
206
207
208 /**
209 * Round float to nearest int.
210 */
211 static inline int
212 util_iround(float f)
213 {
214 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
215 int r;
216 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
217 return r;
218 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
219 int r;
220 _asm {
221 fld f
222 fistp r
223 }
224 return r;
225 #else
226 if (f >= 0.0f)
227 return (int) (f + 0.5f);
228 else
229 return (int) (f - 0.5f);
230 #endif
231 }
232
233
234 /**
235 * Approximate floating point comparison
236 */
237 static inline boolean
238 util_is_approx(float a, float b, float tol)
239 {
240 return fabsf(b - a) <= tol;
241 }
242
243
244 /**
245 * util_is_X_inf_or_nan = test if x is NaN or +/- Inf
246 * util_is_X_nan = test if x is NaN
247 * util_X_inf_sign = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
248 *
249 * NaN can be checked with x != x, however this fails with the fast math flag
250 **/
251
252
253 /**
254 * Single-float
255 */
256 static inline boolean
257 util_is_inf_or_nan(float x)
258 {
259 union fi tmp;
260 tmp.f = x;
261 return (tmp.ui & 0x7f800000) == 0x7f800000;
262 }
263
264
265 static inline boolean
266 util_is_nan(float x)
267 {
268 union fi tmp;
269 tmp.f = x;
270 return (tmp.ui & 0x7fffffff) > 0x7f800000;
271 }
272
273
274 static inline int
275 util_inf_sign(float x)
276 {
277 union fi tmp;
278 tmp.f = x;
279 if ((tmp.ui & 0x7fffffff) != 0x7f800000) {
280 return 0;
281 }
282
283 return (x < 0) ? -1 : 1;
284 }
285
286
287 /**
288 * Double-float
289 */
290 static inline boolean
291 util_is_double_inf_or_nan(double x)
292 {
293 union di tmp;
294 tmp.d = x;
295 return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL;
296 }
297
298
299 static inline boolean
300 util_is_double_nan(double x)
301 {
302 union di tmp;
303 tmp.d = x;
304 return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL;
305 }
306
307
308 static inline int
309 util_double_inf_sign(double x)
310 {
311 union di tmp;
312 tmp.d = x;
313 if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) {
314 return 0;
315 }
316
317 return (x < 0) ? -1 : 1;
318 }
319
320
321 /**
322 * Half-float
323 */
324 static inline boolean
325 util_is_half_inf_or_nan(int16_t x)
326 {
327 return (x & 0x7c00) == 0x7c00;
328 }
329
330
331 static inline boolean
332 util_is_half_nan(int16_t x)
333 {
334 return (x & 0x7fff) > 0x7c00;
335 }
336
337
338 static inline int
339 util_half_inf_sign(int16_t x)
340 {
341 if ((x & 0x7fff) != 0x7c00) {
342 return 0;
343 }
344
345 return (x < 0) ? -1 : 1;
346 }
347
348
349 /**
350 * Return float bits.
351 */
352 static inline unsigned
353 fui( float f )
354 {
355 union fi fi;
356 fi.f = f;
357 return fi.ui;
358 }
359
360 static inline float
361 uif(uint32_t ui)
362 {
363 union fi fi;
364 fi.ui = ui;
365 return fi.f;
366 }
367
368
369 /**
370 * Convert ubyte to float in [0, 1].
371 * XXX a 256-entry lookup table would be slightly faster.
372 */
373 static inline float
374 ubyte_to_float(ubyte ub)
375 {
376 return (float) ub * (1.0f / 255.0f);
377 }
378
379
380 /**
381 * Convert float in [0,1] to ubyte in [0,255] with clamping.
382 */
383 static inline ubyte
384 float_to_ubyte(float f)
385 {
386 union fi tmp;
387
388 tmp.f = f;
389 if (tmp.i < 0) {
390 return (ubyte) 0;
391 }
392 else if (tmp.i >= 0x3f800000 /* 1.0f */) {
393 return (ubyte) 255;
394 }
395 else {
396 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
397 return (ubyte) tmp.i;
398 }
399 }
400
401 static inline float
402 byte_to_float_tex(int8_t b)
403 {
404 return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
405 }
406
407 static inline int8_t
408 float_to_byte_tex(float f)
409 {
410 return (int8_t) (127.0F * f);
411 }
412
413 /**
414 * Calc log base 2
415 */
416 static inline unsigned
417 util_logbase2(unsigned n)
418 {
419 #if defined(HAVE___BUILTIN_CLZ)
420 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
421 #else
422 unsigned pos = 0;
423 if (n >= 1<<16) { n >>= 16; pos += 16; }
424 if (n >= 1<< 8) { n >>= 8; pos += 8; }
425 if (n >= 1<< 4) { n >>= 4; pos += 4; }
426 if (n >= 1<< 2) { n >>= 2; pos += 2; }
427 if (n >= 1<< 1) { pos += 1; }
428 return pos;
429 #endif
430 }
431
432
433 /**
434 * Returns the smallest power of two >= x
435 */
436 static inline unsigned
437 util_next_power_of_two(unsigned x)
438 {
439 #if defined(HAVE___BUILTIN_CLZ)
440 if (x <= 1)
441 return 1;
442
443 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
444 #else
445 unsigned val = x;
446
447 if (x <= 1)
448 return 1;
449
450 if (util_is_power_of_two(x))
451 return x;
452
453 val--;
454 val = (val >> 1) | val;
455 val = (val >> 2) | val;
456 val = (val >> 4) | val;
457 val = (val >> 8) | val;
458 val = (val >> 16) | val;
459 val++;
460 return val;
461 #endif
462 }
463
464
465 /**
466 * Return number of bits set in n.
467 */
468 static inline unsigned
469 util_bitcount(unsigned n)
470 {
471 #if defined(HAVE___BUILTIN_POPCOUNT)
472 return __builtin_popcount(n);
473 #else
474 /* K&R classic bitcount.
475 *
476 * For each iteration, clear the LSB from the bitfield.
477 * Requires only one iteration per set bit, instead of
478 * one iteration per bit less than highest set bit.
479 */
480 unsigned bits;
481 for (bits = 0; n; bits++) {
482 n &= n - 1;
483 }
484 return bits;
485 #endif
486 }
487
488
489 static inline unsigned
490 util_bitcount64(uint64_t n)
491 {
492 #ifdef HAVE___BUILTIN_POPCOUNTLL
493 return __builtin_popcountll(n);
494 #else
495 return util_bitcount(n) + util_bitcount(n >> 32);
496 #endif
497 }
498
499
500 /**
501 * Reverse bits in n
502 * Algorithm taken from:
503 * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
504 */
505 static inline unsigned
506 util_bitreverse(unsigned n)
507 {
508 n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
509 n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
510 n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
511 n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
512 n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
513 return n;
514 }
515
516 /**
517 * Convert from little endian to CPU byte order.
518 */
519
520 #ifdef PIPE_ARCH_BIG_ENDIAN
521 #define util_le64_to_cpu(x) util_bswap64(x)
522 #define util_le32_to_cpu(x) util_bswap32(x)
523 #define util_le16_to_cpu(x) util_bswap16(x)
524 #else
525 #define util_le64_to_cpu(x) (x)
526 #define util_le32_to_cpu(x) (x)
527 #define util_le16_to_cpu(x) (x)
528 #endif
529
530 #define util_cpu_to_le64(x) util_le64_to_cpu(x)
531 #define util_cpu_to_le32(x) util_le32_to_cpu(x)
532 #define util_cpu_to_le16(x) util_le16_to_cpu(x)
533
534 /**
535 * Reverse byte order of a 32 bit word.
536 */
537 static inline uint32_t
538 util_bswap32(uint32_t n)
539 {
540 #if defined(HAVE___BUILTIN_BSWAP32)
541 return __builtin_bswap32(n);
542 #else
543 return (n >> 24) |
544 ((n >> 8) & 0x0000ff00) |
545 ((n << 8) & 0x00ff0000) |
546 (n << 24);
547 #endif
548 }
549
550 /**
551 * Reverse byte order of a 64bit word.
552 */
553 static inline uint64_t
554 util_bswap64(uint64_t n)
555 {
556 #if defined(HAVE___BUILTIN_BSWAP64)
557 return __builtin_bswap64(n);
558 #else
559 return ((uint64_t)util_bswap32((uint32_t)n) << 32) |
560 util_bswap32((n >> 32));
561 #endif
562 }
563
564
565 /**
566 * Reverse byte order of a 16 bit word.
567 */
568 static inline uint16_t
569 util_bswap16(uint16_t n)
570 {
571 return (n >> 8) |
572 (n << 8);
573 }
574
575 static inline void*
576 util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
577 {
578 #ifdef PIPE_ARCH_BIG_ENDIAN
579 size_t i, e;
580 assert(n % 4 == 0);
581
582 for (i = 0, e = n / 4; i < e; i++) {
583 uint32_t * restrict d = (uint32_t* restrict)dest;
584 const uint32_t * restrict s = (const uint32_t* restrict)src;
585 d[i] = util_bswap32(s[i]);
586 }
587 return dest;
588 #else
589 return memcpy(dest, src, n);
590 #endif
591 }
592
593 /**
594 * Clamp X to [MIN, MAX].
595 * This is a macro to allow float, int, uint, etc. types.
596 */
597 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
598
599 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
600 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
601
602 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
603 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
604
605 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
606 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
607
608
609 /**
610 * Align a value, only works pot alignemnts.
611 */
612 static inline int
613 align(int value, int alignment)
614 {
615 return (value + alignment - 1) & ~(alignment - 1);
616 }
617
618 static inline uint64_t
619 align64(uint64_t value, unsigned alignment)
620 {
621 return (value + alignment - 1) & ~((uint64_t)alignment - 1);
622 }
623
624 /**
625 * Works like align but on npot alignments.
626 */
627 static inline size_t
628 util_align_npot(size_t value, size_t alignment)
629 {
630 if (value % alignment)
631 return value + (alignment - (value % alignment));
632 return value;
633 }
634
635 static inline unsigned
636 u_minify(unsigned value, unsigned levels)
637 {
638 return MAX2(1, value >> levels);
639 }
640
641 #ifndef COPY_4V
642 #define COPY_4V( DST, SRC ) \
643 do { \
644 (DST)[0] = (SRC)[0]; \
645 (DST)[1] = (SRC)[1]; \
646 (DST)[2] = (SRC)[2]; \
647 (DST)[3] = (SRC)[3]; \
648 } while (0)
649 #endif
650
651
652 #ifndef COPY_4FV
653 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
654 #endif
655
656
657 #ifndef ASSIGN_4V
658 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
659 do { \
660 (DST)[0] = (V0); \
661 (DST)[1] = (V1); \
662 (DST)[2] = (V2); \
663 (DST)[3] = (V3); \
664 } while (0)
665 #endif
666
667
668 static inline uint32_t
669 util_unsigned_fixed(float value, unsigned frac_bits)
670 {
671 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
672 }
673
674 static inline int32_t
675 util_signed_fixed(float value, unsigned frac_bits)
676 {
677 return (int32_t)(value * (1<<frac_bits));
678 }
679
680 unsigned
681 util_fpstate_get(void);
682 unsigned
683 util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
684 void
685 util_fpstate_set(unsigned fpstate);
686
687
688
689 #ifdef __cplusplus
690 }
691 #endif
692
693 #endif /* U_MATH_H */