32- int, DWORD - 32bit unsigned int
pow(x,y)=x^y
:
: pow(x,y) = exp2(y*log2(x)). :
integer pow(a,b)=a^b a>=0 , b>=0
( ), :
DWORD powuu(DWORD a,DWORD b)
{
int i,bits=32;
DWORD d=1;
for (i=0;i<bits;i++)
{
d*=d;
if (DWORD(b&0x80000000)) d*=a;
b<<=1;
}
return d;
}
integer pow(a,b)=a^b b>=0
if a
int powiu(int a,DWORD b)
{
int sig=0,c;
if ((a<0)&&(DWORD(b&1)) { sig=1; a=-a; }
c=powuu(a,b); if (sig) c=-c;
return c;
}
pow(a,b)=a^b
, b<0, 1/powiu(a,-b). , , , ( PI ). :
float powfii(int a,int b)
{
if (b<0) return 1.0/float(powiu(a,-b));
else return powiu(a,b);
}
pow(a,b)=a^b, b
- a^(1/bb), bb - . , :
a^(1/2) square root(a)a^(1/bb) bb_root(a)
c MSB LSB , pow(c,bb)<=a, bit, . sqrt:
int bits(DWORD p)
{
DWORD m=0x80000000; int b=32;
for (;m;m>>=1,b--)
if (p>=m) break;
return b;
}
DWORD sqrt(const DWORD &x)
{
DWORD m,a;
m=(bits(x)>>1);
if (m) m=1<<m; else m=1;
for (a=0;m;m>>=1) { a|=m; if (a*a>x) a^=m; }
return a;
}
if (a*a>x) if (pow(a,bb)>x), bb=1/b... b - , , bb - . m - , m=(bits(x)>>1); m=(bits(x)/bb);
[edit1] sqrt
const int _fx32_fract=16;
const int _fx32_one =1<<_fx32_fract;
DWORD fx32_mul(const DWORD &x,const DWORD &y)
{
DWORD a=x,b=y;
asm {
mov eax,a
mov ebx,b
mul eax,ebx
mov ebx,_fx32_one
div ebx
mov a,eax;
}
return a;
}
DWORD fx32_sqrt(const DWORD &x)
{
DWORD m,a;
if (!x) return 0;
m=bits(x);
if (m>_fx32_fract) m-=_fx32_fract; else m=0;
m>>=1;
m=_fx32_one<<m;
for (a=0;m;m>>=1)
{
a|=m;
if (fx32_mul(a,a)>x)
a^=m;
}
return a;
}
. 16 , 16 .
[edit2] 32- pow ++ example
, - :
const int _fx32_bits=32;
const int _fx32_fract_bits=16;
const int _fx32_integ_bits=_fx32_bits-_fx32_fract_bits;
const int _fx32_one =1<<_fx32_fract_bits;
const float _fx32_onef =_fx32_one;
const int _fx32_fract_mask=_fx32_one-1;
const int _fx32_integ_mask=0xFFFFFFFF-_fx32_fract_mask;
const int _fx32_sMSB_mask =1<<(_fx32_bits-1);
const int _fx32_uMSB_mask =1<<(_fx32_bits-2);
float fx32_get(int x) { return float(x)/_fx32_onef; }
int fx32_set(float x) { return int(float(x*_fx32_onef)); }
int fx32_mul(const int &x,const int &y)
{
int a=x,b=y;
asm {
mov eax,a
mov ebx,b
mul eax,ebx
mov ebx,_fx32_one
div ebx
mov a,eax;
}
return a;
}
int fx32_div(const int &x,const int &y)
{
int a=x,b=y;
asm {
mov eax,a
mov ebx,_fx32_one
mul eax,ebx
mov ebx,b
div ebx
mov a,eax;
}
return a;
}
int fx32_abs_sqrt(int x)
{
int m,a;
if (!x) return 0;
if (x<0) x=-x;
m=bits(x);
for (a=x,m=0;a;a>>=1,m++);
m-=_fx32_fract_bits;
if (m<0) m=0; m>>=1;
m=_fx32_one<<m;
for (a=0;m;m>>=1)
{
a|=m;
if (fx32_mul(a,a)>x)
a^=m;
}
return a;
}
int fx32_pow(int x,int y)
{
if (!y) return _fx32_one;
if (!x) return 0;
if (y==-_fx32_one) return fx32_div(_fx32_one,x);
if (y==+_fx32_one) return x;
int m,a,b,_y; int sx,sy;
sx=0; if (x<0) { sx=1; x=-x; }
sy=0; if (y<0) { sy=1; y=-y; }
_y=y&_fx32_fract_mask;
y=y&_fx32_integ_mask;
a=_fx32_one;
if (y)
{
for (m=_fx32_uMSB_mask;(m>_fx32_one)&&(m>y);m>>=1);
for (;m>=_fx32_one;m>>=1)
{
a=fx32_mul(a,a);
if (int(y&m)) a=fx32_mul(a,x);
}
}
if (_y)
{
for (b=x,m=_fx32_one>>1;m;m>>=1)
{
b=fx32_abs_sqrt(b);
if (int(_y&m)) a=fx32_mul(a,b);
}
}
if (sy) { if (a) a=fx32_div(_fx32_one,a); else a=0; }
if (sx) { if (_y) a=0; else if(int(y&_fx32_one)) a=-a; }
return a;
}
:
float a,b,c0,c1,d;
int x,y;
for (a=0.0,x=fx32_set(a);a<=10.0;a+=0.1,x=fx32_set(a))
for (b=-2.5,y=fx32_set(b);b<=2.5;b+=0.1,y=fx32_set(b))
{
if (!x) continue;
if (!y) continue;
c0=pow(a,b);
c1=fx32_get(fx32_pow(x,y));
d=0.0;
if (fabs(c1)<1e-3) d=c1-c0; else d=(c0/c1)-1.0;
if (fabs(d)>0.1)
d=d;
}
a,bx,y a,bc0 - .c1 - fx32_powd
- , , . , , ...
P.S. :