Skip to content

Instantly share code, notes, and snippets.

@nkottary
Created November 27, 2015 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkottary/a53749c02697e5434578 to your computer and use it in GitHub Desktop.
Save nkottary/a53749c02697e5434578 to your computer and use it in GitHub Desktop.
My fix for number test failure on ARM.
function getexponent(x::Float32)
rep = reinterpret(UInt32, x)
rawexp = ((rep & reinterpret(UInt32, Float32(Inf))) >> 23)
exp = convert(Int, rawexp) - 127 # 127 is bias
return exp
end
function getexponent(x::Float64)
rep = reinterpret(UInt64, x)
rawexp = ((rep & reinterpret(UInt64, Inf)) >> 52)
exp = convert(Int, rawexp) - 1023 # 1023 is bias
return exp
end
istoobig(::Type{UInt64}, x) = getexponent(x) >= 64
istoobig(::Type{Int64}, x) = getexponent(x) >= 63
istoobig(::Type{UInt128}, x) = getexponent(x) >= 128
istoobig(::Type{Int128}, x) = getexponent(x) >= 127
for Ti in (Int64,UInt64,Int128,UInt128)
for Tf in (Float32,Float64)
@eval begin
function myequal(x::$Tf, y::$Ti)
if istoobig($Ti, x); return false; end
fy = ($Tf)(y)
return (x == fy) & (y == unsafe_trunc($Ti, fy))
end
myequal(y::$Ti, x::$Tf) = myequal(x, y)
end
end
end
uint64float32() = typemax(UInt64) == Float32(2.0^64)
uint64float64() = typemax(UInt64) == 2.0^64
int64float32() = typemax(Int64) == Float32(2.0^63)
int64float64() = typemax(Int64) == 2.0^63
uint128float32() = typemax(UInt128) == Float32(2.0^128)
uint128float64() = typemax(UInt128) == 2.0^128
int128float32() = typemax(Int128) == Float32(2.0^127)
int128float64() = typemax(Int128) == 2.0^127
myuint64float32() = myequal(typemax(UInt64), Float32(2.0^64))
myuint64float64() = myequal(typemax(UInt64), 2.0^64)
myint64float32() = myequal(typemax(Int64), Float32(2.0^63))
myint64float64() = myequal(typemax(Int64), 2.0^63)
myuint128float32() = myequal(typemax(UInt128), Float32(2.0^128))
myuint128float64() = myequal(typemax(UInt128), 2.0^128)
myint128float32() = myequal(typemax(Int128), Float32(2.0^127))
myint128float64() = myequal(typemax(Int128), 2.0^127)
function main()
# Incorrect results
@show uint64float32()
@show uint64float64()
@show int64float32()
@show int64float64()
# Correct results but relying on UB
@show uint128float32()
@show uint128float64()
@show int128float32()
@show int128float64()
println("\n-------------------------------------\n")
# Correct results
@show myuint64float32()
@show myuint64float64()
@show myint64float32()
@show myint64float64()
@show myuint128float32()
@show myuint128float64()
@show myint128float32()
@show myint128float64()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment