From cd9bd149162ecdd9f2715367617771ea55b7cd7e Mon Sep 17 00:00:00 2001 From: Max Graey Date: Mon, 14 Sep 2020 04:04:38 +0300 Subject: Simplify subtracting zero from float expressions (#3125) `x - 0.0` -> `x` `x + (-0.0)` -> `x` `x - (-0.0)` -> `x + 0.0` where `x` is `f32` or `f64`. --- src/passes/OptimizeInstructions.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src') diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index b4a5b9217..676e753ce 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1299,6 +1299,27 @@ private: } } } + if (type.isFloat()) { + auto value = right->value.getFloat(); + if (value == 0.0) { + if (binary->op == Abstract::getBinary(type, Abstract::Sub)) { + if (std::signbit(value)) { + // x - (-0.0) ==> x + 0.0 + binary->op = Abstract::getBinary(type, Abstract::Add); + right->value = right->value.neg(); + return binary; + } else { + // x - 0.0 ==> x + return binary->left; + } + } else if (binary->op == Abstract::getBinary(type, Abstract::Add)) { + if (std::signbit(value)) { + // x + (-0.0) ==> x + return binary->left; + } + } + } + } if (type.isInteger() || type.isFloat()) { // note that this is correct even on floats with a NaN on the left, // as a NaN would skip the computation and just return the NaN, -- cgit v1.2.3