Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

u_math.h 13KB

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