Test commit of 0 integer fixed points

add-0-integer-specialisation
Pharap 7 years ago
parent fb3f82fd72
commit 8cc3094f7b
  1. 3
      src/FixedPoints/FixedPoints.h
  2. 128
      src/FixedPoints/SFixed0.h
  3. 209
      src/FixedPoints/SFixed0FreeFunctions.h
  4. 191
      src/FixedPoints/SFixed0MemberFunctions.h
  5. 128
      src/FixedPoints/UFixed0.h
  6. 209
      src/FixedPoints/UFixed0FreeFunctions.h
  7. 175
      src/FixedPoints/UFixed0MemberFunctions.h
  8. 26
      src/FixedPoints/Utils.h

@ -17,4 +17,7 @@
#include "UFixed.h"
#include "SFixed.h"
#include "UFixed0.h"
#include "SFixed0.h"
#include "Utils.h"

@ -0,0 +1,128 @@
// Copyright 2017-2018 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
//
// Declaration
//
// Predeclaration
template< unsigned Integer, unsigned Fraction > class SFixed;
template< unsigned Fraction >
class SFixed<0, Fraction>
{
public:
constexpr const static uintmax_t IntegerSize = 1;
constexpr const static uintmax_t FractionSize = Fraction;
constexpr const static uintmax_t LogicalSize = IntegerSize + FractionSize;
constexpr const static uintmax_t Scale = UINTMAX_C(1) << FractionSize;
public:
static_assert(LogicalSize <= FIXED_POINTS_DETAILS::BitSize<intmax_t>::Value, "Platform does not have a native type large enough for SFixed.");
public:
using IntegerType = FIXED_POINTS_DETAILS::LeastInt<IntegerSize>;
using FractionType = FIXED_POINTS_DETAILS::LeastUInt<FractionSize>;
using InternalType = FIXED_POINTS_DETAILS::LeastInt<LogicalSize>;
constexpr const static uintmax_t InternalSize = FIXED_POINTS_DETAILS::BitSize<InternalType>::Value;
using ShiftType = FIXED_POINTS_DETAILS::LeastUInt<LogicalSize>;
using MaskType = FIXED_POINTS_DETAILS::LeastUInt<LogicalSize>;
public:
constexpr const static ShiftType IntegerShift = FractionSize;
constexpr const static ShiftType FractionShift = 0;
constexpr const static MaskType IntegerMask = FIXED_POINTS_DETAILS::IdentityMask<IntegerSize>::Value;
constexpr const static MaskType FractionMask = FIXED_POINTS_DETAILS::IdentityMask<FractionSize>::Value;
constexpr const static MaskType IdentityMask = (IntegerMask << IntegerShift) | (FractionMask << FractionShift);
constexpr const static MaskType MidpointMask = FIXED_POINTS_DETAILS::MsbMask<FractionSize>::Value;
constexpr const static MaskType LesserMidpointMask = MidpointMask - 1;
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) = delete;
constexpr SFixed(const unsigned char & value) = delete;
constexpr SFixed(const signed char & value) = delete;
constexpr SFixed(const unsigned short int & value) = delete;
constexpr SFixed(const signed short int & value) = delete;
constexpr SFixed(const unsigned int & value) = delete;
constexpr SFixed(const signed int & value) = delete;
constexpr SFixed(const unsigned long int & value) = delete;
constexpr SFixed(const signed long int & value) = delete;
constexpr SFixed(const unsigned long long int & value) = delete;
constexpr SFixed(const signed long long int & value) = delete;
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;
constexpr FractionType getFraction(void) const;
constexpr explicit operator IntegerType(void) const;
constexpr explicit operator float(void) const;
constexpr explicit operator double(void) const;
constexpr explicit operator long double(void) const;
template< unsigned IntegerOut, unsigned FractionOut >
constexpr explicit operator SFixed<IntegerOut, FractionOut>(void) const;
constexpr static SFixed fromInternal(const InternalType & value);
constexpr SFixed operator -(void) const;
SFixed & operator +=(const SFixed & other);
SFixed & operator -=(const SFixed & other);
SFixed & operator *=(const SFixed & other);
SFixed & operator /=(const SFixed & other);
public:
constexpr const static SFixed Epsilon = SFixed::fromInternal(1);
constexpr const static SFixed MinValue = SFixed::fromInternal(FIXED_POINTS_DETAILS::MsbMask<InternalSize>::Value);
constexpr const static SFixed MaxValue = SFixed::fromInternal(~FIXED_POINTS_DETAILS::MsbMask<InternalSize>::Value);
// Pi, E, Phi and Tau will never be representable by 0 integer fixed points
};
FIXED_POINTS_END_NAMESPACE
#include "SFixed0MemberFunctions.h"
#include "SFixed0FreeFunctions.h"

