From 892df22ee8575dbdb86aae9a18c0dca0f025c62a Mon Sep 17 00:00:00 2001 From: Pharap <2933055+Pharap@users.noreply.github.com> Date: Thu, 25 Mar 2021 09:54:07 +0000 Subject: [PATCH] Implement bitwise shift operators (#82) The bitwise shift operators are provided solely for optimisation purposes. --- src/FixedPoints/SFixedFreeFunctions.h | 81 ++++++++++++++++++++------- src/FixedPoints/UFixedFreeFunctions.h | 81 ++++++++++++++++++++------- 2 files changed, 124 insertions(+), 38 deletions(-) diff --git a/src/FixedPoints/SFixedFreeFunctions.h b/src/FixedPoints/SFixedFreeFunctions.h index bfafdc9..fcf8d71 100644 --- a/src/FixedPoints/SFixedFreeFunctions.h +++ b/src/FixedPoints/SFixedFreeFunctions.h @@ -277,6 +277,28 @@ constexpr auto operator /(const SFixed & left, const {\ return (SFixed(left) op right);\ } + +#define DELETED_BITSHIFT_OPERATOR( type, op )\ + template< unsigned Integer, unsigned Fraction >\ + constexpr SFixed operator op (const SFixed & left, const type & right) = delete;\ + \ + template< unsigned Integer, unsigned Fraction >\ + inline SFixed operator op##= (const SFixed & left, const type & right) = delete; + +#define BITSHIFT_OPERATOR( type, op )\ + template< unsigned Integer, unsigned Fraction >\ + constexpr SFixed operator op (const SFixed & left, const type & right)\ + {\ + using InternalType = typename SFixed::InternalType;\ + return SFixed::fromInternal(static_cast(left.getInternal() op right));\ + }\ + \ + template< unsigned Integer, unsigned Fraction >\ + inline SFixed & operator op##= (SFixed & left, const type & right)\ + {\ + left = (left op right);\ + return left;\ + } #define LOGIC_OPERATORS( type )\ LOGIC_OPERATOR( type, == )\ @@ -291,33 +313,54 @@ constexpr auto operator /(const SFixed & left, const ARITHMETIC_OPERATOR( type, - )\ ARITHMETIC_OPERATOR( type, * )\ ARITHMETIC_OPERATOR( type, / ) + +#define DELETED_BITSHIFT_OPERATORS( type ) \ + DELETED_BITSHIFT_OPERATOR( type, << )\ + DELETED_BITSHIFT_OPERATOR( type, >> ) + +#define BITSHIFT_OPERATORS( type ) \ + BITSHIFT_OPERATOR( type, << )\ + BITSHIFT_OPERATOR( type, >> ) -#define OPERATORS( type ) \ +#define FLOAT_OPERATORS( type ) \ LOGIC_OPERATORS( type )\ - ARITHMETIC_OPERATORS( type ) - -OPERATORS( float ) -OPERATORS( double ) -OPERATORS( long double ) - -OPERATORS( char ) -OPERATORS( unsigned char ) -OPERATORS( signed char ) -OPERATORS( unsigned short int ) -OPERATORS( signed short int ) -OPERATORS( unsigned int ) -OPERATORS( signed int ) -OPERATORS( unsigned long int ) -OPERATORS( signed long int ) -OPERATORS( unsigned long long int ) -OPERATORS( signed long long int ) + ARITHMETIC_OPERATORS( type )\ + DELETED_BITSHIFT_OPERATORS( type ) + +#define INTEGER_OPERATORS( type ) \ + LOGIC_OPERATORS( type )\ + ARITHMETIC_OPERATORS( type )\ + BITSHIFT_OPERATORS( type ) + +FLOAT_OPERATORS( float ) +FLOAT_OPERATORS( double ) +FLOAT_OPERATORS( long double ) + +INTEGER_OPERATORS( char ) +INTEGER_OPERATORS( unsigned char ) +INTEGER_OPERATORS( signed char ) +INTEGER_OPERATORS( unsigned short int ) +INTEGER_OPERATORS( signed short int ) +INTEGER_OPERATORS( unsigned int ) +INTEGER_OPERATORS( signed int ) +INTEGER_OPERATORS( unsigned long int ) +INTEGER_OPERATORS( signed long int ) +INTEGER_OPERATORS( unsigned long long int ) +INTEGER_OPERATORS( signed long long int ) // Prevent Macro-bleed: -#undef OPERATORS +#undef INTEGER_OPERATORS +#undef FLOAT_OPERATORS + #undef ARITHMETIC_OPERATORS #undef LOGIC_OPERATORS +#undef BITSHIFT_OPERATORS +#undef DELETED_BITSHIFT_OPERATORS + #undef ARITHMETIC_OPERATOR #undef LOGIC_OPERATOR +#undef BITSHIFT_OPERATOR +#undef DELETED_BITSHIFT_OPERATOR FIXED_POINTS_END_NAMESPACE \ No newline at end of file diff --git a/src/FixedPoints/UFixedFreeFunctions.h b/src/FixedPoints/UFixedFreeFunctions.h index 64d959b..a424bec 100644 --- a/src/FixedPoints/UFixedFreeFunctions.h +++ b/src/FixedPoints/UFixedFreeFunctions.h @@ -277,6 +277,28 @@ constexpr auto operator /(const UFixed & left, const {\ return (UFixed(left) op right);\ } + +#define DELETED_BITSHIFT_OPERATOR( type, op )\ + template< unsigned Integer, unsigned Fraction >\ + constexpr UFixed operator op (const UFixed & left, const type & right) = delete;\ + \ + template< unsigned Integer, unsigned Fraction >\ + inline UFixed operator op##= (const UFixed & left, const type & right) = delete; + +#define BITSHIFT_OPERATOR( type, op )\ + template< unsigned Integer, unsigned Fraction >\ + constexpr UFixed operator op (const UFixed & left, const type & right)\ + {\ + using InternalType = typename UFixed::InternalType;\ + return UFixed::fromInternal(static_cast(left.getInternal() op right));\ + }\ + \ + template< unsigned Integer, unsigned Fraction >\ + inline UFixed & operator op##= (UFixed & left, const type & right)\ + {\ + left = (left op right);\ + return left;\ + } #define LOGIC_OPERATORS( type )\ LOGIC_OPERATOR( type, == )\ @@ -291,33 +313,54 @@ constexpr auto operator /(const UFixed & left, const ARITHMETIC_OPERATOR( type, - )\ ARITHMETIC_OPERATOR( type, * )\ ARITHMETIC_OPERATOR( type, / ) + +#define DELETED_BITSHIFT_OPERATORS( type ) \ + DELETED_BITSHIFT_OPERATOR( type, << )\ + DELETED_BITSHIFT_OPERATOR( type, >> ) + +#define BITSHIFT_OPERATORS( type ) \ + BITSHIFT_OPERATOR( type, << )\ + BITSHIFT_OPERATOR( type, >> ) -#define OPERATORS( type ) \ +#define FLOAT_OPERATORS( type ) \ LOGIC_OPERATORS( type )\ - ARITHMETIC_OPERATORS( type ) - -OPERATORS( float ) -OPERATORS( double ) -OPERATORS( long double ) - -OPERATORS( char ) -OPERATORS( unsigned char ) -OPERATORS( signed char ) -OPERATORS( unsigned short int ) -OPERATORS( signed short int ) -OPERATORS( unsigned int ) -OPERATORS( signed int ) -OPERATORS( unsigned long int ) -OPERATORS( signed long int ) -OPERATORS( unsigned long long int ) -OPERATORS( signed long long int ) + ARITHMETIC_OPERATORS( type )\ + DELETED_BITSHIFT_OPERATORS( type ) + +#define INTEGER_OPERATORS( type ) \ + LOGIC_OPERATORS( type )\ + ARITHMETIC_OPERATORS( type )\ + BITSHIFT_OPERATORS( type ) + +FLOAT_OPERATORS( float ) +FLOAT_OPERATORS( double ) +FLOAT_OPERATORS( long double ) + +INTEGER_OPERATORS( char ) +INTEGER_OPERATORS( unsigned char ) +INTEGER_OPERATORS( signed char ) +INTEGER_OPERATORS( unsigned short int ) +INTEGER_OPERATORS( signed short int ) +INTEGER_OPERATORS( unsigned int ) +INTEGER_OPERATORS( signed int ) +INTEGER_OPERATORS( unsigned long int ) +INTEGER_OPERATORS( signed long int ) +INTEGER_OPERATORS( unsigned long long int ) +INTEGER_OPERATORS( signed long long int ) // Prevent Macro-bleed: -#undef OPERATORS +#undef INTEGER_OPERATORS +#undef FLOAT_OPERATORS + #undef ARITHMETIC_OPERATORS #undef LOGIC_OPERATORS +#undef BITSHIFT_OPERATORS +#undef DELETED_BITSHIFT_OPERATORS + #undef ARITHMETIC_OPERATOR #undef LOGIC_OPERATOR +#undef BITSHIFT_OPERATOR +#undef DELETED_BITSHIFT_OPERATOR FIXED_POINTS_END_NAMESPACE \ No newline at end of file