Skip to content

Commit ce3f003

Browse files
committed
also export widemul (ref #6169)
widen methods for Rational and Complex
1 parent a953172 commit ce3f003

File tree

9 files changed

+15
-14
lines changed

9 files changed

+15
-14
lines changed

NEWS.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ Library improvements
221221
* Ranges and arrays with the same elements are now unequal. This allows hashing
222222
and comparing ranges to be faster. ([#5778])
223223

224-
* New function `widen` for widening numeric types and values ([#6169])
224+
* New function `widen` for widening numeric types and values, and `widemul`
225+
for multiplying to a larger type ([#6169])
225226

226227
Deprecated or removed
227228
---------------------

base/complex.jl

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ promote_rule{T<:Real,S<:Real}(::Type{Complex{T}}, ::Type{S}) =
2626
promote_rule{T<:Real,S<:Real}(::Type{Complex{T}}, ::Type{Complex{S}}) =
2727
Complex{promote_type(T,S)}
2828

29+
widen{T}(::Type{Complex{T}}) = Complex{widen(T)}
30+
2931
real(z::Complex) = z.re
3032
imag(z::Complex) = z.im
3133
real(x::Real) = x

base/exports.jl

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ export
448448
uint64,
449449
uint8,
450450
unsigned,
451+
widemul,
451452
zero,
452453

453454
# specfun

base/gmp.jl

-2
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,8 @@ ndigits(x::BigInt, b::Integer=10) = x.size == 0 ? 1 : ndigits0z(x,b)
412412

413413
isprime(x::BigInt, reps=25) = ccall((:__gmpz_probab_prime_p,:libgmp), Cint, (Ptr{BigInt}, Cint), &x, reps) > 0
414414

415-
widemul(x::BigInt, y::BigInt) = x*y
416415
widemul(x::Int128, y::Uint128) = BigInt(x)*BigInt(y)
417416
widemul(x::Uint128, y::Int128) = BigInt(x)*BigInt(y)
418-
widemul{T<:Integer}(x::T, y::T) = BigInt(x)*BigInt(y)
419417

420418
prevpow2(x::BigInt) = x.size < 0 ? -prevpow2(-x) : (x <= 2 ? x : one(BigInt) << (ndigits(x, 2)-1))
421419
nextpow2(x::BigInt) = x.size < 0 ? -nextpow2(-x) : (x <= 2 ? x : one(BigInt) << ndigits(x-1, 2))

base/int.jl

-11
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,6 @@ end
486486

487487
## wide multiplication, Int128 multiply and divide ##
488488

489-
widemul(x::Union(Int8,Uint8,Int16), y::Union(Int8,Uint8,Int16)) = int32(x)*int32(y)
490-
widemul(x::Uint16, y::Uint16) = uint32(x)*uint32(y)
491-
492-
widemul(x::Int32, y::Int32) = int64(x)*int64(y)
493-
widemul(x::Uint32, y::Uint32) = uint64(x)*uint64(y)
494-
495-
widemul(x::Integer, y::Integer) = widemul(promote(x,y)...)
496-
497489
if WORD_SIZE==32
498490
function widemul(u::Int64, v::Int64)
499491
local u0::Uint64, v0::Uint64, w0::Uint64
@@ -564,9 +556,6 @@ if WORD_SIZE==32
564556
>>>(x::Int128, y::Int32) = y == 0 ? x : box(Int128,lshr_int(unbox(Int128,x),unbox(Int32,y)))
565557
>>>(x::Uint128, y::Int32) = y == 0 ? x : box(Uint128,lshr_int(unbox(Uint128,x),unbox(Int32,y)))
566558
else
567-
widemul(u::Int64, v::Int64) = int128(u)*int128(v)
568-
widemul(u::Uint64, v::Uint64) = uint128(u)*uint128(v)
569-
570559
*(x::Int128, y::Int128) = box(Int128,mul_int(unbox(Int128,x),unbox(Int128,y)))
571560
*(x::Uint128, y::Uint128) = box(Uint128,mul_int(unbox(Uint128,x),unbox(Uint128,y)))
572561

base/number.jl

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ ctranspose(x::Number) = conj(x)
3030
inv(x::Number) = one(x)/x
3131
angle(z::Real) = atan2(zero(z), z)
3232

33+
widemul(x::Number, y::Number) = widen(x)*widen(y)
34+
3335
start(x::Number) = false
3436
next(x::Number, state) = (x, true)
3537
done(x::Number, state) = state

base/rational.jl

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ promote_rule{T<:Integer,S<:Integer}(::Type{Rational{T}}, ::Type{S}) = Rational{p
5757
promote_rule{T<:Integer,S<:Integer}(::Type{Rational{T}}, ::Type{Rational{S}}) = Rational{promote_type(T,S)}
5858
promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Rational{T}}, ::Type{S}) = promote_type(T,S)
5959

60+
widen{T}(::Type{Rational{T}}) = Rational{widen(T)}
61+
6062
function rationalize{T<:Integer}(::Type{T}, x::FloatingPoint; tol::Real=eps(x))
6163
if isnan(x); return zero(T)//zero(T); end
6264
if x < typemin(T); return -one(T)//zero(T); end

doc/stdlib/base.rst

+4
Original file line numberDiff line numberDiff line change
@@ -3008,6 +3008,10 @@ Mathematical Functions
30083008

30093009
Compute the number of digits in number ``n`` written in base ``b``.
30103010

3011+
.. function:: widemul(x, y)
3012+
3013+
Multiply ``x`` and ``y``, giving the result as a larger type.
3014+
30113015
Data Formats
30123016
------------
30133017

test/numbers.jl

+2
Original file line numberDiff line numberDiff line change
@@ -1772,3 +1772,5 @@ end
17721772
## Note: this should change to e.g. Float128 at some point
17731773
@test widen(Float64) === BigFloat
17741774
@test widen(BigInt) === BigInt
1775+
1776+
@test widemul(typemax(Int64),typemax(Int64)) == 85070591730234615847396907784232501249

0 commit comments

Comments
 (0)