Skip to content

Commit

Permalink
Remove __VA_OPT__ usage by two different macros
Browse files Browse the repository at this point in the history
Also fix the format string for size_t arguments.

ChangeLog:

	* vir/simd.h: Copy vir_simd_precondition macros to
	vir_simd_precondition_vaargs. The former allows no additional
	arguments, the latter does.
	* vir/simd_execution.h: Use vir_simd_precondition_vaargs and use
	%zu for size_t.
  • Loading branch information
mattkretz committed Sep 27, 2024
1 parent 5bd2e8a commit 4e5f64d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
63 changes: 50 additions & 13 deletions vir/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ namespace vir::detail
#endif

#if VIR_CHECK_PRECONDITIONS < 0
#define vir_simd_precondition(expr, msg, ...) \
#define vir_simd_precondition(expr, msg) \
(void) bool(expr)

#define vir_simd_precondition_vaargs(expr, msg, ...) \
(void) bool(expr)

#elif defined __clang__ or __GNUC__ >= 10
#if (VIR_CHECK_PRECONDITIONS & 1) == 1
#define VIR_CONSTPROP_PRECONDITION_FAILURE_ACTION __error__
Expand All @@ -119,7 +123,21 @@ namespace vir::detail
#else
#define VIR_ATTR_NOIPA
#endif
#define vir_simd_precondition(expr, msg, ...) \
#define vir_simd_precondition(expr, msg) \
do { \
const bool precondition_result = bool(expr); \
if (__builtin_constant_p(precondition_result) and not precondition_result) \
[]() __attribute__((__noinline__, __noreturn__, VIR_ATTR_NOIPA \
VIR_CONSTPROP_PRECONDITION_FAILURE_ACTION("precondition failure." \
"\n" VIR_SIMD_LOC "note: " msg " (precondition '" #expr "' does not hold)"))) \
{ vir::detail::trap(); }(); \
else if (__builtin_expect(not precondition_result, false)) \
vir::detail::invoke_ub( \
VIR_SIMD_LOC "precondition failure in '%s': " msg " ('" #expr "' does not hold)\n", \
VIR_PRETTY_FUNCTION_); \
} while(false)

#define vir_simd_precondition_vaargs(expr, msg, ...) \
do { \
const bool precondition_result = bool(expr); \
if (__builtin_constant_p(precondition_result) and not precondition_result) \
Expand All @@ -130,17 +148,28 @@ namespace vir::detail
else if (__builtin_expect(not precondition_result, false)) \
vir::detail::invoke_ub( \
VIR_SIMD_LOC "precondition failure in '%s': " msg " ('" #expr "' does not hold)\n", \
VIR_PRETTY_FUNCTION_ __VA_OPT__(,) __VA_ARGS__); \
VIR_PRETTY_FUNCTION_, __VA_ARGS__); \
} while(false)

#else
#define vir_simd_precondition(expr, msg, ...) \
#define vir_simd_precondition(expr, msg) \
do { \
const bool precondition_result = bool(expr); \
if (not precondition_result) [[unlikely]] \
vir::detail::invoke_ub( \
VIR_SIMD_LOC "precondition failure in '%s': " msg " ('" #expr "' does not hold)\n", \
VIR_PRETTY_FUNCTION_); \
} while(false)

#define vir_simd_precondition_vaargs(expr, msg, ...) \
do { \
const bool precondition_result = bool(expr); \
if (not precondition_result) [[unlikely]] \
vir::detail::invoke_ub( \
VIR_SIMD_LOC "precondition failure in '%s': " msg " ('" #expr "' does not hold)\n", \
VIR_PRETTY_FUNCTION_ __VA_OPT__(,) __VA_ARGS__); \
VIR_PRETTY_FUNCTION_, __VA_ARGS__); \
} while(false)

#endif


Expand Down Expand Up @@ -654,14 +683,16 @@ namespace vir::stdx
constexpr reference
operator[](size_t i)
{
vir_simd_precondition(i < size(), "Subscript %d is out of range [0, %d]", i, size() - 1);
vir_simd_precondition_vaargs(i < size(), "Subscript %zu is out of range [0, %zu]",
i, size() - 1);
return data;
}

constexpr value_type
operator[](size_t i) const
{
vir_simd_precondition(i < size(), "Subscript %d is out of range [0, %d]", i, size() - 1);
vir_simd_precondition_vaargs(i < size(), "Subscript %zu is out of range [0, %zu]",
i, size() - 1);
return data;
}

Expand Down Expand Up @@ -805,14 +836,16 @@ namespace vir::stdx
constexpr reference
operator[](size_t i)
{
vir_simd_precondition(i < size(), "Subscript %d is out of range [0, %d]", i, size() - 1);
vir_simd_precondition_vaargs(i < size(), "Subscript %zu is out of range [0, %zu]",
i, size() - 1);
return data[i];
}

constexpr value_type
operator[](size_t i) const
{
vir_simd_precondition(i < size(), "Subscript %d is out of range [0, %d]", i, size() - 1);
vir_simd_precondition_vaargs(i < size(), "Subscript %zu is out of range [0, %zu]",
i, size() - 1);
return data[i];
}

Expand Down Expand Up @@ -1264,14 +1297,16 @@ namespace vir::stdx
constexpr reference
operator[](size_t i)
{
vir_simd_precondition(i < size(), "Subscript %d is out of range [0, %d]", i, size() - 1);
vir_simd_precondition_vaargs(i < size(), "Subscript %zu is out of range [0, %zu]",
i, size() - 1);
return data;
}

constexpr value_type
operator[](size_t i) const
{
vir_simd_precondition(i < size(), "Subscript %d is out of range [0, %d]", i, size() - 1);
vir_simd_precondition_vaargs(i < size(), "Subscript %zu is out of range [0, %zu]",
i, size() - 1);
return data;
}

Expand Down Expand Up @@ -1591,14 +1626,16 @@ namespace vir::stdx
constexpr reference
operator[](size_t i)
{
vir_simd_precondition(i < size(), "Subscript %d is out of range [0, %d]", i, size() - 1);
vir_simd_precondition_vaargs(i < size(), "Subscript %zu is out of range [0, %zu]",
i, size() - 1);
return data[i];
}

constexpr value_type
operator[](size_t i) const
{
vir_simd_precondition(i < size(), "Subscript %d is out of range [0, %d]", i, size() - 1);
vir_simd_precondition_vaargs(i < size(), "Subscript %zu is out of range [0, %zu]",
i, size() - 1);
return data[i];
}

Expand Down
18 changes: 9 additions & 9 deletions vir/simd_execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,9 @@ case##N:
std::size_t distance = std::distance(first1, last1);
constexpr bool assume_matching_size = ExecutionPolicy::_assume_matching_size;
if constexpr (assume_matching_size)
vir_simd_precondition(
distance % size == 0, "The explicit assumption, that the range size (%d) is a multiple "
"of the SIMD width (%d), does not hold.", size);
vir_simd_precondition_vaargs(
distance % size == 0, "The explicit assumption, that the range size (%zu) is a multiple"
" of the SIMD width (%d), does not hold.", distance, size());

if (std::is_constant_evaluated())
{
Expand Down Expand Up @@ -892,9 +892,9 @@ case##N:
std::size_t distance = std::distance(first1, last1);
constexpr bool assume_matching_size = ExecutionPolicy::_assume_matching_size;
if constexpr (assume_matching_size)
vir_simd_precondition(
distance % size == 0, "The explicit assumption, that the range size (%d) is a multiple "
"of the SIMD width (%d), does not hold.", size);
vir_simd_precondition_vaargs(
distance % size == 0, "The explicit assumption, that the range size (%zu) is a multiple"
" of the SIMD width (%d), does not hold.", distance, size);

if (std::is_constant_evaluated())
{
Expand Down Expand Up @@ -1034,9 +1034,9 @@ case##N:
std::size_t distance = std::distance(first, last);
constexpr bool assume_matching_size = ExecutionPolicy::_assume_matching_size;
if constexpr (assume_matching_size)
vir_simd_precondition(
distance % size == 0, "The explicit assumption, that the range size (%d) is a multiple "
"of the SIMD width (%d), does not hold.", size);
vir_simd_precondition_vaargs(
distance % size == 0, "The explicit assumption, that the range size (%zu) is a multiple"
" of the SIMD width (%d), does not hold.", distance, size);

if (std::is_constant_evaluated())
{
Expand Down

0 comments on commit 4e5f64d

Please sign in to comment.