1 //===-- Exhaustive test template for math functions -------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <fenv.h>
10 #include <functional>
11 #include <iostream>
12 #include <sstream>
13 #include <string>
14 #include <thread>
15 #include <vector>
16 
17 #include "exhaustive_test.h"
18 #include "src/__support/FPUtil/FPBits.h"
19 #include "utils/UnitTest/Test.h"
20 
21 template <typename T>
22 void LlvmLibcExhaustiveTest<T>::test_full_range(T start, T stop, int nthreads,
23                                                 mpfr::RoundingMode rounding) {
24   std::vector<std::thread> thread_list(nthreads);
25   T increment = (stop - start - 1) / nthreads + 1;
26   T begin = start;
27   T end = start + increment - 1;
28   for (int i = 0; i < nthreads; ++i) {
29     thread_list.emplace_back([this, begin, end, rounding]() {
30       std::stringstream msg;
31       msg << "-- Testing from " << begin << " to " << end << " [0x" << std::hex
32           << begin << ", 0x" << end << "), [" << std::hexfloat
33           << float(__llvm_libc::fputil::FPBits<float>(
34                  static_cast<uint32_t>(begin)))
35           << ", "
36           << float(
37                  __llvm_libc::fputil::FPBits<float>(static_cast<uint32_t>(end)))
38           << ") ..." << std::endl;
39       std::cout << msg.str();
40       msg.str("");
41 
42       bool result = check(begin, end, rounding);
43 
44       msg << "** Finished testing from " << std::dec << begin << " to " << end
45           << " [0x" << std::hex << begin << ", 0x" << end << "), ["
46           << std::hexfloat
47           << float(__llvm_libc::fputil::FPBits<float>(
48                  static_cast<uint32_t>(begin)))
49           << ", "
50           << float(
51                  __llvm_libc::fputil::FPBits<float>(static_cast<uint32_t>(end)))
52           << ") : " << (result ? "PASSED" : "FAILED") << std::endl;
53       std::cout << msg.str();
54     });
55     begin += increment;
56     end += increment;
57     if (end > stop)
58       end = stop;
59   }
60   for (auto &thread : thread_list) {
61     if (thread.joinable()) {
62       thread.join();
63     }
64   }
65 }
66 
67 template void
68 LlvmLibcExhaustiveTest<uint32_t>::test_full_range(uint32_t, uint32_t, int,
69                                                   mpfr::RoundingMode);
70 template void
71 LlvmLibcExhaustiveTest<uint64_t>::test_full_range(uint64_t, uint64_t, int,
72                                                   mpfr::RoundingMode);
73