We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4a4380b commit fc43b3cCopy full SHA for fc43b3c
src/s2/util/math/exactfloat/exactfloat.cc
@@ -64,15 +64,17 @@ ExactFloat::ExactFloat(double v) {
64
}
65
66
67
+// Calculates abs(v) without UB. SafeAbs(INT_MIN) == INT_MIN.
68
+// Generates the same code as std::abs().
69
+// https://godbolt.org/z/eT6KW1zGb
70
+int SafeAbs(int v) {
71
+ return v < 0 ? -static_cast<unsigned>(v) : v;
72
+}
73
+
74
ExactFloat::ExactFloat(int v) {
75
sign_ = (v >= 0) ? 1 : -1;
76
bn_exp_ = 0;
-
- if (v == std::numeric_limits<int>::min()) {
- bn_ = Bignum(static_cast<unsigned>(v));
- } else {
- bn_ = Bignum(abs(v));
- }
77
+ bn_ = Bignum(static_cast<unsigned>(SafeAbs(v)));
78
Canonicalize();
79
80
0 commit comments