generator.pass.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===----------------------------------------------------------------------===//
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  9. // See GCC PR63723.
  10. // UNSUPPORTED: gcc-4.9
  11. // <experimental/simd>
  12. //
  13. // [simd.class]
  14. // template <class G> explicit simd(G&& gen);
  15. #include <experimental/simd>
  16. #include <cstdint>
  17. #include "test_macros.h"
  18. namespace ex = std::experimental::parallelism_v2;
  19. template <class T, class... Args>
  20. auto not_supported_simd128_ctor(Args&&... args) -> decltype(
  21. ex::fixed_size_simd<T, 16 / sizeof(T)>(std::forward<Args>(args)...),
  22. void()) = delete;
  23. template <class T>
  24. void not_supported_simd128_ctor(...) {}
  25. template <class T, class... Args>
  26. auto supported_simd128_ctor(Args&&... args) -> decltype(
  27. ex::fixed_size_simd<T, 16 / sizeof(T)>(std::forward<Args>(args)...),
  28. void()) {}
  29. template <class T>
  30. void supported_simd128_ctor(...) = delete;
  31. struct identity {
  32. template <size_t value>
  33. int operator()(std::integral_constant<size_t, value>) const {
  34. return value;
  35. }
  36. };
  37. void compile_generator() {
  38. supported_simd128_ctor<int>(identity());
  39. not_supported_simd128_ctor<int>([](int i) { return float(i); });
  40. not_supported_simd128_ctor<int>([](intptr_t i) { return (int*)(i); });
  41. not_supported_simd128_ctor<int>([](int* i) { return i; });
  42. }
  43. struct limited_identity {
  44. template <size_t value>
  45. typename std::conditional<value <= 2, int32_t, int64_t>::type
  46. operator()(std::integral_constant<size_t, value>) const {
  47. return value;
  48. }
  49. };
  50. void compile_limited_identity() {
  51. supported_simd128_ctor<int64_t>(limited_identity());
  52. not_supported_simd128_ctor<int32_t>(limited_identity());
  53. }
  54. template <typename SimdType>
  55. void test_generator() {
  56. {
  57. SimdType a([](int i) { return i; });
  58. assert(a[0] == 0);
  59. assert(a[1] == 1);
  60. assert(a[2] == 2);
  61. assert(a[3] == 3);
  62. }
  63. {
  64. SimdType a([](int i) { return 2 * i - 1; });
  65. assert(a[0] == -1);
  66. assert(a[1] == 1);
  67. assert(a[2] == 3);
  68. assert(a[3] == 5);
  69. }
  70. }
  71. int main(int, char**) {
  72. // TODO: adjust the tests when this assertion fails.
  73. assert(ex::native_simd<int32_t>::size() >= 4);
  74. test_generator<ex::native_simd<int32_t>>();
  75. test_generator<ex::fixed_size_simd<int32_t, 4>>();
  76. return 0;
  77. }