@ -0,0 +1,209 @@
// Copyright 2017-2018 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.
FIXED_POINTS_BEGIN_NAMESPACE
//
// Literal-type Operators
// Generated by macro to make maintenance easier
//
#define LOGIC_OPERATOR( type, op )\
template< unsigned Fraction >\
constexpr bool operator op (const SFixed<0, Fraction> & left, const type & right)\
{\
return (left op SFixed<0, Fraction>(right));\
}\
\
template< unsigned Fraction >\
constexpr bool operator op (const type & left, const SFixed<0, Fraction> & right)\
{\
return (SFixed<0, Fraction>(left) op right);\
}
#define DELETED_LOGIC_OPERATOR_EQ( type )\
template< unsigned Fraction >\
constexpr bool operator == (const SFixed<0, Fraction> & left, const type & right)\
{\
return false;\
}\
\
template< unsigned Fraction >\
constexpr bool operator == (const type & left, const SFixed<0, Fraction> & right)\
{\
return false;\
}
#define DELETED_LOGIC_OPERATOR_NE( type )\
template< unsigned Fraction >\
constexpr bool operator != (const SFixed<0, Fraction> & left, const type & right)\
{\
return true;\
}\
\
template< unsigned Fraction >\
constexpr bool operator != (const type & left, const SFixed<0, Fraction> & right)\
{\
return true;\
}
#define DELETED_LOGIC_OPERATOR_LT( type )\
template< unsigned Fraction >\
constexpr bool operator < (const SFixed<0, Fraction> & left, const type & right)\
{\
return true;\
}\
\
template< unsigned Fraction >\
constexpr bool operator < (const type & left, const SFixed<0, Fraction> & right)\
{\
return false;\
}
#define DELETED_LOGIC_OPERATOR_LTE( type )\
template< unsigned Fraction >\
constexpr bool operator <= (const SFixed<0, Fraction> & left, const type & right)\
{\
return true;\
}\
\
template< unsigned Fraction >\
constexpr bool operator <= (const type & left, const SFixed<0, Fraction> & right)\
{\
return false;\
}
#define DELETED_LOGIC_OPERATOR_GT( type )\
template< unsigned Fraction >\
constexpr bool operator > (const SFixed<0, Fraction> & left, const type & right)\
{\
return false;\
}\
\
template< unsigned Fraction >\
constexpr bool operator > (const type & left, const SFixed<0, Fraction> & right)\
{\
return true;\
}
#define DELETED_LOGIC_OPERATOR_GTE( type )\
template< unsigned Fraction >\
constexpr bool operator >= (const SFixed<0, Fraction> & left, const type & right)\
{\
return false;\
}\
\
template< unsigned Fraction >\
constexpr bool operator >= (const type & left, const SFixed<0, Fraction> & right)\
{\
return true;\
}
#define ARITHMETIC_OPERATOR( type, op )\
template< unsigned Fraction >\
constexpr SFixed<0, Fraction> operator op (const SFixed<0, Fraction> & left, const type & right)\
{\
return (left op SFixed<0, Fraction>(right));\
}\
\
template< unsigned Fraction >\
constexpr SFixed<0, Fraction> operator op (const type & left, const SFixed<0, Fraction> & right)\
{\
return (SFixed<0, Fraction>(left) op right);\
}
#define DELETED_ARITHMETIC_OPERATOR( type, op )\
template< unsigned Fraction >\
constexpr SFixed<0, Fraction> operator op (const SFixed<0, Fraction> & left, const type & right)\
{\
return 0;\
}\
\
template< unsigned Fraction >\
constexpr SFixed<0, Fraction> operator op (const type & left, const SFixed<0, Fraction> & right)\
{\
return 0;\
}
#define LOGIC_OPERATORS( type ) \
LOGIC_OPERATOR( type, == )\
LOGIC_OPERATOR( type, != )\
LOGIC_OPERATOR( type, < )\
LOGIC_OPERATOR( type, <= )\
LOGIC_OPERATOR( type, > )\
LOGIC_OPERATOR( type, >= )
#define ARITHMETIC_OPERATORS( type ) \
ARITHMETIC_OPERATOR( type, + )\
ARITHMETIC_OPERATOR( type, - )\
ARITHMETIC_OPERATOR( type, * )\
ARITHMETIC_OPERATOR( type, / )
#define DELETED_LOGIC_OPERATORS( type ) \
DELETED_LOGIC_OPERATOR_EQ( type )\
DELETED_LOGIC_OPERATOR_NE( type )\
DELETED_LOGIC_OPERATOR_LT( type )\
DELETED_LOGIC_OPERATOR_LTE( type )\
DELETED_LOGIC_OPERATOR_GT( type )\
DELETED_LOGIC_OPERATOR_GTE( type )
#define DELETED_ARITHMETIC_OPERATORS( type ) \
DELETED_ARITHMETIC_OPERATOR( type, + )\
DELETED_ARITHMETIC_OPERATOR( type, - )\
DELETED_ARITHMETIC_OPERATOR( type, * )\
DELETED_ARITHMETIC_OPERATOR( type, / )
#define OPERATORS( type ) \
LOGIC_OPERATORS( type )\
ARITHMETIC_OPERATORS( type )
#define DELETED_OPERATORS( type ) \
DELETED_LOGIC_OPERATORS( type )\
DELETED_ARITHMETIC_OPERATORS( type )
OPERATORS( float )
OPERATORS( double )
OPERATORS( long double )
DELETED_OPERATORS( char )
DELETED_OPERATORS( unsigned char )
DELETED_OPERATORS( signed char )
DELETED_OPERATORS( unsigned short int )
DELETED_OPERATORS( signed short int )
DELETED_OPERATORS( unsigned int )
DELETED_OPERATORS( signed int )
DELETED_OPERATORS( unsigned long int )
DELETED_OPERATORS( signed long int )
DELETED_OPERATORS( unsigned long long int )
DELETED_OPERATORS( signed long long int )
// Prevent Macro-bleed:
#undef DELETED_OPERATORS
#undef OPERATORS
#undef DELETED_ARITHMETIC_OPERATORS
#undef DELETED_LOGIC_OPERATORS
#undef ARITHMETIC_OPERATORS
#undef LOGIC_OPERATORS
#undef DELETED_ARITHMETIC_OPERATOR
#undef ARITHMETIC_OPERATOR
#undef DELETED_LOGIC_OPERATOR_EQ
#undef DELETED_LOGIC_OPERATOR_NE
#undef DELETED_LOGIC_OPERATOR_LT
#undef DELETED_LOGIC_OPERATOR_LTE
#undef DELETED_LOGIC_OPERATOR_GT
#undef DELETED_LOGIC_OPERATOR_GTE
FIXED_POINTS_END_NAMESPACE

