blob: 9bda07ee9ea4bf3661a2647a921f31af08ff7a4f [file] [log] [blame]
Tim Shen38cd7de2018-07-30 22:27:38 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Tim Shen38cd7de2018-07-30 22:27:38 +00006//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++98, c++03, c++11, c++14
10
11// <experimental/simd>
12//
13// loads [simd.load]
14// template <class U, class Flags> void copy_from(const U* mem, Flags f);
15
16#include <experimental/simd>
17#include <cstdint>
18
19#include "test_macros.h"
20
21namespace ex = std::experimental::parallelism_v2;
22
23template <class T, class... Args>
24auto not_supported_load(Args&&... args) -> decltype(
25 std::declval<ex::native_simd<T>>().copy_from(std::forward<Args>(args)...),
26 void()) = delete;
27
28template <class T>
29void not_supported_load(...) {}
30
31template <class T, class... Args>
32auto supported_load(Args&&... args) -> decltype(
33 std::declval<ex::native_simd<T>>().copy_from(std::forward<Args>(args)...),
34 void()) {}
35
36template <class T>
37void supported_load(...) = delete;
38
39void compile_load() {
40 supported_load<int>((int*)nullptr, ex::element_aligned_tag());
41 supported_load<uint32_t>((int*)nullptr, ex::element_aligned_tag());
42 supported_load<double>((float*)nullptr, ex::element_aligned_tag());
43 supported_load<uint16_t>((unsigned int*)nullptr, ex::element_aligned_tag());
44 supported_load<uint32_t>((float*)nullptr, ex::element_aligned_tag());
45
46 not_supported_load<int>((int*)nullptr, int());
47}
48
49template <typename SimdType>
50void test_load() {
51 alignas(32) int32_t buffer[] = {4, 3, 2, 1};
52 {
53 SimdType a;
54 a.copy_from(buffer, ex::element_aligned_tag());
55 assert(a[0] == 4);
56 assert(a[1] == 3);
57 assert(a[2] == 2);
58 assert(a[3] == 1);
59 }
60 {
61 SimdType a;
62 a.copy_from(buffer, ex::vector_aligned_tag());
63 assert(a[0] == 4);
64 assert(a[1] == 3);
65 assert(a[2] == 2);
66 assert(a[3] == 1);
67 }
68 {
69 SimdType a;
70 a.copy_from(buffer, ex::overaligned_tag<32>());
71 assert(a[0] == 4);
72 assert(a[1] == 3);
73 assert(a[2] == 2);
74 assert(a[3] == 1);
75 }
76
77 {
78 SimdType a;
79 a.copy_from(buffer, ex::element_aligned);
80 assert(a[0] == 4);
81 assert(a[1] == 3);
82 assert(a[2] == 2);
83 assert(a[3] == 1);
84 }
85 {
86 SimdType a;
87 a.copy_from(buffer, ex::vector_aligned);
88 assert(a[0] == 4);
89 assert(a[1] == 3);
90 assert(a[2] == 2);
91 assert(a[3] == 1);
92 }
93 {
94 SimdType a;
95 a.copy_from(buffer, ex::overaligned<32>);
96 assert(a[0] == 4);
97 assert(a[1] == 3);
98 assert(a[2] == 2);
99 assert(a[3] == 1);
100 }
101}
102
103template <typename SimdType>
104void test_converting_load() {
105 float buffer[] = {1., 2., 4., 8.};
106 SimdType a;
107 a.copy_from(buffer, ex::element_aligned_tag());
108 assert(a[0] == 1);
109 assert(a[1] == 2);
110 assert(a[2] == 4);
111 assert(a[3] == 8);
112}
113
114int main() {
115 // TODO: adjust the tests when this assertion fails.
116 assert(ex::native_simd<int32_t>::size() >= 4);
117 test_load<ex::native_simd<int32_t>>();
118 test_load<ex::fixed_size_simd<int32_t, 4>>();
119 test_converting_load<ex::native_simd<int32_t>>();
120 test_converting_load<ex::fixed_size_simd<int32_t, 4>>();
121}