- 注册时间
- 2008-8-30
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 560
- 在线时间
- 小时
|
发表于 2012-1-4 23:22:25
|
显示全部楼层
我抄过来改了下,只和sqrt()比较了下正确性-
-
- #ifdef USING_ASM
- inline DWORD __fastcall Log2_Floor(DWORD x)
- {
- __asm
- {
- bsr eax,x
- jnz RETURN
- mov eax,0ffffffffh
- RETURN:
- }
- }
- #else
- inline DWORD Log2_Floor(DWORD x)
- {
- DWORD l=0;
- if(x&0xffff0000){l|=16;x>>=16;}
- if(x&0x0000ff00){l|= 8;x>>= 8;}
- if(x&0x000000f0){l|= 4;x>>= 4;}
- if(x&0x0000000c){l|= 2;x>>= 2;}
- if(x&0x00000002){l|= 1;/*x>>= 1;*/}
- return l;
- }
- #endif
-
- DWORD __fastcall Sqrt(DWORD n)
- {
- static WORD sqrtTab[]=
- {
- 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
- 5, 5, 5, 5, 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,
- 256,258,260,262,264,266,268,270,272,274,276,278,280,282,284,286,
- 288,288,290,292,294,296,298,300,300,302,304,306,308,310,310,312,
- 314,316,318,320,320,322,324,326,326,328,330,332,334,334,336,338,
- 340,340,342,344,346,346,348,350,352,352,354,356,356,358,360,362,
- 362,364,366,366,368,370,370,372,374,374,376,378,378,380,382,384,
- 384,386,386,388,390,390,392,394,394,396,398,398,400,402,402,404,
- 406,406,408,408,410,412,412,414,416,416,418,418,420,422,422,424,
- 424,426,428,428,430,430,432,434,434,436,436,438,438,440,442,442,
- 444,444,446,448,448,450,450,452,452,454,454,456,458,458,460,460,
- 462,462,464,464,466,468,468,470,470,472,472,474,474,476,476,478,
- 480,480,482,482,484,484,486,486,488,488,490,490,492,492,494,494,
- 496,496,498,498,500,500,502,502,504,504,506,506,508,508,510,512
-
- };
- static const DWORD Table2[]=
- {
- 0<<10,1<<10,4<<10,9<<10,16<<10,25<<10,36<<10,49<<10,
- };
-
- if(n<64) return sqrtTab[n];
-
- DWORD r;
- if(n<65536)
- {
- r=sqrtTab[n>>10];
- n-=Table2[r];
- r<<=10;
- DWORD c=0x100|r;r>>=1;
- if(n>=c){n-=c;r|=0x100;}
- c=0x40|r;r>>=1;
- if(n>=c){n-=c;r|=0x40;}
- c=0x10|r;r>>=1;
- if(n>=c){n-=c;r|=0x10;}
- c=0x04|r;r>>=1;
- if(n>=c){n-=c;r|=0x04;}
- c=0x01|r;r>>=1;
- if(n>=c) r|=0x01;
- return r;
- }
-
- switch(Log2_Floor(n)&0xfe)
- {
- #define SQRT_ADJUST {r=(r+n/r)>>1;if(r*r>n) --r;break;}
- case 16:r=sqrtTab[n>>10] ;SQRT_ADJUST;
- case 18:r=sqrtTab[n>>12]<<1;SQRT_ADJUST;
- case 20:r=sqrtTab[n>>14]<<2;SQRT_ADJUST;
- case 22:r=sqrtTab[n>>16]<<3;SQRT_ADJUST;
- case 24:r=sqrtTab[n>>18]<<4;SQRT_ADJUST;
- case 26:r=sqrtTab[n>>20]<<5;SQRT_ADJUST;
- case 28:r=sqrtTab[n>>22]<<6;SQRT_ADJUST;
- case 30:r=sqrtTab[n>>24]<<7;SQRT_ADJUST;
- #undef SQRT_ADJUST
- }
- return r;
- }
-
复制代码 |
|