Skip to content

Commit 71ecbf7

Browse files
committed
[libc] Add check for support and a test for libc SIMD helpers
Summary: This adds a few basic tests for the SIMD helpers and adds a CMake variable we can use to detect support.
1 parent 4294907 commit 71ecbf7

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

libc/cmake/modules/CheckCompilerFeatures.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(
1515
"fixed_point"
1616
"cfloat16"
1717
"cfloat128"
18+
"ext_vector_type"
1819
)
1920

2021
# Making sure ALL_COMPILER_FEATURES is sorted.
@@ -126,6 +127,8 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
126127
set(LIBC_COMPILER_HAS_BUILTIN_ROUND TRUE)
127128
elseif(${feature} STREQUAL "builtin_roundeven")
128129
set(LIBC_COMPILER_HAS_BUILTIN_ROUNDEVEN TRUE)
130+
elseif(${feature} STREQUAL "ext_vector_type")
131+
set(LIBC_COMPILER_HAS_EXT_VECTOR_TYPE TRUE)
129132
endif()
130133
endif()
131134
endforeach()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "src/__support/macros/attributes.h"
2+
3+
#if !LIBC_HAS_VECTOR_TYPE
4+
#error unsupported
5+
#endif
6+
7+
bool [[clang::ext_vector_type(1)]] v;

libc/src/__support/CPP/simd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ template <size_t N> LIBC_INLINE constexpr int find_first_set(simd<bool, N> m) {
115115
}
116116
template <size_t N> LIBC_INLINE constexpr int find_last_set(simd<bool, N> m) {
117117
constexpr size_t size = simd_size_v<simd<bool, N>>;
118-
return size - __builtin_clzg(m);
118+
return size - 1 - __builtin_clzg(m);
119119
}
120120

121121
// Elementwise operations.

libc/test/src/__support/CPP/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,15 @@ add_libc_test(
160160
DEPENDS
161161
libc.src.__support.CPP.type_traits
162162
)
163+
164+
if(LIBC_COMPILER_HAS_EXT_VECTOR_TYPE)
165+
add_libc_test(
166+
simd_test
167+
SUITE
168+
libc-cpp-utils-tests
169+
SRCS
170+
simd_test.cpp
171+
DEPENDS
172+
libc.src.__support.CPP.simd
173+
)
174+
endif()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===-- Unittests for cpp::simd -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/CPP/simd.h"
10+
#include "src/__support/CPP/utility.h"
11+
12+
#include "test/UnitTest/FPMatcher.h"
13+
#include "test/UnitTest/Test.h"
14+
15+
static_assert(LIBC_HAS_VECTOR_TYPE, "compiler needs ext_vector_type support");
16+
17+
using namespace LIBC_NAMESPACE::cpp;
18+
19+
TEST(LlvmLibcSIMDTest, Basic) {}
20+
TEST(LlvmLibcSIMDTest, VectorCreation) {
21+
simd<int> v1 = splat(5);
22+
simd<int> v2 = iota<int>();
23+
24+
EXPECT_EQ(v1[0], 5);
25+
EXPECT_EQ(v2[0], 0);
26+
}
27+
28+
TEST(LlvmLibcSIMDTest, TypeTraits) {
29+
simd<int> v1 = splat(0);
30+
31+
static_assert(is_simd_v<decltype(v1)>, "v1 should be a SIMD type");
32+
static_assert(!is_simd_v<int>, "int is not a SIMD type");
33+
static_assert(is_simd_mask_v<simd<bool, 4>>, "should be a SIMD mask");
34+
35+
using Elem = simd_element_type_t<decltype(v1)>;
36+
static_assert(is_same_v<Elem, int>, "element type should be int");
37+
}
38+
39+
TEST(LlvmLibcSIMDTest, ElementwiseOperations) {
40+
simd<int> v1 = splat(1);
41+
simd<int> v2 = splat(-1);
42+
43+
simd<int> v_abs = abs(v2);
44+
simd<int> v_min = min(v1, v2);
45+
simd<int> v_max = max(v1, v2);
46+
47+
EXPECT_EQ(v_min[0], -1);
48+
EXPECT_EQ(v_max[0], 1);
49+
EXPECT_EQ(v_abs[0], 1);
50+
}
51+
52+
TEST(LlvmLibcSIMDTest, ReductionOperations) {
53+
simd<int> v = splat(1);
54+
55+
int sum = reduce(v);
56+
int prod = reduce(v, multiplies<>{});
57+
58+
EXPECT_EQ(sum, static_cast<int>(simd_size_v<decltype(v)>));
59+
EXPECT_EQ(prod, 1);
60+
}
61+
62+
TEST(LlvmLibcSIMDTest, MaskOperations) {
63+
simd<bool, 8> mask{true, false, true, false, false, false, false, false};
64+
65+
EXPECT_TRUE(any_of(mask));
66+
EXPECT_FALSE(all_of(mask));
67+
EXPECT_TRUE(some_of(mask));
68+
EXPECT_EQ(find_first_set(mask), 0);
69+
EXPECT_EQ(find_last_set(mask), 2);
70+
}

0 commit comments

Comments
 (0)