blob: bd9d563190a67179a577fdc90b889a3224e39e63 [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>
David Zarzyckia0686012019-09-26 11:12:29 +000018#include <cassert>
Tim Shen38cd7de2018-07-30 22:27:38 +000019
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
JF Bastien2df59c52019-02-04 20:31:13 +0000115int main(int, char**) {
Tim Shen38cd7de2018-07-30 22:27:38 +0000116 // 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>>();
JF Bastien2df59c52019-02-04 20:31:13 +0000122
123 return 0;
Tim Shen38cd7de2018-07-30 22:27:38 +0000124}