summaryrefslogtreecommitdiffstats
path: root/third_party/highway/hwy/tests/reverse_test.cc
blob: 0179b817e0aa0ac9ec57d4535b6458ebda2bfd5b (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2022 Google LLC
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#undef HWY_TARGET_INCLUDE
#define HWY_TARGET_INCLUDE "tests/reverse_test.cc"
#include "hwy/foreach_target.h"  // IWYU pragma: keep
#include "hwy/highway.h"
#include "hwy/tests/test_util-inl.h"

HWY_BEFORE_NAMESPACE();
namespace hwy {
namespace HWY_NAMESPACE {

struct TestReverse {
  template <class T, class D>
  HWY_NOINLINE void operator()(T /*unused*/, D d) {
    const size_t N = Lanes(d);
    const RebindToUnsigned<D> du;  // Iota does not support float16_t.
    const auto v = BitCast(d, Iota(du, 1));
    auto expected = AllocateAligned<T>(N);
    auto copy = AllocateAligned<T>(N);
    HWY_ASSERT(expected && copy);

    // Can't set float16_t value directly, need to permute in memory.
    Store(v, d, copy.get());
    for (size_t i = 0; i < N; ++i) {
      expected[i] = copy[N - 1 - i];
    }
    HWY_ASSERT_VEC_EQ(d, expected.get(), Reverse(d, v));
  }
};

struct TestReverse2 {
  template <class T, class D>
  HWY_NOINLINE void operator()(T /*unused*/, D d) {
    const size_t N = Lanes(d);
    const RebindToUnsigned<D> du;  // Iota does not support float16_t.
    const auto v = BitCast(d, Iota(du, 1));
    auto expected = AllocateAligned<T>(N);
    auto copy = AllocateAligned<T>(N);
    HWY_ASSERT(expected && copy);
    if (N == 1) {
      Store(v, d, expected.get());
      HWY_ASSERT_VEC_EQ(d, expected.get(), Reverse2(d, v));
      return;
    }

    // Can't set float16_t value directly, need to permute in memory.
    Store(v, d, copy.get());
    for (size_t i = 0; i < N; ++i) {
      expected[i] = copy[i ^ 1];
    }
    HWY_ASSERT_VEC_EQ(d, expected.get(), Reverse2(d, v));
  }
};

struct TestReverse4 {
  template <class T, class D>
  HWY_NOINLINE void operator()(T /*unused*/, D d) {
    const size_t N = Lanes(d);
    const RebindToUnsigned<D> du;  // Iota does not support float16_t.
    const auto v = BitCast(d, Iota(du, 1));
    auto expected = AllocateAligned<T>(N);
    auto copy = AllocateAligned<T>(N);
    HWY_ASSERT(expected && copy);

    // Can't set float16_t value directly, need to permute in memory.
    Store(v, d, copy.get());
    for (size_t i = 0; i < N; ++i) {
      expected[i] = copy[i ^ 3];
    }
    HWY_ASSERT_VEC_EQ(d, expected.get(), Reverse4(d, v));
  }
};

struct TestReverse8 {
  template <class T, class D>
  HWY_NOINLINE void operator()(T /*unused*/, D d) {
    const size_t N = Lanes(d);
    const RebindToUnsigned<D> du;  // Iota does not support float16_t.
    const auto v = BitCast(d, Iota(du, 1));
    auto expected = AllocateAligned<T>(N);
    auto copy = AllocateAligned<T>(N);
    HWY_ASSERT(expected && copy);

    // Can't set float16_t value directly, need to permute in memory.
    Store(v, d, copy.get());
    for (size_t i = 0; i < N; ++i) {
      expected[i] = copy[i ^ 7];
    }
    HWY_ASSERT_VEC_EQ(d, expected.get(), Reverse8(d, v));
  }
};

static HWY_INLINE uint8_t ReverseBytesOfValue(uint8_t val) { return val; }

static HWY_INLINE uint16_t ReverseBytesOfValue(uint16_t val) {
  const uint32_t u32_val = val;
  return static_cast<uint16_t>(((u32_val << 8) & 0xFF00u) |
                               ((u32_val >> 8) & 0x00FFu));
}

static HWY_INLINE uint32_t ReverseBytesOfValue(uint32_t val) {
  return static_cast<uint32_t>(
      ((val << 24) & 0xFF000000u) | ((val << 8) & 0x00FF0000u) |
      ((val >> 8) & 0x0000FF00u) | ((val >> 24) & 0x000000FFu));
}

static HWY_INLINE uint64_t ReverseBytesOfValue(uint64_t val) {
  return static_cast<uint64_t>(
      ((val << 56) & 0xFF00000000000000u) |
      ((val << 40) & 0x00FF000000000000u) |
      ((val << 24) & 0x0000FF0000000000u) | ((val << 8) & 0x000000FF00000000u) |
      ((val >> 8) & 0x00000000FF000000u) | ((val >> 24) & 0x0000000000FF0000u) |
      ((val >> 40) & 0x000000000000FF00u) |
      ((val >> 56) & 0x00000000000000FFu));
}

template <class T, HWY_IF_SIGNED(T)>
static HWY_INLINE T ReverseBytesOfValue(T val) {
  using TU = MakeUnsigned<T>;
  return static_cast<T>(ReverseBytesOfValue(static_cast<TU>(val)));
}

struct TestReverseLaneBytes {
  template <class T, class D>
  HWY_NOINLINE void operator()(T /*unused*/, D d) {
    const size_t N = Lanes(d);
    auto in = AllocateAligned<T>(N);
    auto expected = AllocateAligned<T>(N);
    HWY_ASSERT(in && expected);

    const auto v_iota = Iota(d, 0);
    for (size_t i = 0; i < N; i++) {
      expected[i] = ReverseBytesOfValue(static_cast<T>(i));
    }
    HWY_ASSERT_VEC_EQ(d, expected.get(), ReverseLaneBytes(v_iota));

    RandomState rng;
    for (size_t rep = 0; rep < AdjustedReps(10000); ++rep) {
      for (size_t i = 0; i < N; i++) {
        in[i] = static_cast<T>(Random64(&rng));
        expected[i] = ReverseBytesOfValue(in[i]);
      }

      const auto v = Load(d, in.get());
      HWY_ASSERT_VEC_EQ(d, expected.get(), ReverseLaneBytes(v));
    }
  }
};

class TestReverseBits {
 private:
  template <class T>
  static HWY_INLINE T ReverseBitsOfEachByte(T val) {
    using TU = MakeUnsigned<T>;
    constexpr TU kMaxUnsignedVal{LimitsMax<TU>()};
    constexpr TU kShrMask1 =
        static_cast<TU>(0x5555555555555555u & kMaxUnsignedVal);
    constexpr TU kShrMask2 =
        static_cast<TU>(0x3333333333333333u & kMaxUnsignedVal);
    constexpr TU kShrMask3 =
        static_cast<TU>(0x0F0F0F0F0F0F0F0Fu & kMaxUnsignedVal);

    constexpr TU kShlMask1 = static_cast<TU>(~kShrMask1);
    constexpr TU kShlMask2 = static_cast<TU>(~kShrMask2);
    constexpr TU kShlMask3 = static_cast<TU>(~kShrMask3);

    TU result = static_cast<TU>(val);
    result = static_cast<TU>(((result << 1) & kShlMask1) |
                             ((result >> 1) & kShrMask1));
    result = static_cast<TU>(((result << 2) & kShlMask2) |
                             ((result >> 2) & kShrMask2));
    result = static_cast<TU>(((result << 4) & kShlMask3) |
                             ((result >> 4) & kShrMask3));
    return static_cast<T>(result);
  }

  template <class T>
  static HWY_INLINE T ReverseBitsOfValue(T val) {
    return ReverseBytesOfValue(ReverseBitsOfEachByte(val));
  }

 public:
  template <class T, class D>
  HWY_NOINLINE void operator()(T /*unused*/, D d) {
    const size_t N = Lanes(d);
    auto in = AllocateAligned<T>(N);
    auto expected = AllocateAligned<T>(N);
    HWY_ASSERT(in && expected);

    const auto v_iota = Iota(d, 0);
    for (size_t i = 0; i < N; i++) {
      expected[i] = ReverseBitsOfValue(static_cast<T>(i));
    }
    HWY_ASSERT_VEC_EQ(d, expected.get(), ReverseBits(v_iota));

    RandomState rng;
    for (size_t rep = 0; rep < AdjustedReps(10000); ++rep) {
      for (size_t i = 0; i < N; i++) {
        in[i] = static_cast<T>(Random64(&rng));
        expected[i] = ReverseBitsOfValue(in[i]);
      }

      const auto v = Load(d, in.get());
      HWY_ASSERT_VEC_EQ(d, expected.get(), ReverseBits(v));
    }
  }
};

HWY_NOINLINE void TestAllReverse() {
  ForAllTypes(ForPartialVectors<TestReverse>());
}

HWY_NOINLINE void TestAllReverse2() {
  ForUIF64(ForGEVectors<128, TestReverse2>());
  ForUIF32(ForGEVectors<64, TestReverse2>());
  ForUIF16(ForGEVectors<32, TestReverse2>());
  ForUI8(ForGEVectors<16, TestReverse2>());
}

HWY_NOINLINE void TestAllReverse4() {
  ForUIF64(ForGEVectors<256, TestReverse4>());
  ForUIF32(ForGEVectors<128, TestReverse4>());
  ForUIF16(ForGEVectors<64, TestReverse4>());
  ForUI8(ForGEVectors<32, TestReverse4>());
}

HWY_NOINLINE void TestAllReverse8() {
  ForUIF64(ForGEVectors<512, TestReverse8>());
  ForUIF32(ForGEVectors<256, TestReverse8>());
  ForUIF16(ForGEVectors<128, TestReverse8>());
  ForUI8(ForGEVectors<64, TestReverse8>());
}

HWY_NOINLINE void TestAllReverseLaneBytes() {
  ForUI163264(ForPartialVectors<TestReverseLaneBytes>());
}

HWY_NOINLINE void TestAllReverseBits() {
  ForIntegerTypes(ForPartialVectors<TestReverseBits>());
}

struct TestReverseBlocks {
  template <class T, class D>
  HWY_NOINLINE void operator()(T /*unused*/, D d) {
    const size_t N = Lanes(d);
    const RebindToUnsigned<D> du;  // Iota does not support float16_t.
    const auto v = BitCast(d, Iota(du, 1));
    auto expected = AllocateAligned<T>(N);
    auto copy = AllocateAligned<T>(N);
    HWY_ASSERT(expected && copy);

    constexpr size_t kLanesPerBlock = 16 / sizeof(T);
    const size_t num_blocks = N / kLanesPerBlock;
    HWY_ASSERT(num_blocks != 0);

    // Can't set float16_t value directly, need to permute in memory.
    Store(v, d, copy.get());
    for (size_t i = 0; i < N; ++i) {
      const size_t idx_block = i / kLanesPerBlock;
      const size_t base = (num_blocks - 1 - idx_block) * kLanesPerBlock;
      expected[i] = copy[base + (i % kLanesPerBlock)];
    }
    HWY_ASSERT_VEC_EQ(d, expected.get(), ReverseBlocks(d, v));
  }
};

HWY_NOINLINE void TestAllReverseBlocks() {
  ForAllTypes(ForGEVectors<128, TestReverseBlocks>());
}

// NOLINTNEXTLINE(google-readability-namespace-comments)
}  // namespace HWY_NAMESPACE
}  // namespace hwy
HWY_AFTER_NAMESPACE();

#if HWY_ONCE

namespace hwy {
HWY_BEFORE_TEST(HwyReverseTest);
HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverse);
HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverse2);
HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverse4);
HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverse8);
HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverseLaneBytes);
HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverseBits);
HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverseBlocks);
}  // namespace hwy

#endif