17{
18 constexpr auto size = 40;
19 const auto input = [&] {
20 std::vector<float> exponential(
size);
21 std::iota(exponential.begin(), exponential.end(), -20.f);
22 std::vector<float> input(exponential.size());
23 std::transform(
24 exponential.begin(), exponential.end(), input.begin(),
25 [](float x) { return std::exp(x); });
26 return input;
27 }();
28 const auto expected = [&] {
29 std::vector<float> expected(
size);
30 std::transform(input.begin(), input.end(), expected.begin(), [](float x) {
31 return std::log2(x);
32 });
33 return expected;
34 }();
35 const auto actual = [&] {
36 std::vector<float> actual(
size);
37 std::transform(input.begin(), input.end(), actual.begin(),
FastLog2);
38 return actual;
39 }();
40 const auto error = [&] {
41 std::vector<float> error(
size);
42 std::transform(
43 expected.begin(), expected.end(), actual.begin(), error.begin(),
44 [](float e, float a) { return std::abs(e - a); });
45 return error;
46 }();
47 const auto maxError = *std::max_element(error.begin(), error.end());
48 REQUIRE(maxError < 1e-2);
49}
constexpr float FastLog2(float x)
Approximates the base-2 logarithm of a float to two decimal places, adapted from https://stackoverflo...