Skip to content

Commit 62fee34

Browse files
authored
hex-float: adapt to strict fp compilation options (KhronosGroup#6778)
- avoid implicit conversions from float to double - guard floating point equality checks with pragmas
1 parent 7fa1f02 commit 62fee34

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

source/util/hex_float.h

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@
3636
namespace spvtools {
3737
namespace utils {
3838

39+
namespace {
40+
// Returns true if floating point value a equals b.
41+
// This is refactored to localize turning off warnings.
42+
template <typename FloatTy>
43+
bool FloatEquals(FloatTy a, FloatTy b) {
44+
#ifdef __GNUC__
45+
#pragma GCC diagnostic push
46+
#pragma GCC diagnostic ignored "-Wfloat-equal"
47+
#endif
48+
#ifdef __clang__
49+
#pragma clang diagnostic push
50+
#pragma clang diagnostic ignored "-Wfloat-equal"
51+
#endif
52+
return a == b;
53+
#ifdef __GNUC__
54+
#pragma GCC diagnostic pop
55+
#endif
56+
#ifdef __clang__
57+
#pragma clang diagnostic pop
58+
#endif
59+
}
60+
} // namespace
61+
3962
class Float4_E2M1 {
4063
public:
4164
Float4_E2M1(uint8_t v) : val(v) {}
@@ -1279,7 +1302,7 @@ class HexFloat {
12791302
template <typename other_traits>
12801303
CastResult castTo(HexFixedPoint<other_traits>& other,
12811304
round_direction round_dir) {
1282-
HexFloat<FloatProxy<double>> hf(0.0f);
1305+
HexFloat<FloatProxy<double>> hf(0.0);
12831306
castTo(hf, round_dir);
12841307

12851308
using other_int_type = typename other_traits::int_type;
@@ -1289,12 +1312,12 @@ class HexFloat {
12891312
other_traits::implicit_exponent_inverse_double;
12901313
const other_int_type max = std::numeric_limits<other_int_type>::max();
12911314
const other_int_type min = std::numeric_limits<other_int_type>::min();
1292-
if (d == std::numeric_limits<double>::infinity() || d > max) {
1315+
if (FloatEquals(d, std::numeric_limits<double>::infinity()) || d > max) {
12931316
// saturate
12941317
other.set_value(static_cast<other_uint_type>(max));
12951318
return CastResult::LostPrecision;
12961319
}
1297-
if (d == -std::numeric_limits<double>::infinity() || d < min) {
1320+
if (FloatEquals(d, -std::numeric_limits<double>::infinity()) || d < min) {
12981321
// saturate
12991322
other.set_value(static_cast<other_uint_type>(min));
13001323
return CastResult::LostPrecision;
@@ -2087,7 +2110,7 @@ inline std::istream& operator>>
20872110
HexFloat<FloatProxy<Float8_E8M0>,
20882111
HexFloatTraits<FloatProxy<Float8_E8M0>>>& value) {
20892112
// need a double since 0x1p-127 is out of float32 range
2090-
HexFloat<FloatProxy<double>> float_val(0.0f);
2113+
HexFloat<FloatProxy<double>> float_val(0.0);
20912114
is >> float_val;
20922115
if (is.fail()) return is;
20932116

@@ -2116,15 +2139,16 @@ inline std::istream& operator>>
21162139
inline std::istream& operator>>(std::istream& is,
21172140
HexFixedPoint<MXInt8>& value) {
21182141
// need a double since 0x1p-127 is out of float32 range
2119-
HexFloat<FloatProxy<double>> float_val(0.0f);
2142+
HexFloat<FloatProxy<double>> float_val(0.0);
21202143
is >> float_val;
21212144
if (is.fail()) return is;
21222145

2123-
if (float_val.value().getAsFloat() > MXInt8::maxf) {
2146+
if (float_val.value().getAsFloat() > static_cast<double>(MXInt8::maxf)) {
21242147
is.setstate(std::ios_base::failbit);
21252148
value.set_value(MXInt8::max);
21262149
return is;
2127-
} else if (float_val.value().getAsFloat() < MXInt8::minf) {
2150+
} else if (float_val.value().getAsFloat() <
2151+
static_cast<double>(MXInt8::minf)) {
21282152
is.setstate(std::ios_base::failbit);
21292153
value.set_value(MXInt8::min);
21302154
return is;

0 commit comments

Comments
 (0)