From 9f33df9574e94f875a348b6ef65de73dfc207232 Mon Sep 17 00:00:00 2001 From: andreika-git Date: Sun, 17 Nov 2019 15:08:57 +0200 Subject: [PATCH] cisnan() warnings fix (#1008) * Fix cisnan() macro warnings (strict-aliasing) * Hey, we found a bug thanks to our fix! Let's fix it too! --- firmware/util/efilib.h | 12 ++++++++++++ firmware/util/math/interpolation.h | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/firmware/util/efilib.h b/firmware/util/efilib.h index 3f2af839b0..657b079f36 100644 --- a/firmware/util/efilib.h +++ b/firmware/util/efilib.h @@ -45,7 +45,19 @@ int indexOf(const char *string, char ch); float atoff(const char *string); int atoi(const char *string); +#if defined(__cplusplus) && defined(__OPTIMIZE__) +#include +// "g++ -O2" version, adds more strict type check and yet no "strict-aliasing" warnings! +#define cisnan(f) ({ \ + static_assert(sizeof(f) == sizeof(int32_t)); \ + union cisnanu_t { std::remove_reference_t __f; int32_t __i; } __cisnan_u = { f }; \ + __cisnan_u.__i == 0x7FC00000; \ +}) +#else +// "g++ -O0" or other C++/C compilers #define cisnan(f) (*(((int*) (&f))) == 0x7FC00000) +#endif /* __cplusplus && __OPTIMIZE__ */ + #define UNUSED(x) (void)(x) int absI(int32_t value); diff --git a/firmware/util/math/interpolation.h b/firmware/util/math/interpolation.h index 8f29fc0016..5cf4b35cfd 100644 --- a/firmware/util/math/interpolation.h +++ b/firmware/util/math/interpolation.h @@ -46,7 +46,8 @@ int needInterpolationLogging(void); */ template int findIndexMsgExt(const char *msg, const kType array[], int size, kType value) { - if (cisnan(value)) { + float fvalue = (float)value; + if (cisnan(fvalue)) { firmwareError(ERROR_NAN_FIND_INDEX, "NaN in findIndex%s", msg); return 0; }