Skip to content

Commit

Permalink
The loop over all possible float32 does do non-negligible work. So,
Browse files Browse the repository at this point in the history
mirror that work (with an ineliminable sum0 to ensure compilers to not
optimize it out) to get better estimates of "just the extra work for
`lna`", to the extent this can even be defined in the context of quite
a lot of shared CPU resources over various superscalar piplines.
  • Loading branch information
c-blake committed Dec 25, 2024
1 parent 3d6979f commit a678da8
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions adix/lna.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ func lna*(x: float32): float32 {.inline.} =
when isMainModule:
when defined(bench):
import std/[times, math, formatFloat, strformat]
var sum = 0.0; var n = 0
var sum0 = 0.0; var sum = 0.0; var n = 0
let t00 = epochTime()
for i in 0 .. (1u64 shl 32) - 1:
var i = uint32(i)
let x = cast[ptr float32](i.addr)[]
if x.isNaN: continue
if x == 0.0f32: continue # -inf
inc n
if not (x.isNaN or 2*x==x): sum0 += x
let dt0 = epochTime() - t00
let t0 = epochTime()
for i in 0 .. (1u64 shl 32) - 1:
var i = uint32(i)
Expand All @@ -55,8 +64,8 @@ when isMainModule:
else : (let l = lna(x))
inc n
if not (l.isNaN or 2*x==x): sum += l
let dt = epochTime() - t0
echo &"sum: {sum} in {dt:.6f} second; n: {n}; {dt/n.float*1e9:.2f} ns/eval"
let dt = epochTime() - t0 - dt0
echo &"sum0: {sum0:.0f} sum: {sum} in {dt:.6f} second; n: {n}; {dt/n.float*1e9:.2f} ns/eval"
else:
when not declared(stdout): import std/[syncio, formatFloat]
import std/[math, heapqueue]
Expand Down

0 comments on commit a678da8

Please sign in to comment.