PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

int64 sqrt

Hello,

Ive come to the conclusion that trying to prevent normalization of large vectors is more trouble than its worth, so id like to modify my normalisation function to handle intermediate results larger than the maximum s15.16 number. (im working in 2d)

Using David Blythe fixed point implementation I have

Code:

EGL_Fixed EGL_Sqrt(EGL_Fixed a) {
EGL_Fixed s;
I32 i;
s = (a + EGL_ONE) >> 1;
// 6 iterations to converge //
for (i = 0; i < 6; i++)
s = (s + EGL_Div(a, s)) >> 1;
return s;
}

EGL_Fixed EGL_InvSqrt(EGL_Fixed a) {
EGL_Fixed x /*= (a+EGL_ONE)>>1*//*a>>1*/;
/* 1/(2sqrt(x)) - extra divide by 2 to scale to 16 bits */
static const U16 __gl_rsq_tab[] = { /* domain 0.5 .. 1.0-1/16 */
0xb504, 0xaaaa, 0xa1e8, 0x9a5f, 0x93cd, 0x8e00, 0x88d6, 0x8432,
};
I32 i, exp;
if (a == EGL_ZERO) return 0x7fffffff;
if (a == EGL_ONE) return a;

#ifdef EGL_USE_CLZ
exp = _CountLeadingZeros(a);
#else
x = a;
exp = 31;
if (x & 0xffff0000) { exp -= 16; x >>= 16; }
if (x & 0xff00) { exp -= 8; x >>= 8; }
if (x & 0xf0) { exp -= 4; x >>= 4; }
if (x & 0xc) { exp -= 2; x >>= 2; }
if (x & 0x2) { exp -= 1; }
#endif
x = __gl_rsq_tab[(a>>(28-exp))&0x7]<<1;
//printf("t %f %x %f %d %d\n", __GL_F_2_FLOAT(a), a, __GL_F_2_FLOAT(x), exp, 28-exp);
exp -= 16;
if (exp <= 0)
x >>= -exp>>1;
else
x <<= (exp>>1)+(exp&1);
if (exp&1) x = EGL_Mul(x, __gl_rsq_tab[0]);
//printf("nx %f\n", __GL_F_2_FLOAT(x));

/* newton-raphson */
/* x = x/2*(3-(a*x)*x) */
i = 0;
do {
//printf("%f\n", __GL_F_2_FLOAT(x));
x = EGL_Mul((x>>1),(EGL_ONE*3 - EGL_Mul(EGL_Mul(a,x),x)));
} while(++i < 3);
//printf("rsqrt %f %f\n", __GL_F_2_FLOAT(a), __GL_F_2_FLOAT(x));
return x;
}



At my disposal, my normalisation code looks like


Code:

inline void Normalize()
{
I64 lx = (((I64) m_x * (I64) m_x) >> EGL_PRECISION);
I64 ly = (((I64) m_y * (I64)m_y) >> EGL_PRECISION);

I64 intermediate = lx+ly;

EGL_Fixed l = (EGL_Fixed)intermediate;

*this *= EGL_InvSqrt(l);
//*this /= EGL_Sqrt(l);
}




now what im guessing i need to do is mask the intemediate value to see if the number has overflowed, then call an 64 bit invSqrt if it has, Now I don’t claim to be a fixed point guru so I could use a little help on what to do next,

Thanks
Factor out a factor of 4 brefore calling into the function; adjust the result by the corresponding multiple of 2. Is this the same question as the one posted on the Khronos site?

- HM
no that question was nothing to do with me, i just looked it up odd huh :roll:

ok, ill try that, will that work with the inverse sqrt as well? i dont really understand whats going on inside these function (well i do a bit but there hard to follow)
Ok testing invSqrt, it didn’t seem to work,

So this is my implementation so far (don’t laugh :lol: ), can you tell me how to make it faster?

Code:


inline void Normalize()
{

I64 lx = (((I64) m_x * (I64) m_x) >> EGL_PRECISION);
I64 ly = (((I64) m_y * (I64) m_y) >> EGL_PRECISION);

I64 intermediate = lx+ly;
I32 factor = 0;

while(intermediate > (I64)EGL_PINF)
{
intermediate = (intermediate*((I64)F(0.25f)))>> EGL_PRECISION;
++factor;
}

EGL_Fixed result = EGL_Sqrt((EGL_Fixed)intermediate);

if(factor)
result = EGL_Mul(result,EGL_Mul(I(factor),I(2)));

*this /= result;
}


Also the EGL_Sqrt function seems to be slow to converge on larger numbers, is this because you really need a larger starting approximation?


I seem to remember once in a far of land seeing a implementation that didn’t use divide, anyone got any clues? Or is this the quickest way to sqrt on non fpu hardware?

This is proving to be quite a bottle-neck in my code as I have to normalise a large number of vectors each frame to prevent overflows else where.
looking at that link i think i need to implement the code

Code:

q = 0.0; kq2 = 0.0; kq = 0.0;
for (i = 1; i <= precision; i++) {
t = kq2+kq*0.5^i+k*0.5^(2*i+2);
if (t <= 0.25) {
q += 0.5^(i+1);
kq2 = t;
kq += k*0.5^(i+1);
}
}


but it dosnt make much sence to me, Could you explain how to use this to make a function for desktop use, It seems to have lots of powers in it? Or am I reading it wrong?

+
Also it said "I assume that k is normalized so. 0.25<=k<1, as is usual for square root routines" isn’t the point of the function to normalize? I think im getting myself in a muddle here!...
^ is bitwise exclusive or in C.

dan East
Hehe I feel silly now, I thought this wanst the case because it was on the 0.5,,,,,,,,


still need some help though
They are probably mixing and matching pseudo code with C, because you can't apply binary operators to floats (unless you cast them to an integer type).

Dan East
Structure wrote:
Also the EGL_Sqrt function seems to be slow to converge on larger numbers, is this because you really need a larger starting approximation?

Yes, beyond a certain threshold it's faster to use the inverse of the inverse sqrt. But with fixed point arithmetic, this might yield precision problems.

- HM
Hi, thanks for all the ongoing help people, as you can probably tell I come from quite a high level programming background so this low level stuff is quite new to me,


Id just like to confirm that the divide by power of 4 multiply by 2 trick should not work with the inverse sqrt function, (or is it being very inaccurate with large numbers??)

Also id like to confirm that im not doing anything wildly stupid in my normalisation function mentioned above.