Last active
December 20, 2015 03:08
-
-
Save bitkevin/6061071 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --------------------------------------------------------------------- | |
// 来自lua源码,移植性好 | |
int luaO_log2 (unsigned int x) { | |
static const lu_byte log_2[256] = { | |
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, | |
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, | |
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, | |
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, | |
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, | |
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, | |
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, | |
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 | |
}; | |
int l = -1; | |
while (x >= 256) { l += 8; x >>= 8; } | |
return l + log_2[x]; | |
} | |
// --------------------------------------------------------------------- | |
// 利用float存储时,在内存中直接保存了指数值,根据其内存分布,直接取指数 | |
// 移植性受到float内存分布的限制,但目前来说都是IEEE标准的 | |
typedef unsigned long uint32; | |
typedef long int32; | |
static inline int32 ilog2(uint32 x) { | |
return ilog2((float)x); | |
} | |
// integer log2 of a float | |
static inline int32 ilog2(float x) | |
{ | |
uint32 ix = (uint32&)x; | |
uint32 exp = (ix >> 23) & 0xFF; | |
int32 log2 = int32(exp) - 127; | |
return log2; | |
} | |
// --------------------------------------------------------------------- | |
// 汇编实现,简单直接,移植性欠缺一些 | |
static inline int32 ilog2_x86(uint32 x) | |
{ | |
int32 retval; | |
__asm { | |
bsr eax, x | |
mov retval, eax | |
} | |
return retval; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment