std::experi...s_simd_mask(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 std::experimental::is_simd, std::experimental::is_simd_mask 🌸

okay bestie so this is from the C++ Standard Libary manual, section 3, and it's genuinely one of those pages where the whole personality is "am I a SIMD type or not" β€” and honestly? iconic. Let's get into it πŸ’…


✨ NAME ✨

std::experimental::is_simd, std::experimental::is_simd_mask β€” literally just std::experimental::is_simd, std::experimental::is_simd_mask. That's the name. Periodt 🫢


✨ SYNOPSIS ✨

so this whole thing lives in the header <experimental/simd> πŸŽ€

template< class T >                   (1) (parallelism TS v2)
struct is_simd;
template< class T >                   (2) (parallelism TS v2)
struct is_simd_mask;
  1. bestie, if T is a specialization of the simd class template, then this provides the member constant value equal true. For literally any other type, value is false. that's the whole job πŸ’–
  2. and if T is a specialization of the simd_mask class template, it provides the member constant value equal true. For any other type, value is false. slay ✨

✨ TEMPLATE PARAMETERS ✨

  • T β€” a type to check πŸ‘€

🌈 Helper variable template 🌈

template< class T >
(parallelism TS v2)
inline constexpr bool is_simd_v = is_simd<T>::value;
template< class T >
(parallelism TS v2)
inline constexpr bool is_simd_mask_v = is_simd_mask<T>::value;

✨ INHERITED FROM std::integral_constant ✨

πŸ¦‹ Member constants πŸ¦‹

  • value β€” true if T is a simd/simd_mask type, false otherwise. [static] (public static member constant) ⭐

πŸ¦‹ Member functions πŸ¦‹

  • operator bool β€” converts the object to bool, returns value (public member function) πŸ’œ
  • operator() β€” returns value (C++14) (public member function) πŸ’œ

πŸ¦‹ Member types πŸ¦‹

  • value_type β†’ bool
  • type β†’ std::integral_constant<bool, value>

πŸŽ€ NOTES πŸŽ€

okay this is the part you actually need to read, ngl. is_simd_v<T> is necessary but not sufficient for testing whether T can be used as a SIMD type 😭

for example, is_simd_v<simd<bool>> is true, even though bool is not included in the permissible vectorizable types. the missing condition is std::is_constructible_v<T>, which is false for simd<bool>. so yeah β€” vibe check twice, bestie πŸ’€


πŸ”₯ EXAMPLE πŸ”₯

// Run this code

#include <experimental/simd>
#include <iostream>
#include <string_view>

namespace stdx = std::experimental;

template<typename T>
void test_simd(std::string_view type_name)
{
    std::cout << std::boolalpha
              << "Type: " << type_name << '\n'
              << "  is_simd: " << stdx::is_simd_v<T> << '\n'
              << "  is_constructible: " << std::is_constructible_v<T> << '\n';
}

template<typename T>
void test_simd_mask(std::string_view type_name)
{
    std::cout << std::boolalpha
              << "Type: " << type_name << '\n'
              << "  is_simd_mask: " << stdx::is_simd_mask_v<T> << '\n'
              << "  is_constructible: " << std::is_constructible_v<T> << "\n\n";
}

int main()
{
    test_simd<int>("int");
    test_simd_mask<int>("int");

    test_simd<stdx::simd<float>>("simd<float>");
    test_simd_mask<stdx::simd_mask<float>>("simd_mask<float>");

    test_simd<stdx::simd<bool>>("simd<bool>");
    test_simd_mask<stdx::simd_mask<bool>>("simd_mask<bool>");
}

⭐ Output ⭐

Type: int
  is_simd: false
  is_constructible: true
Type: int
  is_simd_mask: false
  is_constructible: true

Type: simd<float>
  is_simd: true
  is_constructible: true
Type: simd_mask<float>
  is_simd_mask: true
  is_constructible: true

Type: simd<bool>
  is_simd: true
  is_constructible: false
Type: simd_mask<bool>
  is_simd_mask: true
  is_constructible: false

see how simd<bool> is like "yes I'm a simd" but "no I'm not constructible"? that's the whole note. that's the drama. i told you so πŸ™ˆ

and that's the page!! πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά sourced from http://cppreference.com, dated 2024.06.10, and yeah that's the std::experi...s_simd_mask(3) footer β€” go forth and type-trait responsibly, bestie πŸ‘‘

πŸ“¦ where this page comes from