//--BEGIN--pot.c--
// Returns the least power of 2 greater than or equal to "x".
// Note that for x=0 and for x>2147483648 this returns 0!
#ifdef __GNUC__
unsigned ceilPowerOf2(unsigned x) {
unsigned eax;
__asm__(
"xor eax,eax\n"
"dec ecx\n"
"bsr ecx,ecx\n"
"cmovz ecx,eax\n"
"setnz al\n"
"inc eax\n"
"shl eax,cl\n"
: "=a" (eax)
: "c" (x)
);
return eax;
}
#else
__declspec(naked) unsigned __fastcall ceilPowerOf2(unsigned x) {
__asm {
xor eax,eax
dec ecx
bsr ecx,ecx
cmovz ecx,eax
setnz al
inc eax
shl eax,cl
ret
}
}
#endif
// Returns the greatest power of 2 less than or equal to "x".
// Note that for x=0 this returns 0!
#ifdef __GNUC__
unsigned floorPowerOf2(unsigned x) {
unsigned eax;
__asm__(
"xor eax,eax\n"
"bsr ecx,ecx\n"
"setnz al\n"
"shl eax,cl\n"
: "=a" (eax)
: "c" (x)
);
return eax;
}
#else
__declspec(naked) unsigned __fastcall floorPowerOf2(unsigned x) {
__asm {
xor eax,eax
bsr ecx,ecx
setnz al
shl eax,cl
ret
}
}
#endif
//--END--pot.c-- |