Skip to content

Commit

Permalink
Merge pull request #20 from mkitti/mk/signed_and_unsigned_type_reorg
Browse files Browse the repository at this point in the history
Reorganize type hierarchy such that SafeSigned <: Signed, SafeUnsigned <:Unsigned
  • Loading branch information
JeffreySarnoff authored Sep 10, 2021
2 parents 471647c + 1080c72 commit 823b331
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
34 changes: 20 additions & 14 deletions src/binary_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@ end


for OP in (:(<), :(<=), :(>=), :(>), :(!=), :(==), :isless, :isequal)
for SI in (SafeSigned, SafeUnsigned, SafeInteger)
@eval begin
@inline function $OP(x::T, y::T) where T<: $SI
ix = baseint(x)
iy = baseint(y)
result = $OP(ix, iy)
return safeint(result)
end

@inline function $OP(x::T1, y::T2) where {T1<:$SI, T2<: $SI}
xx, yy = promote(x, y)
return $OP(xx, yy)
end
end
end
@eval begin

@inline function $OP(x::T, y::T) where T<:SafeInteger
ix = baseint(x)
iy = baseint(y)
result = $OP(ix, iy)
return safeint(result)
end

@inline function $OP(x::T1, y::T2) where {T1<:SafeInteger, T2<:SafeInteger}
xx, yy = promote(x, y)
return $OP(xx, yy)
end

@inline function $OP(x::T1, y::T2) where {T1<:SafeSigned, T2<:Signed}
xx, yy = promote(x, y)
return $OP(xx, yy)
Expand Down Expand Up @@ -75,7 +77,11 @@ for OP in (:(<), :(<=), :(>=), :(>), :(!=), :(==), :isless, :isequal)
xx, yy = promote(x, y)
return $OP(xx, yy)
end
end
@inline function $OP(x::T1, y::T2) where {T1<:SafeInteger, T2<:Integer}
xx, yy = promote(x, y)
return $OP(xx, yy)
end
end
end


Expand Down
4 changes: 4 additions & 0 deletions src/int_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ end
return safeint(baseint( signbit(y) ? -abs(x) : abs(x) ))
end
@inline copysign(x::T, y::T) where T<:SafeUnsigned = x
@inline copysign(x::SafeSigned, y::Float16) = safeint( copysign(baseint(x), y) )
@inline copysign(x::SafeSigned, y::Float32) = safeint( copysign(baseint(x), y) )
@inline copysign(x::SafeSigned, y::Float64) = safeint( copysign(baseint(x), y) )
@inline copysign(x::SafeSigned, y::Signed) = safeint( copysign(baseint(x), y) )

@inline function flipsign(x::T, y::T) where T<:SafeSigned
return safeint(baseint( signbit(y) ? -(x) : x ))
Expand Down
12 changes: 8 additions & 4 deletions src/promote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ promote_rule(::Type{S}, ::Type{T}) where S<:SafeSigned where T<:SafeUnsigned =
promote_rule(::Type{S}, ::Type{T}) where S<:SafeUnsigned where T<:SafeUnsigned =
safeint(promote_type(baseint(S), baseint(T)))

promote_rule(::Type{S}, ::Type{T}) where S<:SafeSigned where T<:Signed = S
promote_rule(::Type{S}, ::Type{T}) where S<:SafeSigned where T<:Unsigned = S
promote_rule(::Type{S}, ::Type{T}) where S<:SafeUnsigned where T<:Signed = S
promote_rule(::Type{S}, ::Type{T}) where S<:SafeUnsigned where T<:Unsigned = S
promote_rule(::Type{S}, ::Type{T}) where S<:SafeSigned where T<:Signed =
T <: SafeInteger ? Base.Bottom : S
promote_rule(::Type{S}, ::Type{T}) where S<:SafeSigned where T<:Unsigned =
T <: SafeInteger ? Base.Bottom : S
promote_rule(::Type{S}, ::Type{T}) where S<:SafeUnsigned where T<:Signed =
T <: SafeInteger ? Base.Bottom : S
promote_rule(::Type{S}, ::Type{T}) where S<:SafeUnsigned where T<:Unsigned =
T <: SafeInteger ? Base.Bottom : S

promote_rule(::Type{S}, ::Type{T}) where S<:SafeSigned where T<:Base.IEEEFloat = T
promote_rule(::Type{S}, ::Type{T}) where S<:SafeUnsigned where T<:Base.IEEEFloat = T
Expand Down
6 changes: 3 additions & 3 deletions src/type.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
abstract type SafeInteger <: Integer end
abstract type SafeUnsigned <: SafeInteger end
abstract type SafeSigned <: SafeInteger end
abstract type SafeUnsigned <: Unsigned end
abstract type SafeSigned <: Signed end
const SafeInteger = Union{SafeUnsigned, SafeSigned}

primitive type SafeInt8 <: SafeSigned 8 end
primitive type SafeInt16 <: SafeSigned 16 end
Expand Down

0 comments on commit 823b331

Please sign in to comment.