Debian Codegraph @

src/lib/core/highway/hwy/examples/skeleton_test.cc

raw symbols SCIP
1 // Copyright 2020 Google LLC
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 // Example of unit test for the "skeleton" library.
17
18 #include "hwy/examples/skeleton.h"
19
20 #include <stdio.h>
21
22 #undef HWY_TARGET_INCLUDE
23 #define HWY_TARGET_INCLUDE "examples/skeleton_test.cc"
24 #include "hwy/foreach_target.h" // IWYU pragma: keep
25
26 // Must come after foreach_target.h to avoid redefinition errors.
27 #include "hwy/highway.h"
28 #include "hwy/tests/test_util-inl.h"
29
30 // Optional: factor out parts of the implementation into *-inl.h
31 // (must also come after foreach_target.h to avoid redefinition errors)
32 #include "hwy/examples/skeleton-inl.h"
33
34 HWY_BEFORE_NAMESPACE();
35 namespace skeleton {
36 namespace HWY_NAMESPACE {
37
38 namespace hn = hwy::HWY_NAMESPACE;
39
40 // Calls function defined in skeleton.cc.
41 struct TestFloorLog2 {
42 template <class T, class DF>
43 HWY_NOINLINE void operator()(T /*unused*/, DF df) {
44 const size_t count = 5 * hn::Lanes(df);
45 auto in = hwy::AllocateAligned<uint8_t>(count);
46 auto expected = hwy::AllocateAligned<uint8_t>(count);
47
48 hwy::RandomState rng;
49 for (size_t i = 0; i < count; ++i) {
50 expected[i] = Random32(&rng) & 7;
51 in[i] = static_cast<uint8_t>(1u << expected[i]);
52 }
53 auto out = hwy::AllocateAligned<uint8_t>(count);
54 CallFloorLog2(in.get(), count, out.get());
55 int sum = 0;
56 for (size_t i = 0; i < count; ++i) {
57 HWY_ASSERT_EQ(expected[i], out[i]);
58 sum += out[i];
59 }
60 hwy::PreventElision(sum);
61 }
62 };
63
64 HWY_NOINLINE void TestAllFloorLog2() {
65 hn::ForPartialVectors<TestFloorLog2>()(float());
66 }
67
68 // Calls function defined in skeleton-inl.h.
69 struct TestSumMulAdd {
70 template <class T, class D>
71 HWY_NOINLINE void operator()(T /*unused*/, D d) {
72 hwy::RandomState rng;
73 const size_t count = 4096;
74 EXPECT_EQ(0, count % hn::Lanes(d));
75 auto mul = hwy::AllocateAligned<T>(count);
76 auto x = hwy::AllocateAligned<T>(count);
77 auto add = hwy::AllocateAligned<T>(count);
78 for (size_t i = 0; i < count; ++i) {
79 mul[i] = static_cast<T>(Random32(&rng) & 0xF);
80 x[i] = static_cast<T>(Random32(&rng) & 0xFF);
81 add[i] = static_cast<T>(Random32(&rng) & 0xFF);
82 }
83 double expected_sum = 0.0;
84 for (size_t i = 0; i < count; ++i) {
85 expected_sum += mul[i] * x[i] + add[i];
86 }
87
88 MulAddLoop(d, mul.get(), add.get(), count, x.get());
89 HWY_ASSERT_EQ(4344240.0, expected_sum);
90 }
91 };
92
93 HWY_NOINLINE void TestAllSumMulAdd() {
94 hn::ForFloatTypes(hn::ForPartialVectors<TestSumMulAdd>());
95 }
96
97 // NOLINTNEXTLINE(google-readability-namespace-comments)
98 } // namespace HWY_NAMESPACE
99 } // namespace skeleton
100 HWY_AFTER_NAMESPACE();
101
102 #if HWY_ONCE
103
104 namespace skeleton {
105 HWY_BEFORE_TEST(SkeletonTest);
106 HWY_EXPORT_AND_TEST_P(SkeletonTest, TestAllFloorLog2);
107 HWY_EXPORT_AND_TEST_P(SkeletonTest, TestAllSumMulAdd);
108 } // namespace skeleton
109
110 #endif