Skip to content

Commit

Permalink
Popagate StaticInt on max/min/minmax (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokazama authored May 5, 2022
1 parent fe85e11 commit 0a5b294
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Static"
uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
authors = ["chriselrod", "ChrisRackauckas", "Tokazama"]
version = "0.6.3"
version = "0.6.4"

[deps]
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
Expand Down
4 changes: 3 additions & 1 deletion src/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ Base.:(*)(::Zero, ::Zero) = Zero()

@inline Base.:(-)(::StaticInt{M}) where {M} = StaticInt{-M}()

for f in [:(+), :(-), :(*), :(÷), :(%), :(<<), :(>>), :(>>>), :(&), :(|), :()]
for f in [:(+), :(-), :(*), :(÷), :(%), :(<<), :(>>), :(>>>), :(&), :(|), :(), :min, :max]
eval(:(Base.$f(::StaticInt{M}, ::StaticInt{N}) where {M,N} = StaticInt{$f(M,N)}()))
end

Base.minmax(x::StaticInt, y::StaticInt) = y < x ? (y, x) : (x, y)

for f in [:(<<), :(>>), :(>>>)]
@eval begin
Base.$f(@nospecialize(x::StaticInt), y::UInt) = $f(Int(x), y)
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ using Test
@test eltype(one(StaticInt)) <: Int

x = StaticInt(1)
y = static(3)
@test @inferred(minmax(x, y)) == @inferred(minmax(y, x)) == minmax(1, 3)
@test @inferred(Bool(x)) isa Bool
@test @inferred(BigInt(x)) isa BigInt
@test @inferred(Integer(x)) === x
Expand All @@ -26,7 +28,7 @@ using Test
for i Any[StaticInt(0), StaticInt(1), StaticInt(2), 3]
for j Any[StaticInt(0), StaticInt(1), StaticInt(2), 3]
i === j === 3 && continue
for f [+, -, *, ÷, %, <<, >>, >>>, &, |, , ==, , ]
for f [+, -, *, ÷, %, <<, >>, >>>, &, |, , ==, , , min, max]
(iszero(j) && ((f === ÷) || (f === %))) && continue # integer division error
@test convert(Int, @inferred(f(i,j))) == f(convert(Int, i), convert(Int, j))
end
Expand Down

2 comments on commit 0a5b294

@Tokazama
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/59728

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.4 -m "<description of version>" 0a5b294ed54e0a368cf6638a505d7d5d6b6427cb
git push origin v0.6.4

Please sign in to comment.