Skip to content

Commit aafb2a0

Browse files
authored
[FFI] Fix JSON parser/writer for the fast-math flag (apache#18221)
This PR fixes the JSON parser and writer for the support of the fast-math flag. Prior to this PR, there would be the following error when compiling TVM with fast-math. This PR fixes this issue. ``` /home/ruihangl/Workspace/tvm/ffi/src/ffi/extra/json_writer.cc: In static member function ‘static bool tvm::ffi::json::JSONWriter::FastMathSafeIsNaN(double)’: /home/ruihangl/Workspace/tvm/ffi/src/ffi/extra/json_writer.cc:69:22: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing] 69 | uint64_t bits = *reinterpret_cast<const uint64_t*>(&x); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/ruihangl/Workspace/tvm/ffi/src/ffi/extra/json_writer.cc: In static member function ‘static bool tvm::ffi::json::JSONWriter::FastMathSafeIsInf(double)’: /home/ruihangl/Workspace/tvm/ffi/src/ffi/extra/json_writer.cc:84:22: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing] 84 | uint64_t bits = *reinterpret_cast<const uint64_t*>(&x); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ```
1 parent 9d7a8a3 commit aafb2a0

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/ffi/extra/json_parser.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,26 +298,38 @@ class JSONParserContext {
298298
private:
299299
static double FastMathSafePosInf() {
300300
#ifdef __FAST_MATH__
301-
const uint64_t inf_bits = 0x7FF0000000000000ULL;
302-
return *reinterpret_cast<const double*>(&inf_bits);
301+
union {
302+
uint64_t from;
303+
double to;
304+
} u;
305+
u.from = 0x7FF0000000000000ULL; // write "from", read "to"
306+
return u.to;
303307
#else
304308
return std::numeric_limits<double>::infinity();
305309
#endif
306310
}
307311

308312
static double FastMathSafeNegInf() {
309313
#ifdef __FAST_MATH__
310-
const uint64_t inf_bits = 0xFFF0000000000000ULL;
311-
return *reinterpret_cast<const double*>(&inf_bits);
314+
union {
315+
uint64_t from;
316+
double to;
317+
} u;
318+
u.from = 0xFFF0000000000000ULL; // write "from", read "to"
319+
return u.to;
312320
#else
313321
return -std::numeric_limits<double>::infinity();
314322
#endif
315323
}
316324

317325
static double FastMathSafeNaN() {
318326
#ifdef __FAST_MATH__
319-
const uint64_t nan_bits = 0x7FF8000000000000ULL;
320-
return *reinterpret_cast<const double*>(&nan_bits);
327+
union {
328+
uint64_t from;
329+
double to;
330+
} u;
331+
u.from = 0x7FF8000000000000ULL; // write "from", read "to"
332+
return u.to;
321333
#else
322334
return std::numeric_limits<double>::quiet_NaN();
323335
#endif

src/ffi/extra/json_writer.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ class JSONWriter {
6666
// IEEE 754 standard: https://en.wikipedia.org/wiki/IEEE_754
6767
// NaN is encoded as all 1s in the exponent and non-zero in the mantissa
6868
static_assert(sizeof(double) == sizeof(uint64_t), "Unexpected double size");
69-
uint64_t bits = *reinterpret_cast<const uint64_t*>(&x);
69+
union {
70+
double from;
71+
uint64_t to;
72+
} u;
73+
u.from = x; // write "from", read "to"
74+
uint64_t bits = u.to;
7075
uint64_t exponent = (bits >> 52) & 0x7FF;
7176
uint64_t mantissa = bits & 0xFFFFFFFFFFFFFull;
7277
return (exponent == 0x7FF) && (mantissa != 0);
@@ -81,7 +86,12 @@ class JSONWriter {
8186
// IEEE 754 standard: https://en.wikipedia.org/wiki/IEEE_754
8287
// Inf is encoded as all 1s in the exponent and zero in the mantissa
8388
static_assert(sizeof(double) == sizeof(uint64_t), "Unexpected double size");
84-
uint64_t bits = *reinterpret_cast<const uint64_t*>(&x);
89+
union {
90+
double from;
91+
uint64_t to;
92+
} u;
93+
u.from = x; // write "from", read "to"
94+
uint64_t bits = u.to;
8595
uint64_t exponent = (bits >> 52) & 0x7FF;
8696
uint64_t mantissa = bits & 0xFFFFFFFFFFFFFull;
8797
// inf is encoded as all 1s in the exponent and zero in the mantissa

0 commit comments

Comments
 (0)