@ -0,0 +1,191 @@
// Copyright 2017-2018 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.
FIXED_POINTS_BEGIN_NAMESPACE
//
// Constructors
//
template< unsigned Fraction >
constexpr SFixed<0, Fraction>::SFixed(const RawType & value)
: value(static_cast<InternalType>(value))
{
}
template< unsigned Fraction >
constexpr SFixed<0, Fraction>::SFixed(void)
: value(0)
{
}
// Here the comma operator is used to circumvent an unused parameter warning
template< unsigned Fraction >
constexpr SFixed<0, Fraction>::SFixed(const IntegerType & integer, const FractionType & fraction)
: value((integer, fraction))
{
}
template< unsigned Fraction >
constexpr SFixed<0, Fraction>::SFixed(const double & value)
: value(static_cast<InternalType>(value * static_cast<double>(Scale)))
{
}
template< unsigned Fraction >
constexpr SFixed<0, Fraction>::SFixed(const float & value)
: value(static_cast<InternalType>(value * static_cast<float>(Scale)))
{
}
template< unsigned Fraction >
constexpr SFixed<0, Fraction>::SFixed(const long double & value)
: value(static_cast<InternalType>(value * static_cast<long double>(Scale)))
{
}
//
// Getters
//
template< unsigned Fraction >
constexpr typename SFixed<0, Fraction>::InternalType SFixed<0, Fraction>::getInternal(void) const
{
return this->value;
}
template< unsigned Fraction >
constexpr typename SFixed<0, Fraction>::IntegerType SFixed<0, Fraction>::getInteger(void) const
{
static_assert(false, "UNDEFINED BEHAVIOUR");
//return (static_cast<IntegerType>(this->value >> IntegerShift) & IntegerMask) | ((this->value < 0) ? ~IntegerMask : 0);
}
template< unsigned Fraction >
constexpr typename SFixed<0, Fraction>::FractionType SFixed<0, Fraction>::getFraction(void) const
{
return static_cast<FractionType>(this->value >> FractionShift) & FractionMask;
}
//
// Cast Operators
//
template< unsigned Fraction >
constexpr SFixed<0, Fraction>::operator IntegerType(void) const
{
return this->getInteger();
}
template< unsigned Fraction >
constexpr SFixed<0, Fraction>::operator float(void) const
{
return (1.0F / Scale) *
static_cast<InternalType>
((this->value & IdentityMask) |
((this->value < 0) ? ~IdentityMask : 0));
}
template< unsigned Fraction >
constexpr SFixed<0, Fraction>::operator double(void) const
{
return (1.0 / Scale) *
static_cast<InternalType>
((this->value & IdentityMask) |
((this->value < 0) ? ~IdentityMask : 0));
}
template< unsigned Fraction >
constexpr SFixed<0, Fraction>::operator long double(void) const
{
return (1.0L / Scale) *
static_cast<InternalType>
((this->value & IdentityMask) |
((this->value < 0) ? ~IdentityMask : 0));
}
template< unsigned Fraction >
template< unsigned IntegerOut, unsigned FractionOut >
constexpr SFixed<0, Fraction>::operator SFixed<IntegerOut, FractionOut>(void) const
{
using OutputType = SFixed<IntegerOut, FractionOut>;
using OutputInternalType = typename OutputType::InternalType;
using OutputShiftType = typename OutputType::ShiftType;
using InputType = SFixed<0, Fraction>;
using InputShiftType = typename InputType::ShiftType;
return
(FractionOut > FractionSize) ?
OutputType::fromInternal(static_cast<OutputInternalType>(static_cast<OutputShiftType>(this->value) << ((FractionOut > FractionSize) ? (FractionOut - FractionSize) : 0))) :
(FractionSize > FractionOut) ?
OutputType::fromInternal(static_cast<OutputInternalType>(static_cast<InputShiftType>(this->value) >> ((FractionSize > FractionOut) ? (FractionSize - FractionOut) : 0))) :
OutputType::fromInternal(this->value);
}
//
// Static Functions
//
template< unsigned Fraction >
constexpr SFixed<0, Fraction> SFixed<0, Fraction>::fromInternal(const typename SFixed<0, Fraction>::InternalType & value)
{
return SFixed<0, Fraction>(RawType(value));
}
template< unsigned Fraction >
constexpr SFixed<0, Fraction> SFixed<0, Fraction>::operator -(void) const
{
return SFixed<0, Fraction>::fromInternal(-this->value);
}
//
// Compound Assignment Operators
//
template< unsigned Fraction >
SFixed<0, Fraction> & SFixed<0, Fraction>::operator +=(const SFixed<0, Fraction> & other)
{
this->value += other.value;
return *this;
}
template< unsigned Fraction >
SFixed<0, Fraction> & SFixed<0, Fraction>::operator -=(const SFixed<0, Fraction> & other)
{
this->value -= other.value;
return *this;
}
template< unsigned Fraction >
SFixed<0, Fraction> & SFixed<0, Fraction>::operator *=(const SFixed<0, Fraction> & other)
{
using InternalType = typename SFixed<0, Fraction>::InternalType;
using PrecisionType = typename SFixed<Integer * 2, Fraction * 2>::InternalType;
const PrecisionType temp = (static_cast<PrecisionType>(this->value) * static_cast<PrecisionType>(other.value)) >> Fraction;
this->value = static_cast<InternalType>(temp);
return *this;
}
template< unsigned Fraction >
SFixed<0, Fraction> & SFixed<0, Fraction>::operator /=(const SFixed<0, Fraction> & other)
{
using InternalType = typename SFixed<0, Fraction>::InternalType;
using PrecisionType = typename SFixed<Integer * 2, Fraction * 2>::InternalType;
const PrecisionType temp = (static_cast<PrecisionType>(this->value) << Fraction) / static_cast<PrecisionType>(other.value);
this->value = static_cast<InternalType>(temp);
return *this;
}
FIXED_POINTS_END_NAMESPACE

