Skip to content

Commit

Permalink
func -> proc with interior result='s; Also move proc after `imp…
Browse files Browse the repository at this point in the history
…ort`s.
  • Loading branch information
c-blake committed Dec 24, 2024
1 parent b0d0333 commit adf3af9
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions adix/lna.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,28 @@ when isMainModule:
sum += l
echo "sum: ", sum, " in ", epochTime() - t0, " seconds; n: ", n
else:
func lnaT*(x: float32): float32 {.inline.} =
var x = x
let p = cast[ptr f4s](x.addr)
p.sign = 0 # force x to be positive
let e = (p.expo.cint - 127).float*LN2 # ln(x*2^y) == ln(x) + y*ln2
p.expo = 127 # force x to [1, 2)
if x > 1.88f32:
let y = x.float*0.5 - 1.0
{.cast(noSideEffect).}: echo "x: ",x," e: ",e," y: ",y
e + LN2 + y*(1.0 + y*(-0.5 + y*(1.0/3.0 + y*(-0.25 + y*(0.2 - y/6.0)))))
elif x < 1.06f32:
let y = x.float - 1.0
{.cast(noSideEffect).}: echo "X: ",x," E: ",e," Y: ",y
e + y*(1.0 + y*(-0.5 + y*(1.0/3.0 + y*(-0.25 + y*(0.2 - y/6.0)))))
else:
let d = x.float * r1_2 # x -> dbl [sqrt(1/2), sqrt2)
let r = (d.float - 1.0)/(d.float + 1.0) # r for r)atio of -1/+1
let s = r*r # s for s)quare
{.cast(noSideEffect).}: echo "x: ",x," d: ",d," e: ",e," r: ",r," s: ",s,
" iM: ",s.lnaSeries
float32(e + LNr2 + r*s.lnaSeries) # (1.37288 +- 0.00003)X faster on SkyLake
when not declared(stdout): import std/[syncio, formatFloat]
import std/[math, heapqueue]
proc lnaT*(x: float32): float32 {.inline.} =
var x = x
let p = cast[ptr f4s](x.addr)
p.sign = 0 # force x to be positive
let e = (p.expo.cint - 127).float*LN2 # ln(x*2^y) == ln(x) + y*ln2
p.expo = 127 # force x to [1, 2)
if x > 1.88f32:
let y = x.float*0.5 - 1.0
echo "x: ",x," e: ",e," y: ",y
result = e + LN2 + y*(1.0 + y*(-0.5 + y*(1.0/3.0 + y*(-0.25 + y*(0.2 - y/6.0)))))
elif x < 1.06f32:
let y = x.float - 1.0
echo "X: ",x," E: ",e," Y: ",y
result = e + y*(1.0 + y*(-0.5 + y*(1.0/3.0 + y*(-0.25 + y*(0.2 - y/6.0)))))
else:
let d = x.float * r1_2 # x -> dbl [sqrt(1/2), sqrt2)
let r = (d.float - 1.0)/(d.float + 1.0) # r for r)atio of -1/+1
let s = r*r # s for s)quare
echo "x: ",x," d: ",d," e: ",e," r: ",r," s: ",s," iM: ",s.lnaSeries
result = float32(e + LNr2 + r*s.lnaSeries)
const n = 15 # echo top <ThisMany> absolute & relative errors
var abErr, rlErr: HeapQueue[(float, float32, float32, float32)]
for i in 0 .. (1u64 shl 32) - 1:
Expand Down

0 comments on commit adf3af9

Please sign in to comment.