转 http://blog.codingnow.com/cloud/CodeCollection
一个快速开方的函数
float CarmSqrt(float x){
union{
int intPart;
float floatPart;
} convertor;
union{
int intPart;
float floatPart;
} convertor2;
convertor.floatPart = x;
convertor2.floatPart = x;
convertor.intPart = 0x1FBCF800 + (convertor.intPart >> 1);
convertor2.intPart = 0x5f3759df - (convertor2.intPart >> 1);
return 0.5f*(convertor.floatPart + (x * convertor2.floatPart));
}
快速 double 转整型
union luai_Cast { double l_d; long l_l; };
#define lua_number2int(i,d) /
{ volatile union luai_Cast u; /
u.l_d = (d) + 6755399441055744.0;
(i) = u.l_l; }
RGB565 的 alpha 混合
unsigned short alpha_blender(unsigned int x,unsigned int y,unsigned int alpha)
{
x = (x | (x<<16)) & 0x7E0F81F;
y = (y | (y<<16)) & 0x7E0F81F;
unsigned int result = ((x - y) * alpha / 32 + y) & 0x7E0F81F;
return (unsigned short)((result&0xFFFF) | (result>>16));
}
一个不错的字符串 hash 函数
unsigned long hash(const char *name,size_t len)
{
unsigned long h=(unsigned long)len;
size_t step = (len>>5)+1;
for (size_t i=len; i>=step; i-=step)
h = h ^ ((h<<5)+(h>>2)+(unsigned long)name[i-1]);
return h;
}
UTF8 到 UTF16 的转换(单个字符)
int UTF8toUTF16(int c)
{
signed char *t=(signed char*)&c;
int ret=*t &(0x0f | (~(*t>>1) &0x1f) | ~(*t>>7));
int i;
assert ((*t & 0xc0) != 0x80);
for (i=1;i<3;i++) {
if ((t[i] & 0xc0)!=0x80)
break;
ret=ret<<6 | (t[i] & 0x3f);
}
return ret;
}