From ea71949e815683b3fbe603221dc8476254cf3c67 Mon Sep 17 00:00:00 2001 From: Pharap <2933055+Pharap@users.noreply.github.com> Date: Fri, 26 Mar 2021 22:26:27 +0000 Subject: [PATCH] Add conversion operators for SFixed and UFixed (#93) This will finally allow SFixed and UFixed to be converted to and from each other. --- src/FixedPoints/SFixed.h | 8 ++++++++ src/FixedPoints/SFixedMemberFunctions.h | 19 +++++++++++++++++++ src/FixedPoints/UFixed.h | 8 ++++++++ src/FixedPoints/UFixedMemberFunctions.h | 19 +++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/src/FixedPoints/SFixed.h b/src/FixedPoints/SFixed.h index 01c95f1..4d93cf7 100644 --- a/src/FixedPoints/SFixed.h +++ b/src/FixedPoints/SFixed.h @@ -22,6 +22,9 @@ FIXED_POINTS_BEGIN_NAMESPACE // Declaration // +template< unsigned Integer, unsigned Fraction > +class UFixed; + template< unsigned Integer, unsigned Fraction > class SFixed { @@ -103,6 +106,11 @@ public: template< unsigned IntegerOut, unsigned FractionOut > constexpr explicit operator SFixed() const; + template< unsigned IntegerOut, unsigned FractionOut > + constexpr explicit operator UFixed() const; + + constexpr explicit operator UFixed() const; + static constexpr SFixed fromInternal(const InternalType & value); constexpr SFixed operator -() const; diff --git a/src/FixedPoints/SFixedMemberFunctions.h b/src/FixedPoints/SFixedMemberFunctions.h index 9d1268b..69f35e5 100644 --- a/src/FixedPoints/SFixedMemberFunctions.h +++ b/src/FixedPoints/SFixedMemberFunctions.h @@ -198,6 +198,25 @@ constexpr SFixed::operator SFixed() OutputType::fromInternal(this->value); } +template< unsigned Integer, unsigned Fraction > +template< unsigned IntegerOut, unsigned FractionOut > +constexpr SFixed::operator UFixed() const +{ + using OutputType = UFixed; + using IntermediaryType = SFixed; + + return static_cast(static_cast(*this)); +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator UFixed() const +{ + using OutputType = UFixed; + using OutputInternalType = typename OutputType::InternalType; + + return OutputType::fromInternal(static_cast(this->value)); +} + // // Static Functions // diff --git a/src/FixedPoints/UFixed.h b/src/FixedPoints/UFixed.h index 747996c..4444f55 100644 --- a/src/FixedPoints/UFixed.h +++ b/src/FixedPoints/UFixed.h @@ -22,6 +22,9 @@ FIXED_POINTS_BEGIN_NAMESPACE // Declaration // +template< unsigned Integer, unsigned Fraction > +class SFixed; + template< unsigned Integer, unsigned Fraction > class UFixed { @@ -104,6 +107,11 @@ public: template< unsigned IntegerOut, unsigned FractionOut > constexpr explicit operator UFixed() const; + template< unsigned IntegerOut, unsigned FractionOut > + constexpr explicit operator SFixed() const; + + constexpr explicit operator SFixed() const; + static constexpr UFixed fromInternal(const InternalType & value); UFixed & operator ++(); diff --git a/src/FixedPoints/UFixedMemberFunctions.h b/src/FixedPoints/UFixedMemberFunctions.h index 2540b9f..00def81 100644 --- a/src/FixedPoints/UFixedMemberFunctions.h +++ b/src/FixedPoints/UFixedMemberFunctions.h @@ -189,6 +189,25 @@ constexpr UFixed::operator UFixed() OutputType::fromInternal(this->value); } +template< unsigned Integer, unsigned Fraction > +template< unsigned IntegerOut, unsigned FractionOut > +constexpr UFixed::operator SFixed() const +{ + using OutputType = SFixed; + using IntermediaryType = UFixed; + + return static_cast(static_cast(*this)); +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator SFixed() const +{ + using OutputType = SFixed; + using OutputInternalType = typename OutputType::InternalType; + + return OutputType::fromInternal(static_cast(this->value)); +} + // // Static Functions //