From 4f8fe991534c7a6dc43f7e9335859a52b58a3be8 Mon Sep 17 00:00:00 2001 From: Pharap <2933055+Pharap@users.noreply.github.com> Date: Sun, 21 Jan 2018 19:57:41 +0000 Subject: [PATCH] Simplify both versions of roundFixed (#12) These simplifications slightly reduce the size of the generated code. --- src/FixedPoints/Utils.h | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/FixedPoints/Utils.h b/src/FixedPoints/Utils.h index 4619a3c..14458f0 100644 --- a/src/FixedPoints/Utils.h +++ b/src/FixedPoints/Utils.h @@ -127,21 +127,17 @@ template< unsigned Integer, unsigned Fraction > constexpr UFixed roundFixed(const UFixed & value) { using OutputType = UFixed; - return (value.getInternal() & OutputType::MidpointMask) != 0 ? ceilFixed(value) : floorFixed(value); + return ((value.getFraction() >= OutputType(0.5).getFraction()) != 0) ? ceilFixed(value) : floorFixed(value); } template< unsigned Integer, unsigned Fraction > constexpr SFixed roundFixed(const SFixed & value) { using OutputType = SFixed; - return - ( - ((value.getInternal() & OutputType::MidpointMask) != 0) && - (!signbitFixed(value) || - ((value.getInternal() & OutputType::LesserMidpointMask) != 0)) - ) - ? ceilFixed(value) - : floorFixed(value); + return + signbitFixed(value) + ? ((value.getFraction() <= OutputType(0.5).getFraction()) != 0) ? floorFixed(value) : ceilFixed(value) + : ((value.getFraction() >= OutputType(0.5).getFraction()) != 0) ? ceilFixed(value) : floorFixed(value); } template< unsigned Integer, unsigned Fraction >