From a678da80a4cee122aea3329fdb5fdd2834a9d836 Mon Sep 17 00:00:00 2001 From: Charles Blake Date: Wed, 25 Dec 2024 07:51:26 -0500 Subject: [PATCH] The loop over all possible float32 does do non-negligible work. So, 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. --- adix/lna.nim | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/adix/lna.nim b/adix/lna.nim index 599bbd1..297e8e1 100644 --- a/adix/lna.nim +++ b/adix/lna.nim @@ -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) @@ -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]