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

Reorganize type hierarchy such that SafeSigned <: Signed, SafeUnsigned <:Unsigned #20

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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