@ -0,0 +1,128 @@
// Copyright 2017-2018 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
//
// Declaration
//
// Predeclaration
template< unsigned Integer, unsigned Fraction > class UFixed;
template< unsigned Fraction >
class UFixed<0, Fraction>
{
public:
constexpr const static uintmax_t IntegerSize = 0;
constexpr const static uintmax_t FractionSize = Fraction;
constexpr const static uintmax_t LogicalSize = IntegerSize + FractionSize;
constexpr const static uintmax_t Scale = UINTMAX_C(1) << FractionSize;
public:
static_assert(LogicalSize <= FIXED_POINTS_DETAILS::BitSize<uintmax_t>::Value, "Platform does not have a native type large enough for UFixed.");
public:
using IntegerType = FIXED_POINTS_DETAILS::LeastUInt<IntegerSize>;
using FractionType = FIXED_POINTS_DETAILS::LeastUInt<FractionSize>;
using InternalType = FIXED_POINTS_DETAILS::LeastUInt<LogicalSize>;
constexpr const static uintmax_t InternalSize = FIXED_POINTS_DETAILS::BitSize<InternalType>::Value;
using ShiftType = FIXED_POINTS_DETAILS::LeastUInt<LogicalSize>;
using MaskType = FIXED_POINTS_DETAILS::LeastUInt<LogicalSize>;
public:
constexpr const static ShiftType IntegerShift = FractionSize;
constexpr const static ShiftType FractionShift = 0;
constexpr const static MaskType IntegerMask = FIXED_POINTS_DETAILS::IdentityMask<IntegerSize>::Value;
constexpr const static MaskType FractionMask = FIXED_POINTS_DETAILS::IdentityMask<FractionSize>::Value;
constexpr const static MaskType IdentityMask = (IntegerMask << IntegerShift) | (FractionMask << FractionShift);
constexpr const static MaskType MidpointMask = FIXED_POINTS_DETAILS::MsbMask<FractionSize>::Value;
constexpr const static MaskType LesserMidpointMask = MidpointMask - 1;
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 UFixed(const RawType & value);
public:
constexpr UFixed(void);
constexpr UFixed(const IntegerType & integer, const FractionType & fraction);
constexpr UFixed(const char & value) = delete;
constexpr UFixed(const unsigned char & value) = delete;
constexpr UFixed(const signed char & value) = delete;
constexpr UFixed(const unsigned short int & value) = delete;
constexpr UFixed(const signed short int & value) = delete;
constexpr UFixed(const unsigned int & value) = delete;
constexpr UFixed(const signed int & value) = delete;
constexpr UFixed(const unsigned long int & value) = delete;
constexpr UFixed(const signed long int & value) = delete;
constexpr UFixed(const unsigned long long int & value) = delete;
constexpr UFixed(const signed long long int & value) = delete;
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;
constexpr explicit operator IntegerType(void) const;
constexpr explicit operator float(void) const;
constexpr explicit operator double(void) const;
constexpr explicit operator long double(void) const;
template< unsigned IntegerOut, unsigned FractionOut >
constexpr explicit operator UFixed<IntegerOut, FractionOut>(void) const;
constexpr static UFixed fromInternal(const InternalType & value);
UFixed & operator +=(const UFixed & other);
UFixed & operator -=(const UFixed & other);
UFixed & operator *=(const UFixed & other);
UFixed & operator /=(const UFixed & other);
public:
constexpr const static UFixed Epsilon = UFixed::fromInternal(1);
constexpr const static UFixed MinValue = UFixed::fromInternal(0);
constexpr const static UFixed MaxValue = UFixed::fromInternal(~0);
// Pi, E, Phi and Tau will never be representable by 0 integer fixed points
};
FIXED_POINTS_END_NAMESPACE
#include "UFixed0MemberFunctions.h"
#include "UFixed0FreeFunctions.h"

