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