Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x % Unsigned, x % Signed #34864

Merged
merged 36 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6a35def
support `x % Unsigned` where `isa(x, Signed)`
JeffreySarnoff Feb 24, 2020
6b87b76
support `x % Unsigned` where `isa(x, Signed)`
JeffreySarnoff Feb 25, 2020
17f3c79
support `x % Unsigned` where `isa(x, Signed)`
JeffreySarnoff Feb 25, 2020
58f8708
`x % Unsigned` where `isa(x, Signed)` (remove comma)
JeffreySarnoff Feb 25, 2020
65367c5
`x::Signed % Unsigned, x::Unsigned % Signed`
JeffreySarnoff Feb 25, 2020
1a1b028
`rem(x::Unsigned, Signed)`
JeffreySarnoff Feb 25, 2020
bcaa805
`x::Unsigned % Signed`
JeffreySarnoff Feb 25, 2020
fb30b63
remove trailing space
JeffreySarnoff Feb 25, 2020
bfd416b
interconvert BitInteger pairs
JeffreySarnoff Feb 25, 2020
536948e
remove `unsigned(<:Signed)` lines, import `unsigned` from Base
JeffreySarnoff Feb 25, 2020
152d6e8
add tests, improve tests
JeffreySarnoff Feb 25, 2020
98204f5
news about `signed(T)`, `unsigned(T)`
JeffreySarnoff Feb 25, 2020
c1988c1
remove trailing whitespace
JeffreySarnoff Feb 25, 2020
61e2362
fix test
JeffreySarnoff Feb 25, 2020
3fbcf18
fix missing `begin`
JeffreySarnoff Feb 25, 2020
a1da6f4
add missing end
JeffreySarnoff Feb 25, 2020
8542442
revise tests
JeffreySarnoff Feb 25, 2020
9172d4d
fixup typo in test
JeffreySarnoff Feb 25, 2020
612b19d
restore multinverses.jl
JeffreySarnoff Feb 29, 2020
53ec10b
remove `unsigned(::Type{BitSigned})` (was available). add `signed(::…
JeffreySarnoff Feb 29, 2020
3371f21
add tests `BigInt(x) % Signed` `BigInt(x) % Unsigned`
JeffreySarnoff Feb 29, 2020
0518e73
resolve conflict?
JeffreySarnoff Feb 29, 2020
a8d2684
Merge branch 'master' into jas/remUnsigned
JeffreySarnoff Feb 29, 2020
e16cffb
add back what was a merge conflict in News
JeffreySarnoff Feb 29, 2020
a67e742
Update NEWS.md
JeffreySarnoff Mar 3, 2020
cd61928
Merge branch 'master' into jas/remUnsigned
JeffreySarnoff Mar 5, 2020
ecb9966
remove `signed(UIntNN) = IntNN` (does it fix?)
JeffreySarnoff Mar 14, 2020
3cbbec4
remove `signed(UIntNN) == IntNN` tests (does it fix?)
JeffreySarnoff Mar 14, 2020
574d8a6
restore `signed(::Type{UIntNN}) = IntNN`
JeffreySarnoff Mar 14, 2020
100884e
restore `signed(::Type{UIntNN}) = IntNN` tests
JeffreySarnoff Mar 14, 2020
cf1954a
whitespace
JeffreySarnoff Mar 14, 2020
03891ac
Merge branch 'master' into jas/remUnsigned
JeffreySarnoff Mar 27, 2020
6dc7fab
remove old line
JeffreySarnoff Mar 27, 2020
9c2befc
combine 3 testsets
JeffreySarnoff Mar 27, 2020
7b00bd5
docstrings for signed(T) unsigned(T)
JeffreySarnoff Mar 27, 2020
f46360a
move docstring up
JeffreySarnoff Mar 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ New library features

* `isapprox` (or `≈`) now has a one-argument "curried" method `isapprox(x)` which returns a function, like `isequal` (or `==`)` ([#32305]).
* `Ref{NTuple{N,T}}` can be passed to `Ptr{T}`/`Ref{T}` `ccall` signatures ([#34199])

* `x::Signed % Unsigned` and `x::Unsigned % Signed` are supported for integer bitstypes.
* `unsigned(signed_type)` and `signed(unsigned_type)` are supported for integer bitstypes.

Standard library changes
------------------------
Expand Down
18 changes: 18 additions & 0 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ const BitUnsigned64T = Union{Type{UInt8}, Type{UInt16}, Type{UInt32}, Type{UIn

const BitIntegerType = Union{map(T->Type{T}, BitInteger_types)...}

# interconvert equilength BitInteger types

unsigned(::Type{Int8}) = UInt8
Copy link
Member

Choose a reason for hiding this comment

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

These should already be defined. Why is this necessary?

Copy link
Contributor Author

@JeffreySarnoff JeffreySarnoff Feb 29, 2020

Choose a reason for hiding this comment

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

unsigned(::Type{IntN}) = UIntN are defined in Base/multinverses.jl.
signed(::Type{UIntN}) = IntN are not defined.
I thought the two sets of definitions belonged in the same place, and that Int.jl was more appropriate than multinverses.jl. I moved the defs from multinverses.jl to Int.jl and added the defs for signed(_) there. I added unsigned to the using Base: line in multinverses.jl.

I can revert this and I could add the signed(_) defs into multinverses.jl. multiinverses is a module, would I need to export signed after adding it there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

all that is undone

unsigned(::Type{Int16}) = UInt16
unsigned(::Type{Int32}) = UInt32
unsigned(::Type{Int64}) = UInt64
unsigned(::Type{Int128}) = UInt128
unsigned(::Type{T}) where {T<:Unsigned} = T

signed(::Type{UInt8}) = Int8
signed(::Type{UInt16}) = Int16
signed(::Type{UInt32}) = Int32
signed(::Type{UInt64}) = Int64
signed(::Type{UInt128}) = Int128
signed(::Type{T}) where {T<:Signed} = T

## integer comparisons ##

(<)(x::T, y::T) where {T<:BitSigned} = slt_int(x, y)
Expand Down Expand Up @@ -506,6 +522,8 @@ if nameof(@__MODULE__) === :Base
end

rem(x::T, ::Type{T}) where {T<:Integer} = x
rem(x::Signed, ::Type{Unsigned}) = x % unsigned(typeof(x))
rem(x::Unsigned, ::Type{Signed}) = x % signed(typeof(x))
rem(x::Integer, T::Type{<:Integer}) = convert(T, x) # `x % T` falls back to `convert`
rem(x::Integer, ::Type{Bool}) = ((x & 1) != 0)
mod(x::Integer, ::Type{T}) where {T<:Integer} = rem(x, T)
Expand Down
9 changes: 1 addition & 8 deletions base/multinverses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
module MultiplicativeInverses

import Base: div, divrem, rem, unsigned
using Base: IndexLinear, IndexCartesian, tail
using Base: IndexLinear, IndexCartesian, tail, unsigned
export multiplicativeinverse

unsigned(::Type{Int8}) = UInt8
unsigned(::Type{Int16}) = UInt16
unsigned(::Type{Int32}) = UInt32
unsigned(::Type{Int64}) = UInt64
unsigned(::Type{Int128}) = UInt128
unsigned(::Type{T}) where {T<:Unsigned} = T

abstract type MultiplicativeInverse{T} end

# Computes integer division by a constant using multiply, add, and bitshift.
Expand Down
16 changes: 15 additions & 1 deletion test/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,27 @@ end
@test unsafe_trunc(Int8, 128) === Int8(-128)
@test unsafe_trunc(Int8, -127) === Int8(-127)
@test unsafe_trunc(Int8, -128) === Int8(-128)
@test unsafe_trunc(Int8, -129) === Int8(127)
@test unsafe_trunc(I, -129) === Int8(127)
end
@testset "x % T returns a T, T = $T" for T in [Base.BitInteger_types..., BigInt],
U in [Base.BitInteger_types..., BigInt]
@test typeof(rand(U(0):U(127)) % T) === T
end

@testset "signed(<:Unsigned), unsigned(<:Signed) for bitstypes" begin
for (S,U) in zip(Base.BitSigned_types, Base.BitUnsigned_types)
@test signed(U) === S
@test unsigned(S) === U
end
@testset "s::Signed % Unsigned returns u::T, T = $T" for T in Base.BitSigned_types
@test (typemin(T) % Unsigned) % Signed === typemin(T)
@test typeof(typemin(T) % Unsigned) <: Unsigned
end
@testset "u::Unsigned % Signed returns s::T, T = $T" for T in Base.BitUnsigned_types
@test (typemax(T) % Signed) % Unsigned === typemax(T)
@test typeof(typemax(T) % Signed) <: Signed
end

@testset "issue #15489" begin
@test 0x00007ffea27edaa0 + (-40) === (-40) + 0x00007ffea27edaa0 === 0x00007ffea27eda78
@test UInt64(1) * Int64(-1) === typemax(UInt64)
Expand Down