/* --------------------------------------------------------------------- ** Function: asqrt ** Discription: Calculate squareroot out of a 32bit integer. ** call: ret = asqrt(n); ** define: long asm asqrt(long n) ** ** This sqrt is computed in three interations using ** the formula: 1/2(sqrt + n/sqrt). ** ** The orginal source was written in MC68000 assembly by ** # Andrew Tyler # ** ** e-mail me if the routine is suitable for proper PSX ** applications. (datareg(at)home.centernet.de) */ long asm asqrt(long n) { .set reorder bne a0,$0,sqrt0 // if asqrt(NULL) then return zero li v0,0 jr ra sqrt0: li t0,0x80000000 // this loop finds li t1,32 // the the first nonzero bit. sqrt2: and t2,t0,a0 // - check if bit set bne t2,$0,sqrt1 // - if set continue at sqrt1 addiu t1,t1,-1 // - bit count -1 srl t0,t0,1 // - next lower power of 2 bne t0,$0,sqrt2 // - do it for 32 times sqrt1: srl t1,t1,1 li t0,1 sllv t0,t0,t1 div a0,t0 mflo t1 add t0,t0,t1 srl t0,t0,1 div a0,t0 mflo t1 add t0,t0,t1 srl t0,t0,1 div a0,t0 mflo t1 add t0,t0,t1 srl v0,t0,1 jr ra // return form sub }