Remove UFixedBase and SFixedBase (#46)

* Delete SFixedBase.h

* Delete UFixedBase.h
pull/51/head
Pharap 7 years ago committed by GitHub
parent ff3308fd96
commit 6054d11516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/FixedPoints/FixedPoints.h
  2. 40
      src/FixedPoints/SFixed.h
  3. 93
      src/FixedPoints/SFixedBase.h
  4. 94
      src/FixedPoints/SFixedMemberFunctions.h
  5. 41
      src/FixedPoints/UFixed.h
  6. 93
      src/FixedPoints/UFixedBase.h
  7. 94
      src/FixedPoints/UFixedMemberFunctions.h

@ -14,9 +14,6 @@
#include "Details.h" #include "Details.h"
#include "UFixedBase.h"
#include "SFixedBase.h"
#include "UFixed.h" #include "UFixed.h"
#include "SFixed.h" #include "SFixed.h"

@ -15,7 +15,6 @@
#pragma once #pragma once
#include "Details.h" #include "Details.h"
#include "SFixedBase.h"
FIXED_POINTS_BEGIN_NAMESPACE FIXED_POINTS_BEGIN_NAMESPACE
@ -24,7 +23,7 @@ FIXED_POINTS_BEGIN_NAMESPACE
// //
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
class SFixed : FIXED_POINTS_DETAILS::SFixedBase< Integer, Fraction > class SFixed
{ {
public: public:
static_assert(((Integer + 1) + Fraction) <= FIXED_POINTS_DETAILS::BitSize<intmax_t>::Value, "Platform does not have a native type large enough for SFixed."); static_assert(((Integer + 1) + Fraction) <= FIXED_POINTS_DETAILS::BitSize<intmax_t>::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<FractionSize>::Value; constexpr const static MaskType MidpointMask = FIXED_POINTS_DETAILS::MsbMask<FractionSize>::Value;
constexpr const static MaskType LesserMidpointMask = MidpointMask - 1; constexpr const static MaskType LesserMidpointMask = MidpointMask - 1;
private:
using Base = FIXED_POINTS_DETAILS::SFixedBase<Integer, Fraction>;
using RawType = typename Base::RawType;
public: protected:
using Base::Base; 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(void);
constexpr SFixed(const IntegerType & integer, const FractionType & fraction); 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 InternalType getInternal(void) const;
constexpr IntegerType getInteger(void) const; constexpr IntegerType getInteger(void) const;

@ -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<Integer + 1>;
using FractionType = FIXED_POINTS_DETAILS::LeastUInt<Fraction>;
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<InternalType>(value)) {}
public:
constexpr SFixedBase(const char & value)
: value(static_cast<InternalType>(static_cast< LargerType<char, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const unsigned char & value)
: value(static_cast<InternalType>(static_cast< LargerType<unsigned char, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const signed char & value)
: value(static_cast<InternalType>(static_cast< LargerType<signed char, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const unsigned short int & value)
: value(static_cast<InternalType>(static_cast< LargerType<unsigned short int, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const signed short int & value)
: value(static_cast<InternalType>(static_cast< LargerType<signed short int, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const unsigned int & value)
: value(static_cast<InternalType>(static_cast< LargerType<unsigned int, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const signed int & value)
: value(static_cast<InternalType>(static_cast< LargerType<signed int, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const unsigned long int & value)
: value(static_cast<InternalType>(static_cast< LargerType<unsigned long int, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const signed long int & value)
: value(static_cast<InternalType>(static_cast< LargerType<signed long int, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const unsigned long long int & value)
: value(static_cast<InternalType>(static_cast< LargerType<unsigned long long int, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const signed long long int & value)
: value(static_cast<InternalType>(static_cast< LargerType<signed long long int, InternalType> >(value) << Fraction)) {}
constexpr SFixedBase(const double & value)
: value(static_cast<InternalType>(value * static_cast<double>(Scale))) {}
constexpr SFixedBase(const float & value)
: value(static_cast<InternalType>(value * static_cast<float>(Scale))) {}
constexpr SFixedBase(const long double & value)
: value(static_cast<InternalType>(value * static_cast<long double>(Scale))) {}
};
}
FIXED_POINTS_END_NAMESPACE

@ -18,15 +18,105 @@ FIXED_POINTS_BEGIN_NAMESPACE
// Constructors // Constructors
// //
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const RawType & value)
: value(static_cast<InternalType>(value))
{
}
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(void) constexpr SFixed<Integer, Fraction>::SFixed(void)
: Base() : value(0)
{ {
} }
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const IntegerType & integer, const FractionType & fraction) constexpr SFixed<Integer, Fraction>::SFixed(const IntegerType & integer, const FractionType & fraction)
: Base(RawType((static_cast<InternalType>(integer) << FractionSize) | fraction)) : value((static_cast<InternalType>(integer) << FractionSize) | fraction)
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const char & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<char, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const unsigned char & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<unsigned char, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const signed char & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<signed char, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const unsigned short int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<unsigned short int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const signed short int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<signed short int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const unsigned int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<unsigned int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const signed int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<signed int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const unsigned long int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<unsigned long int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const signed long int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<signed long int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const unsigned long long int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<unsigned long long int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const signed long long int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<signed long long int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const double & value)
: value(static_cast<InternalType>(value * static_cast<double>(Scale)))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const float & value)
: value(static_cast<InternalType>(value * static_cast<float>(Scale)))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction>::SFixed(const long double & value)
: value(static_cast<InternalType>(value * static_cast<long double>(Scale)))
{ {
} }

@ -15,7 +15,6 @@
#pragma once #pragma once
#include "Details.h" #include "Details.h"
#include "UFixedBase.h"
FIXED_POINTS_BEGIN_NAMESPACE FIXED_POINTS_BEGIN_NAMESPACE
@ -24,7 +23,7 @@ FIXED_POINTS_BEGIN_NAMESPACE
// //
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
class UFixed : FIXED_POINTS_DETAILS::UFixedBase< Integer, Fraction > class UFixed
{ {
public: public:
static_assert((Integer + Fraction) <= FIXED_POINTS_DETAILS::BitSize<uintmax_t>::Value, "Platform does not have a native type large enough for UFixed."); static_assert((Integer + Fraction) <= FIXED_POINTS_DETAILS::BitSize<uintmax_t>::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<FractionSize>::Value; constexpr const static MaskType MidpointMask = FIXED_POINTS_DETAILS::MsbMask<FractionSize>::Value;
constexpr const static MaskType LesserMidpointMask = MidpointMask - 1; constexpr const static MaskType LesserMidpointMask = MidpointMask - 1;
private: protected:
using Base = FIXED_POINTS_DETAILS::UFixedBase<Integer, Fraction>; class RawType
using RawType = typename Base::RawType; {
private:
const InternalType value;
public: public:
using Base::Base; 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(void);
constexpr UFixed(const IntegerType & integer, const FractionType & fraction); 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 InternalType getInternal(void) const;
constexpr IntegerType getInteger(void) const; constexpr IntegerType getInteger(void) const;
constexpr FractionType getFraction(void) const; constexpr FractionType getFraction(void) const;

@ -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<Integer>;
using FractionType = FIXED_POINTS_DETAILS::LeastUInt<Fraction>;
using InternalType = FIXED_POINTS_DETAILS::LeastUInt<Integer + 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 UFixedBase(void) : value(0) {}
constexpr UFixedBase(const RawType & value) : value(static_cast<InternalType>(value)) {}
public:
constexpr UFixedBase(const char & value)
: value(static_cast<InternalType>(static_cast< LargerType<char, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const unsigned char & value)
: value(static_cast<InternalType>(static_cast< LargerType<unsigned char, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const signed char & value)
: value(static_cast<InternalType>(static_cast< LargerType<signed char, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const unsigned short int & value)
: value(static_cast<InternalType>(static_cast< LargerType<unsigned short int, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const signed short int & value)
: value(static_cast<InternalType>(static_cast< LargerType<signed short int, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const unsigned int & value)
: value(static_cast<InternalType>(static_cast< LargerType<unsigned int, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const signed int & value)
: value(static_cast<InternalType>(static_cast< LargerType<signed int, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const unsigned long int & value)
: value(static_cast<InternalType>(static_cast< LargerType<unsigned long int, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const signed long int & value)
: value(static_cast<InternalType>(static_cast< LargerType<signed long int, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const unsigned long long int & value)
: value(static_cast<InternalType>(static_cast< LargerType<unsigned long long int, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const signed long long int & value)
: value(static_cast<InternalType>(static_cast< LargerType<signed long long int, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const double & value)
: value(static_cast<InternalType>(value * static_cast<double>(Scale))) {}
constexpr UFixedBase(const float & value)
: value(static_cast<InternalType>(value * static_cast<float>(Scale))) {}
constexpr UFixedBase(const long double & value)
: value(static_cast<InternalType>(value * static_cast<long double>(Scale))) {}
};
}
FIXED_POINTS_END_NAMESPACE

@ -18,15 +18,105 @@ FIXED_POINTS_BEGIN_NAMESPACE
// Constructors // Constructors
// //
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const RawType & value)
: value(static_cast<InternalType>(value))
{
}
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(void) constexpr UFixed<Integer, Fraction>::UFixed(void)
: Base() : value(0)
{ {
} }
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const IntegerType & integer, const FractionType & fraction) constexpr UFixed<Integer, Fraction>::UFixed(const IntegerType & integer, const FractionType & fraction)
: Base(RawType((static_cast<InternalType>(integer) << FractionSize) | fraction)) : value((static_cast<InternalType>(integer) << FractionSize) | fraction)
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const char & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<char, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const unsigned char & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<unsigned char, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const signed char & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<signed char, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const unsigned short int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<unsigned short int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const signed short int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<signed short int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const unsigned int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<unsigned int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const signed int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<signed int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const unsigned long int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<unsigned long int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const signed long int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<signed long int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const unsigned long long int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<unsigned long long int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const signed long long int & value)
: value(static_cast<InternalType>(static_cast< FIXED_POINTS_DETAILS::LargerType<signed long long int, InternalType> >(value) << FractionSize))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const double & value)
: value(static_cast<InternalType>(value * static_cast<double>(Scale)))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const float & value)
: value(static_cast<InternalType>(value * static_cast<float>(Scale)))
{
}
template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction>::UFixed(const long double & value)
: value(static_cast<InternalType>(value * static_cast<long double>(Scale)))
{ {
} }

Loading…
Cancel
Save