Skip to content

Commit 892df22

Browse files
authored
Implement bitwise shift operators (#82)
The bitwise shift operators are provided solely for optimisation purposes.
1 parent af74462 commit 892df22

File tree

2 files changed

+124
-38
lines changed

2 files changed

+124
-38
lines changed

src/FixedPoints/SFixedFreeFunctions.h

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,28 @@ constexpr auto operator /(const SFixed<IntegerLeft, FractionLeft> & left, const
277277
{\
278278
return (SFixed<Integer, Fraction>(left) op right);\
279279
}
280+
281+
#define DELETED_BITSHIFT_OPERATOR( type, op )\
282+
template< unsigned Integer, unsigned Fraction >\
283+
constexpr SFixed<Integer, Fraction> operator op (const SFixed<Integer, Fraction> & left, const type & right) = delete;\
284+
\
285+
template< unsigned Integer, unsigned Fraction >\
286+
inline SFixed<Integer, Fraction> operator op##= (const SFixed<Integer, Fraction> & left, const type & right) = delete;
287+
288+
#define BITSHIFT_OPERATOR( type, op )\
289+
template< unsigned Integer, unsigned Fraction >\
290+
constexpr SFixed<Integer, Fraction> operator op (const SFixed<Integer, Fraction> & left, const type & right)\
291+
{\
292+
using InternalType = typename SFixed<Integer, Fraction>::InternalType;\
293+
return SFixed<Integer, Fraction>::fromInternal(static_cast<InternalType>(left.getInternal() op right));\
294+
}\
295+
\
296+
template< unsigned Integer, unsigned Fraction >\
297+
inline SFixed<Integer, Fraction> & operator op##= (SFixed<Integer, Fraction> & left, const type & right)\
298+
{\
299+
left = (left op right);\
300+
return left;\
301+
}
280302

281303
#define LOGIC_OPERATORS( type )\
282304
LOGIC_OPERATOR( type, == )\
@@ -291,33 +313,54 @@ constexpr auto operator /(const SFixed<IntegerLeft, FractionLeft> & left, const
291313
ARITHMETIC_OPERATOR( type, - )\
292314
ARITHMETIC_OPERATOR( type, * )\
293315
ARITHMETIC_OPERATOR( type, / )
316+
317+
#define DELETED_BITSHIFT_OPERATORS( type ) \
318+
DELETED_BITSHIFT_OPERATOR( type, << )\
319+
DELETED_BITSHIFT_OPERATOR( type, >> )
320+
321+
#define BITSHIFT_OPERATORS( type ) \
322+
BITSHIFT_OPERATOR( type, << )\
323+
BITSHIFT_OPERATOR( type, >> )
294324

295-
#define OPERATORS( type ) \
325+
#define FLOAT_OPERATORS( type ) \
296326
LOGIC_OPERATORS( type )\
297-
ARITHMETIC_OPERATORS( type )
298-
299-
OPERATORS( float )
300-
OPERATORS( double )
301-
OPERATORS( long double )
302-
303-
OPERATORS( char )
304-
OPERATORS( unsigned char )
305-
OPERATORS( signed char )
306-
OPERATORS( unsigned short int )
307-
OPERATORS( signed short int )
308-
OPERATORS( unsigned int )
309-
OPERATORS( signed int )
310-
OPERATORS( unsigned long int )
311-
OPERATORS( signed long int )
312-
OPERATORS( unsigned long long int )
313-
OPERATORS( signed long long int )
327+
ARITHMETIC_OPERATORS( type )\
328+
DELETED_BITSHIFT_OPERATORS( type )
329+
330+
#define INTEGER_OPERATORS( type ) \
331+
LOGIC_OPERATORS( type )\
332+
ARITHMETIC_OPERATORS( type )\
333+
BITSHIFT_OPERATORS( type )
334+
335+
FLOAT_OPERATORS( float )
336+
FLOAT_OPERATORS( double )
337+
FLOAT_OPERATORS( long double )
338+
339+
INTEGER_OPERATORS( char )
340+
INTEGER_OPERATORS( unsigned char )
341+
INTEGER_OPERATORS( signed char )
342+
INTEGER_OPERATORS( unsigned short int )
343+
INTEGER_OPERATORS( signed short int )
344+
INTEGER_OPERATORS( unsigned int )
345+
INTEGER_OPERATORS( signed int )
346+
INTEGER_OPERATORS( unsigned long int )
347+
INTEGER_OPERATORS( signed long int )
348+
INTEGER_OPERATORS( unsigned long long int )
349+
INTEGER_OPERATORS( signed long long int )
314350

315351
// Prevent Macro-bleed:
316352

317-
#undef OPERATORS
353+
#undef INTEGER_OPERATORS
354+
#undef FLOAT_OPERATORS
355+
318356
#undef ARITHMETIC_OPERATORS
319357
#undef LOGIC_OPERATORS
358+
#undef BITSHIFT_OPERATORS
359+
#undef DELETED_BITSHIFT_OPERATORS
360+
320361
#undef ARITHMETIC_OPERATOR
321362
#undef LOGIC_OPERATOR
363+
#undef BITSHIFT_OPERATOR
364+
#undef DELETED_BITSHIFT_OPERATOR
322365

323366
FIXED_POINTS_END_NAMESPACE

src/FixedPoints/UFixedFreeFunctions.h

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,28 @@ constexpr auto operator /(const UFixed<IntegerLeft, FractionLeft> & left, const
277277
{\
278278
return (UFixed<Integer, Fraction>(left) op right);\
279279
}
280+
281+
#define DELETED_BITSHIFT_OPERATOR( type, op )\
282+
template< unsigned Integer, unsigned Fraction >\
283+
constexpr UFixed<Integer, Fraction> operator op (const UFixed<Integer, Fraction> & left, const type & right) = delete;\
284+
\
285+
template< unsigned Integer, unsigned Fraction >\
286+
inline UFixed<Integer, Fraction> operator op##= (const UFixed<Integer, Fraction> & left, const type & right) = delete;
287+
288+
#define BITSHIFT_OPERATOR( type, op )\
289+
template< unsigned Integer, unsigned Fraction >\
290+
constexpr UFixed<Integer, Fraction> operator op (const UFixed<Integer, Fraction> & left, const type & right)\
291+
{\
292+
using InternalType = typename UFixed<Integer, Fraction>::InternalType;\
293+
return UFixed<Integer, Fraction>::fromInternal(static_cast<InternalType>(left.getInternal() op right));\
294+
}\
295+
\
296+
template< unsigned Integer, unsigned Fraction >\
297+
inline UFixed<Integer, Fraction> & operator op##= (UFixed<Integer, Fraction> & left, const type & right)\
298+
{\
299+
left = (left op right);\
300+
return left;\
301+
}
280302

281303
#define LOGIC_OPERATORS( type )\
282304
LOGIC_OPERATOR( type, == )\
@@ -291,33 +313,54 @@ constexpr auto operator /(const UFixed<IntegerLeft, FractionLeft> & left, const
291313
ARITHMETIC_OPERATOR( type, - )\
292314
ARITHMETIC_OPERATOR( type, * )\
293315
ARITHMETIC_OPERATOR( type, / )
316+
317+
#define DELETED_BITSHIFT_OPERATORS( type ) \
318+
DELETED_BITSHIFT_OPERATOR( type, << )\
319+
DELETED_BITSHIFT_OPERATOR( type, >> )
320+
321+
#define BITSHIFT_OPERATORS( type ) \
322+
BITSHIFT_OPERATOR( type, << )\
323+
BITSHIFT_OPERATOR( type, >> )
294324

295-
#define OPERATORS( type ) \
325+
#define FLOAT_OPERATORS( type ) \
296326
LOGIC_OPERATORS( type )\
297-
ARITHMETIC_OPERATORS( type )
298-
299-
OPERATORS( float )
300-
OPERATORS( double )
301-
OPERATORS( long double )
302-
303-
OPERATORS( char )
304-
OPERATORS( unsigned char )
305-
OPERATORS( signed char )
306-
OPERATORS( unsigned short int )
307-
OPERATORS( signed short int )
308-
OPERATORS( unsigned int )
309-
OPERATORS( signed int )
310-
OPERATORS( unsigned long int )
311-
OPERATORS( signed long int )
312-
OPERATORS( unsigned long long int )
313-
OPERATORS( signed long long int )
327+
ARITHMETIC_OPERATORS( type )\
328+
DELETED_BITSHIFT_OPERATORS( type )
329+
330+
#define INTEGER_OPERATORS( type ) \
331+
LOGIC_OPERATORS( type )\
332+
ARITHMETIC_OPERATORS( type )\
333+
BITSHIFT_OPERATORS( type )
334+
335+
FLOAT_OPERATORS( float )
336+
FLOAT_OPERATORS( double )
337+
FLOAT_OPERATORS( long double )
338+
339+
INTEGER_OPERATORS( char )
340+
INTEGER_OPERATORS( unsigned char )
341+
INTEGER_OPERATORS( signed char )
342+
INTEGER_OPERATORS( unsigned short int )
343+
INTEGER_OPERATORS( signed short int )
344+
INTEGER_OPERATORS( unsigned int )
345+
INTEGER_OPERATORS( signed int )
346+
INTEGER_OPERATORS( unsigned long int )
347+
INTEGER_OPERATORS( signed long int )
348+
INTEGER_OPERATORS( unsigned long long int )
349+
INTEGER_OPERATORS( signed long long int )
314350

315351
// Prevent Macro-bleed:
316352

317-
#undef OPERATORS
353+
#undef INTEGER_OPERATORS
354+
#undef FLOAT_OPERATORS
355+
318356
#undef ARITHMETIC_OPERATORS
319357
#undef LOGIC_OPERATORS
358+
#undef BITSHIFT_OPERATORS
359+
#undef DELETED_BITSHIFT_OPERATORS
360+
320361
#undef ARITHMETIC_OPERATOR
321362
#undef LOGIC_OPERATOR
363+
#undef BITSHIFT_OPERATOR
364+
#undef DELETED_BITSHIFT_OPERATOR
322365

323366
FIXED_POINTS_END_NAMESPACE

0 commit comments

Comments
 (0)