-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Closed
Labels
Description
I guess (and did a very quick search) LLVM does not have a mod intrinsic, otherwise I would expect it to do this optimization.
mod(i::Integer, 2^n) could be rewritten as something like (I think I meant (0b1<<n) & i(0b1 << (n+1)) - 1). I don't know how to benchmark small functions but here's a wonky benchmark:
f(i) = sin(mod(i, 4))
mod4(i) = 0b11 & i
g(i) = sin(mod4(i))
function facc(n)
a = 0.0
for i = 1:n
a += f(i)
end
return a
end
function gacc(n)
a = 0.0
for i = 1:n
a += g(i)
end
return a
endjulia> BenchmarkTools.@benchmark facc(10)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 100.668 ns (0.00% GC)
median time: 100.920 ns (0.00% GC)
mean time: 111.223 ns (0.00% GC)
maximum time: 488.910 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 943
julia> BenchmarkTools.@benchmark gacc(10)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 67.257 ns (0.00% GC)
median time: 67.278 ns (0.00% GC)
mean time: 76.017 ns (0.00% GC)
maximum time: 298.556 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 977