[libcxx-commits] [libcxx] [libc++] <experimental/simd> Fix conversion constructor (PR #74693)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Dec 6 19:44:29 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: ZhangYin (joy2myself)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/74693.diff


3 Files Affected:

- (modified) libcxx/include/experimental/__simd/simd.h (+2-5) 
- (modified) libcxx/include/experimental/__simd/vec_ext.h (+9) 
- (modified) libcxx/test/std/experimental/simd/simd.class/simd_ctor_conversion.pass.cpp (+1) 


``````````diff
diff --git a/libcxx/include/experimental/__simd/simd.h b/libcxx/include/experimental/__simd/simd.h
index ffb328eb345b1..568cf1a7c1b0c 100644
--- a/libcxx/include/experimental/__simd/simd.h
+++ b/libcxx/include/experimental/__simd/simd.h
@@ -55,11 +55,8 @@ class simd {
             enable_if_t<!is_same_v<_Up, _Tp> && is_same_v<abi_type, simd_abi::fixed_size<size()>> &&
                             __is_non_narrowing_convertible_v<_Up, value_type>,
                         int> = 0>
-  _LIBCPP_HIDE_FROM_ABI simd(const simd<_Up, simd_abi::fixed_size<size()>>& __v) noexcept {
-    for (size_t __i = 0; __i < size(); __i++) {
-      (*this)[__i] = static_cast<value_type>(__v[__i]);
-    }
-  }
+  _LIBCPP_HIDE_FROM_ABI simd(const simd<_Up, simd_abi::fixed_size<size()>>& __v) noexcept
+      : __s_(_Impl::__conversion(__v)) {}
 
   // generator constructor
   template <class _Generator, enable_if_t<__can_generate_v<value_type, _Generator, size()>, int> = 0>
diff --git a/libcxx/include/experimental/__simd/vec_ext.h b/libcxx/include/experimental/__simd/vec_ext.h
index baaeda6a7401a..78c86dd71ee67 100644
--- a/libcxx/include/experimental/__simd/vec_ext.h
+++ b/libcxx/include/experimental/__simd/vec_ext.h
@@ -64,6 +64,15 @@ struct __simd_operations<_Tp, simd_abi::__vec_ext<_Np>> {
     return __result;
   }
 
+  template <class _From>
+  static _LIBCPP_HIDE_FROM_ABI _SimdStorage __conversion(_From __v) noexcept {
+    _SimdStorage __result;
+    for (int __i = 0; __i < _Np; ++__i) {
+      __result.__set(__i, static_cast<_Tp>(__v[__i]));
+    }
+    return __result;
+  }
+
   template <class _Generator, size_t... _Is>
   static _LIBCPP_HIDE_FROM_ABI _SimdStorage __generate_init(_Generator&& __g, std::index_sequence<_Is...>) {
     return _SimdStorage{{__g(std::integral_constant<size_t, _Is>())...}};
diff --git a/libcxx/test/std/experimental/simd/simd.class/simd_ctor_conversion.pass.cpp b/libcxx/test/std/experimental/simd/simd.class/simd_ctor_conversion.pass.cpp
index 7ce4bed9c7db8..6142be1c555c3 100644
--- a/libcxx/test/std/experimental/simd/simd.class/simd_ctor_conversion.pass.cpp
+++ b/libcxx/test/std/experimental/simd/simd.class/simd_ctor_conversion.pass.cpp
@@ -8,6 +8,7 @@
 
 // UNSUPPORTED: c++03, c++11, c++14
 // XFAIL: target=powerpc{{.*}}le-unknown-linux-gnu
+// ADDITIONAL_COMPILE_FLAGS: -O3
 
 // <experimental/simd>
 //

``````````

</details>


https://github.com/llvm/llvm-project/pull/74693


More information about the libcxx-commits mailing list