diff options
author | Roland Scheidegger <[email protected]> | 2011-06-09 00:44:32 +0200 |
---|---|---|
committer | Roland Scheidegger <[email protected]> | 2011-06-09 01:14:50 +0200 |
commit | b537f54b3a2bd47ec861f5c432c49529684eef21 (patch) | |
tree | 60ee6fa35ab28b60f9f6f19222e815666b71d536 /src/mesa/main/imports.h | |
parent | b3d5822e932767a572f392ae2b3ca1dfee5f6b03 (diff) |
mesa: use __builtin_clz for logbase2 when available
Also rename to _mesa_logbase2 and move to imports.h to keep the ugly
ifdef GNUC stuff outside other files (also to allow reuse).
Diffstat (limited to 'src/mesa/main/imports.h')
-rw-r--r-- | src/mesa/main/imports.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h index ce7baabe2c7..3fa1db02aee 100644 --- a/src/mesa/main/imports.h +++ b/src/mesa/main/imports.h @@ -486,6 +486,27 @@ _mesa_next_pow_two_64(uint64_t x) } +/* + * Returns the floor form of binary logarithm for a 32-bit integer. + */ +static INLINE GLuint +_mesa_logbase2(GLuint n) +{ +#if defined(__GNUC__) && \ + ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4) + return (31 - __builtin_clz(n | 1)); +#else + GLuint 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; +#endif +} + + /** * Return 1 if this is a little endian machine, 0 if big endian. */ |