Skip to content

Commit

Permalink
add HWY_UNREACHABLE and add documentation for related macros
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 716584567
  • Loading branch information
jan-wassenberg authored and copybara-github committed Jan 17, 2025
1 parent 7cccd1b commit 56f6b5a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions g3doc/quick_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2476,6 +2476,19 @@ Ops in this section are only available if `HWY_TARGET != HWY_SCALAR`:
* `HWY_ALIGN_MAX`: as `HWY_ALIGN`, but independent of `HWY_TARGET` and may be
used outside `HWY_NAMESPACE`.
* `HWY_RESTRICT`: use after a pointer, e.g. `T* HWY_RESTRICT p`, to indicate
the pointer is not aliased, i.e. it is the only way to access the data. This
may improve code generation by preventing unnecessary reloads.
* `HWY_LIKELY`: use `if (HWY_LIKELY(condition))` to signal to the compiler
that `condition` is likely to be true. This may improve performance by
influencing the layout of the generated code.
* `HWY_UNLIKELY`: like `HWY_LIKELY`, but for conditions likely to be false.
* `HWY_UNREACHABLE;`: signals to the compiler that control will never reach
this point, which may improve code generation.
## Advanced macros
Beware that these macros describe the current target being compiled. Imagine a
Expand Down
6 changes: 6 additions & 0 deletions hwy/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
#define HWY_NORETURN __declspec(noreturn)
#define HWY_LIKELY(expr) (expr)
#define HWY_UNLIKELY(expr) (expr)
#define HWY_UNREACHABLE __assume(false)
#define HWY_PRAGMA(tokens) __pragma(tokens)
#define HWY_DIAGNOSTICS(tokens) HWY_PRAGMA(warning(tokens))
#define HWY_DIAGNOSTICS_OFF(msc, gcc) HWY_DIAGNOSTICS(msc)
Expand Down Expand Up @@ -128,6 +129,11 @@
#define HWY_NORETURN __attribute__((noreturn))
#define HWY_LIKELY(expr) __builtin_expect(!!(expr), 1)
#define HWY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
#if HWY_COMPILER_GCC || __has_builtin(__builtin_unreachable)
#define HWY_UNREACHABLE __builtin_unreachable()
#else
#define HWY_UNREACHABLE
#endif
#define HWY_PRAGMA(tokens) _Pragma(#tokens)
#define HWY_DIAGNOSTICS(tokens) HWY_PRAGMA(GCC diagnostic tokens)
#define HWY_DIAGNOSTICS_OFF(msc, gcc) HWY_DIAGNOSTICS(gcc)
Expand Down
9 changes: 9 additions & 0 deletions hwy/base_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include <limits>

#include "hwy/nanobenchmark.h"

#undef HWY_TARGET_INCLUDE
#define HWY_TARGET_INCLUDE "base_test.cc"
#include "hwy/foreach_target.h" // IWYU pragma: keep
Expand All @@ -28,6 +30,12 @@ namespace hwy {
namespace HWY_NAMESPACE {
namespace {

HWY_NOINLINE void TestUnreachable() {
if (!hwy::Unpredictable1()) {
HWY_UNREACHABLE;
}
}

HWY_NOINLINE void TestAllLimits() {
HWY_ASSERT_EQ(uint8_t{0}, LimitsMin<uint8_t>());
HWY_ASSERT_EQ(uint16_t{0}, LimitsMin<uint16_t>());
Expand Down Expand Up @@ -848,6 +856,7 @@ HWY_AFTER_NAMESPACE();
namespace hwy {
namespace {
HWY_BEFORE_TEST(BaseTest);
HWY_EXPORT_AND_TEST_P(BaseTest, TestUnreachable);
HWY_EXPORT_AND_TEST_P(BaseTest, TestAllLimits);
HWY_EXPORT_AND_TEST_P(BaseTest, TestAllLowestHighest);
HWY_EXPORT_AND_TEST_P(BaseTest, TestAllType);
Expand Down

0 comments on commit 56f6b5a

Please sign in to comment.