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

fixed Float16 from Float64 and BigFloat #42837

Merged
merged 7 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 15 additions & 2 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,21 @@ Float32(x::BigFloat, r::MPFRRoundingMode=ROUNDING_MODE[]) =
_cpynansgn(ccall((:mpfr_get_flt,:libmpfr), Float32, (Ref{BigFloat}, MPFRRoundingMode), x, r), x)
Float32(x::BigFloat, r::RoundingMode) = Float32(x, convert(MPFRRoundingMode, r))

# TODO: avoid double rounding
Float16(x::BigFloat) = Float16(Float64(x))
function Float16(x::BigFloat) :: Float16
res = Float32(x)
resi = reinterpret(UInt32, res)
if (resi&0x7fffffff) < 0x38800000 # if Float16(res) is subnormal
#shift so that the mantissa lines up where it would for normal Float16
shift = 113-((resi & 0x7f800000)>>23)
shift<23 && (resi >>= shift)
end
if (resi & 0x1fff == 0x1000) # if we are halfway between 2 Float16 values
vchuravy marked this conversation as resolved.
Show resolved Hide resolved
memcpy(&resi, &res, sizeof(res));
# adjust the value by 1 ULP in the direction that will make Float16(res) give the right answer
res = nextfloat(res, cmp(x, res))
end
return res
end

promote_rule(::Type{BigFloat}, ::Type{<:Real}) = BigFloat
promote_rule(::Type{BigInt}, ::Type{<:AbstractFloat}) = BigFloat
Expand Down
18 changes: 17 additions & 1 deletion src/runtime_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "julia.h"
#include "julia_internal.h"
#include "APInt-C.h"
#include "stdio.h"
vchuravy marked this conversation as resolved.
Show resolved Hide resolved

const unsigned int host_char_bit = 8;

Expand Down Expand Up @@ -205,7 +206,22 @@ JL_DLLEXPORT uint16_t __gnu_f2h_ieee(float param)

JL_DLLEXPORT uint16_t __truncdfhf2(double param)
{
return float_to_half((float)param);
float res = (float)param;
uint32_t resi;
memcpy(&resi, &res, sizeof(res));
if ((resi&0x7fffffffu) < 0x38800000u){ // if Float16(res) is subnormal
// shift so that the mantissa lines up where it would for normal Float16
uint32_t shift = 113u-((resi & 0x7f800000u)>>23u);
if (shift<23u)
resi >>= shift;
}
if ((resi & 0x1fffu) == 0x1000u) { // if we are halfway between 2 Float16 values
memcpy(&resi, &res, sizeof(res));
// adjust the value by 1 ULP in the direction that will make Float16(res) give the right answer
resi += (fabs(res) < fabs(param)) - (fabs(param) < fabs(res));
memcpy(&res, &resi, sizeof(res));
}
return float_to_half(res);
}

#endif
Expand Down
23 changes: 23 additions & 0 deletions test/float16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,26 @@ const minsubf16_32 = Float32(minsubf16)

# issues #33076
@test Float16(1f5) == Inf16

@testset "conversion to Float16 from" begin
for T in (Float32, Float64, BigFloat)
@testset "conversion from $T" begin
for i in 1:2^16
f = reinterpret(Float16, UInt16(i-1))
isfinite(f) || continue
abs(f)<=eps(f) && continue
if f < 0
epsdown = T(eps(f))/2
epsup = issubnormal(f) ? epsdown : T(eps(nextfloat(f)))/2
else
epsup = T(eps(f))/2
epsdown = issubnormal(f) ? epsup : T(eps(prevfloat(f)))/2
end
@test isequal(f, Float16(nextfloat(T(f) - epsdown)))
@test isequal(f, Float16(prevfloat(T(f) + epsup)))
@test isequal(prevfloat(f), Float16(prevfloat(T(f) - epsdown)))
@test isequal(nextfloat(f), Float16(nextfloat(T(f) + epsup)))
end
end
end
end