[libcxx-commits] [libcxx] [libc++] <experimental/simd> Add unary operators for class simd_mask (PR #118468)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Dec 3 02:52:24 PST 2024


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/118468.diff


4 Files Affected:

- (modified) libcxx/include/experimental/__simd/scalar.h (+2) 
- (modified) libcxx/include/experimental/__simd/simd_mask.h (+3) 
- (modified) libcxx/include/experimental/__simd/vec_ext.h (+2) 
- (added) libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_unary.pass.cpp (+37) 


``````````diff
diff --git a/libcxx/include/experimental/__simd/scalar.h b/libcxx/include/experimental/__simd/scalar.h
index da318d2f4650fd..8aa4d4fb312722 100644
--- a/libcxx/include/experimental/__simd/scalar.h
+++ b/libcxx/include/experimental/__simd/scalar.h
@@ -93,6 +93,8 @@ struct __mask_operations<_Tp, simd_abi::__scalar> {
   static _LIBCPP_HIDE_FROM_ABI void __load(_MaskStorage& __s, const bool* __mem) noexcept { __s.__data = __mem[0]; }
 
   static _LIBCPP_HIDE_FROM_ABI void __store(_MaskStorage __s, bool* __mem) noexcept { __mem[0] = __s.__data; }
+
+  static _LIBCPP_HIDE_FROM_ABI _MaskStorage __negate(_MaskStorage __s) noexcept { return {{!__s.__data}}; }
 };
 
 } // namespace parallelism_v2
diff --git a/libcxx/include/experimental/__simd/simd_mask.h b/libcxx/include/experimental/__simd/simd_mask.h
index 6b6f671bf3e64c..9db2cdb5af0fa6 100644
--- a/libcxx/include/experimental/__simd/simd_mask.h
+++ b/libcxx/include/experimental/__simd/simd_mask.h
@@ -79,6 +79,9 @@ class simd_mask {
   // scalar access [simd.mask.subscr]
   _LIBCPP_HIDE_FROM_ABI reference operator[](size_t __i) noexcept { return reference(__s_, __i); }
   _LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
+
+  // unary operators
+  _LIBCPP_HIDE_FROM_ABI simd_mask operator!() const noexcept { return simd_mask(_Impl::__negate(__s_), __storage_tag); }
 };
 
 template <class _Tp, class _Abi>
diff --git a/libcxx/include/experimental/__simd/vec_ext.h b/libcxx/include/experimental/__simd/vec_ext.h
index abc7e9595be9c8..f40aba8750925a 100644
--- a/libcxx/include/experimental/__simd/vec_ext.h
+++ b/libcxx/include/experimental/__simd/vec_ext.h
@@ -121,6 +121,8 @@ struct __mask_operations<_Tp, simd_abi::__vec_ext<_Np>> {
     for (size_t __i = 0; __i < _Np; __i++)
       __mem[__i] = static_cast<bool>(__s.__data[__i]);
   }
+
+  static _LIBCPP_HIDE_FROM_ABI _MaskStorage __negate(_MaskStorage __s) noexcept { return {{~__s.__data}}; }
 };
 
 } // namespace parallelism_v2
diff --git a/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_unary.pass.cpp b/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_unary.pass.cpp
new file mode 100644
index 00000000000000..20a84eb28026a6
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_unary.pass.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14
+
+// <experimental/simd>
+//
+// [simd.mask.class]
+// simd_mask operator!() const noexcept;
+
+#include "../test_utils.h"
+#include <experimental/simd>
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, std::size_t>
+struct CheckSimdMaskNotOperator {
+  template <class SimdAbi>
+  void operator()() {
+    constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+    ex::simd_mask<T, SimdAbi> origin_mask(true);
+    static_assert(noexcept(!origin_mask));
+    std::array<bool, array_size> expected_value;
+    std::fill(expected_value.begin(), expected_value.end(), false);
+    assert_simd_mask_values_equal<array_size>(!origin_mask, expected_value);
+  }
+};
+
+int main(int, char**) {
+  test_all_simd_abi<CheckSimdMaskNotOperator>();
+  return 0;
+}

``````````

</details>


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


More information about the libcxx-commits mailing list