summaryrefslogtreecommitdiff
path: root/libc/test/src/__support/CPP/simd_test.cpp
blob: 8bead8461d6499c9fe4456f86ef45a6160637040 (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
//===-- Unittests for cpp::simd -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/simd.h"
#include "src/__support/CPP/utility.h"

#include "test/UnitTest/Test.h"

static_assert(LIBC_HAS_VECTOR_TYPE, "compiler needs ext_vector_type support");

using namespace LIBC_NAMESPACE;

TEST(LlvmLibcSIMDTest, Basic) {}
TEST(LlvmLibcSIMDTest, VectorCreation) {
  cpp::simd<int> v1 = cpp::splat(5);
  cpp::simd<int> v2 = cpp::iota<int>();

  EXPECT_EQ(v1[0], 5);
  EXPECT_EQ(v2[0], 0);
}

TEST(LlvmLibcSIMDTest, TypeTraits) {
  cpp::simd<int> v1 = cpp::splat(0);

  static_assert(cpp::is_simd_v<decltype(v1)>, "v1 should be a SIMD type");
  static_assert(!cpp::is_simd_v<int>, "int is not a SIMD type");
  static_assert(cpp::is_simd_mask_v<cpp::simd<bool, 4>>,
                "should be a SIMD mask");

  using Elem = cpp::simd_element_type_t<decltype(v1)>;
  static_assert(cpp::is_same_v<Elem, int>, "element type should be int");
}

TEST(LlvmLibcSIMDTest, ElementwiseOperations) {
  cpp::simd<int> vi1 = cpp::splat(1);
  cpp::simd<int> vi2 = cpp::splat(-1);

  cpp::simd<int> v_abs = cpp::abs(vi2);
  cpp::simd<int> v_min = cpp::min(vi1, vi2);
  cpp::simd<int> v_max = cpp::max(vi1, vi2);

  EXPECT_EQ(v_abs[0], 1);
  EXPECT_EQ(v_min[0], -1);
  EXPECT_EQ(v_max[0], 1);
}

TEST(LlvmLibcSIMDTest, ReductionOperations) {
  cpp::simd<int> v = cpp::splat(1);

  int sum = cpp::reduce(v);
  int prod = cpp::reduce(v, cpp::multiplies<>{});

  EXPECT_EQ(sum, static_cast<int>(cpp::simd_size_v<decltype(v)>));
  EXPECT_EQ(prod, 1);
}

TEST(LlvmLibcSIMDTest, MaskOperations) {
  cpp::simd<bool, 8> mask{true, false, true, false, false, false, false, false};

  EXPECT_TRUE(cpp::any_of(mask));
  EXPECT_FALSE(cpp::all_of(mask));
  EXPECT_FALSE(cpp::none_of(mask));
  EXPECT_TRUE(cpp::some_of(mask));
  EXPECT_EQ(cpp::find_first_set(mask), 0);
  EXPECT_EQ(cpp::find_last_set(mask), 2);
  EXPECT_EQ(cpp::popcount(mask), 2);
}

TEST(LlvmLibcSIMDTest, SplitConcat) {
  cpp::simd<char, 8> v{1, 1, 2, 2, 3, 3, 4, 4};
  auto [v1, v2, v3, v4] = cpp::split<2, 2, 2, 2>(v);
  EXPECT_TRUE(cpp::all_of(v1 == 1));
  EXPECT_TRUE(cpp::all_of(v2 == 2));
  EXPECT_TRUE(cpp::all_of(v3 == 3));
  EXPECT_TRUE(cpp::all_of(v4 == 4));

  cpp::simd<char, 8> m = cpp::concat(v1, v2, v3, v4);
  EXPECT_TRUE(cpp::all_of(m == v));

  cpp::simd<char, 1> c(~0);
  cpp::simd<char, 8> n = cpp::concat(c, c, c, c, c, c, c, c);
  EXPECT_TRUE(cpp::all_of(n == ~0));
}

TEST(LlvmLibcSIMDTest, LoadStore) {
  constexpr size_t SIZE = cpp::simd_size_v<cpp::simd<int>>;
  alignas(alignof(cpp::simd<int>)) int buf[SIZE];

  cpp::simd<int> v1 = cpp::splat(1);
  cpp::store(v1, buf);
  cpp::simd<int> v2 = cpp::load<cpp::simd<int>>(buf);

  EXPECT_TRUE(cpp::all_of(v1 == 1));
  EXPECT_TRUE(cpp::all_of(v2 == 1));

  cpp::simd<int> v3 = cpp::splat(2);
  cpp::store(v3, buf, /*aligned=*/true);
  cpp::simd<int> v4 = cpp::load<cpp::simd<int>>(buf, /*aligned=*/true);

  EXPECT_TRUE(cpp::all_of(v3 == 2));
  EXPECT_TRUE(cpp::all_of(v4 == 2));
}

TEST(LlvmLibcSIMDTest, MaskedLoadStore) {
  constexpr size_t SIZE = cpp::simd_size_v<cpp::simd<int>>;
  alignas(alignof(cpp::simd<int>)) int buf[SIZE] = {0};

  cpp::simd<int> mask = cpp::iota(0) % 2 == 0;
  cpp::simd<int> v1 = cpp::splat(1);

  cpp::store_masked<cpp::simd<int>>(mask, v1, buf);
  cpp::simd<int> v2 = cpp::load_masked<cpp::simd<int>>(mask, buf);

  EXPECT_TRUE(cpp::all_of((v2 == 1) == mask));
}

TEST(LlvmLibcSIMDTest, GatherScatter) {
  constexpr int SIZE = cpp::simd_size_v<cpp::simd<int>>;
  alignas(alignof(cpp::simd<int>)) int buf[SIZE];

  cpp::simd<int> mask = cpp::iota(1);
  cpp::simd<int> idx = cpp::iota(0);
  cpp::simd<int> v1 = cpp::splat(1);

  cpp::scatter<cpp::simd<int>>(mask, idx, v1, buf);
  cpp::simd<int> v2 = cpp::gather<cpp::simd<int>>(mask, idx, buf);

  EXPECT_TRUE(cpp::all_of(v1 == 1));
  EXPECT_TRUE(cpp::all_of(v2 == 1));
}

TEST(LlvmLibcSIMDTest, MaskedCompressExpand) {
  constexpr size_t SIZE = cpp::simd_size_v<cpp::simd<int>>;
  alignas(alignof(cpp::simd<int>)) int buf[SIZE] = {0};

  cpp::simd<int> mask_expand = cpp::iota(0) % 2 == 0;
  cpp::simd<int> mask_compress = 1;

  cpp::simd<int> v1 = cpp::iota(0);

  cpp::compress<cpp::simd<int>>(mask_compress, v1, buf);
  cpp::simd<int> v2 = cpp::expand<cpp::simd<int>>(mask_expand, buf);

  EXPECT_TRUE(cpp::all_of(!mask_expand || v2 <= SIZE / 2));
}