]> code.delx.au - pulseaudio/commitdiff
rework pa_ulog2 and base it on __builtin_clz if available, make pa_make_power_of_two...
authorLennart Poettering <lennart@poettering.net>
Thu, 28 Aug 2008 23:13:50 +0000 (01:13 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 28 Aug 2008 23:13:50 +0000 (01:13 +0200)
src/pulsecore/core-util.h

index 7167972bffebb849613bc9e259cfee49e2cc39e7..c9e307f5da131ea2efb5ee691ed5a5cb02c7a83d 100644 (file)
@@ -142,29 +142,35 @@ static inline int pa_is_power_of_two(unsigned n) {
     return !(n & (n - 1));
 }
 
-static inline unsigned pa_make_power_of_two(unsigned n) {
-    unsigned j = n;
+static inline unsigned pa_ulog2(unsigned n) {
 
-    if (pa_is_power_of_two(n))
-        return n;
+    if (n <= 1)
+        return 0;
 
-    while (j) {
-        j = j >> 1;
-        n = n | j;
-    }
+#if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
+    return 8U * (unsigned) sizeof(unsigned) - (unsigned) __builtin_clz(n) - 1;
+#else
+{
+    unsigned r = 0;
 
-    return n + 1;
-}
+    for (;;) {
+        n = n >> 1;
 
-static inline unsigned pa_ulog2(unsigned n) {
-    unsigned r = 0;
+        if (!n)
+            return r;
 
-    while (n) {
         r++;
-        n = n >> 1;
     }
+}
+#endif
+}
+
+static inline unsigned pa_make_power_of_two(unsigned n) {
+
+    if (pa_is_power_of_two(n))
+        return n;
 
-    return r;
+    return 1U << (pa_ulog2(n) + 1);
 }
 
 void pa_close_pipe(int fds[2]);