8000 [Bug #20662] Preserve payloads in packed NaN by nobu · Pull Request #11352 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

[Bug #20662] Preserve payloads in packed NaN #11352

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Bug #20662] Preserve payload in packed single-precision NaN
  • Loading branch information
nobu authored and hsbt committed Sep 20, 2024
commit 964d0317132be70d2197b6fd3da739ee679e6640
42 changes: 36 additions & 6 deletions pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,44 @@ unknown_directive(const char *mode, char type, VALUE fmt)
mode, unknown, fmt);
}

/* convert float <=> double as IEEE 754-2008, with keeping quiet bit */
static const uint32_t F32_QBIT = (uint32_t)1U << (FLT_MANT_DIG-2);
static const uint64_t F64_QBIT = (uint64_t)1U << (DBL_MANT_DIG-2);
static const uint32_t F32_MANT = ~((uint32_t)~0U << (FLT_MANT_DIG-1));
static const int F64_QBIT_OFFSET = DBL_MANT_DIG - FLT_MANT_DIG;

typedef union {
float f;
double d;
uint32_t u32;
uint64_t u64;
} float_double_conv;

static inline double
unpack_flt2dbl(uint32_t u)
{
float_double_conv tmp = {.u32 = u};
tmp.d = tmp.f;
if (isnan(tmp.d)) {
uint64_t qbit = ((uint64_t)(u & F32_QBIT) << F64_QBIT_OFFSET);
tmp.u64 = (tmp.u64 & ~F64_QBIT) | qbit;
}
return tmp.d;
}

static float
VALUE_to_float(VALUE obj)
{
VALUE v = rb_to_float(obj);
double d = RFLOAT_VALUE(v);

if (isnan(d)) {
return NAN;
float_double_conv tmp = {.d = d};
uint32_t qbit = (uint32_t)((tmp.u64 & F64_QBIT) >> F64_QBIT_OFFSET);
tmp.f = (float)d;
tmp.u32 = (tmp.u32 & ~F32_QBIT) | qbit;
if (!(tmp.u32 & F32_MANT)) tmp.u32 |= 1;
return tmp.f;
}
else if (d < -FLT_MAX) {
return -INFINITY;
Expand Down Expand Up @@ -1291,9 +1321,9 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)
case 'F':
PACK_LENGTH_ADJUST_SIZE(sizeof(float));
while (len-- > 0) {
float tmp;
UNPACK_FETCH(&tmp, float);
UNPACK_PUSH(DBL2NUM((double)tmp));
FLOAT_CONVWITH(tmp);
UNPACK_FETCH(&tmp.buf, float);
UNPACK_PUSH(DBL2NUM(unpack_flt2dbl(tmp.u)));
}
PACK_ITEM_ADJUST();
break;
Expand All @@ -1304,7 +1334,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)
FLOAT_CONVWITH(tmp);
UNPACK_FETCH(tmp.buf, float);
VTOHF(tmp);
UNPACK_PUSH(DBL2NUM(tmp.f));
UNPACK_PUSH(DBL2NUM(unpack_flt2dbl(tmp.u)));
}
PACK_ITEM_ADJUST();
break;
Expand Down Expand Up @@ -1337,7 +1367,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)
FLOAT_CONVWITH(tmp);
UNPACK_FETCH(tmp.buf, float);
NTOHF(tmp);
UNPACK_PUSH(DBL2NUM(tmp.f));
UNPACK_PUSH(DBL2NUM(unpack_flt2dbl(tmp.u)));
}
PACK_ITEM_ADJUST();
break;
Expand Down
19 changes: 18 additions & 1 deletion test/ruby/test_pack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -532,16 +532,33 @@ def test_pack_unpack_fdeEgG
nan = inf/inf
[0.0, 1.0, 3.0, inf, -inf, nan].each do |x|
%w(f d e E g G).each do |f|
v = [x].pack(f).unpack(f)
packed = [x].pack(f)
v = packed.unpack(f)
if x.nan?
assert_predicate(v.first, :nan?)
assert_equal(packed, v.pack(f))
else
assert_equal([x], v)
end
end
end
end

def test_pack_unpack_float_nan
snan = [0x7fc00000, 0x7fffe000, 0x7fbfe000, 0x7ffff000, 0x7fbff000]
snan.concat(snan.map {|x| 0x80000000 | x}) # negative
snan.each do |x|
msg = "nan(%x)" % x
packed = [x].pack("L")
f = packed.unpack1("f")

assert_predicate(f, :nan?, msg)
assert_equal packed, [f].pack("f"), msg
b64 = [((x & 0xff80_0000) << 32) | 0x70_0000_0000_0000 | ((x & 0x007f_ffff) << 29)].pack("Q")
assert_equal b64, [f].pack("d"), msg
end
end

def test_pack_unpack_x
assert_equal("", [].pack("x0"))
assert_equal("\0", [].pack("x"))
Expand Down
0