一個快速開方的函數(shù)
參考鏈接:
http://greatsorcerer.go2.icpcn.com/info/fastsqrt.html
快速 double 轉(zhuǎn)整型
參考鏈接:
double to int 神奇的 magic number
RGB565 的 alpha 混合
參考鏈接:
64K 色的 Alpha 混合
一個不錯的字符串 hash 函數(shù)
一個方便的 hash 函數(shù)應(yīng)該散列的比較開,計算速度跟字符串長度關(guān)系不大,又不能只計算字符串的開頭或末尾。這里的算法是從 Lua 中看來的。
關(guān)于臟矩形技術(shù)的演示
由于代碼過長,單起一頁:臟矩形 demo
參考鏈接:
Blog上的帖子:臟矩形演示demo
UTF8 到 UTF16 的轉(zhuǎn)換(單個字符)
這只是一個字符的轉(zhuǎn)換,如果轉(zhuǎn)換字符串,可以再做一點優(yōu)化。
/* 來至 Quake 3 的源碼 */ 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); return0.5f*(convertor.floatPart + (x * convertor2.floatPart)); }
參考鏈接:
![在新窗口中打開 [External Link]](http://blog.codingnow.com/cloud//images/www.gif)
快速 double 轉(zhuǎn)整型
union luai_Cast {double l_d; long l_l; }; #define lua_number2int(i,d) \{volatileunion luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
參考鏈接:
![在新窗口中打開 [InterWiki]](http://blog.codingnow.com/cloud//images/inter.gif)
RGB565 的 alpha 混合
unsignedshort alpha_blender(unsignedint x,unsignedint y,unsignedint alpha){ x = (x | (x<<16)) & 0x7E0F81F; y = (y | (y<<16)) & 0x7E0F81F; unsignedint result = ((x - y) * alpha / 32 + y) & 0x7E0F81F; return(unsignedshort)((result&0xFFFF) | (result>>16)); }
參考鏈接:
![在新窗口中打開 [InterWiki]](http://blog.codingnow.com/cloud//images/inter.gif)
一個不錯的字符串 hash 函數(shù)
unsignedlong hash(constchar *name,size_t len){unsignedlong h=(unsignedlong)len; size_t step = (len>>5)+1; for(size_t i=len; i>=step; i-=step) h = h ^ ((h<<5)+(h>>2)+(unsignedlong)name[i-1]); return h; }
一個方便的 hash 函數(shù)應(yīng)該散列的比較開,計算速度跟字符串長度關(guān)系不大,又不能只計算字符串的開頭或末尾。這里的算法是從 Lua 中看來的。
關(guān)于臟矩形技術(shù)的演示
由于代碼過長,單起一頁:臟矩形 demo
參考鏈接:
![在新窗口中打開 [External Link]](http://blog.codingnow.com/cloud//images/www.gif)
UTF8 到 UTF16 的轉(zhuǎn)換(單個字符)
int UTF8toUTF16(int c){signedchar *t=(signedchar*)&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; }
這只是一個字符的轉(zhuǎn)換,如果轉(zhuǎn)換字符串,可以再做一點優(yōu)化。