-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cxx
More file actions
125 lines (92 loc) · 2.65 KB
/
Copy pathmain.cxx
File metadata and controls
125 lines (92 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright David Browne 2020-2026.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include "cxcm.hxx"
#include <iostream>
#define DOCTEST_CONFIG_IMPLEMENT
#include "doctest.h"
// not accurate, but gives answers in the ballpark
double fast_sqrt_simple(double x)
{
double a = std::floor(std::log2(x));
double b = std::ceil(std::log2(x));
double lower_bound = std::pow(2, std::floor(a / 2.0));
double upper_bound = std::pow(2, std::floor(b / 2.0));
return (lower_bound + upper_bound) / 2.0;
}
void test_sqrt_float(unsigned int i, long long &above, long long &below, long long &same)
{
float f = std::bit_cast<float>(i);
auto val1 = std::sqrt(f);
auto val2 = cxcm::impl::constexpr_sqrt(f);
auto res1 = std::bit_cast<unsigned int>(val1);
auto res2 = std::bit_cast<unsigned int>(val2);
if (res1 < res2)
{
++above;
// std::printf("trouble maker above? %X: %X < %X\n", i, res1, res2);
}
else if (res1 > res2)
{
++below;
// std::printf("trouble maker below? %X: %X > %X\n", i, res1, res2);
}
else
{
++same;
}
}
//void print_loop_count_results()
//{
// for (auto [loops, count] : cxcm::relaxed::detail::loop_count_map)
// std::printf("%d loops : %d\n", loops, count);
//}
void test_all_floats_sqrt()
{
long long below{};
long long above{};
long long same{};
test_sqrt_float(0xFFFFFFFF, above, below, same);
for (unsigned int i = 0x00000000; i < 0xFFFFFFFF; ++i)
{
test_sqrt_float(i, above, below, same);
}
// print_loop_count_results();
std::printf("\n");
std::printf("same std : %lld\n", same);
std::printf("below std : %lld\n", below);
std::printf("above std : %lld\n", above);
}
// this function is a place to just test out whatever
void sandbox_function()
{
// put fun code here
#if defined(__INTEL_LLVM_COMPILER)
std::cout << "__INTEL_LLVM_COMPILER = " << __INTEL_LLVM_COMPILER << "\n";
std::cout << "__VERSION__: " << __VERSION__ << "\n";
#endif
// test_all_floats_sqrt();
}
int main(int argc, char *argv[])
{
//
// fun stuff
//
sandbox_function();
//
// doctest
//
int doctest_result = 0;
// comment out if we define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN somewhere to get the main() from doctest
#define DONT_USE_DOCTEST_MAIN
#if defined(DONT_USE_DOCTEST_MAIN)
doctest::Context context;
context.applyCommandLine(argc, argv);
doctest_result = context.run(); // run
if (context.shouldExit()) // important - query flags (and --exit) rely on the user doing this
return doctest_result; // propagate the result of the tests
#endif
return EXIT_SUCCESS + doctest_result;
}