@ -0,0 +1,209 @@
// Copyright 2017-2018 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.
FIXED_POINTS_BEGIN_NAMESPACE
//
// Literal-type Operators
// Generated by macro to make maintenance easier
//
#define LOGIC_OPERATOR( type, op )\
template< unsigned Fraction >\
constexpr bool operator op (const UFixed<0, Fraction> & left, const type & right)\
{\
return (left op UFixed<0, Fraction>(right));\
}\
\
template< unsigned Fraction >\
constexpr bool operator op (const type & left, const UFixed<0, Fraction> & right)\
{\
return (UFixed<0, Fraction>(left) op right);\
}
#define DELETED_LOGIC_OPERATOR_EQ( type )\
template< unsigned Fraction >\
constexpr bool operator == (const UFixed<0, Fraction> & left, const type & right)\
{\
return false;\
}\
\
template< unsigned Fraction >\
constexpr bool operator == (const type & left, const UFixed<0, Fraction> & right)\
{\
return false;\
}
#define DELETED_LOGIC_OPERATOR_NE( type )\
template< unsigned Fraction >\
constexpr bool operator != (const UFixed<0, Fraction> & left, const type & right)\
{\
return true;\
}\
\
template< unsigned Fraction >\
constexpr bool operator != (const type & left, const UFixed<0, Fraction> & right)\
{\
return true;\
}
#define DELETED_LOGIC_OPERATOR_LT( type )\
template< unsigned Fraction >\
constexpr bool operator < (const UFixed<0, Fraction> & left, const type & right)\
{\
return true;\
}\
\
template< unsigned Fraction >\
constexpr bool operator < (const type & left, const UFixed<0, Fraction> & right)\
{\
return false;\
}
#define DELETED_LOGIC_OPERATOR_LTE( type )\
template< unsigned Fraction >\
constexpr bool operator <= (const UFixed<0, Fraction> & left, const type & right)\
{\
return true;\
}\
\
template< unsigned Fraction >\
constexpr bool operator <= (const type & left, const UFixed<0, Fraction> & right)\
{\
return false;\
}
#define DELETED_LOGIC_OPERATOR_GT( type )\
template< unsigned Fraction >\
constexpr bool operator > (const UFixed<0, Fraction> & left, const type & right)\
{\
return false;\
}\
\
template< unsigned Fraction >\
constexpr bool operator > (const type & left, const UFixed<0, Fraction> & right)\
{\
return true;\
}
#define DELETED_LOGIC_OPERATOR_GTE( type )\
template< unsigned Fraction >\
constexpr bool operator >= (const UFixed<0, Fraction> & left, const type & right)\
{\
return false;\
}\
\
template< unsigned Fraction >\
constexpr bool operator >= (const type & left, const UFixed<0, Fraction> & right)\
{\
return true;\
}
#define ARITHMETIC_OPERATOR( type, op )\
template< unsigned Fraction >\
constexpr UFixed<0, Fraction> operator op (const UFixed<0, Fraction> & left, const type & right)\
{\
return (left op UFixed<0, Fraction>(right));\
}\
\
template< unsigned Fraction >\
constexpr UFixed<0, Fraction> operator op (const type & left, const UFixed<0, Fraction> & right)\
{\
return (UFixed<0, Fraction>(left) op right);\
}
#define DELETED_ARITHMETIC_OPERATOR( type, op )\
template< unsigned Fraction >\
constexpr UFixed<0, Fraction> operator op (const UFixed<0, Fraction> & left, const type & right)\
{\
return 0;\
}\
\
template< unsigned Fraction >\
constexpr UFixed<0, Fraction> operator op (const type & left, const UFixed<0, Fraction> & right)\
{\
return 0;\
}
#define LOGIC_OPERATORS( type ) \
LOGIC_OPERATOR( type, == )\
LOGIC_OPERATOR( type, != )\
LOGIC_OPERATOR( type, < )\
LOGIC_OPERATOR( type, <= )\
LOGIC_OPERATOR( type, > )\
LOGIC_OPERATOR( type, >= )
#define ARITHMETIC_OPERATORS( type ) \
ARITHMETIC_OPERATOR( type, + )\
ARITHMETIC_OPERATOR( type, - )\
ARITHMETIC_OPERATOR( type, * )\
ARITHMETIC_OPERATOR( type, / )
#define DELETED_LOGIC_OPERATORS( type ) \
DELETED_LOGIC_OPERATOR_EQ( type )\
DELETED_LOGIC_OPERATOR_NE( type )\
DELETED_LOGIC_OPERATOR_LT( type )\
DELETED_LOGIC_OPERATOR_LTE( type )\
DELETED_LOGIC_OPERATOR_GT( type )\
DELETED_LOGIC_OPERATOR_GTE( type )
#define DELETED_ARITHMETIC_OPERATORS( type ) \
DELETED_ARITHMETIC_OPERATOR( type, + )\
DELETED_ARITHMETIC_OPERATOR( type, - )\
DELETED_ARITHMETIC_OPERATOR( type, * )\
DELETED_ARITHMETIC_OPERATOR( type, / )
#define OPERATORS( type ) \
LOGIC_OPERATORS( type )\
ARITHMETIC_OPERATORS( type )
#define DELETED_OPERATORS( type ) \
DELETED_LOGIC_OPERATORS( type )\
DELETED_ARITHMETIC_OPERATORS( type )
OPERATORS( float )
OPERATORS( double )
OPERATORS( long double )
DELETED_OPERATORS( char )
DELETED_OPERATORS( unsigned char )
DELETED_OPERATORS( signed char )
DELETED_OPERATORS( unsigned short int )
DELETED_OPERATORS( signed short int )
DELETED_OPERATORS( unsigned int )
DELETED_OPERATORS( signed int )
DELETED_OPERATORS( unsigned long int )
DELETED_OPERATORS( signed long int )
DELETED_OPERATORS( unsigned long long int )
DELETED_OPERATORS( signed long long int )
// Prevent Macro-bleed:
#undef DELETED_OPERATORS
#undef OPERATORS
#undef DELETED_ARITHMETIC_OPERATORS
#undef DELETED_LOGIC_OPERATORS
#undef ARITHMETIC_OPERATORS
#undef LOGIC_OPERATORS
#undef DELETED_ARITHMETIC_OPERATOR
#undef ARITHMETIC_OPERATOR
#undef DELETED_LOGIC_OPERATOR_EQ
#undef DELETED_LOGIC_OPERATOR_NE
#undef DELETED_LOGIC_OPERATOR_LT
#undef DELETED_LOGIC_OPERATOR_LTE
#undef DELETED_LOGIC_OPERATOR_GT
#undef DELETED_LOGIC_OPERATOR_GTE
FIXED_POINTS_END_NAMESPACE

