2008-08-22 22:13:47 +01:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
s/Tungsten Graphics/VMware/
Tungsten Graphics Inc. was acquired by VMware Inc. in 2008. Leaving the
old copyright name is creating unnecessary confusion, hence this change.
This was the sed script I used:
$ cat tg2vmw.sed
# Run as:
#
# git reset --hard HEAD && find include scons src -type f -not -name 'sed*' -print0 | xargs -0 sed -i -f tg2vmw.sed
#
# Rename copyrights
s/Tungsten Gra\(ph\|hp\)ics,\? [iI]nc\.\?\(, Cedar Park\)\?\(, Austin\)\?\(, \(Texas\|TX\)\)\?\.\?/VMware, Inc./g
/Copyright/s/Tungsten Graphics\(,\? [iI]nc\.\)\?\(, Cedar Park\)\?\(, Austin\)\?\(, \(Texas\|TX\)\)\?\.\?/VMware, Inc./
s/TUNGSTEN GRAPHICS/VMWARE/g
# Rename emails
s/alanh@tungstengraphics.com/alanh@vmware.com/
s/jens@tungstengraphics.com/jowen@vmware.com/g
s/jrfonseca-at-tungstengraphics-dot-com/jfonseca-at-vmware-dot-com/
s/jrfonseca\?@tungstengraphics.com/jfonseca@vmware.com/g
s/keithw\?@tungstengraphics.com/keithw@vmware.com/g
s/michel@tungstengraphics.com/daenzer@vmware.com/g
s/thomas-at-tungstengraphics-dot-com/thellstom-at-vmware-dot-com/
s/zack@tungstengraphics.com/zackr@vmware.com/
# Remove dead links
s@Tungsten Graphics (http://www.tungstengraphics.com)@Tungsten Graphics@g
# C string src/gallium/state_trackers/vega/api_misc.c
s/"Tungsten Graphics, Inc"/"VMware, Inc"/
Reviewed-by: Brian Paul <brianp@vmware.com>
2014-01-17 16:27:50 +00:00
|
|
|
* Copyright 2008 VMware, Inc.
|
2008-08-22 22:13:47 +01:00
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
|
s/Tungsten Graphics/VMware/
Tungsten Graphics Inc. was acquired by VMware Inc. in 2008. Leaving the
old copyright name is creating unnecessary confusion, hence this change.
This was the sed script I used:
$ cat tg2vmw.sed
# Run as:
#
# git reset --hard HEAD && find include scons src -type f -not -name 'sed*' -print0 | xargs -0 sed -i -f tg2vmw.sed
#
# Rename copyrights
s/Tungsten Gra\(ph\|hp\)ics,\? [iI]nc\.\?\(, Cedar Park\)\?\(, Austin\)\?\(, \(Texas\|TX\)\)\?\.\?/VMware, Inc./g
/Copyright/s/Tungsten Graphics\(,\? [iI]nc\.\)\?\(, Cedar Park\)\?\(, Austin\)\?\(, \(Texas\|TX\)\)\?\.\?/VMware, Inc./
s/TUNGSTEN GRAPHICS/VMWARE/g
# Rename emails
s/alanh@tungstengraphics.com/alanh@vmware.com/
s/jens@tungstengraphics.com/jowen@vmware.com/g
s/jrfonseca-at-tungstengraphics-dot-com/jfonseca-at-vmware-dot-com/
s/jrfonseca\?@tungstengraphics.com/jfonseca@vmware.com/g
s/keithw\?@tungstengraphics.com/keithw@vmware.com/g
s/michel@tungstengraphics.com/daenzer@vmware.com/g
s/thomas-at-tungstengraphics-dot-com/thellstom-at-vmware-dot-com/
s/zack@tungstengraphics.com/zackr@vmware.com/
# Remove dead links
s@Tungsten Graphics (http://www.tungstengraphics.com)@Tungsten Graphics@g
# C string src/gallium/state_trackers/vega/api_misc.c
s/"Tungsten Graphics, Inc"/"VMware, Inc"/
Reviewed-by: Brian Paul <brianp@vmware.com>
2014-01-17 16:27:50 +00:00
|
|
|
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
|
2008-08-22 22:13:47 +01:00
|
|
|
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Math utilities and approximations for common math functions.
|
|
|
|
* Reduced precision is usually acceptable in shaders...
|
2008-08-22 22:51:38 +01:00
|
|
|
*
|
|
|
|
* "fast" is used in the names of functions which are low-precision,
|
|
|
|
* or at least lower-precision than the normal C lib functions.
|
2008-08-22 22:13:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef U_MATH_H
|
|
|
|
#define U_MATH_H
|
|
|
|
|
|
|
|
|
2008-08-22 22:51:38 +01:00
|
|
|
#include "pipe/p_compiler.h"
|
2008-08-22 22:13:47 +01:00
|
|
|
|
2015-02-23 20:33:50 +00:00
|
|
|
#include "c99_math.h"
|
2015-05-21 02:23:06 +01:00
|
|
|
#include <assert.h>
|
2013-07-20 00:31:18 +01:00
|
|
|
#include <float.h>
|
2008-08-25 00:48:55 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2011-12-09 20:05:27 +00:00
|
|
|
#ifdef PIPE_OS_UNIX
|
|
|
|
#include <strings.h> /* for ffs */
|
|
|
|
#endif
|
|
|
|
|
2008-08-25 00:48:55 +01:00
|
|
|
|
2015-03-05 00:37:29 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-10-09 12:10:07 +01:00
|
|
|
#ifndef M_SQRT2
|
|
|
|
#define M_SQRT2 1.41421356237309504880
|
|
|
|
#endif
|
|
|
|
|
2008-09-30 17:13:40 +01:00
|
|
|
#define POW2_TABLE_SIZE_LOG2 9
|
|
|
|
#define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
|
|
|
|
#define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
|
|
|
|
#define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
|
2008-08-22 22:13:47 +01:00
|
|
|
extern float pow2_table[POW2_TABLE_SIZE];
|
|
|
|
|
|
|
|
|
2009-07-31 16:30:56 +01:00
|
|
|
/**
|
|
|
|
* Initialize math module. This should be called before using any
|
|
|
|
* other functions in this module.
|
|
|
|
*/
|
2008-08-22 22:13:47 +01:00
|
|
|
extern void
|
|
|
|
util_init_math(void);
|
|
|
|
|
|
|
|
|
2008-08-25 00:48:55 +01:00
|
|
|
union fi {
|
|
|
|
float f;
|
2008-09-30 17:13:40 +01:00
|
|
|
int32_t i;
|
|
|
|
uint32_t ui;
|
2008-08-25 00:48:55 +01:00
|
|
|
};
|
2008-08-22 22:13:47 +01:00
|
|
|
|
|
|
|
|
2012-05-22 14:53:30 +01:00
|
|
|
union di {
|
|
|
|
double d;
|
|
|
|
int64_t i;
|
|
|
|
uint64_t ui;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-10-29 20:36:41 +00:00
|
|
|
/**
|
|
|
|
* Extract the IEEE float32 exponent.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline signed
|
2014-08-15 23:21:15 +01:00
|
|
|
util_get_float32_exponent(float x)
|
|
|
|
{
|
2013-10-29 20:36:41 +00:00
|
|
|
union fi f;
|
|
|
|
|
|
|
|
f.f = x;
|
|
|
|
|
|
|
|
return ((f.ui >> 23) & 0xff) - 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 22:13:47 +01:00
|
|
|
/**
|
2008-09-30 17:13:40 +01:00
|
|
|
* Fast version of 2^x
|
2008-08-22 22:13:47 +01:00
|
|
|
* Identity: exp2(a + b) = exp2(a) * exp2(b)
|
2008-09-30 17:13:40 +01:00
|
|
|
* Let ipart = int(x)
|
|
|
|
* Let fpart = x - ipart;
|
|
|
|
* So, exp2(x) = exp2(ipart) * exp2(fpart)
|
2008-08-22 22:13:47 +01:00
|
|
|
* Compute exp2(ipart) with i << ipart
|
|
|
|
* Compute exp2(fpart) with lookup table.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline float
|
2008-09-30 17:13:40 +01:00
|
|
|
util_fast_exp2(float x)
|
2008-08-22 22:13:47 +01:00
|
|
|
{
|
2008-09-30 17:13:40 +01:00
|
|
|
int32_t ipart;
|
|
|
|
float fpart, mpart;
|
|
|
|
union fi epart;
|
2009-08-01 10:03:30 +01:00
|
|
|
|
2008-09-30 17:13:40 +01:00
|
|
|
if(x > 129.00000f)
|
|
|
|
return 3.402823466e+38f;
|
2009-08-01 10:03:30 +01:00
|
|
|
|
2009-07-31 16:30:56 +01:00
|
|
|
if (x < -126.99999f)
|
2008-09-30 17:13:40 +01:00
|
|
|
return 0.0f;
|
|
|
|
|
|
|
|
ipart = (int32_t) x;
|
|
|
|
fpart = x - (float) ipart;
|
2009-08-01 10:03:30 +01:00
|
|
|
|
2008-09-30 17:13:40 +01:00
|
|
|
/* same as
|
|
|
|
* epart.f = (float) (1 << ipart)
|
2009-07-31 16:30:56 +01:00
|
|
|
* but faster and without integer overflow for ipart > 31
|
|
|
|
*/
|
2008-09-30 17:13:40 +01:00
|
|
|
epart.i = (ipart + 127 ) << 23;
|
2009-08-01 10:03:30 +01:00
|
|
|
|
2008-09-30 17:13:40 +01:00
|
|
|
mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
|
2009-08-01 10:03:30 +01:00
|
|
|
|
2008-09-30 17:13:40 +01:00
|
|
|
return epart.f * mpart;
|
2008-08-22 22:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-09-30 17:13:40 +01:00
|
|
|
* Fast approximation to exp(x).
|
2008-08-22 22:13:47 +01:00
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline float
|
2008-09-30 17:13:40 +01:00
|
|
|
util_fast_exp(float x)
|
2008-08-22 22:13:47 +01:00
|
|
|
{
|
2008-09-30 17:13:40 +01:00
|
|
|
const float k = 1.44269f; /* = log2(e) */
|
|
|
|
return util_fast_exp2(k * x);
|
2008-08-22 22:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-14 19:55:05 +00:00
|
|
|
#define LOG2_TABLE_SIZE_LOG2 16
|
|
|
|
#define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
|
|
|
|
#define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
|
2008-09-30 17:13:40 +01:00
|
|
|
extern float log2_table[LOG2_TABLE_SIZE];
|
|
|
|
|
|
|
|
|
2009-07-31 16:30:56 +01:00
|
|
|
/**
|
|
|
|
* Fast approximation to log2(x).
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline float
|
2008-09-30 17:13:40 +01:00
|
|
|
util_fast_log2(float x)
|
2008-08-22 22:13:47 +01:00
|
|
|
{
|
|
|
|
union fi num;
|
2008-09-30 17:13:40 +01:00
|
|
|
float epart, mpart;
|
|
|
|
num.f = x;
|
|
|
|
epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
|
2008-11-14 19:55:05 +00:00
|
|
|
/* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
|
|
|
|
mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
|
2008-09-30 17:13:40 +01:00
|
|
|
return epart + mpart;
|
2008-08-22 22:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-31 16:30:56 +01:00
|
|
|
/**
|
|
|
|
* Fast approximation to x^y.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline float
|
2008-08-22 22:13:47 +01:00
|
|
|
util_fast_pow(float x, float y)
|
|
|
|
{
|
|
|
|
return util_fast_exp2(util_fast_log2(x) * y);
|
|
|
|
}
|
|
|
|
|
2009-08-21 18:07:35 +01:00
|
|
|
/* Note that this counts zero as a power of two.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline boolean
|
2009-08-21 18:07:35 +01:00
|
|
|
util_is_power_of_two( unsigned v )
|
|
|
|
{
|
|
|
|
return (v & (v-1)) == 0;
|
|
|
|
}
|
|
|
|
|
2008-08-22 22:13:47 +01:00
|
|
|
|
2008-08-22 22:51:38 +01:00
|
|
|
/**
|
|
|
|
* Floor(x), returned as int.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline int
|
2008-08-22 22:51:38 +01:00
|
|
|
util_ifloor(float f)
|
|
|
|
{
|
|
|
|
int ai, bi;
|
|
|
|
double af, bf;
|
|
|
|
union fi u;
|
2009-07-31 16:30:56 +01:00
|
|
|
af = (3 << 22) + 0.5 + (double) f;
|
|
|
|
bf = (3 << 22) + 0.5 - (double) f;
|
2008-08-22 22:51:38 +01:00
|
|
|
u.f = (float) af; ai = u.i;
|
|
|
|
u.f = (float) bf; bi = u.i;
|
|
|
|
return (ai - bi) >> 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Round float to nearest int.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline int
|
2008-08-22 22:51:38 +01:00
|
|
|
util_iround(float f)
|
|
|
|
{
|
|
|
|
#if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
|
|
|
|
int r;
|
|
|
|
__asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
|
|
|
|
return r;
|
|
|
|
#elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
|
|
|
|
int r;
|
|
|
|
_asm {
|
2009-07-31 16:30:56 +01:00
|
|
|
fld f
|
|
|
|
fistp r
|
|
|
|
}
|
2008-08-22 22:51:38 +01:00
|
|
|
return r;
|
|
|
|
#else
|
|
|
|
if (f >= 0.0f)
|
|
|
|
return (int) (f + 0.5f);
|
|
|
|
else
|
|
|
|
return (int) (f - 0.5f);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-27 16:16:11 +01:00
|
|
|
/**
|
|
|
|
* Approximate floating point comparison
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline boolean
|
2010-05-27 16:16:11 +01:00
|
|
|
util_is_approx(float a, float b, float tol)
|
|
|
|
{
|
2015-07-13 02:01:42 +01:00
|
|
|
return fabsf(b - a) <= tol;
|
2010-05-27 16:16:11 +01:00
|
|
|
}
|
|
|
|
|
2008-08-22 22:51:38 +01:00
|
|
|
|
2009-03-31 23:06:19 +01:00
|
|
|
/**
|
2012-05-22 14:53:30 +01:00
|
|
|
* util_is_X_inf_or_nan = test if x is NaN or +/- Inf
|
|
|
|
* util_is_X_nan = test if x is NaN
|
|
|
|
* util_X_inf_sign = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
|
|
|
|
*
|
|
|
|
* NaN can be checked with x != x, however this fails with the fast math flag
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Single-float
|
2009-03-31 23:06:19 +01:00
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline boolean
|
2009-03-31 23:06:19 +01:00
|
|
|
util_is_inf_or_nan(float x)
|
|
|
|
{
|
|
|
|
union fi tmp;
|
|
|
|
tmp.f = x;
|
2012-05-22 14:53:30 +01:00
|
|
|
return (tmp.ui & 0x7f800000) == 0x7f800000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline boolean
|
2012-05-22 14:53:30 +01:00
|
|
|
util_is_nan(float x)
|
|
|
|
{
|
|
|
|
union fi tmp;
|
|
|
|
tmp.f = x;
|
|
|
|
return (tmp.ui & 0x7fffffff) > 0x7f800000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline int
|
2012-05-22 14:53:30 +01:00
|
|
|
util_inf_sign(float x)
|
|
|
|
{
|
|
|
|
union fi tmp;
|
|
|
|
tmp.f = x;
|
|
|
|
if ((tmp.ui & 0x7fffffff) != 0x7f800000) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (x < 0) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Double-float
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline boolean
|
2012-05-22 14:53:30 +01:00
|
|
|
util_is_double_inf_or_nan(double x)
|
|
|
|
{
|
|
|
|
union di tmp;
|
|
|
|
tmp.d = x;
|
2012-07-02 12:08:01 +01:00
|
|
|
return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL;
|
2012-05-22 14:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline boolean
|
2012-05-22 14:53:30 +01:00
|
|
|
util_is_double_nan(double x)
|
|
|
|
{
|
|
|
|
union di tmp;
|
|
|
|
tmp.d = x;
|
2012-07-02 12:08:01 +01:00
|
|
|
return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL;
|
2012-05-22 14:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline int
|
2012-05-22 14:53:30 +01:00
|
|
|
util_double_inf_sign(double x)
|
|
|
|
{
|
|
|
|
union di tmp;
|
|
|
|
tmp.d = x;
|
2012-07-02 12:08:01 +01:00
|
|
|
if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) {
|
2012-05-22 14:53:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (x < 0) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Half-float
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline boolean
|
2012-05-22 14:53:30 +01:00
|
|
|
util_is_half_inf_or_nan(int16_t x)
|
|
|
|
{
|
|
|
|
return (x & 0x7c00) == 0x7c00;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline boolean
|
2012-05-22 14:53:30 +01:00
|
|
|
util_is_half_nan(int16_t x)
|
|
|
|
{
|
|
|
|
return (x & 0x7fff) > 0x7c00;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline int
|
2012-05-22 14:53:30 +01:00
|
|
|
util_half_inf_sign(int16_t x)
|
|
|
|
{
|
|
|
|
if ((x & 0x7fff) != 0x7c00) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (x < 0) ? -1 : 1;
|
2009-03-31 23:06:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-25 00:48:55 +01:00
|
|
|
/**
|
|
|
|
* Find first bit set in word. Least significant bit is 1.
|
|
|
|
* Return 0 if no bits set.
|
|
|
|
*/
|
2012-01-12 15:41:24 +00:00
|
|
|
#ifndef FFS_DEFINED
|
|
|
|
#define FFS_DEFINED 1
|
|
|
|
|
2015-02-26 11:47:46 +00:00
|
|
|
#if defined(_MSC_VER) && (_M_IX86 || _M_AMD64 || _M_IA64)
|
2009-06-01 16:58:34 +01:00
|
|
|
unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
|
|
|
|
#pragma intrinsic(_BitScanForward)
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline
|
2009-03-25 20:58:38 +00:00
|
|
|
unsigned long ffs( unsigned long u )
|
|
|
|
{
|
|
|
|
unsigned long i;
|
2009-07-31 16:30:56 +01:00
|
|
|
if (_BitScanForward(&i, u))
|
2009-03-25 20:58:38 +00:00
|
|
|
return i + 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline
|
2008-08-25 00:48:55 +01:00
|
|
|
unsigned ffs( unsigned u )
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2009-07-31 16:30:56 +01:00
|
|
|
if (u == 0) {
|
2008-08-25 00:48:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__asm bsf eax, [u]
|
|
|
|
__asm inc eax
|
|
|
|
__asm mov [i], eax
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
2015-03-17 01:49:39 +00:00
|
|
|
#elif defined(__MINGW32__) || defined(PIPE_OS_ANDROID) || \
|
|
|
|
defined(HAVE___BUILTIN_FFS)
|
2009-01-30 14:59:32 +00:00
|
|
|
#define ffs __builtin_ffs
|
|
|
|
#endif
|
|
|
|
|
2015-09-01 23:29:17 +01:00
|
|
|
#ifdef HAVE___BUILTIN_FFSLL
|
|
|
|
#define ffsll __builtin_ffsll
|
|
|
|
#else
|
|
|
|
static inline int
|
|
|
|
ffsll(long long int val)
|
|
|
|
{
|
|
|
|
int bit;
|
|
|
|
|
|
|
|
bit = ffs((unsigned) (val & 0xffffffff));
|
|
|
|
if (bit != 0)
|
|
|
|
return bit;
|
|
|
|
|
|
|
|
bit = ffs((unsigned) (val >> 32));
|
|
|
|
if (bit != 0)
|
|
|
|
return 32 + bit;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-12 15:41:24 +00:00
|
|
|
#endif /* FFS_DEFINED */
|
|
|
|
|
2015-03-17 01:49:39 +00:00
|
|
|
/**
|
|
|
|
* Find first bit set in long long. Least significant bit is 1.
|
|
|
|
* Return 0 if no bits set.
|
|
|
|
*/
|
|
|
|
#ifndef FFSLL_DEFINED
|
|
|
|
#define FFSLL_DEFINED 1
|
|
|
|
|
|
|
|
#if defined(__MINGW32__) || defined(PIPE_OS_ANDROID) || \
|
|
|
|
defined(HAVE___BUILTIN_FFSLL)
|
|
|
|
#define ffsll __builtin_ffsll
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* FFSLL_DEFINED */
|
|
|
|
|
2012-03-29 23:21:11 +01:00
|
|
|
/**
|
|
|
|
* Find last bit set in a word. The least significant bit is 1.
|
|
|
|
* Return 0 if no bits are set.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline unsigned
|
2014-08-15 23:21:15 +01:00
|
|
|
util_last_bit(unsigned u)
|
2012-03-29 23:21:11 +01:00
|
|
|
{
|
2015-02-21 02:46:43 +00:00
|
|
|
#if defined(HAVE___BUILTIN_CLZ)
|
2012-03-29 16:51:50 +01:00
|
|
|
return u == 0 ? 0 : 32 - __builtin_clz(u);
|
|
|
|
#else
|
2012-03-29 23:21:11 +01:00
|
|
|
unsigned r = 0;
|
|
|
|
while (u) {
|
|
|
|
r++;
|
|
|
|
u >>= 1;
|
|
|
|
}
|
|
|
|
return r;
|
2012-03-29 16:51:50 +01:00
|
|
|
#endif
|
2012-03-29 23:21:11 +01:00
|
|
|
}
|
|
|
|
|
2015-05-10 19:35:15 +01:00
|
|
|
/**
|
|
|
|
* Find last bit set in a word. The least significant bit is 1.
|
|
|
|
* Return 0 if no bits are set.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline unsigned
|
2015-05-10 19:35:15 +01:00
|
|
|
util_last_bit64(uint64_t u)
|
|
|
|
{
|
|
|
|
#if defined(HAVE___BUILTIN_CLZLL)
|
|
|
|
return u == 0 ? 0 : 64 - __builtin_clzll(u);
|
|
|
|
#else
|
|
|
|
unsigned r = 0;
|
|
|
|
while (u) {
|
|
|
|
r++;
|
|
|
|
u >>= 1;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-04-25 23:07:41 +01:00
|
|
|
/**
|
|
|
|
* Find last bit in a word that does not match the sign bit. The least
|
|
|
|
* significant bit is 1.
|
|
|
|
* Return 0 if no bits are set.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline unsigned
|
2014-08-15 23:21:15 +01:00
|
|
|
util_last_bit_signed(int i)
|
2014-04-25 23:07:41 +01:00
|
|
|
{
|
|
|
|
if (i >= 0)
|
|
|
|
return util_last_bit(i);
|
|
|
|
else
|
|
|
|
return util_last_bit(~(unsigned)i);
|
|
|
|
}
|
2008-08-25 00:48:55 +01:00
|
|
|
|
2011-09-21 18:22:43 +01:00
|
|
|
/* Destructively loop over all of the bits in a mask as in:
|
|
|
|
*
|
|
|
|
* while (mymask) {
|
|
|
|
* int i = u_bit_scan(&mymask);
|
|
|
|
* ... process element i
|
|
|
|
* }
|
2014-08-15 23:21:15 +01:00
|
|
|
*
|
2011-09-21 18:22:43 +01:00
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline int
|
2014-08-15 23:21:15 +01:00
|
|
|
u_bit_scan(unsigned *mask)
|
2011-09-21 18:22:43 +01:00
|
|
|
{
|
|
|
|
int i = ffs(*mask) - 1;
|
|
|
|
*mask &= ~(1 << i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:21:41 +00:00
|
|
|
#ifndef _MSC_VER
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline int
|
2015-01-31 16:15:16 +00:00
|
|
|
u_bit_scan64(uint64_t *mask)
|
|
|
|
{
|
|
|
|
int i = ffsll(*mask) - 1;
|
|
|
|
*mask &= ~(1llu << i);
|
|
|
|
return i;
|
|
|
|
}
|
2015-02-04 15:21:41 +00:00
|
|
|
#endif
|
2011-09-21 18:22:43 +01:00
|
|
|
|
2015-08-28 19:53:08 +01:00
|
|
|
/* For looping over a bitmask when you want to loop over consecutive bits
|
|
|
|
* manually, for example:
|
|
|
|
*
|
|
|
|
* while (mask) {
|
|
|
|
* int start, count, i;
|
|
|
|
*
|
|
|
|
* u_bit_scan_consecutive_range(&mask, &start, &count);
|
|
|
|
*
|
|
|
|
* for (i = 0; i < count; i++)
|
|
|
|
* ... process element (start+i)
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
u_bit_scan_consecutive_range(unsigned *mask, int *start, int *count)
|
|
|
|
{
|
|
|
|
*start = ffs(*mask) - 1;
|
|
|
|
*count = ffs(~(*mask >> *start)) - 1;
|
|
|
|
*mask &= ~(((1 << *count) - 1) << *start);
|
|
|
|
}
|
|
|
|
|
2008-08-25 00:48:55 +01:00
|
|
|
/**
|
|
|
|
* Return float bits.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline unsigned
|
2008-08-25 00:48:55 +01:00
|
|
|
fui( float f )
|
|
|
|
{
|
|
|
|
union fi fi;
|
|
|
|
fi.f = f;
|
|
|
|
return fi.ui;
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline float
|
2014-06-27 15:09:32 +01:00
|
|
|
uif(uint32_t ui)
|
|
|
|
{
|
2015-02-19 16:29:18 +00:00
|
|
|
union fi fi;
|
|
|
|
fi.ui = ui;
|
|
|
|
return fi.f;
|
2014-06-27 15:09:32 +01:00
|
|
|
}
|
|
|
|
|
2008-08-25 00:48:55 +01:00
|
|
|
|
2009-07-31 16:30:56 +01:00
|
|
|
/**
|
|
|
|
* Convert ubyte to float in [0, 1].
|
|
|
|
* XXX a 256-entry lookup table would be slightly faster.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline float
|
2008-08-25 00:48:55 +01:00
|
|
|
ubyte_to_float(ubyte ub)
|
|
|
|
{
|
|
|
|
return (float) ub * (1.0f / 255.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert float in [0,1] to ubyte in [0,255] with clamping.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline ubyte
|
2008-08-25 00:48:55 +01:00
|
|
|
float_to_ubyte(float f)
|
|
|
|
{
|
|
|
|
union fi tmp;
|
|
|
|
|
|
|
|
tmp.f = f;
|
|
|
|
if (tmp.i < 0) {
|
|
|
|
return (ubyte) 0;
|
|
|
|
}
|
2013-06-13 04:03:02 +01:00
|
|
|
else if (tmp.i >= 0x3f800000 /* 1.0f */) {
|
2008-08-25 00:48:55 +01:00
|
|
|
return (ubyte) 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
|
|
|
|
return (ubyte) tmp.i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline float
|
2011-03-02 04:34:14 +00:00
|
|
|
byte_to_float_tex(int8_t b)
|
|
|
|
{
|
|
|
|
return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline int8_t
|
2011-03-02 04:34:14 +00:00
|
|
|
float_to_byte_tex(float f)
|
|
|
|
{
|
|
|
|
return (int8_t) (127.0F * f);
|
|
|
|
}
|
2008-08-25 00:48:55 +01:00
|
|
|
|
2009-08-01 10:03:30 +01:00
|
|
|
/**
|
|
|
|
* Calc log base 2
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline unsigned
|
2009-08-01 10:03:30 +01:00
|
|
|
util_logbase2(unsigned n)
|
|
|
|
{
|
2015-02-21 02:46:43 +00:00
|
|
|
#if defined(HAVE___BUILTIN_CLZ)
|
2011-06-08 22:00:54 +01:00
|
|
|
return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
|
|
|
|
#else
|
2011-06-02 15:43:07 +01:00
|
|
|
unsigned pos = 0;
|
|
|
|
if (n >= 1<<16) { n >>= 16; pos += 16; }
|
|
|
|
if (n >= 1<< 8) { n >>= 8; pos += 8; }
|
|
|
|
if (n >= 1<< 4) { n >>= 4; pos += 4; }
|
|
|
|
if (n >= 1<< 2) { n >>= 2; pos += 2; }
|
|
|
|
if (n >= 1<< 1) { pos += 1; }
|
|
|
|
return pos;
|
2011-06-08 22:00:54 +01:00
|
|
|
#endif
|
2009-08-01 10:03:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-27 19:40:14 +01:00
|
|
|
/**
|
|
|
|
* Returns the smallest power of two >= x
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline unsigned
|
2009-09-27 19:40:14 +01:00
|
|
|
util_next_power_of_two(unsigned x)
|
|
|
|
{
|
2015-02-21 02:46:43 +00:00
|
|
|
#if defined(HAVE___BUILTIN_CLZ)
|
2011-06-08 22:00:54 +01:00
|
|
|
if (x <= 1)
|
|
|
|
return 1;
|
2009-09-27 19:40:14 +01:00
|
|
|
|
2011-06-08 22:00:54 +01:00
|
|
|
return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
|
|
|
|
#else
|
|
|
|
unsigned val = x;
|
2009-09-27 19:40:14 +01:00
|
|
|
|
2011-06-08 22:00:54 +01:00
|
|
|
if (x <= 1)
|
|
|
|
return 1;
|
2009-09-27 19:40:14 +01:00
|
|
|
|
2011-06-08 22:00:54 +01:00
|
|
|
if (util_is_power_of_two(x))
|
|
|
|
return x;
|
|
|
|
|
|
|
|
val--;
|
|
|
|
val = (val >> 1) | val;
|
|
|
|
val = (val >> 2) | val;
|
|
|
|
val = (val >> 4) | val;
|
|
|
|
val = (val >> 8) | val;
|
|
|
|
val = (val >> 16) | val;
|
|
|
|
val++;
|
|
|
|
return val;
|
|
|
|
#endif
|
2009-09-27 19:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-24 01:04:22 +00:00
|
|
|
/**
|
|
|
|
* Return number of bits set in n.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline unsigned
|
2009-11-24 01:04:22 +00:00
|
|
|
util_bitcount(unsigned n)
|
|
|
|
{
|
2015-02-21 02:46:43 +00:00
|
|
|
#if defined(HAVE___BUILTIN_POPCOUNT)
|
2009-11-24 01:04:22 +00:00
|
|
|
return __builtin_popcount(n);
|
|
|
|
#else
|
2009-11-28 18:13:51 +00:00
|
|
|
/* K&R classic bitcount.
|
|
|
|
*
|
|
|
|
* For each iteration, clear the LSB from the bitfield.
|
|
|
|
* Requires only one iteration per set bit, instead of
|
|
|
|
* one iteration per bit less than highest set bit.
|
|
|
|
*/
|
2015-02-27 19:49:29 +00:00
|
|
|
unsigned bits;
|
|
|
|
for (bits = 0; n; bits++) {
|
2009-11-28 18:13:51 +00:00
|
|
|
n &= n - 1;
|
2009-11-24 01:04:22 +00:00
|
|
|
}
|
|
|
|
return bits;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:26:21 +01:00
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline unsigned
|
2014-09-24 17:26:21 +01:00
|
|
|
util_bitcount64(uint64_t n)
|
|
|
|
{
|
|
|
|
#ifdef HAVE___BUILTIN_POPCOUNTLL
|
|
|
|
return __builtin_popcountll(n);
|
|
|
|
#else
|
|
|
|
return util_bitcount(n) + util_bitcount(n >> 32);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-25 23:07:41 +01:00
|
|
|
/**
|
|
|
|
* Reverse bits in n
|
|
|
|
* Algorithm taken from:
|
|
|
|
* http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline unsigned
|
2014-04-25 23:07:41 +01:00
|
|
|
util_bitreverse(unsigned n)
|
|
|
|
{
|
|
|
|
n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
|
|
|
|
n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
|
|
|
|
n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
|
|
|
|
n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
|
|
|
|
n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
|
|
|
|
return n;
|
|
|
|
}
|
2009-11-24 01:04:22 +00:00
|
|
|
|
2011-11-02 17:21:48 +00:00
|
|
|
/**
|
|
|
|
* Convert from little endian to CPU byte order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef PIPE_ARCH_BIG_ENDIAN
|
2014-02-19 22:17:33 +00:00
|
|
|
#define util_le64_to_cpu(x) util_bswap64(x)
|
2011-11-02 17:21:48 +00:00
|
|
|
#define util_le32_to_cpu(x) util_bswap32(x)
|
|
|
|
#define util_le16_to_cpu(x) util_bswap16(x)
|
|
|
|
#else
|
2014-02-19 22:17:33 +00:00
|
|
|
#define util_le64_to_cpu(x) (x)
|
2011-11-02 17:21:48 +00:00
|
|
|
#define util_le32_to_cpu(x) (x)
|
|
|
|
#define util_le16_to_cpu(x) (x)
|
|
|
|
#endif
|
|
|
|
|
2014-02-20 15:46:28 +00:00
|
|
|
#define util_cpu_to_le64(x) util_le64_to_cpu(x)
|
|
|
|
#define util_cpu_to_le32(x) util_le32_to_cpu(x)
|
|
|
|
#define util_cpu_to_le16(x) util_le16_to_cpu(x)
|
2011-11-02 17:21:48 +00:00
|
|
|
|
2009-11-26 15:58:59 +00:00
|
|
|
/**
|
|
|
|
* Reverse byte order of a 32 bit word.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline uint32_t
|
2009-11-26 15:58:59 +00:00
|
|
|
util_bswap32(uint32_t n)
|
|
|
|
{
|
2015-02-21 02:46:43 +00:00
|
|
|
#if defined(HAVE___BUILTIN_BSWAP32)
|
2009-11-26 15:58:59 +00:00
|
|
|
return __builtin_bswap32(n);
|
|
|
|
#else
|
|
|
|
return (n >> 24) |
|
|
|
|
((n >> 8) & 0x0000ff00) |
|
|
|
|
((n << 8) & 0x00ff0000) |
|
|
|
|
(n << 24);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-19 22:17:33 +00:00
|
|
|
/**
|
|
|
|
* Reverse byte order of a 64bit word.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline uint64_t
|
2014-02-19 22:17:33 +00:00
|
|
|
util_bswap64(uint64_t n)
|
|
|
|
{
|
|
|
|
#if defined(HAVE___BUILTIN_BSWAP64)
|
|
|
|
return __builtin_bswap64(n);
|
|
|
|
#else
|
2014-07-22 22:07:03 +01:00
|
|
|
return ((uint64_t)util_bswap32((uint32_t)n) << 32) |
|
2014-02-19 22:17:33 +00:00
|
|
|
util_bswap32((n >> 32));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-11-26 15:58:59 +00:00
|
|
|
|
2010-02-26 15:37:18 +00:00
|
|
|
/**
|
|
|
|
* Reverse byte order of a 16 bit word.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline uint16_t
|
2010-02-26 15:37:18 +00:00
|
|
|
util_bswap16(uint16_t n)
|
|
|
|
{
|
|
|
|
return (n >> 8) |
|
|
|
|
(n << 8);
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline void*
|
2014-07-18 20:55:08 +01:00
|
|
|
util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
|
|
|
|
{
|
|
|
|
#ifdef PIPE_ARCH_BIG_ENDIAN
|
|
|
|
size_t i, e;
|
2014-07-28 21:40:05 +01:00
|
|
|
assert(n % 4 == 0);
|
2014-07-18 20:55:08 +01:00
|
|
|
|
|
|
|
for (i = 0, e = n / 4; i < e; i++) {
|
|
|
|
uint32_t * restrict d = (uint32_t* restrict)dest;
|
|
|
|
const uint32_t * restrict s = (const uint32_t* restrict)src;
|
|
|
|
d[i] = util_bswap32(s[i]);
|
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
#else
|
|
|
|
return memcpy(dest, src, n);
|
|
|
|
#endif
|
|
|
|
}
|
2010-02-26 15:37:18 +00:00
|
|
|
|
2009-07-31 16:30:56 +01:00
|
|
|
/**
|
|
|
|
* Clamp X to [MIN, MAX].
|
|
|
|
* This is a macro to allow float, int, uint, etc. types.
|
|
|
|
*/
|
2008-08-25 00:48:55 +01:00
|
|
|
#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
|
|
|
|
|
|
|
|
#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
|
|
|
|
#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
|
|
|
|
|
2011-04-27 11:54:24 +01:00
|
|
|
#define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
|
|
|
|
#define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
|
2009-11-20 22:08:58 +00:00
|
|
|
|
2011-04-27 11:54:24 +01:00
|
|
|
#define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
|
|
|
|
#define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
|
2010-08-26 20:03:03 +01:00
|
|
|
|
2008-08-25 00:48:55 +01:00
|
|
|
|
2010-06-13 15:07:49 +01:00
|
|
|
/**
|
|
|
|
* Align a value, only works pot alignemnts.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline int
|
2008-08-25 00:48:55 +01:00
|
|
|
align(int value, int alignment)
|
|
|
|
{
|
|
|
|
return (value + alignment - 1) & ~(alignment - 1);
|
|
|
|
}
|
|
|
|
|
2010-06-13 15:07:49 +01:00
|
|
|
/**
|
|
|
|
* Works like align but on npot alignments.
|
|
|
|
*/
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline size_t
|
2010-06-13 15:07:49 +01:00
|
|
|
util_align_npot(size_t value, size_t alignment)
|
|
|
|
{
|
|
|
|
if (value % alignment)
|
|
|
|
return value + (alignment - (value % alignment));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline unsigned
|
2009-11-19 22:39:34 +00:00
|
|
|
u_minify(unsigned value, unsigned levels)
|
2009-08-08 04:16:39 +01:00
|
|
|
{
|
2009-11-19 22:39:34 +00:00
|
|
|
return MAX2(1, value >> levels);
|
2009-08-08 04:16:39 +01:00
|
|
|
}
|
2008-08-25 00:48:55 +01:00
|
|
|
|
|
|
|
#ifndef COPY_4V
|
|
|
|
#define COPY_4V( DST, SRC ) \
|
|
|
|
do { \
|
|
|
|
(DST)[0] = (SRC)[0]; \
|
|
|
|
(DST)[1] = (SRC)[1]; \
|
|
|
|
(DST)[2] = (SRC)[2]; \
|
|
|
|
(DST)[3] = (SRC)[3]; \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef COPY_4FV
|
|
|
|
#define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ASSIGN_4V
|
|
|
|
#define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
|
|
|
|
do { \
|
|
|
|
(DST)[0] = (V0); \
|
|
|
|
(DST)[1] = (V1); \
|
|
|
|
(DST)[2] = (V2); \
|
|
|
|
(DST)[3] = (V3); \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline uint32_t
|
2014-08-15 23:21:15 +01:00
|
|
|
util_unsigned_fixed(float value, unsigned frac_bits)
|
2009-10-31 20:05:19 +00:00
|
|
|
{
|
2010-01-01 19:39:09 +00:00
|
|
|
return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
|
2009-10-31 20:05:19 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline int32_t
|
2014-08-15 23:21:15 +01:00
|
|
|
util_signed_fixed(float value, unsigned frac_bits)
|
2009-10-31 20:05:19 +00:00
|
|
|
{
|
2010-01-01 19:39:09 +00:00
|
|
|
return (int32_t)(value * (1<<frac_bits));
|
2009-10-31 20:05:19 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 04:45:55 +01:00
|
|
|
unsigned
|
|
|
|
util_fpstate_get(void);
|
|
|
|
unsigned
|
|
|
|
util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
|
|
|
|
void
|
|
|
|
util_fpstate_set(unsigned fpstate);
|
|
|
|
|
2009-10-31 20:05:19 +00:00
|
|
|
|
|
|
|
|
2008-08-22 22:53:28 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-08-22 22:13:47 +01:00
|
|
|
#endif /* U_MATH_H */
|