Skip to content

mod(i::Integer, const_power_of_2) optimization #34990

@goretkin

Description

@goretkin

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 (0b1<<n) & i (I think I meant (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
end
julia> 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    mathsMathematical functionsperformanceMust go faster

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions