Skip to content

Commit b35b469

Browse files
committed
Add NAZARA_UNREACHABLE()
1 parent 84e1f98 commit b35b469

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

include/NazaraUtils/Prerequisites.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,19 @@
362362

363363
#endif // NAZARA_NO_LIKELY_ATTRIBUTE
364364

365+
// Unreachable macro
366+
#ifndef NAZARA_NO_UNREACHABLE_MACRO
367+
368+
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
369+
#define NAZARA_UNREACHABLE() __builtin_unreachable()
370+
#elif defined(NAZARA_COMPILER_MSVC)
371+
#define NAZARA_UNREACHABLE() __assume(false)
372+
#endif
373+
374+
#endif // NAZARA_NO_UNREACHABLE_MACRO
375+
376+
// Fallbacks
377+
365378
#ifndef NAZARA_ASSUME
366379
#define NAZARA_ASSUME(expr)
367380
#endif
@@ -378,6 +391,10 @@
378391
#define NAZARA_UNLIKELY(expr) (expr)
379392
#endif
380393

394+
#ifndef NAZARA_UNREACHABLE
395+
#define NAZARA_UNREACHABLE() throw Nz::UnreachableError{};
396+
#endif
397+
381398
// Detect arch
382399
#ifndef NAZARA_NO_ARCH_DETECTION
383400

@@ -467,6 +484,8 @@ namespace Nz
467484

468485
using Int64 = int64_t;
469486
using UInt64 = uint64_t;
487+
488+
struct UnreachableError {};
470489
}
471490

472491
#endif // NAZARAUTILS_PREREQUISITES_HPP

tests/src/AttributeTests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ NAZARA_FORCEINLINE float sqrtCheck(float x)
1010
if NAZARA_UNLIKELY(x < 0)
1111
{
1212
throw std::runtime_error("square root of negative value");
13+
14+
NAZARA_UNREACHABLE();
1315
}
1416

1517
NAZARA_ASSUME(x != 0.f);

0 commit comments

Comments
 (0)