|
| 1 | +using Base: BitInteger, promote, afoldl, @_inline_meta |
| 2 | +import Base.Checked: checked_neg, checked_add, checked_sub, checked_mul, checked_abs, |
| 3 | + checked_div, checked_fld, checked_cld, checked_mod, checked_rem |
| 4 | +using Base.Checked: mul_with_overflow |
| 5 | + |
| 6 | +if VERSION ≥ v"1.11-alpha" |
| 7 | + import Base: power_by_squaring |
| 8 | + import Base.Checked: checked_pow |
| 9 | +else |
| 10 | + using Base: throw_domerr_powbysq, to_power_type |
| 11 | + using Base.Checked: throw_overflowerr_binaryop |
| 12 | +end |
| 13 | + |
| 14 | +# resolve ambiguity when `-` used as symbol |
| 15 | +checked_negsub(x) = checked_neg(x) |
| 16 | +checked_negsub(x, y) = checked_sub(x, y) |
| 17 | + |
| 18 | +# The Base div methods have checked semantics, so just pass through |
| 19 | +checked_div(x...) = Base.:÷(x...) |
| 20 | +checked_fld(x...) = Base.fld(x...) |
| 21 | +checked_cld(x...) = Base.cld(x...) |
| 22 | +checked_rem(x...) = Base.:%(x...) # Yes, % is `rem`, not `mod` |
| 23 | +checked_mod(x...) = Base.mod(x...) |
| 24 | +checked_divrem(x...) = Base.divrem(x...) |
| 25 | + |
| 26 | +# convert multi-argument calls into nested two-argument calls |
| 27 | +checked_add(a, b, c, xs...) = @checked (@_inline_meta; afoldl(+, (+)((+)(a, b), c), xs...)) |
| 28 | +checked_sub(a, b, c, xs...) = @checked (@_inline_meta; afoldl(-, (-)((-)(a, b), c), xs...)) |
| 29 | +checked_mul(a, b, c, xs...) = @checked (@_inline_meta; afoldl(*, (*)((*)(a, b), c), xs...)) |
| 30 | + |
| 31 | +# promote unmatched number types to same type |
| 32 | +checked_add(x::Number, y::Number) = checked_add(promote(x, y)...) |
| 33 | +checked_sub(x::Number, y::Number) = checked_sub(promote(x, y)...) |
| 34 | +checked_mul(x::Number, y::Number) = checked_mul(promote(x, y)...) |
| 35 | +checked_pow(x::Number, y::Number) = checked_pow(promote(x, y)...) |
| 36 | + |
| 37 | +# fallback to `unchecked_` for `Number` types that don't have more specific `checked_` methods |
| 38 | +checked_neg(x::T) where T <: Number = unchecked_neg(x) |
| 39 | +checked_add(x::T, y::T) where T <: Number = unchecked_add(x, y) |
| 40 | +checked_sub(x::T, y::T) where T <: Number = unchecked_sub(x, y) |
| 41 | +checked_mul(x::T, y::T) where T <: Number = unchecked_mul(x, y) |
| 42 | +checked_pow(x::T, y::T) where T <: Number = unchecked_pow(x, y) |
| 43 | +checked_abs(x::T) where T <: Number = unchecked_abs(x) |
| 44 | + |
| 45 | +# fallback to `unchecked_` for non-`Number` types |
| 46 | +checked_neg(x) = unchecked_neg(x) |
| 47 | +checked_add(x, y) = unchecked_add(x, y) |
| 48 | +checked_sub(x, y) = unchecked_sub(x, y) |
| 49 | +checked_mul(x, y) = unchecked_mul(x, y) |
| 50 | +checked_pow(x, y) = unchecked_pow(x, y) |
| 51 | +checked_abs(x) = unchecked_abs(x) |
| 52 | + |
| 53 | +if VERSION < v"1.11" |
| 54 | + # Base.Checked only gained checked powers in 1.11 |
| 55 | + |
| 56 | + checked_pow(x_::T, p::S) where {T <: BitInteger, S <: BitInteger} = |
| 57 | + power_by_squaring(x_, p; mul = checked_mul) |
| 58 | + |
| 59 | + # Base.@assume_effects :terminates_locally # present in Julia 1.11 code, but only supported from 1.8 on |
| 60 | + function power_by_squaring(x_, p::Integer; mul=*) |
| 61 | + x = to_power_type(x_) |
| 62 | + if p == 1 |
| 63 | + return copy(x) |
| 64 | + elseif p == 0 |
| 65 | + return one(x) |
| 66 | + elseif p == 2 |
| 67 | + return mul(x, x) |
| 68 | + elseif p < 0 |
| 69 | + isone(x) && return copy(x) |
| 70 | + isone(-x) && return iseven(p) ? one(x) : copy(x) |
| 71 | + throw_domerr_powbysq(x, p) |
| 72 | + end |
| 73 | + t = trailing_zeros(p) + 1 |
| 74 | + p >>= t |
| 75 | + while (t -= 1) > 0 |
| 76 | + x = mul(x, x) |
| 77 | + end |
| 78 | + y = x |
| 79 | + while p > 0 |
| 80 | + t = trailing_zeros(p) + 1 |
| 81 | + p >>= t |
| 82 | + while (t -= 1) >= 0 |
| 83 | + x = mul(x, x) |
| 84 | + end |
| 85 | + y = mul(y, x) |
| 86 | + end |
| 87 | + return y |
| 88 | + end |
| 89 | +end |
0 commit comments