summaryrefslogtreecommitdiff
path: root/ilog2.c
diff options
context:
space:
mode:
authorCyrill Gorcunov <gorcunov@gmail.com>2010-04-22 10:29:19 +0400
committerCyrill Gorcunov <gorcunov@gmail.com>2010-04-22 19:03:39 +0400
commit641ca7fd6af6f1b7c3ee46abf401bc47aa2f7c45 (patch)
tree25de661a47ad8a48e632125e1e2bcab4866ec732 /ilog2.c
parentc8fd6fdae4ad716ca66c0c4c5a2b6b2b4e7d1dc1 (diff)
downloadnasm-641ca7fd6af6f1b7c3ee46abf401bc47aa2f7c45.tar.gz
nasm-641ca7fd6af6f1b7c3ee46abf401bc47aa2f7c45.tar.bz2
nasm-641ca7fd6af6f1b7c3ee46abf401bc47aa2f7c45.zip
ilog2: Use ROUND helper
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Diffstat (limited to 'ilog2.c')
-rw-r--r--ilog2.c65
1 files changed, 22 insertions, 43 deletions
diff --git a/ilog2.c b/ilog2.c
index ab75e20..df11561 100644
--- a/ilog2.c
+++ b/ilog2.c
@@ -34,6 +34,14 @@
#include "compiler.h"
#include "nasmlib.h"
+#define ROUND(v, a, w) \
+ do { \
+ if (v & (((1 << w) - 1) << w)) { \
+ a += w; \
+ v >>= w; \
+ } \
+ } while (0)
+
#if defined(__GNUC__) && defined(__x86_64__)
int ilog2_32(uint32_t v)
@@ -75,26 +83,11 @@ int ilog2_32(uint32_t v)
{
int p = 0;
- if (v & 0xffff0000) {
- p += 16;
- v >>= 16;
- }
- if (v & 0xff00) {
- p += 8;
- v >>= 8;
- }
- if (v & 0xf0) {
- p += 4;
- v >>= 4;
- }
- if (v & 0xc) {
- p += 2;
- v >>= 2;
- }
- if (v & 0x2) {
- p += 1;
- v >>= 1;
- }
+ ROUND(v, p, 16);
+ ROUND(v, p, 8);
+ ROUND(v, p, 4);
+ ROUND(v, p, 2);
+ ROUND(v, p, 1);
return p;
}
@@ -130,31 +123,17 @@ int ilog2_64(uint64_t vv)
int p = 0;
uint32_t v;
- if ((v = vv >> 32) != 0) {
+ v = vv >> 32;
+ if (v)
p += 32;
- } else {
+ else
v = vv;
- }
- if (v & 0xffff0000) {
- p += 16;
- v >>= 16;
- }
- if (v & 0xff00) {
- p += 8;
- v >>= 8;
- }
- if (v & 0xf0) {
- p += 4;
- v >>= 4;
- }
- if (v & 0xc) {
- p += 2;
- v >>= 2;
- }
- if (v & 0x2) {
- p += 1;
- v >>= 1;
- }
+
+ ROUND(v, p, 16);
+ ROUND(v, p, 8);
+ ROUND(v, p, 4);
+ ROUND(v, p, 2);
+ ROUND(v, p, 1);
return p;
}