Skip to content

Commit ea71949

Browse files
authored
Add conversion operators for SFixed and UFixed (#93)
This will finally allow SFixed and UFixed to be converted to and from each other.
1 parent 109d1d2 commit ea71949

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/FixedPoints/SFixed.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ FIXED_POINTS_BEGIN_NAMESPACE
2222
// Declaration
2323
//
2424

25+
template< unsigned Integer, unsigned Fraction >
26+
class UFixed;
27+
2528
template< unsigned Integer, unsigned Fraction >
2629
class SFixed
2730
{
@@ -103,6 +106,11 @@ class SFixed
103106
template< unsigned IntegerOut, unsigned FractionOut >
104107
constexpr explicit operator SFixed<IntegerOut, FractionOut>() const;
105108

109+
template< unsigned IntegerOut, unsigned FractionOut >
110+
constexpr explicit operator UFixed<IntegerOut, FractionOut>() const;
111+
112+
constexpr explicit operator UFixed<Integer + 1, Fraction>() const;
113+
106114
static constexpr SFixed fromInternal(const InternalType & value);
107115

108116
constexpr SFixed operator -() const;

src/FixedPoints/SFixedMemberFunctions.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,25 @@ constexpr SFixed<Integer, Fraction>::operator SFixed<IntegerOut, FractionOut>()
198198
OutputType::fromInternal(this->value);
199199
}
200200

201+
template< unsigned Integer, unsigned Fraction >
202+
template< unsigned IntegerOut, unsigned FractionOut >
203+
constexpr SFixed<Integer, Fraction>::operator UFixed<IntegerOut, FractionOut>() const
204+
{
205+
using OutputType = UFixed<IntegerOut, FractionOut>;
206+
using IntermediaryType = SFixed<IntegerOut - 1, FractionOut>;
207+
208+
return static_cast<OutputType>(static_cast<IntermediaryType>(*this));
209+
}
210+
211+
template< unsigned Integer, unsigned Fraction >
212+
constexpr SFixed<Integer, Fraction>::operator UFixed<Integer + 1, Fraction>() const
213+
{
214+
using OutputType = UFixed<Integer + 1, Fraction>;
215+
using OutputInternalType = typename OutputType::InternalType;
216+
217+
return OutputType::fromInternal(static_cast<OutputInternalType>(this->value));
218+
}
219+
201220
//
202221
// Static Functions
203222
//

src/FixedPoints/UFixed.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ FIXED_POINTS_BEGIN_NAMESPACE
2222
// Declaration
2323
//
2424

25+
template< unsigned Integer, unsigned Fraction >
26+
class SFixed;
27+
2528
template< unsigned Integer, unsigned Fraction >
2629
class UFixed
2730
{
@@ -104,6 +107,11 @@ class UFixed
104107
template< unsigned IntegerOut, unsigned FractionOut >
105108
constexpr explicit operator UFixed<IntegerOut, FractionOut>() const;
106109

110+
template< unsigned IntegerOut, unsigned FractionOut >
111+
constexpr explicit operator SFixed<IntegerOut, FractionOut>() const;
112+
113+
constexpr explicit operator SFixed<Integer - 1, Fraction>() const;
114+
107115
static constexpr UFixed fromInternal(const InternalType & value);
108116

109117
UFixed & operator ++();

src/FixedPoints/UFixedMemberFunctions.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,25 @@ constexpr UFixed<Integer, Fraction>::operator UFixed<IntegerOut, FractionOut>()
189189
OutputType::fromInternal(this->value);
190190
}
191191

192+
template< unsigned Integer, unsigned Fraction >
193+
template< unsigned IntegerOut, unsigned FractionOut >
194+
constexpr UFixed<Integer, Fraction>::operator SFixed<IntegerOut, FractionOut>() const
195+
{
196+
using OutputType = SFixed<IntegerOut, FractionOut>;
197+
using IntermediaryType = UFixed<IntegerOut + 1, FractionOut>;
198+
199+
return static_cast<OutputType>(static_cast<IntermediaryType>(*this));
200+
}
201+
202+
template< unsigned Integer, unsigned Fraction >
203+
constexpr UFixed<Integer, Fraction>::operator SFixed<Integer - 1, Fraction>() const
204+
{
205+
using OutputType = SFixed<Integer - 1, Fraction>;
206+
using OutputInternalType = typename OutputType::InternalType;
207+
208+
return OutputType::fromInternal(static_cast<OutputInternalType>(this->value));
209+
}
210+
192211
//
193212
// Static Functions
194213
//

0 commit comments

Comments
 (0)