From 385f033bb5f2f7af9382d4504670221bbdb3c78c Mon Sep 17 00:00:00 2001 From: tomcombriat Date: Sat, 18 Nov 2023 15:37:25 +0100 Subject: [PATCH] Added sub/add between SFix --- FixMath2.h | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/FixMath2.h b/FixMath2.h index bfc969bea..411c7b391 100644 --- a/FixMath2.h +++ b/FixMath2.h @@ -88,6 +88,12 @@ class UFixMath2 return UFixMath2(tt,true); } + // With other types: add to integer part, unsafe + template + UFixMath2 operator+ (const T op) const + { + return UFixMath2(internal_value+(op<(tt,true); } + + // With other types: add to integer part, unsafe + template + UFixMath2 operator- (const T op) const + { + return UFixMath2(internal_value+(op< + SFixMath2(const SFixMath2<_NI,_NF>& uf) { + internal_value = SHIFTR((typename IntegerType<((MAX(NI+NF-1,_NI+_NF-1))>>3)+1>::unsigned_type) uf.asRaw(),(_NF-NF)); + } + + //////// ADDITION OVERLOADS + // Between SFix + template + SFixMath2 operator+ (const SFixMath2<_NI,_NF>& op) const + { + constexpr byte new_NI = MAX(NI, _NI) + 1; + constexpr byte new_NF = MAX(NF, _NF); + typedef typename IntegerType< ((new_NI+new_NF-1)>>3)+1>::unsigned_type return_type; + SFixMath2 left(*this); + SFixMath2 right(op); + + return_type tt = return_type(left.asRaw()) + right.asRaw(); + return SFixMath2(tt,true); + } + + // With other types: add to integer part, unsafe + template + SFixMath2 operator+ (const T op) const + { + return SFixMath2(internal_value+(op< // We do not have the +1 after MAX(NI, _NI) because the substraction between two SFix should fit in the bigger of the two. + SFixMath2 operator- (const SFixMath2<_NI,_NF>& op) const + { + constexpr byte new_NI = MAX(NI, _NI); + constexpr byte new_NF = MAX(NF, _NF); + typedef typename IntegerType< ((new_NI+new_NF-1)>>3)+1>::unsigned_type return_type; + SFixMath2 left(*this); + SFixMath2 right(op); + + return_type tt = return_type(left.asRaw()) - right.asRaw(); + return SFixMath2(tt,true); + } + + + // With other types: add to integer part, unsafe + template + SFixMath2 operator- (const T op) const + { + return SFixMath2(internal_value+(op<