aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-04-28 10:30:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-04-28 10:30:14 -0300
commit01b303c87e4b3d3c9daed6312658bb73638498a6 (patch)
tree53fb6d2bf92795333ca5501501094667ede0f6d4
parent762c7370376dbd13cd8aeb4d8c8da0bb153269c3 (diff)
downloadlua-01b303c87e4b3d3c9daed6312658bb73638498a6.tar.gz
lua-01b303c87e4b3d3c9daed6312658bb73638498a6.tar.bz2
lua-01b303c87e4b3d3c9daed6312658bb73638498a6.zip
simpler log2 implementation
-rw-r--r--lobject.c31
1 files changed, 11 insertions, 20 deletions
diff --git a/lobject.c b/lobject.c
index 7106478a..26accff2 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.96 2003/02/18 16:02:56 roberto Exp roberto $ 2** $Id: lobject.c,v 1.97 2003/04/03 13:35:34 roberto Exp roberto $
3** Some generic functions over Lua objects 3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -45,29 +45,20 @@ int luaO_int2fb (unsigned int x) {
45 45
46 46
47int luaO_log2 (unsigned int x) { 47int luaO_log2 (unsigned int x) {
48 static const lu_byte log_8[255] = { 48 static const lu_byte log_2[256] = {
49 0, 49 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
50 1,1,
51 2,2,2,2,
52 3,3,3,3,3,3,3,3,
53 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
54 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
55 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 50 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
56 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
57 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
58 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 51 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
59 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 52 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
60 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 53 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
54 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
55 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
56 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
61 }; 57 };
62 if (x >= 0x00010000) { 58 int l = -1;
63 if (x >= 0x01000000) return log_8[((x>>24) & 0xff) - 1]+24; 59 while (x >= 256) { l += 8; x >>= 8; }
64 else return log_8[((x>>16) & 0xff) - 1]+16; 60 return l + log_2[x];
65 } 61
66 else {
67 if (x >= 0x00000100) return log_8[((x>>8) & 0xff) - 1]+8;
68 else if (x) return log_8[(x & 0xff) - 1];
69 return -1; /* special `log' for 0 */
70 }
71} 62}
72 63
73 64