From 6054d115166aaddf69f1acaef1b5412593aebebc Mon Sep 17 00:00:00 2001 From: Pharap <2933055+Pharap@users.noreply.github.com> Date: Tue, 29 May 2018 01:04:30 +0100 Subject: [PATCH] Remove UFixedBase and SFixedBase (#46) * Delete SFixedBase.h * Delete UFixedBase.h --- src/FixedPoints/FixedPoints.h | 3 - src/FixedPoints/SFixed.h | 40 ++++++++--- src/FixedPoints/SFixedBase.h | 93 ------------------------ src/FixedPoints/SFixedMemberFunctions.h | 94 ++++++++++++++++++++++++- src/FixedPoints/UFixed.h | 41 ++++++++--- src/FixedPoints/UFixedBase.h | 93 ------------------------ src/FixedPoints/UFixedMemberFunctions.h | 94 ++++++++++++++++++++++++- 7 files changed, 249 insertions(+), 209 deletions(-) delete mode 100644 src/FixedPoints/SFixedBase.h delete mode 100644 src/FixedPoints/UFixedBase.h diff --git a/src/FixedPoints/FixedPoints.h b/src/FixedPoints/FixedPoints.h index 4ff50fa..56aa945 100644 --- a/src/FixedPoints/FixedPoints.h +++ b/src/FixedPoints/FixedPoints.h @@ -14,9 +14,6 @@ #include "Details.h" -#include "UFixedBase.h" -#include "SFixedBase.h" - #include "UFixed.h" #include "SFixed.h" diff --git a/src/FixedPoints/SFixed.h b/src/FixedPoints/SFixed.h index b29266e..9bb19b5 100644 --- a/src/FixedPoints/SFixed.h +++ b/src/FixedPoints/SFixed.h @@ -15,7 +15,6 @@ #pragma once #include "Details.h" -#include "SFixedBase.h" FIXED_POINTS_BEGIN_NAMESPACE @@ -24,7 +23,7 @@ FIXED_POINTS_BEGIN_NAMESPACE // template< unsigned Integer, unsigned Fraction > -class SFixed : FIXED_POINTS_DETAILS::SFixedBase< Integer, Fraction > +class SFixed { public: static_assert(((Integer + 1) + Fraction) <= FIXED_POINTS_DETAILS::BitSize::Value, "Platform does not have a native type large enough for SFixed."); @@ -55,16 +54,41 @@ public: constexpr const static MaskType MidpointMask = FIXED_POINTS_DETAILS::MsbMask::Value; constexpr const static MaskType LesserMidpointMask = MidpointMask - 1; - -private: - using Base = FIXED_POINTS_DETAILS::SFixedBase; - using RawType = typename Base::RawType; -public: - using Base::Base; +protected: + class RawType + { + private: + const InternalType value; + + public: + constexpr inline explicit RawType(const InternalType & value) : value(value) {} + constexpr inline explicit operator InternalType(void) const { return this->value; } + }; +protected: + InternalType value; + +protected: + constexpr SFixed(const RawType & value); + +public: constexpr SFixed(void); constexpr SFixed(const IntegerType & integer, const FractionType & fraction); + constexpr SFixed(const char & value); + constexpr SFixed(const unsigned char & value); + constexpr SFixed(const signed char & value); + constexpr SFixed(const unsigned short int & value); + constexpr SFixed(const signed short int & value); + constexpr SFixed(const unsigned int & value); + constexpr SFixed(const signed int & value); + constexpr SFixed(const unsigned long int & value); + constexpr SFixed(const signed long int & value); + constexpr SFixed(const unsigned long long int & value); + constexpr SFixed(const signed long long int & value); + constexpr SFixed(const double & value); + constexpr SFixed(const float & value); + constexpr SFixed(const long double & value); constexpr InternalType getInternal(void) const; constexpr IntegerType getInteger(void) const; diff --git a/src/FixedPoints/SFixedBase.h b/src/FixedPoints/SFixedBase.h deleted file mode 100644 index b500e19..0000000 --- a/src/FixedPoints/SFixedBase.h +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2017 Pharap -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "Details.h" - -FIXED_POINTS_BEGIN_NAMESPACE -namespace FIXED_POINTS_DETAILS -{ - template< unsigned Integer, unsigned Fraction > - class SFixedBase - { - public: - using IntegerType = FIXED_POINTS_DETAILS::LeastInt; - using FractionType = FIXED_POINTS_DETAILS::LeastUInt; - using InternalType = FIXED_POINTS_DETAILS::LeastInt<(Integer + 1) + Fraction>; - - constexpr const static unsigned long long Scale = 1ULL << Fraction; - - protected: - class RawType - { - private: - const InternalType value; - - public: - constexpr inline explicit RawType(const InternalType & value) : value(value) {} - constexpr inline explicit operator InternalType(void) const { return this->value; } - }; - - protected: - InternalType value; - - constexpr SFixedBase(void) : value(0) {} - constexpr SFixedBase(const RawType & value) : value(static_cast(value)) {} - - public: - constexpr SFixedBase(const char & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const unsigned char & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const signed char & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const unsigned short int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const signed short int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const unsigned int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const signed int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const unsigned long int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const signed long int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const unsigned long long int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const signed long long int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr SFixedBase(const double & value) - : value(static_cast(value * static_cast(Scale))) {} - - constexpr SFixedBase(const float & value) - : value(static_cast(value * static_cast(Scale))) {} - - constexpr SFixedBase(const long double & value) - : value(static_cast(value * static_cast(Scale))) {} - }; -} -FIXED_POINTS_END_NAMESPACE \ No newline at end of file diff --git a/src/FixedPoints/SFixedMemberFunctions.h b/src/FixedPoints/SFixedMemberFunctions.h index 73b51d6..fad19ac 100644 --- a/src/FixedPoints/SFixedMemberFunctions.h +++ b/src/FixedPoints/SFixedMemberFunctions.h @@ -18,15 +18,105 @@ FIXED_POINTS_BEGIN_NAMESPACE // Constructors // +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const RawType & value) + : value(static_cast(value)) +{ +} + template< unsigned Integer, unsigned Fraction > constexpr SFixed::SFixed(void) - : Base() + : value(0) { } template< unsigned Integer, unsigned Fraction > constexpr SFixed::SFixed(const IntegerType & integer, const FractionType & fraction) - : Base(RawType((static_cast(integer) << FractionSize) | fraction)) + : value((static_cast(integer) << FractionSize) | fraction) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const char & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const unsigned char & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const signed char & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const unsigned short int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const signed short int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const unsigned int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const signed int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const unsigned long int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const signed long int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const unsigned long long int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const signed long long int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const double & value) + : value(static_cast(value * static_cast(Scale))) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const float & value) + : value(static_cast(value * static_cast(Scale))) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::SFixed(const long double & value) + : value(static_cast(value * static_cast(Scale))) { } diff --git a/src/FixedPoints/UFixed.h b/src/FixedPoints/UFixed.h index b57ad58..2c8fe34 100644 --- a/src/FixedPoints/UFixed.h +++ b/src/FixedPoints/UFixed.h @@ -15,7 +15,6 @@ #pragma once #include "Details.h" -#include "UFixedBase.h" FIXED_POINTS_BEGIN_NAMESPACE @@ -24,7 +23,7 @@ FIXED_POINTS_BEGIN_NAMESPACE // template< unsigned Integer, unsigned Fraction > -class UFixed : FIXED_POINTS_DETAILS::UFixedBase< Integer, Fraction > +class UFixed { public: static_assert((Integer + Fraction) <= FIXED_POINTS_DETAILS::BitSize::Value, "Platform does not have a native type large enough for UFixed."); @@ -56,16 +55,42 @@ public: constexpr const static MaskType MidpointMask = FIXED_POINTS_DETAILS::MsbMask::Value; constexpr const static MaskType LesserMidpointMask = MidpointMask - 1; -private: - using Base = FIXED_POINTS_DETAILS::UFixedBase; - using RawType = typename Base::RawType; +protected: + class RawType + { + private: + const InternalType value; -public: - using Base::Base; + public: + constexpr inline explicit RawType(const InternalType & value) : value(value) {} + constexpr inline explicit operator InternalType(void) const { return this->value; } + }; + +protected: + InternalType value; +protected: + constexpr UFixed(const RawType & value); + +public: constexpr UFixed(void); constexpr UFixed(const IntegerType & integer, const FractionType & fraction); - + constexpr UFixed(const char & value); + constexpr UFixed(const unsigned char & value); + constexpr UFixed(const signed char & value); + constexpr UFixed(const unsigned short int & value); + constexpr UFixed(const signed short int & value); + constexpr UFixed(const unsigned int & value); + constexpr UFixed(const signed int & value); + constexpr UFixed(const unsigned long int & value); + constexpr UFixed(const signed long int & value); + constexpr UFixed(const unsigned long long int & value); + constexpr UFixed(const signed long long int & value); + constexpr UFixed(const double & value); + constexpr UFixed(const float & value); + constexpr UFixed(const long double & value); + +public: constexpr InternalType getInternal(void) const; constexpr IntegerType getInteger(void) const; constexpr FractionType getFraction(void) const; diff --git a/src/FixedPoints/UFixedBase.h b/src/FixedPoints/UFixedBase.h deleted file mode 100644 index 74fb725..0000000 --- a/src/FixedPoints/UFixedBase.h +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2017 Pharap -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "Details.h" - -FIXED_POINTS_BEGIN_NAMESPACE -namespace FIXED_POINTS_DETAILS -{ - template< unsigned Integer, unsigned Fraction > - class UFixedBase - { - public: - using IntegerType = FIXED_POINTS_DETAILS::LeastUInt; - using FractionType = FIXED_POINTS_DETAILS::LeastUInt; - using InternalType = FIXED_POINTS_DETAILS::LeastUInt; - - constexpr const static unsigned long long Scale = 1ULL << Fraction; - - protected: - class RawType - { - private: - const InternalType value; - - public: - constexpr inline explicit RawType(const InternalType & value) : value(value) {} - constexpr inline explicit operator InternalType(void) const { return this->value; } - }; - - protected: - InternalType value; - - constexpr UFixedBase(void) : value(0) {} - constexpr UFixedBase(const RawType & value) : value(static_cast(value)) {} - - public: - constexpr UFixedBase(const char & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const unsigned char & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const signed char & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const unsigned short int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const signed short int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const unsigned int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const signed int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const unsigned long int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const signed long int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const unsigned long long int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const signed long long int & value) - : value(static_cast(static_cast< LargerType >(value) << Fraction)) {} - - constexpr UFixedBase(const double & value) - : value(static_cast(value * static_cast(Scale))) {} - - constexpr UFixedBase(const float & value) - : value(static_cast(value * static_cast(Scale))) {} - - constexpr UFixedBase(const long double & value) - : value(static_cast(value * static_cast(Scale))) {} - }; -} -FIXED_POINTS_END_NAMESPACE \ No newline at end of file diff --git a/src/FixedPoints/UFixedMemberFunctions.h b/src/FixedPoints/UFixedMemberFunctions.h index e0709ba..d51cbea 100644 --- a/src/FixedPoints/UFixedMemberFunctions.h +++ b/src/FixedPoints/UFixedMemberFunctions.h @@ -18,15 +18,105 @@ FIXED_POINTS_BEGIN_NAMESPACE // Constructors // +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const RawType & value) + : value(static_cast(value)) +{ +} + template< unsigned Integer, unsigned Fraction > constexpr UFixed::UFixed(void) - : Base() + : value(0) { } template< unsigned Integer, unsigned Fraction > constexpr UFixed::UFixed(const IntegerType & integer, const FractionType & fraction) - : Base(RawType((static_cast(integer) << FractionSize) | fraction)) + : value((static_cast(integer) << FractionSize) | fraction) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const char & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const unsigned char & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const signed char & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const unsigned short int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const signed short int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const unsigned int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const signed int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const unsigned long int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const signed long int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const unsigned long long int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const signed long long int & value) + : value(static_cast(static_cast< FIXED_POINTS_DETAILS::LargerType >(value) << FractionSize)) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const double & value) + : value(static_cast(value * static_cast(Scale))) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const float & value) + : value(static_cast(value * static_cast(Scale))) +{ +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::UFixed(const long double & value) + : value(static_cast(value * static_cast(Scale))) { }