Skip to content

Commit 17349e4

Browse files
committed
Added autotests
Fixed compilation for STM32 (no constexpr abs...) Added autotests for opposite Fixed auto tests for STM32? Changed compilation guards for autotests. More tests Fixed compilation of autotests Fixed compilation for stm32 Fix of auto again
1 parent 76a7d29 commit 17349e4

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

src/FixMath.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ namespace FixMathPrivate {
157157
// Alias declaration for a UFix type with the suitable NI count given RANGE and NF
158158
template<int8_t NF, uint64_t RANGE> using UFixByRange_t=UFix<NIcount<RANGE>()-NF, NF, RANGE>;
159159
// Alias declaration for an SFix type with the suitable NI count given RANGE and NF
160-
template<int8_t NF, uint64_t RANGE> using SFixByRange_t=SFix<NIcount<RANGE>()-NF, NF, RANGE>;
160+
template<int8_t NF, uint64_t RANGE> using SFixByRange_t=SFix<NIcount<RANGE-1>()-NF, NF, RANGE>;
161161
}
162162

163163
/** Instanciate an unsigned fixed point math number.
@@ -1394,8 +1394,8 @@ constexpr SFix<sizeof(T)*8-1,0> toSInt(T val) {
13941394
@endcode
13951395
*/
13961396
template<int64_t value>
1397-
constexpr const FixMathPrivate::SFixByRange_t<0, value < 0 ? abs(-value-1) : value> SFixAuto() {
1398-
return FixMathPrivate::SFixByRange_t<0, value < 0 ? abs(-value-1) : value>::fromRaw(value);
1397+
constexpr const FixMathPrivate::SFixByRange_t<0, value < 0 ? -value : value+1> SFixAuto() {
1398+
return FixMathPrivate::SFixByRange_t<0, value < 0 ? -value : value+1>::fromRaw(value);
13991399
}
14001400

14011401
#include "FixMath_Autotests.h"

src/FixMath_Autotests.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,27 @@ namespace FixMathPrivate {
3737
static_assert((e + e).getNF() == 2, "test fail");
3838
static_assert((d + e).getNI() == 11, "test fail");
3939

40-
// Two's complement peculiar additions
40+
// Two's complement peculiar additions and opposite
4141
static_assert(SFixAuto<-128>().getNI() == 7, "test fail");
4242
static_assert(SFixAuto<127>().getNI() == 7, "test fail");
4343
static_assert(SFixAuto<128>().getNI() == 8, "test fail");
4444

45-
constexpr auto s = UFix<7,0>(-128);
45+
#if (__cplusplus >= 202002L)
46+
constexpr auto s = SFix<7,0>(-128);
4647
static_assert((s+s).getNI() == 8, "test fail");
48+
static_assert((-s).getNI() == 8, "test fail");
49+
50+
constexpr auto zero = SFix<7,0>(0);
51+
constexpr auto negone = SFix<1,0>(-1);
52+
static_assert((zero - s).getNI() == 8, "test fail");
53+
static_assert(-(zero - s) == s, "test fail");
54+
static_assert((s*negone).getNI() == 8, "test fail");
55+
#endif
56+
static_assert((-SFixAuto<-127>()).getNI() == 7, "test fail");
57+
//static_assert((-SFixAuto<127>()).getNI() == 7, "test fail"); // if someone manages that I am very grateful
58+
static_assert((-UFixAuto<127>()).getNI() == 7, "test fail");
59+
60+
4761

4862
// the point of this block is to ascertain that addtion does not overflow, internally, where the internal_type of the operands is too small to hold the result
4963
constexpr auto large = UFix<32,0>(1LL << 31, true);
@@ -58,7 +72,7 @@ namespace FixMathPrivate {
5872
static_assert(b + a - b == a, "test fail");
5973
static_assert(b + a + (-b) == a, "test fail"); // same with unary minus
6074
static_assert(-(-a) == a, "test fail");
61-
#if __cplusplus >= 202002L
75+
#if (__cplusplus >= 202002L)
6276
// These here involve shifts of negative numbers, which used to be "implementation defined" before C++-20.
6377
// It doesn't cause a real-world problem, but the compiler won't accept it in a constexpr
6478
static_assert(UFix<43,9>(0) - b - a == -(a+b), "test fail");

0 commit comments

Comments
 (0)