#define U_MATH_H
-#include "pipe/p_compiler.h"
-
#include "c99_math.h"
#include <assert.h>
#include <float.h>
#include <stdarg.h>
#include "bitscan.h"
+#include "u_endian.h" /* for UTIL_ARCH_BIG_ENDIAN */
#ifdef __cplusplus
extern "C" {
/**
* Approximate floating point comparison
*/
-static inline boolean
+static inline bool
util_is_approx(float a, float b, float tol)
{
return fabsf(b - a) <= tol;
/**
* Single-float
*/
-static inline boolean
+static inline bool
util_is_inf_or_nan(float x)
{
union fi tmp;
}
-static inline boolean
+static inline bool
util_is_nan(float x)
{
union fi tmp;
/**
* Double-float
*/
-static inline boolean
+static inline bool
util_is_double_inf_or_nan(double x)
{
union di tmp;
}
-static inline boolean
+static inline bool
util_is_double_nan(double x)
{
union di tmp;
/**
* Half-float
*/
-static inline boolean
+static inline bool
util_is_half_inf_or_nan(int16_t x)
{
return (x & 0x7c00) == 0x7c00;
}
-static inline boolean
+static inline bool
util_is_half_nan(int16_t x)
{
return (x & 0x7fff) > 0x7c00;
/**
- * Convert ubyte to float in [0, 1].
+ * Convert uint8_t to float in [0, 1].
*/
static inline float
-ubyte_to_float(ubyte ub)
+ubyte_to_float(uint8_t ub)
{
return (float) ub * (1.0f / 255.0f);
}
/**
- * Convert float in [0,1] to ubyte in [0,255] with clamping.
+ * Convert float in [0,1] to uint8_t in [0,255] with clamping.
*/
-static inline ubyte
+static inline uint8_t
float_to_ubyte(float f)
{
/* return 0 for NaN too */
if (!(f > 0.0f)) {
- return (ubyte) 0;
+ return (uint8_t) 0;
}
else if (f >= 1.0f) {
- return (ubyte) 255;
+ return (uint8_t) 255;
}
else {
union fi tmp;
tmp.f = f;
tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
- return (ubyte) tmp.i;
+ return (uint8_t) tmp.i;
+ }
+}
+
+/**
+ * Convert uint16_t to float in [0, 1].
+ */
+static inline float
+ushort_to_float(uint16_t us)
+{
+ return (float) us * (1.0f / 65535.0f);
+}
+
+
+/**
+ * Convert float in [0,1] to uint16_t in [0,65535] with clamping.
+ */
+static inline uint16_t
+float_to_ushort(float f)
+{
+ /* return 0 for NaN too */
+ if (!(f > 0.0f)) {
+ return (uint16_t) 0;
+ }
+ else if (f >= 1.0f) {
+ return (uint16_t) 65535;
+ }
+ else {
+ union fi tmp;
+ tmp.f = f;
+ tmp.f = tmp.f * (65535.0f/65536.0f) + 128.0f;
+ return (uint16_t) tmp.i;
}
}
#endif
}
-
-/**
- * Return number of bits set in n.
- */
-static inline unsigned
-util_bitcount(unsigned n)
-{
-#if defined(HAVE___BUILTIN_POPCOUNT)
- return __builtin_popcount(n);
-#else
- /* 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.
- */
- unsigned bits;
- for (bits = 0; n; bits++) {
- n &= n - 1;
- }
- return bits;
-#endif
-}
-
-
-static inline unsigned
-util_bitcount64(uint64_t n)
-{
-#ifdef HAVE___BUILTIN_POPCOUNTLL
- return __builtin_popcountll(n);
-#else
- return util_bitcount(n) + util_bitcount(n >> 32);
-#endif
-}
-
-
/**
* Reverse bits in n
* Algorithm taken from:
* Convert from little endian to CPU byte order.
*/
-#ifdef PIPE_ARCH_BIG_ENDIAN
+#if UTIL_ARCH_BIG_ENDIAN
#define util_le64_to_cpu(x) util_bswap64(x)
#define util_le32_to_cpu(x) util_bswap32(x)
#define util_le16_to_cpu(x) util_bswap16(x)
static inline void*
util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
{
-#ifdef PIPE_ARCH_BIG_ENDIAN
+#if UTIL_ARCH_BIG_ENDIAN
size_t i, e;
assert(n % 4 == 0);