Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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. #ifdef PIPE_OS_UNIX
  50. #include <strings.h> /* for ffs */
  51. #endif
  52.  
  53.  
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57.  
  58.  
  59. #ifndef M_SQRT2
  60. #define M_SQRT2 1.41421356237309504880
  61. #endif
  62.  
  63. #define POW2_TABLE_SIZE_LOG2 9
  64. #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
  65. #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
  66. #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
  67. extern float pow2_table[POW2_TABLE_SIZE];
  68.  
  69.  
  70. /**
  71.  * Initialize math module.  This should be called before using any
  72.  * other functions in this module.
  73.  */
  74. extern void
  75. util_init_math(void);
  76.  
  77.  
  78. union fi {
  79.    float f;
  80.    int32_t i;
  81.    uint32_t ui;
  82. };
  83.  
  84.  
  85. union di {
  86.    double d;
  87.    int64_t i;
  88.    uint64_t ui;
  89. };
  90.  
  91.  
  92. /**
  93.  * Extract the IEEE float32 exponent.
  94.  */
  95. static INLINE signed
  96. util_get_float32_exponent(float x)
  97. {
  98.    union fi f;
  99.  
  100.    f.f = x;
  101.  
  102.    return ((f.ui >> 23) & 0xff) - 127;
  103. }
  104.  
  105.  
  106. /**
  107.  * Fast version of 2^x
  108.  * Identity: exp2(a + b) = exp2(a) * exp2(b)
  109.  * Let ipart = int(x)
  110.  * Let fpart = x - ipart;
  111.  * So, exp2(x) = exp2(ipart) * exp2(fpart)
  112.  * Compute exp2(ipart) with i << ipart
  113.  * Compute exp2(fpart) with lookup table.
  114.  */
  115. static INLINE float
  116. util_fast_exp2(float x)
  117. {
  118.    int32_t ipart;
  119.    float fpart, mpart;
  120.    union fi epart;
  121.  
  122.    if(x > 129.00000f)
  123.       return 3.402823466e+38f;
  124.  
  125.    if (x < -126.99999f)
  126.       return 0.0f;
  127.  
  128.    ipart = (int32_t) x;
  129.    fpart = x - (float) ipart;
  130.  
  131.    /* same as
  132.     *   epart.f = (float) (1 << ipart)
  133.     * but faster and without integer overflow for ipart > 31
  134.     */
  135.    epart.i = (ipart + 127 ) << 23;
  136.  
  137.    mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
  138.  
  139.    return epart.f * mpart;
  140. }
  141.  
  142.  
  143. /**
  144.  * Fast approximation to exp(x).
  145.  */
  146. static INLINE float
  147. util_fast_exp(float x)
  148. {
  149.    const float k = 1.44269f; /* = log2(e) */
  150.    return util_fast_exp2(k * x);
  151. }
  152.  
  153.  
  154. #define LOG2_TABLE_SIZE_LOG2 16
  155. #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
  156. #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
  157. extern float log2_table[LOG2_TABLE_SIZE];
  158.  
  159.  
  160. /**
  161.  * Fast approximation to log2(x).
  162.  */
  163. static INLINE float
  164. util_fast_log2(float x)
  165. {
  166.    union fi num;
  167.    float epart, mpart;
  168.    num.f = x;
  169.    epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
  170.    /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
  171.    mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
  172.    return epart + mpart;
  173. }
  174.  
  175.  
  176. /**
  177.  * Fast approximation to x^y.
  178.  */
  179. static INLINE float
  180. util_fast_pow(float x, float y)
  181. {
  182.    return util_fast_exp2(util_fast_log2(x) * y);
  183. }
  184.  
  185. /* Note that this counts zero as a power of two.
  186.  */
  187. static INLINE boolean
  188. util_is_power_of_two( unsigned v )
  189. {
  190.    return (v & (v-1)) == 0;
  191. }
  192.  
  193.  
  194. /**
  195.  * Floor(x), returned as int.
  196.  */
  197. static INLINE int
  198. util_ifloor(float f)
  199. {
  200.    int ai, bi;
  201.    double af, bf;
  202.    union fi u;
  203.    af = (3 << 22) + 0.5 + (double) f;
  204.    bf = (3 << 22) + 0.5 - (double) f;
  205.    u.f = (float) af;  ai = u.i;
  206.    u.f = (float) bf;  bi = u.i;
  207.    return (ai - bi) >> 1;
  208. }
  209.  
  210.  
  211. /**
  212.  * Round float to nearest int.
  213.  */
  214. static INLINE int
  215. util_iround(float f)
  216. {
  217. #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
  218.    int r;
  219.    __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
  220.    return r;
  221. #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
  222.    int r;
  223.    _asm {
  224.       fld f
  225.       fistp r
  226.    }
  227.    return r;
  228. #else
  229.    if (f >= 0.0f)
  230.       return (int) (f + 0.5f);
  231.    else
  232.       return (int) (f - 0.5f);
  233. #endif
  234. }
  235.  
  236.  
  237. /**
  238.  * Approximate floating point comparison
  239.  */
  240. static INLINE boolean
  241. util_is_approx(float a, float b, float tol)
  242. {
  243.    return fabs(b - a) <= tol;
  244. }
  245.  
  246.  
  247. /**
  248.  * util_is_X_inf_or_nan = test if x is NaN or +/- Inf
  249.  * util_is_X_nan        = test if x is NaN
  250.  * util_X_inf_sign      = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
  251.  *
  252.  * NaN can be checked with x != x, however this fails with the fast math flag
  253.  **/
  254.  
  255.  
  256. /**
  257.  * Single-float
  258.  */
  259. static INLINE boolean
  260. util_is_inf_or_nan(float x)
  261. {
  262.    union fi tmp;
  263.    tmp.f = x;
  264.    return (tmp.ui & 0x7f800000) == 0x7f800000;
  265. }
  266.  
  267.  
  268. static INLINE boolean
  269. util_is_nan(float x)
  270. {
  271.    union fi tmp;
  272.    tmp.f = x;
  273.    return (tmp.ui & 0x7fffffff) > 0x7f800000;
  274. }
  275.  
  276.  
  277. static INLINE int
  278. util_inf_sign(float x)
  279. {
  280.    union fi tmp;
  281.    tmp.f = x;
  282.    if ((tmp.ui & 0x7fffffff) != 0x7f800000) {
  283.       return 0;
  284.    }
  285.  
  286.    return (x < 0) ? -1 : 1;
  287. }
  288.  
  289.  
  290. /**
  291.  * Double-float
  292.  */
  293. static INLINE boolean
  294. util_is_double_inf_or_nan(double x)
  295. {
  296.    union di tmp;
  297.    tmp.d = x;
  298.    return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL;
  299. }
  300.  
  301.  
  302. static INLINE boolean
  303. util_is_double_nan(double x)
  304. {
  305.    union di tmp;
  306.    tmp.d = x;
  307.    return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL;
  308. }
  309.  
  310.  
  311. static INLINE int
  312. util_double_inf_sign(double x)
  313. {
  314.    union di tmp;
  315.    tmp.d = x;
  316.    if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) {
  317.       return 0;
  318.    }
  319.  
  320.    return (x < 0) ? -1 : 1;
  321. }
  322.  
  323.  
  324. /**
  325.  * Half-float
  326.  */
  327. static INLINE boolean
  328. util_is_half_inf_or_nan(int16_t x)
  329. {
  330.    return (x & 0x7c00) == 0x7c00;
  331. }
  332.  
  333.  
  334. static INLINE boolean
  335. util_is_half_nan(int16_t x)
  336. {
  337.    return (x & 0x7fff) > 0x7c00;
  338. }
  339.  
  340.  
  341. static INLINE int
  342. util_half_inf_sign(int16_t x)
  343. {
  344.    if ((x & 0x7fff) != 0x7c00) {
  345.       return 0;
  346.    }
  347.  
  348.    return (x < 0) ? -1 : 1;
  349. }
  350.  
  351.  
  352. /**
  353.  * Find first bit set in word.  Least significant bit is 1.
  354.  * Return 0 if no bits set.
  355.  */
  356. #ifndef FFS_DEFINED
  357. #define FFS_DEFINED 1
  358.  
  359. #if defined(_MSC_VER) && (_M_IX86 || _M_AMD64 || _M_IA64)
  360. unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
  361. #pragma intrinsic(_BitScanForward)
  362. static INLINE
  363. unsigned long ffs( unsigned long u )
  364. {
  365.    unsigned long i;
  366.    if (_BitScanForward(&i, u))
  367.       return i + 1;
  368.    else
  369.       return 0;
  370. }
  371. #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
  372. static INLINE
  373. unsigned ffs( unsigned u )
  374. {
  375.    unsigned i;
  376.  
  377.    if (u == 0) {
  378.       return 0;
  379.    }
  380.  
  381.    __asm bsf eax, [u]
  382.    __asm inc eax
  383.    __asm mov [i], eax
  384.  
  385.    return i;
  386. }
  387. #elif defined(__MINGW32__) || defined(PIPE_OS_ANDROID) || \
  388.     defined(HAVE___BUILTIN_FFS)
  389. #define ffs __builtin_ffs
  390. #endif
  391.  
  392. #endif /* FFS_DEFINED */
  393.  
  394. /**
  395.  * Find first bit set in long long.  Least significant bit is 1.
  396.  * Return 0 if no bits set.
  397.  */
  398. #ifndef FFSLL_DEFINED
  399. #define FFSLL_DEFINED 1
  400.  
  401. #if defined(__MINGW32__) || defined(PIPE_OS_ANDROID) || \
  402.     defined(HAVE___BUILTIN_FFSLL)
  403. #define ffsll __builtin_ffsll
  404. #endif
  405.  
  406. #endif /* FFSLL_DEFINED */
  407.  
  408. /**
  409.  * Find last bit set in a word.  The least significant bit is 1.
  410.  * Return 0 if no bits are set.
  411.  */
  412. static INLINE unsigned
  413. util_last_bit(unsigned u)
  414. {
  415. #if defined(HAVE___BUILTIN_CLZ)
  416.    return u == 0 ? 0 : 32 - __builtin_clz(u);
  417. #else
  418.    unsigned r = 0;
  419.    while (u) {
  420.        r++;
  421.        u >>= 1;
  422.    }
  423.    return r;
  424. #endif
  425. }
  426.  
  427. /**
  428.  * Find last bit in a word that does not match the sign bit. The least
  429.  * significant bit is 1.
  430.  * Return 0 if no bits are set.
  431.  */
  432. static INLINE unsigned
  433. util_last_bit_signed(int i)
  434. {
  435.    if (i >= 0)
  436.       return util_last_bit(i);
  437.    else
  438.       return util_last_bit(~(unsigned)i);
  439. }
  440.  
  441. /* Destructively loop over all of the bits in a mask as in:
  442.  *
  443.  * while (mymask) {
  444.  *   int i = u_bit_scan(&mymask);
  445.  *   ... process element i
  446.  * }
  447.  *
  448.  */
  449. static INLINE int
  450. u_bit_scan(unsigned *mask)
  451. {
  452.    int i = ffs(*mask) - 1;
  453.    *mask &= ~(1 << i);
  454.    return i;
  455. }
  456.  
  457. #ifndef _MSC_VER
  458. static INLINE int
  459. u_bit_scan64(uint64_t *mask)
  460. {
  461.    int i = ffsll(*mask) - 1;
  462.    *mask &= ~(1llu << i);
  463.    return i;
  464. }
  465. #endif
  466.  
  467. /**
  468.  * Return float bits.
  469.  */
  470. static INLINE unsigned
  471. fui( float f )
  472. {
  473.    union fi fi;
  474.    fi.f = f;
  475.    return fi.ui;
  476. }
  477.  
  478. static INLINE float
  479. uif(uint32_t ui)
  480. {
  481.    union fi fi;
  482.    fi.ui = ui;
  483.    return fi.f;
  484. }
  485.  
  486.  
  487. /**
  488.  * Convert ubyte to float in [0, 1].
  489.  * XXX a 256-entry lookup table would be slightly faster.
  490.  */
  491. static INLINE float
  492. ubyte_to_float(ubyte ub)
  493. {
  494.    return (float) ub * (1.0f / 255.0f);
  495. }
  496.  
  497.  
  498. /**
  499.  * Convert float in [0,1] to ubyte in [0,255] with clamping.
  500.  */
  501. static INLINE ubyte
  502. float_to_ubyte(float f)
  503. {
  504.    union fi tmp;
  505.  
  506.    tmp.f = f;
  507.    if (tmp.i < 0) {
  508.       return (ubyte) 0;
  509.    }
  510.    else if (tmp.i >= 0x3f800000 /* 1.0f */) {
  511.       return (ubyte) 255;
  512.    }
  513.    else {
  514.       tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
  515.       return (ubyte) tmp.i;
  516.    }
  517. }
  518.  
  519. static INLINE float
  520. byte_to_float_tex(int8_t b)
  521. {
  522.    return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
  523. }
  524.  
  525. static INLINE int8_t
  526. float_to_byte_tex(float f)
  527. {
  528.    return (int8_t) (127.0F * f);
  529. }
  530.  
  531. /**
  532.  * Calc log base 2
  533.  */
  534. static INLINE unsigned
  535. util_logbase2(unsigned n)
  536. {
  537. #if defined(HAVE___BUILTIN_CLZ)
  538.    return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
  539. #else
  540.    unsigned pos = 0;
  541.    if (n >= 1<<16) { n >>= 16; pos += 16; }
  542.    if (n >= 1<< 8) { n >>=  8; pos +=  8; }
  543.    if (n >= 1<< 4) { n >>=  4; pos +=  4; }
  544.    if (n >= 1<< 2) { n >>=  2; pos +=  2; }
  545.    if (n >= 1<< 1) {           pos +=  1; }
  546.    return pos;
  547. #endif
  548. }
  549.  
  550.  
  551. /**
  552.  * Returns the smallest power of two >= x
  553.  */
  554. static INLINE unsigned
  555. util_next_power_of_two(unsigned x)
  556. {
  557. #if defined(HAVE___BUILTIN_CLZ)
  558.    if (x <= 1)
  559.        return 1;
  560.  
  561.    return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
  562. #else
  563.    unsigned val = x;
  564.  
  565.    if (x <= 1)
  566.       return 1;
  567.  
  568.    if (util_is_power_of_two(x))
  569.       return x;
  570.  
  571.    val--;
  572.    val = (val >> 1) | val;
  573.    val = (val >> 2) | val;
  574.    val = (val >> 4) | val;
  575.    val = (val >> 8) | val;
  576.    val = (val >> 16) | val;
  577.    val++;
  578.    return val;
  579. #endif
  580. }
  581.  
  582.  
  583. /**
  584.  * Return number of bits set in n.
  585.  */
  586. static INLINE unsigned
  587. util_bitcount(unsigned n)
  588. {
  589. #if defined(HAVE___BUILTIN_POPCOUNT)
  590.    return __builtin_popcount(n);
  591. #else
  592.    /* K&R classic bitcount.
  593.     *
  594.     * For each iteration, clear the LSB from the bitfield.
  595.     * Requires only one iteration per set bit, instead of
  596.     * one iteration per bit less than highest set bit.
  597.     */
  598.    unsigned bits;
  599.    for (bits = 0; n; bits++) {
  600.       n &= n - 1;
  601.    }
  602.    return bits;
  603. #endif
  604. }
  605.  
  606.  
  607. static INLINE unsigned
  608. util_bitcount64(uint64_t n)
  609. {
  610. #ifdef HAVE___BUILTIN_POPCOUNTLL
  611.    return __builtin_popcountll(n);
  612. #else
  613.    return util_bitcount(n) + util_bitcount(n >> 32);
  614. #endif
  615. }
  616.  
  617.  
  618. /**
  619.  * Reverse bits in n
  620.  * Algorithm taken from:
  621.  * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
  622.  */
  623. static INLINE unsigned
  624. util_bitreverse(unsigned n)
  625. {
  626.     n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
  627.     n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
  628.     n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
  629.     n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
  630.     n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
  631.     return n;
  632. }
  633.  
  634. /**
  635.  * Convert from little endian to CPU byte order.
  636.  */
  637.  
  638. #ifdef PIPE_ARCH_BIG_ENDIAN
  639. #define util_le64_to_cpu(x) util_bswap64(x)
  640. #define util_le32_to_cpu(x) util_bswap32(x)
  641. #define util_le16_to_cpu(x) util_bswap16(x)
  642. #else
  643. #define util_le64_to_cpu(x) (x)
  644. #define util_le32_to_cpu(x) (x)
  645. #define util_le16_to_cpu(x) (x)
  646. #endif
  647.  
  648. #define util_cpu_to_le64(x) util_le64_to_cpu(x)
  649. #define util_cpu_to_le32(x) util_le32_to_cpu(x)
  650. #define util_cpu_to_le16(x) util_le16_to_cpu(x)
  651.  
  652. /**
  653.  * Reverse byte order of a 32 bit word.
  654.  */
  655. static INLINE uint32_t
  656. util_bswap32(uint32_t n)
  657. {
  658. #if defined(HAVE___BUILTIN_BSWAP32)
  659.    return __builtin_bswap32(n);
  660. #else
  661.    return (n >> 24) |
  662.           ((n >> 8) & 0x0000ff00) |
  663.           ((n << 8) & 0x00ff0000) |
  664.           (n << 24);
  665. #endif
  666. }
  667.  
  668. /**
  669.  * Reverse byte order of a 64bit word.
  670.  */
  671. static INLINE uint64_t
  672. util_bswap64(uint64_t n)
  673. {
  674. #if defined(HAVE___BUILTIN_BSWAP64)
  675.    return __builtin_bswap64(n);
  676. #else
  677.    return ((uint64_t)util_bswap32((uint32_t)n) << 32) |
  678.           util_bswap32((n >> 32));
  679. #endif
  680. }
  681.  
  682.  
  683. /**
  684.  * Reverse byte order of a 16 bit word.
  685.  */
  686. static INLINE uint16_t
  687. util_bswap16(uint16_t n)
  688. {
  689.    return (n >> 8) |
  690.           (n << 8);
  691. }
  692.  
  693. static INLINE void*
  694. util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
  695. {
  696. #ifdef PIPE_ARCH_BIG_ENDIAN
  697.    size_t i, e;
  698.    assert(n % 4 == 0);
  699.  
  700.    for (i = 0, e = n / 4; i < e; i++) {
  701.       uint32_t * restrict d = (uint32_t* restrict)dest;
  702.       const uint32_t * restrict s = (const uint32_t* restrict)src;
  703.       d[i] = util_bswap32(s[i]);
  704.    }
  705.    return dest;
  706. #else
  707.    return memcpy(dest, src, n);
  708. #endif
  709. }
  710.  
  711. /**
  712.  * Clamp X to [MIN, MAX].
  713.  * This is a macro to allow float, int, uint, etc. types.
  714.  */
  715. #define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
  716.  
  717. #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
  718. #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
  719.  
  720. #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
  721. #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
  722.  
  723. #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
  724. #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
  725.  
  726.  
  727. /**
  728.  * Align a value, only works pot alignemnts.
  729.  */
  730. static INLINE int
  731. align(int value, int alignment)
  732. {
  733.    return (value + alignment - 1) & ~(alignment - 1);
  734. }
  735.  
  736. /**
  737.  * Works like align but on npot alignments.
  738.  */
  739. static INLINE size_t
  740. util_align_npot(size_t value, size_t alignment)
  741. {
  742.    if (value % alignment)
  743.       return value + (alignment - (value % alignment));
  744.    return value;
  745. }
  746.  
  747. static INLINE unsigned
  748. u_minify(unsigned value, unsigned levels)
  749. {
  750.     return MAX2(1, value >> levels);
  751. }
  752.  
  753. #ifndef COPY_4V
  754. #define COPY_4V( DST, SRC )         \
  755. do {                                \
  756.    (DST)[0] = (SRC)[0];             \
  757.    (DST)[1] = (SRC)[1];             \
  758.    (DST)[2] = (SRC)[2];             \
  759.    (DST)[3] = (SRC)[3];             \
  760. } while (0)
  761. #endif
  762.  
  763.  
  764. #ifndef COPY_4FV
  765. #define COPY_4FV( DST, SRC )  COPY_4V(DST, SRC)
  766. #endif
  767.  
  768.  
  769. #ifndef ASSIGN_4V
  770. #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
  771. do {                                     \
  772.    (DST)[0] = (V0);                      \
  773.    (DST)[1] = (V1);                      \
  774.    (DST)[2] = (V2);                      \
  775.    (DST)[3] = (V3);                      \
  776. } while (0)
  777. #endif
  778.  
  779.  
  780. static INLINE uint32_t
  781. util_unsigned_fixed(float value, unsigned frac_bits)
  782. {
  783.    return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
  784. }
  785.  
  786. static INLINE int32_t
  787. util_signed_fixed(float value, unsigned frac_bits)
  788. {
  789.    return (int32_t)(value * (1<<frac_bits));
  790. }
  791.  
  792. unsigned
  793. util_fpstate_get(void);
  794. unsigned
  795. util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
  796. void
  797. util_fpstate_set(unsigned fpstate);
  798.  
  799.  
  800.  
  801. #ifdef __cplusplus
  802. }
  803. #endif
  804.  
  805. #endif /* U_MATH_H */
  806.