Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[libc++] <experimental/simd> Add unary operators for class simd_mask #118468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

joy2myself
Copy link
Member

No description provided.

@joy2myself joy2myself requested a review from a team as a code owner December 3, 2024 10:51
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Dec 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 3, 2024

@llvm/pr-subscribers-libcxx

Author: ZhangYin (joy2myself)

Changes

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 --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;
+}

@joy2myself
Copy link
Member Author

@philnik777 ping.

@philnik777
Copy link
Contributor

@joy2myself Given that simd is now voted into C++26 I'm not sure it's worth pursuing the experimental implementation anymore. Do you have any thoughts on that?

@joy2myself
Copy link
Member Author

@joy2myself Given that simd is now voted into C++26 I'm not sure it's worth pursuing the experimental implementation anymore. Do you have any thoughts on that?

@philnik777 Thanks for raising these points! I researched P1928R15. Here's a proposal based on it's current status:
Since the current paper does modify the core data types of the header file, the implementation code of the current experimental version may not be reused on a large scale, and there is indeed not much value in continuing to advance the implementation.
Since the spec is still evolving, I plan to implement the current draft of P1928R15 in a downstream branch. Would the libc++ team consider accepting this as an experimental preview (marked as unstable)? This would let us iterate alongside standardization progress.
If upstream prefers to wait for finalization, I'll maintain it downstream but would appreciate reserving my commitment to upstream it once WG21 finalizes the spec.
In addition, does libc++ upstream have any form of communication with WG21? Can I participate in WG21's standardization work on the header file through libc++ upstream? Or just get the change information as soon as possible?

@philnik777
Copy link
Contributor

@joy2myself I've started looking into implementing P1928 (and some changes of follow-up papers) myself. I've changed a few aspects compared to the current experimental implementation. I think we'd be happy to continue development directly upstream. Assuming I didn't do something massively wrong, I think the best way to continue would be to start upstreaming my initial patch and then we can finish the implementation in tree. WDYT?

@philnik777
Copy link
Contributor

Oh, and here is my current implementation status: main...philnik777:llvm-project:implement_simd

@joy2myself
Copy link
Member Author

@philnik777 Your initial patch looks great. I agree to upstream this patch and continue to work on it. Can I now start working on the TODOs? Or do you have any plans for subsequent work?

@philnik777
Copy link
Contributor

Feel free to start working on them.

@joy2myself
Copy link
Member Author

Hi @philnik777, I noticed that your initial patch has not been merged yet. My part of constructor implementations are ready but depend on it. Would you prefer I help submit the inital patch or ​​should I wait for your PR to proceed?​

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants