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 c3d005f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
51 changes: 40 additions & 11 deletions vir/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,20 @@ 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,16 +143,24 @@ 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) \
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(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 +675,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 +828,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 +1289,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 +1618,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 c3d005f

Please sign in to comment.