Add conversion operators for SFixed and UFixed (#93)

This will finally allow SFixed and UFixed to be converted to and from each other.
pull/94/head
Pharap 3 years ago committed by GitHub
parent 109d1d21fd
commit ea71949e81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/FixedPoints/SFixed.h
  2. 19
      src/FixedPoints/SFixedMemberFunctions.h
  3. 8
      src/FixedPoints/UFixed.h
  4. 19
      src/FixedPoints/UFixedMemberFunctions.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<IntegerOut, FractionOut>() const;
template< unsigned IntegerOut, unsigned FractionOut >
constexpr explicit operator UFixed<IntegerOut, FractionOut>() const;
constexpr explicit operator UFixed<Integer + 1, Fraction>() const;
static constexpr SFixed fromInternal(const InternalType & value);
constexpr SFixed operator -() const;

@ -198,6 +198,25 @@ constexpr SFixed<Integer, Fraction>::operator SFixed<IntegerOut, FractionOut>()
OutputType::fromInternal(this->value);
}
template< unsigned Integer, unsigned Fraction >
template< unsigned IntegerOut, unsigned FractionOut >
constexpr SFixed<Integer, Fraction>::operator UFixed<IntegerOut, FractionOut>() const
{
using OutputType = UFixed<IntegerOut, FractionOut>;
using IntermediaryType = SFixed<IntegerOut - 1, FractionOut>;
return static_cast<OutputType>(static_cast<IntermediaryType>(*this));
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::operator UFixed<Integer + 1, Fraction>() const
{
using OutputType = UFixed<Integer + 1, Fraction>;
using OutputInternalType = typename OutputType::InternalType;
return OutputType::fromInternal(static_cast<OutputInternalType>(this->value));
}
//
// Static Functions
//

@ -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<IntegerOut, FractionOut>() const;
template< unsigned IntegerOut, unsigned FractionOut >
constexpr explicit operator SFixed<IntegerOut, FractionOut>() const;
constexpr explicit operator SFixed<Integer - 1, Fraction>() const;
static constexpr UFixed fromInternal(const InternalType & value);
UFixed & operator ++();

@ -189,6 +189,25 @@ constexpr UFixed<Integer, Fraction>::operator UFixed<IntegerOut, FractionOut>()
OutputType::fromInternal(this->value);
}
template< unsigned Integer, unsigned Fraction >
template< unsigned IntegerOut, unsigned FractionOut >
constexpr UFixed<Integer, Fraction>::operator SFixed<IntegerOut, FractionOut>() const
{
using OutputType = SFixed<IntegerOut, FractionOut>;
using IntermediaryType = UFixed<IntegerOut + 1, FractionOut>;
return static_cast<OutputType>(static_cast<IntermediaryType>(*this));
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::operator SFixed<Integer - 1, Fraction>() const
{
using OutputType = SFixed<Integer - 1, Fraction>;
using OutputInternalType = typename OutputType::InternalType;
return OutputType::fromInternal(static_cast<OutputInternalType>(this->value));
}
//
// Static Functions
//

Loading…
Cancel
Save