Skip to content

Commit

Permalink
Merge branch '739-block-histogram-test-failures' into 'develop_stream'
Browse files Browse the repository at this point in the history
Fix undefined behavior in test data generation

Closes #739

See merge request amd/libraries/rocPRIM!666
  • Loading branch information
nolmoonen authored and Naraenda committed Jul 18, 2024
2 parents 441a934 + 49b7528 commit 917b151
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions test/rocprim/test_block_histogram.kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ void histogram_kernel(T* device_output, BinType* device_output_bin)
}
}

/// \brief Reduce the value of `maxval` such that it can be represented by `T`.
template<typename T>
auto get_safe_maxval(size_t maxval) -> std::enable_if_t<rocprim::is_floating_point<T>::value, bool>
{
// Assert that the cast is defined behavior, based on the assumption that all floating-point
// types can be represented by a double
EXPECT_LT(static_cast<double>(maxval),
static_cast<double>(test_utils::numeric_limits<T>::max()));
return static_cast<T>(maxval);
}

/// \brief Reduce the value of `maxval` such that it can be represented by `T`.
template<typename T>
auto get_safe_maxval(size_t maxval) -> std::enable_if_t<!rocprim::is_floating_point<T>::value, bool>
{
return test_utils::saturate_cast<T>(maxval);
}

// Test for histogram
template<
class T,
Expand Down Expand Up @@ -110,11 +128,9 @@ void test_block_histogram_input_arrays()
SCOPED_TRACE(testing::Message() << "with ItemsPerThread = " << items_per_thread);

// Generate data
std::vector<T> output = test_utils::get_random_data<T>(
size,
0,
std::min<size_t>(std::numeric_limits<T>::max(), bin - 1),
seed_value);
const size_t max_value = bin - 1;
std::vector<T> output
= test_utils::get_random_data<T>(size, 0, get_safe_maxval<T>(max_value), seed_value);

// Output histogram results
std::vector<BinType> output_bin(bin_sizes, 0);
Expand Down

0 comments on commit 917b151

Please sign in to comment.