Merge branch 'llvm-cliptest-viewport'
[mesa.git] / src / gallium / auxiliary / util / u_math.h
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 #include "util/u_debug.h"
44
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50
51 #if defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
52 __inline double ceil(double val)
53 {
54 double ceil_val;
55
56 if ((val - (long) val) == 0) {
57 ceil_val = val;
58 }
59 else {
60 if (val > 0) {
61 ceil_val = (long) val + 1;
62 }
63 else {
64 ceil_val = (long) val;
65 }
66 }
67
68 return ceil_val;
69 }
70
71 #ifndef PIPE_SUBSYSTEM_WINDOWS_CE_OGL
72 __inline double floor(double val)
73 {
74 double floor_val;
75
76 if ((val - (long) val) == 0) {
77 floor_val = val;
78 }
79 else {
80 if (val > 0) {
81 floor_val = (long) val;
82 }
83 else {
84 floor_val = (long) val - 1;
85 }
86 }
87
88 return floor_val;
89 }
90 #endif
91
92 #pragma function(pow)
93 __inline double __cdecl pow(double val, double exponent)
94 {
95 /* XXX */
96 assert(0);
97 return 0;
98 }
99
100 #pragma function(log)
101 __inline double __cdecl log(double val)
102 {
103 /* XXX */
104 assert(0);
105 return 0;
106 }
107
108 #pragma function(atan2)
109 __inline double __cdecl atan2(double val)
110 {
111 /* XXX */
112 assert(0);
113 return 0;
114 }
115 #else
116 #include <math.h>
117 #include <stdarg.h>
118 #endif
119
120
121 #ifndef M_SQRT2
122 #define M_SQRT2 1.41421356237309504880
123 #endif
124
125
126 #if defined(_MSC_VER)
127
128 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
129
130 static INLINE float cosf( float f )
131 {
132 return (float) cos( (double) f );
133 }
134
135 static INLINE float sinf( float f )
136 {
137 return (float) sin( (double) f );
138 }
139
140 static INLINE float ceilf( float f )
141 {
142 return (float) ceil( (double) f );
143 }
144
145 static INLINE float floorf( float f )
146 {
147 return (float) floor( (double) f );
148 }
149
150 static INLINE float powf( float f, float g )
151 {
152 return (float) pow( (double) f, (double) g );
153 }
154
155 static INLINE float sqrtf( float f )
156 {
157 return (float) sqrt( (double) f );
158 }
159
160 static INLINE float fabsf( float f )
161 {
162 return (float) fabs( (double) f );
163 }
164
165 static INLINE float logf( float f )
166 {
167 return (float) log( (double) f );
168 }
169
170 #else
171 /* Work-around an extra semi-colon in VS 2005 logf definition */
172 #ifdef logf
173 #undef logf
174 #define logf(x) ((float)log((double)(x)))
175 #endif /* logf */
176
177 #define isfinite(x) _finite((double)(x))
178 #define isnan(x) _isnan((double)(x))
179 #endif
180
181 static INLINE double log2( double x )
182 {
183 const double invln2 = 1.442695041;
184 return log( x ) * invln2;
185 }
186
187 #endif /* _MSC_VER */
188
189
190
191
192
193 #define POW2_TABLE_SIZE_LOG2 9
194 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
195 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
196 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
197 extern float pow2_table[POW2_TABLE_SIZE];
198
199
200 /**
201 * Initialize math module. This should be called before using any
202 * other functions in this module.
203 */
204 extern void
205 util_init_math(void);
206
207
208 union fi {
209 float f;
210 int32_t i;
211 uint32_t ui;
212 };
213
214
215 /**
216 * Fast version of 2^x
217 * Identity: exp2(a + b) = exp2(a) * exp2(b)
218 * Let ipart = int(x)
219 * Let fpart = x - ipart;
220 * So, exp2(x) = exp2(ipart) * exp2(fpart)
221 * Compute exp2(ipart) with i << ipart
222 * Compute exp2(fpart) with lookup table.
223 */
224 static INLINE float
225 util_fast_exp2(float x)
226 {
227 int32_t ipart;
228 float fpart, mpart;
229 union fi epart;
230
231 if(x > 129.00000f)
232 return 3.402823466e+38f;
233
234 if (x < -126.99999f)
235 return 0.0f;
236
237 ipart = (int32_t) x;
238 fpart = x - (float) ipart;
239
240 /* same as
241 * epart.f = (float) (1 << ipart)
242 * but faster and without integer overflow for ipart > 31
243 */
244 epart.i = (ipart + 127 ) << 23;
245
246 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
247
248 return epart.f * mpart;
249 }
250
251
252 /**
253 * Fast approximation to exp(x).
254 */
255 static INLINE float
256 util_fast_exp(float x)
257 {
258 const float k = 1.44269f; /* = log2(e) */
259 return util_fast_exp2(k * x);
260 }
261
262
263 #define LOG2_TABLE_SIZE_LOG2 16
264 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
265 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
266 extern float log2_table[LOG2_TABLE_SIZE];
267
268
269 /**
270 * Fast approximation to log2(x).
271 */
272 static INLINE float
273 util_fast_log2(float x)
274 {
275 union fi num;
276 float epart, mpart;
277 num.f = x;
278 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
279 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
280 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
281 return epart + mpart;
282 }
283
284
285 /**
286 * Fast approximation to x^y.
287 */
288 static INLINE float
289 util_fast_pow(float x, float y)
290 {
291 return util_fast_exp2(util_fast_log2(x) * y);
292 }
293
294 /* Note that this counts zero as a power of two.
295 */
296 static INLINE boolean
297 util_is_power_of_two( unsigned v )
298 {
299 return (v & (v-1)) == 0;
300 }
301
302
303 /**
304 * Floor(x), returned as int.
305 */
306 static INLINE int
307 util_ifloor(float f)
308 {
309 int ai, bi;
310 double af, bf;
311 union fi u;
312 af = (3 << 22) + 0.5 + (double) f;
313 bf = (3 << 22) + 0.5 - (double) f;
314 u.f = (float) af; ai = u.i;
315 u.f = (float) bf; bi = u.i;
316 return (ai - bi) >> 1;
317 }
318
319
320 /**
321 * Round float to nearest int.
322 */
323 static INLINE int
324 util_iround(float f)
325 {
326 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
327 int r;
328 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
329 return r;
330 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
331 int r;
332 _asm {
333 fld f
334 fistp r
335 }
336 return r;
337 #else
338 if (f >= 0.0f)
339 return (int) (f + 0.5f);
340 else
341 return (int) (f - 0.5f);
342 #endif
343 }
344
345
346 /**
347 * Approximate floating point comparison
348 */
349 static INLINE boolean
350 util_is_approx(float a, float b, float tol)
351 {
352 return fabs(b - a) <= tol;
353 }
354
355
356 /**
357 * Test if x is NaN or +/- infinity.
358 */
359 static INLINE boolean
360 util_is_inf_or_nan(float x)
361 {
362 union fi tmp;
363 tmp.f = x;
364 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
365 }
366
367
368 /**
369 * Find first bit set in word. Least significant bit is 1.
370 * Return 0 if no bits set.
371 */
372 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
373 unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
374 #pragma intrinsic(_BitScanForward)
375 static INLINE
376 unsigned long ffs( unsigned long u )
377 {
378 unsigned long i;
379 if (_BitScanForward(&i, u))
380 return i + 1;
381 else
382 return 0;
383 }
384 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
385 static INLINE
386 unsigned ffs( unsigned u )
387 {
388 unsigned i;
389
390 if (u == 0) {
391 return 0;
392 }
393
394 __asm bsf eax, [u]
395 __asm inc eax
396 __asm mov [i], eax
397
398 return i;
399 }
400 #elif defined(__MINGW32__)
401 #define ffs __builtin_ffs
402 #endif
403
404 #ifdef __MINGW32__
405 #define ffs __builtin_ffs
406 #endif
407
408
409 /* Could also binary search for the highest bit.
410 */
411 static INLINE unsigned
412 util_unsigned_logbase2(unsigned n)
413 {
414 unsigned log2 = 0;
415 while (n >>= 1)
416 ++log2;
417 return log2;
418 }
419
420
421 /**
422 * Return float bits.
423 */
424 static INLINE unsigned
425 fui( float f )
426 {
427 union fi fi;
428 fi.f = f;
429 return fi.ui;
430 }
431
432
433 /**
434 * Convert ubyte to float in [0, 1].
435 * XXX a 256-entry lookup table would be slightly faster.
436 */
437 static INLINE float
438 ubyte_to_float(ubyte ub)
439 {
440 return (float) ub * (1.0f / 255.0f);
441 }
442
443
444 /**
445 * Convert float in [0,1] to ubyte in [0,255] with clamping.
446 */
447 static INLINE ubyte
448 float_to_ubyte(float f)
449 {
450 const int ieee_0996 = 0x3f7f0000; /* 0.996 or so */
451 union fi tmp;
452
453 tmp.f = f;
454 if (tmp.i < 0) {
455 return (ubyte) 0;
456 }
457 else if (tmp.i >= ieee_0996) {
458 return (ubyte) 255;
459 }
460 else {
461 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
462 return (ubyte) tmp.i;
463 }
464 }
465
466
467 /**
468 * Calc log base 2
469 */
470 static INLINE unsigned
471 util_logbase2(unsigned n)
472 {
473 unsigned log2 = 0;
474 while (n >>= 1)
475 ++log2;
476 return log2;
477 }
478
479
480 /**
481 * Returns the smallest power of two >= x
482 */
483 static INLINE unsigned
484 util_next_power_of_two(unsigned x)
485 {
486 unsigned i;
487
488 if (x == 0)
489 return 1;
490
491 --x;
492
493 for (i = 1; i < sizeof(unsigned) * 8; i <<= 1)
494 x |= x >> i;
495
496 return x + 1;
497 }
498
499
500 /**
501 * Return number of bits set in n.
502 */
503 static INLINE unsigned
504 util_bitcount(unsigned n)
505 {
506 #if defined(PIPE_CC_GCC)
507 return __builtin_popcount(n);
508 #else
509 /* K&R classic bitcount.
510 *
511 * For each iteration, clear the LSB from the bitfield.
512 * Requires only one iteration per set bit, instead of
513 * one iteration per bit less than highest set bit.
514 */
515 unsigned bits = 0;
516 for (bits; n; bits++) {
517 n &= n - 1;
518 }
519 return bits;
520 #endif
521 }
522
523
524 /**
525 * Reverse byte order of a 32 bit word.
526 */
527 static INLINE uint32_t
528 util_bswap32(uint32_t n)
529 {
530 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403)
531 return __builtin_bswap32(n);
532 #else
533 return (n >> 24) |
534 ((n >> 8) & 0x0000ff00) |
535 ((n << 8) & 0x00ff0000) |
536 (n << 24);
537 #endif
538 }
539
540
541 /**
542 * Reverse byte order of a 16 bit word.
543 */
544 static INLINE uint16_t
545 util_bswap16(uint16_t n)
546 {
547 return (n >> 8) |
548 (n << 8);
549 }
550
551
552 /**
553 * Clamp X to [MIN, MAX].
554 * This is a macro to allow float, int, uint, etc. types.
555 */
556 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
557
558 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
559 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
560
561 #define MIN3( A, B, C ) MIN2( MIN2( A, B ), C )
562 #define MAX3( A, B, C ) MAX2( MAX2( A, B ), C )
563
564 #define MIN4( A, B, C, D ) MIN2( MIN2( A, B ), MIN2(C, D) )
565 #define MAX4( A, B, C, D ) MAX2( MAX2( A, B ), MAX2(C, D) )
566
567
568 /**
569 * Align a value, only works pot alignemnts.
570 */
571 static INLINE int
572 align(int value, int alignment)
573 {
574 return (value + alignment - 1) & ~(alignment - 1);
575 }
576
577 /**
578 * Works like align but on npot alignments.
579 */
580 static INLINE size_t
581 util_align_npot(size_t value, size_t alignment)
582 {
583 if (value % alignment)
584 return value + (alignment - (value % alignment));
585 return value;
586 }
587
588 static INLINE unsigned
589 u_minify(unsigned value, unsigned levels)
590 {
591 return MAX2(1, value >> levels);
592 }
593
594 #ifndef COPY_4V
595 #define COPY_4V( DST, SRC ) \
596 do { \
597 (DST)[0] = (SRC)[0]; \
598 (DST)[1] = (SRC)[1]; \
599 (DST)[2] = (SRC)[2]; \
600 (DST)[3] = (SRC)[3]; \
601 } while (0)
602 #endif
603
604
605 #ifndef COPY_4FV
606 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
607 #endif
608
609
610 #ifndef ASSIGN_4V
611 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
612 do { \
613 (DST)[0] = (V0); \
614 (DST)[1] = (V1); \
615 (DST)[2] = (V2); \
616 (DST)[3] = (V3); \
617 } while (0)
618 #endif
619
620
621 static INLINE uint32_t util_unsigned_fixed(float value, unsigned frac_bits)
622 {
623 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
624 }
625
626 static INLINE int32_t util_signed_fixed(float value, unsigned frac_bits)
627 {
628 return (int32_t)(value * (1<<frac_bits));
629 }
630
631
632
633 #ifdef __cplusplus
634 }
635 #endif
636
637 #endif /* U_MATH_H */