Skip to content

Commit

Permalink
Handle flipsign(Float16) efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Dec 7, 2015
1 parent 2b56de1 commit c81dccf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ for T in (Int8,Int16,Int32,Int64,Int128)
end

flipsign(x::Signed, y::Signed) = flipsign(promote(x,y)...)
flipsign(x::Signed, y::Float16) = flipsign(x, reinterpret(Int16,y))
flipsign(x::Signed, y::Float32) = flipsign(x, reinterpret(Int32,y))
flipsign(x::Signed, y::Float64) = flipsign(x, reinterpret(Int64,y))
flipsign(x::Signed, y::Real) = flipsign(x, -oftype(x,signbit(y)))

copysign(x::Signed, y::Signed) = flipsign(x, x$y)
copysign(x::Signed, y::Float16) = copysign(x, reinterpret(Int16,y))
copysign(x::Signed, y::Float32) = copysign(x, reinterpret(Int32,y))
copysign(x::Signed, y::Float64) = copysign(x, reinterpret(Int64,y))
copysign(x::Signed, y::Real) = copysign(x, -oftype(x,signbit(y)))
Expand Down
6 changes: 6 additions & 0 deletions test/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ for y in (4, Float32(4), 4.0, big(4.0))
@test copysign(-3, y) == 3
end

for s1 in (-1,+1), s2 in (-1,+1)
@test flipsign(Int16(3s1), Float16(3s2)) === Int16(3s1*s2)
@test flipsign(Int32(3s1), Float32(3s2)) === Int32(3s1*s2)
@test flipsign(Int64(3s1), Float64(3s2)) === Int64(3s1*s2)
end

@test signed(3) == 3
@test signed(UInt(3)) == 3
@test isa(signed(UInt(3)), Int)
Expand Down

0 comments on commit c81dccf

Please sign in to comment.