Tweaked LFS_ASSERT impl to use __builtin_unreachable

First, realized the the LFS_UNREACHABLE logic was flipped after a
confusing test bug (damn double negatives). But also realized LFS_ASSERT
could be tweaked to "call" __builtin_unreachable() on assert failure to
act as a sort of compiler hint.

Turns out this hint saves a little bit of code, note both builds have
LFS_UNREACHABLE fixed:

                                   code          stack
  without __builtin_unreachable:  28408           1928
  with __builtin_unreachable:     28324 (-0.3%)   1920 (+0.0%)

Since __builtin_unreachable is a compiler extension, its usage respects
LFS_NO_INTRINSICS.
This commit is contained in:
Christopher Haster
2023-10-05 14:16:00 -05:00
parent 07e977bb43
commit 1e13124091
+5 -1
View File
@@ -95,6 +95,8 @@ extern "C"
#ifndef LFS_ASSERT
#ifndef LFS_NO_ASSERT
#define LFS_ASSERT(test) assert(test)
#elif !defined(LFS_NO_INTRINSICS)
#define LFS_ASSERT(test) ((test) ? (void)0 : __builtin_unreachable())
#else
#define LFS_ASSERT(test)
#endif
@@ -102,9 +104,11 @@ extern "C"
#ifndef LFS_UNREACHABLE
#ifndef LFS_NO_ASSERT
#define LFS_UNREACHABLE() LFS_ASSERT(false)
#elif !defined(LFS_NO_INTRINSICS)
#define LFS_UNREACHABLE() __builtin_unreachable()
#else
#define LFS_UNREACHABLE() LFS_ASSERT(false)
#define LFS_UNREACHABLE()
#endif
#endif