07-20-2022, 09:52 PM
Math Functions
Code: (Select All)
proc SYSTEM_BUS_T.k_f(x as float) as float
return x*x
end proc
proc SYSTEM_BUS_T.k_frexp(d as float, ep as float ptr) as float
static as Cheat x
if(d = 0) then
*ep = 0
return 0
end if
x.d = d
*ep = ((x.ms shr K_SHIFT) and K_MASK) - K_BIAS
x.ms = x.ms and not (K_MASK shl K_SHIFT)
x.ms = x.ms or K_BIAS shl K_SHIFT
return x.d
end proc
proc SYSTEM_BUS_T.k_ldexp(d as float, e as float) as float
static as Cheat x
if(d = 0) then
return 0
end if
x.d = d
e += (x.ms shr K_SHIFT) and K_MASK
if(e <= 0) then
return 0 /' underflow '/
end if
if(e >= K_MASK) then /' overflow '/
if(d < 0) then
return NEG_INF
end if
return POS_INF
end if
x.ms = x.ms and not (K_MASK shl K_SHIFT)
x.ms = x.ms or e shl K_SHIFT
return x.d
end proc
proc SYSTEM_BUS_T.k_sqrt(arg as float) as float
static as float x, temp
static as float _exp, i
if(arg <= 0) then
if(arg < 0) then
return 0.0
end if
return 0
end if
x = k_frexp(arg, @_exp)
while(x < 0.5)
x *= 2
_exp = _exp - 1
wend
/'
' NOTE
' this wont work on 1's comp
'/
if(_exp and 1) then
x *= 2
_exp = _exp - 1
end if
temp = 0.5 * (1.0+x)
while(_exp > 60)
temp *= (1L shl 30)
_exp -= 60
wend
while(_exp < -60)
temp /= (1L shl 30)
_exp += 60
wend
if(_exp >= 0) then
temp *= 1L shl (_exp/2)
else
temp /= 1L shl (-_exp/2)
end if
for i=0 to 4
temp = 0.5*(temp + arg/temp)
next
return temp
end proc