Skip to content

Commit

Permalink
Short circuiting mod1 to speed offsetc (#184)
Browse files Browse the repository at this point in the history
* Short circuiting `mod1` to speed `offsetc` for simple models
  • Loading branch information
kbarros authored Nov 1, 2023
1 parent ec1a509 commit efd5ce6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/System/System.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ const Site = Union{NTuple{4, Int}, CartesianIndex{4}}
@inline to_cartesian(i::CartesianIndex{N}) where N = i
@inline to_cartesian(i::NTuple{N, Int}) where N = CartesianIndex(i)

# Like mod1(x, L), but short-circuits early in the common case. See
# https://github.com/SunnySuite/Sunny.jl/pull/184 for discussion.
@inline function altmod1(x::Int, L::Int)
1 <= x <= L ? x : mod1(x, L)
end

# Offset a `cell` by `ncells`
@inline offsetc(cell::CartesianIndex{3}, ncells, latsize) = CartesianIndex(mod1.(Tuple(cell) .+ Tuple(ncells), latsize))

Expand Down

0 comments on commit efd5ce6

Please sign in to comment.