1 //===-- Exhaustive test template for math functions -------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
18 #include "src/__support/FPUtil/FPBits.h"
20 #include "exhaustive_test.h"
22 template <typename T
, typename FloatType
>
23 void LlvmLibcExhaustiveTest
<T
, FloatType
>::test_full_range(
24 T start
, T stop
, mpfr::RoundingMode rounding
) {
25 int n_threads
= std::thread::hardware_concurrency();
26 std::vector
<std::thread
> thread_list
;
27 std::mutex mx_cur_val
;
28 int current_percent
= -1;
29 T current_value
= start
;
30 std::atomic
<uint64_t> failed(0);
31 for (int i
= 0; i
< n_threads
; ++i
) {
32 thread_list
.emplace_back([&, this]() {
34 T range_begin
, range_end
;
37 std::lock_guard
<std::mutex
> lock(mx_cur_val
);
38 if (current_value
== stop
)
41 range_begin
= current_value
;
42 if (stop
>= increment
&& stop
- increment
>= current_value
) {
43 range_end
= current_value
+ increment
;
47 current_value
= range_end
;
48 int pc
= 100.0 * (range_end
- start
) / (stop
- start
);
49 if (current_percent
!= pc
) {
54 if (new_percent
>= 0) {
55 std::stringstream msg
;
56 msg
<< new_percent
<< "% is in process \r";
57 std::cout
<< msg
.str() << std::flush
;
60 bool check_passed
= check(range_begin
, range_end
, rounding
);
62 std::stringstream msg
;
63 msg
<< "Test failed in range: " << std::dec
<< range_begin
<< " to "
64 << range_end
<< " [0x" << std::hex
<< range_begin
<< ", 0x"
65 << range_end
<< "), [" << std::hexfloat
66 << static_cast<FloatType
>(__llvm_libc::fputil::FPBits
<FloatType
>(
67 static_cast<T
>(range_begin
)))
69 << static_cast<FloatType
>(
70 __llvm_libc::fputil::FPBits
<FloatType
>(range_end
))
72 std::cerr
<< msg
.str() << std::flush
;
80 for (auto &thread
: thread_list
) {
81 if (thread
.joinable()) {
85 std::cout
<< std::endl
;
86 std::cout
<< "Test " << ((failed
> 0) ? "FAILED" : "PASSED") << std::endl
;
87 ASSERT_EQ(failed
.load(), uint64_t(0));
91 LlvmLibcExhaustiveTest
<uint32_t>::test_full_range(uint32_t, uint32_t,
93 template void LlvmLibcExhaustiveTest
<uint64_t, double>::test_full_range(
94 uint64_t, uint64_t, mpfr::RoundingMode
);