@ -0,0 +1,175 @@
// Copyright 2017-2018 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.
FIXED_POINTS_BEGIN_NAMESPACE
//
// Constructors
//
template< unsigned Fraction >
constexpr UFixed<0, Fraction>::UFixed(const RawType & value)
: value(static_cast<InternalType>(value))
{
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction>::UFixed(void)
: value(0)
{
}
// Here the comma operator is used to circumvent an unused parameter warning
template< unsigned Fraction >
constexpr UFixed<0, Fraction>::UFixed(const IntegerType & integer, const FractionType & fraction)
: value((integer, fraction))
{
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction>::UFixed(const double & value)
: value(static_cast<InternalType>(value * static_cast<double>(Scale)))
{
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction>::UFixed(const float & value)
: value(static_cast<InternalType>(value * static_cast<float>(Scale)))
{
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction>::UFixed(const long double & value)
: value(static_cast<InternalType>(value * static_cast<long double>(Scale)))
{
}
//
// Getters
//
template< unsigned Fraction >
constexpr typename UFixed<0, Fraction>::InternalType UFixed<0, Fraction>::getInternal(void) const
{
return this->value;
}
template< unsigned Fraction >
constexpr typename UFixed<0, Fraction>::IntegerType UFixed<0, Fraction>::getInteger(void) const
{
return 0;
}
template< unsigned Fraction >
constexpr typename UFixed<0, Fraction>::FractionType UFixed<0, Fraction>::getFraction(void) const
{
return static_cast<FractionType>(this->value) & FractionMask;
}
//
// Cast Operators
//
template< unsigned Fraction >
constexpr UFixed<0, Fraction>::operator IntegerType(void) const
{
return this->getInteger();
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction>::operator float(void) const
{
return ((this->value & IdentityMask) * (1.0F / Scale));
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction>::operator double(void) const
{
return ((this->value & IdentityMask) * (1.0 / Scale));
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction>::operator long double(void) const
{
return ((this->value & IdentityMask) * (1.0L / Scale));
}
template< unsigned Fraction >
template< unsigned IntegerOut, unsigned FractionOut >
constexpr UFixed<0, Fraction>::operator UFixed<IntegerOut, FractionOut>(void) const
{
using OutputType = UFixed<IntegerOut, FractionOut>;
using OutputInternalType = typename OutputType::InternalType;
using OutputShiftType = typename OutputType::ShiftType;
using InputType = UFixed<0, Fraction>;
using InputShiftType = typename InputType::ShiftType;
return
(FractionOut > FractionSize) ?
OutputType::fromInternal(static_cast<OutputInternalType>(static_cast<OutputShiftType>(this->value) << ((FractionOut > FractionSize) ? (FractionOut - FractionSize) : 0))) :
(FractionSize > FractionOut) ?
OutputType::fromInternal(static_cast<OutputInternalType>(static_cast<InputShiftType>(this->value) >> ((FractionSize > FractionOut) ? (FractionSize - FractionOut) : 0))) :
OutputType::fromInternal(this->value);
}
//
// Static Functions
//
template< unsigned Fraction >
constexpr UFixed<0, Fraction> UFixed<0, Fraction>::fromInternal(const typename UFixed<0, Fraction>::InternalType & value)
{
return UFixed<0, Fraction>(RawType(value));
}
//
// Compound Assignment Operators
//
template< unsigned Fraction >
UFixed<0, Fraction> & UFixed<0, Fraction>::operator +=(const UFixed<0, Fraction> & other)
{
this->value += other.value;
return *this;
}
template< unsigned Fraction >
UFixed<0, Fraction> & UFixed<0, Fraction>::operator -=(const UFixed<0, Fraction> & other)
{
this->value -= other.value;
return *this;
}
template< unsigned Fraction >
UFixed<0, Fraction> & UFixed<0, Fraction>::operator *=(const UFixed<0, Fraction> & other)
{
using InternalType = typename UFixed<0, Fraction>::InternalType;
using PrecisionType = typename UFixed<0, Fraction * 2>::InternalType;
const PrecisionType temp = (static_cast<PrecisionType>(this->value) * static_cast<PrecisionType>(other.value)) >> FractionSize;
this->value = static_cast<InternalType>(temp);
return *this;
}
template< unsigned Fraction >
UFixed<0, Fraction> & UFixed<0, Fraction>::operator /=(const UFixed<0, Fraction> & other)
{
using InternalType = typename UFixed<0, Fraction>::InternalType;
using PrecisionType = typename UFixed<0, Fraction * 2>::InternalType;
const PrecisionType temp = (static_cast<PrecisionType>(this->value) << FractionSize) / static_cast<PrecisionType>(other.value);
this->value = static_cast<InternalType>(temp);
return *this;
}
FIXED_POINTS_END_NAMESPACE

@ -103,6 +103,12 @@ constexpr UFixed<Integer, Fraction> floorFixed(const UFixed<Integer, Fraction> &
return OutputType::fromInternal(static_cast<InternalType>(value.getInternal() & ~OutputType::FractionMask));
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction> floorFixed(const UFixed<0, Fraction> & value)
{
return UFixed<0, Fraction>(0, 0);
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction> floorFixed(const SFixed<Integer, Fraction> & value)
{
@ -117,6 +123,12 @@ constexpr UFixed<Integer, Fraction> ceilFixed(const UFixed<Integer, Fraction> &
return UFixed<Integer, Fraction>((value.getFraction() == 0) ? value.getInteger() : (value.getInteger() + 1), 0);
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction> ceilFixed(const UFixed<0, Fraction> & value)
{
return UFixed<0, Fraction>(0, 0);
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction> ceilFixed(const SFixed<Integer, Fraction> & value)
{
@ -130,12 +142,18 @@ constexpr UFixed<Integer, Fraction> roundFixed(const UFixed<Integer, Fraction> &
return (value.getFraction() >= OutputType(0.5).getFraction()) ? ceilFixed(value) : floorFixed(value);
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction> roundFixed(const UFixed<0, Fraction> & value)
{
return UFixed<0, Fraction>(0, 0);
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction> roundFixed(const SFixed<Integer, Fraction> & value)
{
using OutputType = SFixed<Integer, Fraction>;
return
signbitFixed(value) ?
signbitFixed(value)
((value.getFraction() <= OutputType(0.5).getFraction()) ? floorFixed(value) : ceilFixed(value)) :
((value.getFraction() >= OutputType(0.5).getFraction()) ? ceilFixed(value) : floorFixed(value));
}
@ -146,6 +164,12 @@ constexpr UFixed<Integer, Fraction> truncFixed(const UFixed<Integer, Fraction> &
return UFixed<Integer, Fraction>(value.getInteger(), 0);
}
template< unsigned Fraction >
constexpr UFixed<0, Fraction> truncFixed(const UFixed<0, Fraction> & value)
{
return UFixed<0, Fraction>(0, 0);
}
template< unsigned Integer, unsigned Fraction >
constexpr SFixed<Integer, Fraction> truncFixed(const SFixed<Integer, Fraction> & value)
{

Loading…
Cancel
Save