Skip to content

Commit

Permalink
workaround to remove a UB from converting huge double->int8_t
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekazakov committed Nov 20, 2022
1 parent 97cd146 commit ac53823
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test-llvm-pstl/transform_binary.pass.cpp.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
diff --git a/pstl/test/std/algorithms/alg.modifying.operations/transform_binary.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/transform_binary.pass.cpp
index 949e6f5..f32fb72 100644
--- a/pstl/test/std/algorithms/alg.modifying.operations/transform_binary.pass.cpp
+++ b/pstl/test/std/algorithms/alg.modifying.operations/transform_binary.pass.cpp
@@ -41,7 +41,7 @@ check_and_reset(InputIterator1 first1, InputIterator1 last1, InputIterator2 firs
for (; first1 != last1; ++first1, ++first2, ++out_first, ++k)
{
// check
- Out expected = Out(1.5) + *first1 - *first2;
+ Out expected = std::clamp<decltype(Out(1.5) + *first1 - *first2)>(Out(1.5) + *first1 - *first2, std::numeric_limits<Out>::lowest(), std::numeric_limits<Out>::max());
Out actual = *out_first;
if (std::is_floating_point<Out>::value)
{
@@ -113,7 +113,7 @@ main()
test<int32_t, float32_t, float32_t>(non_const(TheOperation<int32_t, float32_t, float32_t>(1.5)));
test<int64_t, float64_t, float32_t>(non_const(TheOperation<int64_t, float64_t, float32_t>(1.5)));
//lambda
- test<int8_t, float64_t, int8_t>([](const int8_t& x, const float64_t& y) { return int8_t(int8_t(1.5) + x - y); });
+ test<int8_t, float64_t, int8_t>([](const int8_t& x, const float64_t& y) { return int8_t(std::clamp<double>(int8_t(1.5) + x - y, std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::max())); });

test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());

0 comments on commit ac53823

Please sign in to comment.