Skip to content

Commit

Permalink
Add unsigned(T) and signed(T)
Browse files Browse the repository at this point in the history
This is in addition to the existing `unsigned(x)`, `signed(x)`.
Also extends `unsigned` and `signed` so you can call them on already
unsigned/signed types (keeping with the Base integers)
  • Loading branch information
NHDaly committed Dec 12, 2018
1 parent 219681e commit 2fddc62
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/BitIntegers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module BitIntegers
import Base: &, *, +, -, <, <<, <=, ==, >>, >>>, |, ~, AbstractFloat, add_with_overflow,
bswap, checked_abs, count_ones, div, flipsign, leading_zeros, mod,
mul_with_overflow, ndigits0zpb, promote_rule, rem, sub_with_overflow,
trailing_zeros, typemax, typemin, unsigned, xor
trailing_zeros, typemax, typemin, signed, unsigned, xor

using Base: add_int, and_int, ashr_int, bswap_int, checked_sadd_int, checked_sdiv_int,
checked_smul_int, checked_srem_int, checked_ssub_int, checked_uadd_int,
Expand Down Expand Up @@ -129,9 +129,13 @@ typemax(::Type{T}) where {T<:XBS} = bitcast(T, typemax(uinttype(T)) >> 1)

# * conversions, promotions

# ** signed
signed(::Type{T}) where T<:XBI = typeof(convert(Signed, zero(T)))

# ** unsigned

unsigned(x::XBS) = reinterpret(typeof(convert(Unsigned, zero(x))), x)
unsigned(::Type{T}) where T<:XBI = typeof(convert(Unsigned, zero(T)))
unsigned(x::T) where T<:XBI = reinterpret(unsigned(T), x)

# ** integers

Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ end
@test xu isa XU
@test xu === Unsigned(x) === unsigned(x)
@test xs === Signed(x) === signed(x)
@test typeof(xu) === unsigned(typeof(xs)) === unsigned(typeof(xu))
@test typeof(xs) === signed(typeof(xu)) === signed(typeof(xs))
if X <: Signed
@test sizeof(X) == sizeof(XU)
@test X !== XU
Expand Down

0 comments on commit 2fddc62

Please sign in to comment.