Skip to content

Commit

Permalink
Base: power_by_squaring: don't require one (#57590)
Browse files Browse the repository at this point in the history
* Follows up on/partially reverts #55634
* Reopens #53504, perhaps it's OK to require some types to implement
their own `^` for good performance
* Fixes #57390, a regression

(cherry picked from commit a984a21)
  • Loading branch information
nsajko authored and KristofferC committed Mar 4, 2025
1 parent 4942b1c commit 4eaae59
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ end

# ^ for any x supporting *
function to_power_type(x::Number)
T = promote_type(typeof(x), typeof(one(x)), typeof(x*x))
T = promote_type(typeof(x), typeof(x*x))
convert(T, x)
end
to_power_type(x) = oftype(x*x, x)
Expand Down
9 changes: 9 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

include("testhelpers/EvenIntegers.jl")
using .EvenIntegers

using Random
using LinearAlgebra
using Base.Experimental: @force_compile
Expand Down Expand Up @@ -1538,6 +1541,12 @@ end
@test all((t -> ===(t...)), zip(x^y, p[y + 1]))
end
end

@testset "rng exponentiation, issue #57590" begin
@test EvenInteger(16) === @inferred EvenInteger(2)^4
@test EvenInteger(16) === @inferred EvenInteger(2)^Int(4) # avoid `literal_pow`
@test EvenInteger(16) === @inferred EvenInteger(2)^EvenInteger(4)
end
end

# Test that sqrt behaves correctly and doesn't exhibit fp80 double rounding.
Expand Down
87 changes: 87 additions & 0 deletions test/testhelpers/EvenIntegers.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""
The even integers, an example of set with an additive identity and closed under
addition and multiplication, but lacking a multiplicative identity, a
[*rng*](https://en.wikipedia.org/wiki/Rng_(algebra)).
"""
module EvenIntegers
export EvenInteger

struct EvenInteger{T <: Integer} <: Integer
x::T
function EvenInteger(x::Integer)
if isodd(x)
throw(ArgumentError("can't convert odd integer to even integer"))
end
new{typeof(x)}(x)
end
end
function EvenInteger(x::EvenInteger)
x
end
function EvenInteger{T}(x::EvenInteger{T}) where {T <: Integer}
x
end
function EvenInteger{T}(x::T) where {T <: Integer}
EvenInteger(x)
end
function EvenInteger{T}(x::Integer) where {T <: Integer}
throw(ArgumentError("not implemented"))
end
function Base.Int(n::EvenInteger)
Int(n.x)
end
function Base.iseven(::EvenInteger)
true
end
function Base.isodd(::EvenInteger)
false
end
function Base.iszero(n::EvenInteger)
iszero(n.x)
end
function Base.isone(::EvenInteger)
false
end
function Base.zero(n::EvenInteger)
EvenInteger(zero(n.x))
end
function Base.zero(::Type{EvenInteger{T}}) where {T <: Integer}
EvenInteger(zero(T))
end
function Base.:(==)(l::EvenInteger, r::EvenInteger)
l.x == r.x
end
function Base.:(<)(l::EvenInteger, r::EvenInteger)
l.x < r.x
end
function Base.promote_rule(::Type{EvenInteger{L}}, ::Type{EvenInteger{R}}) where {L <: Integer, R <: Integer}
EvenInteger{promote_type(L, R)}
end
function Base.promote_rule(::Type{EvenInteger{L}}, ::Type{R}) where {L <: Integer, R <: Integer}
promote_type(L, R)
end
function Base.:(+)(l::EvenInteger, r::EvenInteger)
EvenInteger(l.x + r.x)
end
function Base.:(*)(l::EvenInteger, r::EvenInteger)
EvenInteger(l.x * r.x)
end
function Base.:(-)(n::EvenInteger)
EvenInteger(-n.x)
end
function Base.:(-)(l::EvenInteger, r::EvenInteger)
l + (-r)
end
function right_shift(l::EvenInteger, r::Integer)
l.x >> r
end
function Base.:(>>)(l::EvenInteger, r::Integer)
right_shift(l, r)
end
function Base.:(>>)(l::EvenInteger, r::Int) # resolve dispatch ambiguity
right_shift(l, r)
end
function Base.trailing_zeros(n::EvenInteger)
trailing_zeros(n.x)
end
end

0 comments on commit 4eaae59

Please sign in to comment.