summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/imports.h
diff options
context:
space:
mode:
authorPauli Nieminen <[email protected]>2010-02-06 06:32:40 +0200
committerPauli Nieminen <[email protected]>2010-02-06 19:24:44 +0200
commitd41740e969ce1ef084fa7a51208d43ac6883adf8 (patch)
tree9f1ec89bd15527bfb3cf037a2189ca87fa09f2b1 /src/mesa/main/imports.h
parentf8969dd24ff4264c0c11ea5266093b6ce2ef221a (diff)
mesa/main: Add function to find next higher power of two.
With gcc implementation uses __builtin_clr which counts number of leading zeros. Fallback implementation uses bit manipulation. First it duplicates the highest bit to all lower bits and then adds one to get the power of two number.
Diffstat (limited to 'src/mesa/main/imports.h')
-rw-r--r--src/mesa/main/imports.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
index b01fe5b0ab9..81cb396b2bb 100644
--- a/src/mesa/main/imports.h
+++ b/src/mesa/main/imports.h
@@ -405,6 +405,52 @@ _mesa_is_pow_two(int x)
return !(x & (x - 1));
}
+/**
+ * Round given integer to next higer power of two
+ * If X is zero result is undefined.
+ *
+ * Source for the fallback implementation is
+ * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
+ * http://graphics.stanford.edu/~seander/bithacks.html
+ */
+static INLINE int32_t
+_mesa_next_pow_two_32(uint32_t x)
+{
+#ifdef __GNUC__
+ return 1 << (__builtin_clz(x) ^ 31);
+#else
+ x--;
+ x |= x >> 1;
+ x |= x >> 2;
+ x |= x >> 4;
+ x |= x >> 8;
+ x |= x >> 16;
+ x++;
+ return x;
+#endif
+}
+
+static INLINE int64_t
+_mesa_next_pow_two_64(uint64_t x)
+{
+#ifdef __GNUC__
+ if (sizeof(x) == sizeof(long))
+ return 1 << (__builtin_clzl(x) ^ 63);
+ else
+ return 1 << (__builtin_clzll(x) ^ 63);
+#else
+ x--;
+ x |= x >> 1;
+ x |= x >> 2;
+ x |= x >> 4;
+ x |= x >> 8;
+ x |= x >> 16;
+ x |= x >> 32;
+ x++;
+ return x;
+#endif
+}
+
/***
*** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]