From 995e78b64e27c45845e9a845ba0fbff565c43833 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Wed, 13 Oct 2021 14:51:40 -0500 Subject: [PATCH] add overflow check --- base/intfuncs.jl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index ec57f7f80d809..0cc033f2c801f 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -295,8 +295,14 @@ function power_by_squaring(x::Bool, p::Integer) return (p==0) | x end -^(x::T, p::T) where {T<:Integer} = power_by_squaring(x,p) -^(x::Number, p::Integer) = power_by_squaring(x,p) +@inline function ^(x::T, p::Integer) where T <: BitInteger #check for overflow + ans = power_by_squaring(x,p) + low_bound = ((sizeof(x)<<3)-leading_zeros(x)-1)*p + trailing_ones(typemax(T)) < low_bound || ans < (one(T)<