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

Minor fixes for dump printing #2342

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions src/literal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,13 @@ void FloatWriter<T>::WriteHex(char* out, size_t size, Uint bits) {
exp -= leading_zeroes;
}

*p++ = '.';
while (sig) {
int nybble = (sig >> kTopNybbleShift) & 0xf;
*p++ = s_hex_digits[nybble];
sig <<= 4;
if (sig) {
*p++ = '.';
while (sig) {
int nybble = (sig >> kTopNybbleShift) & 0xf;
*p++ = s_hex_digits[nybble];
sig <<= 4;
}
}
}
*p++ = 'p';
Expand Down
190 changes: 190 additions & 0 deletions src/test-hexfloat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

#include <array>
#include <cstdio>
#include <thread>
#include <vector>
Expand Down Expand Up @@ -262,3 +263,192 @@ class ManyDoublesRoundtripTest : public ThreadedTest {
TEST_F(ManyDoublesRoundtripTest, Run) {
RunThreads();
}

class SpecificFloatsTest : public ::testing::Test {
protected:
uint32_t ConstructUint32(const std::array<uint8_t, 4>& bytes) {
uint32_t result = 0;
for (int i = 0; i < 4; ++i) {
result |= static_cast<uint32_t>(bytes[i]) << (i * 8);
}
return result;
}

uint64_t ConstructUint64(const std::array<uint8_t, 8>& bytes) {
uint64_t result = 0;
for (int i = 0; i < 8; ++i) {
result |= static_cast<uint64_t>(bytes[i]) << (i * 8);
}
return result;
}

void TestFloat(const std::array<uint8_t, 4>& bytes,
const std::string& expected_output) {
uint32_t float_bits = ConstructUint32(bytes);
char buffer[100];
WriteFloatHex(buffer, sizeof(buffer), float_bits);
ASSERT_STREQ(buffer, expected_output.c_str());
uint32_t parsed_bits;
ASSERT_EQ(Result::Ok, ParseFloat(LiteralType::Hexfloat, buffer,
buffer + strlen(buffer), &parsed_bits));
ASSERT_EQ(parsed_bits, float_bits);
}

void TestDouble(const std::array<uint8_t, 8>& bytes,
const std::string& expected_output) {
uint64_t float_bits = ConstructUint64(bytes);
char buffer[100];
WriteDoubleHex(buffer, sizeof(buffer), float_bits);
ASSERT_STREQ(buffer, expected_output.c_str());
uint64_t parsed_bits;
ASSERT_EQ(Result::Ok, ParseDouble(LiteralType::Hexfloat, buffer,
buffer + strlen(buffer), &parsed_bits));
ASSERT_EQ(parsed_bits, float_bits);
}
};

TEST_F(SpecificFloatsTest, Run) {
TestFloat({0x00, 0x00, 0x00, 0x80}, "-0x0p+0");
TestFloat({0x00, 0x00, 0x00, 0x00}, "0x0p+0");
TestFloat({0x01, 0x00, 0x80, 0xd8}, "-0x1.000002p+50");
TestFloat({0x01, 0x00, 0x80, 0xa6}, "-0x1.000002p-50");
TestFloat({0x01, 0x00, 0x80, 0x58}, "0x1.000002p+50");
TestFloat({0x01, 0x00, 0x80, 0x26}, "0x1.000002p-50");
TestFloat({0x01, 0x00, 0x00, 0x7f}, "0x1.000002p+127");
TestFloat({0x02, 0x00, 0x80, 0xd8}, "-0x1.000004p+50");
TestFloat({0x02, 0x00, 0x80, 0xa6}, "-0x1.000004p-50");
TestFloat({0x02, 0x00, 0x80, 0x58}, "0x1.000004p+50");
TestFloat({0x02, 0x00, 0x80, 0x26}, "0x1.000004p-50");
TestFloat({0x03, 0x00, 0x80, 0xd8}, "-0x1.000006p+50");
TestFloat({0x03, 0x00, 0x80, 0xa6}, "-0x1.000006p-50");
TestFloat({0x03, 0x00, 0x80, 0x58}, "0x1.000006p+50");
TestFloat({0x03, 0x00, 0x80, 0x26}, "0x1.000006p-50");
TestFloat({0xb4, 0xa2, 0x11, 0x52}, "0x1.234568p+37");
TestFloat({0xb4, 0xa2, 0x91, 0x5b}, "0x1.234568p+56");
TestFloat({0xb4, 0xa2, 0x11, 0x65}, "0x1.234568p+75");
TestFloat({0x99, 0x76, 0x96, 0xfe}, "-0x1.2ced32p+126");
TestFloat({0x99, 0x76, 0x96, 0x7e}, "0x1.2ced32p+126");
TestFloat({0x03, 0x00, 0x00, 0x80}, "-0x1.8p-148");
TestFloat({0x03, 0x00, 0x00, 0x00}, "0x1.8p-148");
TestFloat({0xff, 0x2f, 0x59, 0x2d}, "0x1.b25ffep-37");
TestFloat({0xa3, 0x79, 0xeb, 0x4c}, "0x1.d6f346p+26");
TestFloat({0x7b, 0x4d, 0x7f, 0x6c}, "0x1.fe9af6p+89");
TestFloat({0x00, 0x00, 0x00, 0xff}, "-0x1p+127");
TestFloat({0x00, 0x00, 0x00, 0x7f}, "0x1p+127");
TestFloat({0x02, 0x00, 0x00, 0x80}, "-0x1p-148");
TestFloat({0x02, 0x00, 0x00, 0x00}, "0x1p-148");
TestFloat({0x01, 0x00, 0x00, 0x80}, "-0x1p-149");
TestFloat({0x01, 0x00, 0x00, 0x00}, "0x1p-149");
TestFloat({0x00, 0x00, 0x80, 0xd8}, "-0x1p+50");
TestFloat({0x00, 0x00, 0x80, 0xa6}, "-0x1p-50");
TestFloat({0x00, 0x00, 0x80, 0x58}, "0x1p+50");
TestFloat({0x00, 0x00, 0x80, 0x26}, "0x1p-50");
TestFloat({0x00, 0x00, 0x80, 0x3f}, "0x1p+0");
TestFloat({0x00, 0x00, 0x80, 0xbf}, "-0x1p+0");
TestFloat({0xff, 0xff, 0x7f, 0x7f}, "0x1.fffffep+127");
TestFloat({0xff, 0xff, 0x7f, 0xff}, "-0x1.fffffep+127");
TestFloat({0x00, 0x00, 0x80, 0x4b}, "0x1p+24");
TestFloat({0x00, 0x00, 0x80, 0xcb}, "-0x1p+24");
TestFloat({0xa4, 0x70, 0x9d, 0x3f}, "0x1.3ae148p+0");

TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, "-0x0p+0");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "0x0p+0");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xc3},
"-0x1.0000000000001p+60");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x43},
"0x1.0000000000001p+60");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5},
"-0x1.0000000000001p+600");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a},
"-0x1.0000000000001p-600");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65},
"0x1.0000000000001p+600");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a},
"0x1.0000000000001p-600");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6},
"-0x1.0000000000001p+97");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
"0x1.0000000000001p+97");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xfe},
"-0x1.0000000000001p+999");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x7e},
"0x1.0000000000001p+999");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xc3},
"-0x1.0000000000002p+60");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x43},
"0x1.0000000000002p+60");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5},
"-0x1.0000000000002p+600");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a},
"-0x1.0000000000002p-600");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65},
"0x1.0000000000002p+600");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a},
"0x1.0000000000002p-600");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6},
"-0x1.0000000000002p+97");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
"0x1.0000000000002p+97");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xfe},
"-0x1.0000000000002p+999");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x7e},
"0x1.0000000000002p+999");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x80},
"-0x1.0000000000003p-1022");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00},
"0x1.0000000000003p-1022");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5},
"-0x1.0000000000003p+600");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a},
"-0x1.0000000000003p-600");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65},
"0x1.0000000000003p+600");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a},
"0x1.0000000000003p-600");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6},
"-0x1.0000000000003p+97");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
"0x1.0000000000003p+97");
TestDouble({0xa0, 0xc8, 0xeb, 0x85, 0xf3, 0xcc, 0xe1, 0xff},
"-0x1.1ccf385ebc8ap+1023");
TestDouble({0xa0, 0xc8, 0xeb, 0x85, 0xf3, 0xcc, 0xe1, 0x7f},
"0x1.1ccf385ebc8ap+1023");
TestDouble({0xdf, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0xc2, 0x43},
"0x1.23456789abcdfp+61");
TestDouble({0xdf, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x44},
"0x1.23456789abcdfp+80");
TestDouble({0xdf, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x22, 0x46},
"0x1.23456789abcdfp+99");
TestDouble({0x11, 0x43, 0x2b, 0xd6, 0xff, 0x25, 0xab, 0x3d},
"0x1.b25ffd62b4311p-37");
TestDouble({0x12, 0xec, 0x36, 0xd6, 0xff, 0x25, 0xab, 0x3d},
"0x1.b25ffd636ec12p-37");
TestDouble({0x58, 0xa4, 0x0c, 0x54, 0x34, 0x6f, 0x9d, 0x41},
"0x1.d6f34540ca458p+26");
TestDouble({0x00, 0x00, 0x00, 0x54, 0x34, 0x6f, 0x9d, 0x41},
"0x1.d6f3454p+26");
TestDouble({0xfa, 0x16, 0x5e, 0x5b, 0xaf, 0xe9, 0x8f, 0x45},
"0x1.fe9af5b5e16fap+89");
TestDouble({0xd5, 0xcb, 0x6b, 0x5b, 0xaf, 0xe9, 0x8f, 0x45},
"0x1.fe9af5b6bcbd5p+89");
TestDouble({0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff},
"-0x1.fffffffffffffp+1023");
TestDouble({0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f},
"0x1.fffffffffffffp+1023");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff}, "-0x1p+1023");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x7f}, "0x1p+1023");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, "-0x1p-1073");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "0x1p-1073");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, "-0x1p-1074");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "0x1p-1074");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xc3}, "-0x1p+60");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x43}, "0x1p+60");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5}, "-0x1p+600");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a}, "-0x1p-600");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65}, "0x1p+600");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a}, "0x1p-600");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6}, "-0x1p+97");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}, "0x1p+97");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xfe}, "-0x1p+999");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x7e}, "0x1p+999");
}
136 changes: 97 additions & 39 deletions test/dump/hexfloat_f32.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@
drop
f32.const -0x1.ff01p1
drop
f32.const +0x0.00000100000000001p-126
drop
f32.const -0x0.00000100000000001p-126
drop
f32.const +0x0.000001fffffffffffp-126
drop
f32.const -0x0.000001fffffffffffp-126
drop
f32.const +0x0.00000500000000000p-126
drop
f32.const -0x0.00000500000000000p-126
drop
f32.const +0x0.00000500000000001p-126
drop
f32.const -0x0.00000500000000001p-126
drop
)
)
(;; STDERR ;;;
Expand Down Expand Up @@ -114,50 +130,92 @@
0000077: 43 ; f32.const
0000078: 8080 7fc0 ; f32 literal
000007c: 1a ; drop
000007d: 0b ; end
0000015: 68 ; FIXUP func body size
0000013: 6a ; FIXUP section size
000007d: 43 ; f32.const
000007e: 0100 0000 ; f32 literal
0000082: 1a ; drop
0000083: 43 ; f32.const
0000084: 0100 0080 ; f32 literal
0000088: 1a ; drop
0000089: 43 ; f32.const
000008a: 0100 0000 ; f32 literal
000008e: 1a ; drop
000008f: 43 ; f32.const
0000090: 0100 0080 ; f32 literal
0000094: 1a ; drop
0000095: 43 ; f32.const
0000096: 0200 0000 ; f32 literal
000009a: 1a ; drop
000009b: 43 ; f32.const
000009c: 0200 0080 ; f32 literal
00000a0: 1a ; drop
00000a1: 43 ; f32.const
00000a2: 0300 0000 ; f32 literal
00000a6: 1a ; drop
00000a7: 43 ; f32.const
00000a8: 0300 0080 ; f32 literal
00000ac: 1a ; drop
00000ad: 0b ; end
; move data: [16, ae) -> [17, af)
0000015: 9801 ; FIXUP func body size
; move data: [14, af) -> [15, b0)
0000013: 9b01 ; FIXUP section size
;;; STDERR ;;)
(;; STDOUT ;;;

hexfloat_f32.wasm: file format wasm 0x1

Code Disassembly:

000016 func[0]:
000017: 43 00 00 00 00 | f32.const 0x0p+0
00001c: 1a | drop
00001d: 43 80 a2 91 48 | f32.const 0x1.2345p+18
000022: 1a | drop
000023: 43 00 00 80 59 | f32.const 0x1p+52
000028: 1a | drop
000029: 43 00 00 00 7f | f32.const 0x1p+127
00002e: 1a | drop
00002f: 43 00 00 80 7c | f32.const 0x1p+122
000034: 1a | drop
000035: 43 00 80 91 7d | f32.const 0x1.23p+124
00003a: 1a | drop
00003b: 43 f0 ff ff 7e | f32.const 0x1.ffffep+126
000040: 1a | drop
000041: 43 fe ff 7f 7e | f32.const 0x1.fffffcp+125
000046: 1a | drop
000047: 43 00 00 00 7f | f32.const 0x1p+127
00004c: 1a | drop
00004d: 43 c4 ff 7f 7f | f32.const 0x1.ffff88p+127
000052: 1a | drop
000053: 43 f8 ff 7f 7f | f32.const 0x1.fffffp+127
000058: 1a | drop
000059: 43 fa ff ff 0a | f32.const 0x1.fffff4p-106
00005e: 1a | drop
00005f: 43 f8 ff ff 0a | f32.const 0x1.fffffp-106
000064: 1a | drop
000065: 43 00 00 00 04 | f32.const 0x1p-119
00006a: 1a | drop
00006b: 43 00 00 88 3f | f32.const 0x1.1p+0
000070: 1a | drop
000071: 43 00 00 80 53 | f32.const 0x1p+40
000076: 1a | drop
000077: 43 80 80 7f c0 | f32.const -0x1.ff01p+1
00007c: 1a | drop
00007d: 0b | end
000018 func[0]:
000019: 43 00 00 00 00 | f32.const 0x0p+0
00001e: 1a | drop
00001f: 43 80 a2 91 48 | f32.const 0x1.2345p+18
000024: 1a | drop
000025: 43 00 00 80 59 | f32.const 0x1p+52
00002a: 1a | drop
00002b: 43 00 00 00 7f | f32.const 0x1p+127
000030: 1a | drop
000031: 43 00 00 80 7c | f32.const 0x1p+122
000036: 1a | drop
000037: 43 00 80 91 7d | f32.const 0x1.23p+124
00003c: 1a | drop
00003d: 43 f0 ff ff 7e | f32.const 0x1.ffffep+126
000042: 1a | drop
000043: 43 fe ff 7f 7e | f32.const 0x1.fffffcp+125
000048: 1a | drop
000049: 43 00 00 00 7f | f32.const 0x1p+127
00004e: 1a | drop
00004f: 43 c4 ff 7f 7f | f32.const 0x1.ffff88p+127
000054: 1a | drop
000055: 43 f8 ff 7f 7f | f32.const 0x1.fffffp+127
00005a: 1a | drop
00005b: 43 fa ff ff 0a | f32.const 0x1.fffff4p-106
000060: 1a | drop
000061: 43 f8 ff ff 0a | f32.const 0x1.fffffp-106
000066: 1a | drop
000067: 43 00 00 00 04 | f32.const 0x1p-119
00006c: 1a | drop
00006d: 43 00 00 88 3f | f32.const 0x1.1p+0
000072: 1a | drop
000073: 43 00 00 80 53 | f32.const 0x1p+40
000078: 1a | drop
000079: 43 80 80 7f c0 | f32.const -0x1.ff01p+1
00007e: 1a | drop
00007f: 43 01 00 00 00 | f32.const 0x1p-149
000084: 1a | drop
000085: 43 01 00 00 80 | f32.const -0x1p-149
00008a: 1a | drop
00008b: 43 01 00 00 00 | f32.const 0x1p-149
000090: 1a | drop
000091: 43 01 00 00 80 | f32.const -0x1p-149
000096: 1a | drop
000097: 43 02 00 00 00 | f32.const 0x1p-148
00009c: 1a | drop
00009d: 43 02 00 00 80 | f32.const -0x1p-148
0000a2: 1a | drop
0000a3: 43 03 00 00 00 | f32.const 0x1.8p-148
0000a8: 1a | drop
0000a9: 43 03 00 00 80 | f32.const -0x1.8p-148
0000ae: 1a | drop
0000af: 0b | end
;;; STDOUT ;;)
Loading
Loading