Stdexperimentalis Simd Stdexperimentalis Simd Mask In C++

The traits std::experimental::issimd and std::experimental::issimdmask are specified as part of the Parallelism Technical Specification Version 2 (Parallelism TS v2) and are found in the header. These characteristics are used to ascertain whether a type represents a specialization of specific SIMD-related class templates that the C++ standard library provides. In this article, we will discuss the std::experimental::issimd and std::experimental::issimdmask in C++ with their syntax, parameters, and examples.

What is the std::experimental::is_simd function?

The SIMD type of T is verified by this characteristic. Single instruction to multiple data (SIMD) types are useful for high-performance computing processes when a single instruction is given to several data elements simultaneously.

The member constant value is_simd returns a true value if T is a specialization of the simd class template. It returns false if T is any other type.

Syntax:

It has the following syntax:

Example

template< class T >
struct is_simd;

Parameters:

  • template< class T >: This template declaration indicates that the template is_simd accepts a single type parameter, T.
  • struct issimd;: A class template called issimd is declared with the expression struct issimd;. The structure of issimd is declared, but its implementation is not described at this time because no definition is given. It is simply a forward declaration.
  • What is the std::experimental::is_simd_mask?

A SIMD mask type check is performed by this feature on T. When a comparison is performed on an element in a SIMD vector, the result is represented by a bit in a SIMD mask type. It is the standard way of representing the result of SIMD comparison operations.

The issimdmask function returns a member constant value that is true if T is a specialization of the simd_mask class template. Otherwise, the value is false for all other types.

Syntax:

It has the following syntax:

Example

template< class T >
struct is_simd_mask;

Parameters:

  • template< class T >: This template declaration indicates that the template is_simd accepts a single type parameter, T.
  • struct issimdmask;: It declares the issimdmask template class or struct, which will function with type T. It would be elsewhere to define the real structure and behavior of issimdmask.
  • Example:

Let us take an example to illustrate the std::experimental::issimd and std::experimental::issimd_mask in C++.

Example

#include <experimental/simd>
#include <iostream>
#include <string_view>
#include <type_traits>
namespace stdx = std::experimental;
template<typename T>
void test_simd(std::string_view type_name)
{
    std::cout << std::boolalpha
              << "The type is: " << type_name << '\n'
              << "  is_simd: " << stdx::is_simd_v<T> << '\n'
              << "  is_constructible: " << std::is_constructible_v<T> << '\n'
              << "  is_trivially_copyable: " << std::is_trivially_copyable_v<T> << '\n'
              << "  alignment: " << alignof(T) << "\n\n";
}
template<typename T>
void test_simd_mask(std::string_view type_name)
{
    std::cout << std::boolalpha
              << "The type is: " << type_name << '\n'
              << "  is_simd_mask: " << stdx::is_simd_mask_v<T> << '\n'
              << "  is_constructible: " << std::is_constructible_v<T> << '\n'
              << "  is_trivially_copyable: " << std::is_trivially_copyable_v<T> << '\n'
              << "  alignment: " << alignof(T) << "\n\n";
}
int main() 
{
    test_simd<int>("int");
    test_simd_mask<int>("int");
    test_simd<float>("float");
    test_simd_mask<float>("float");
    // Testing std::experimental::simd with various types
    test_simd<stdx::simd<float>>("simd<float>");
    test_simd_mask<stdx::simd_mask<float>>("simd_mask<float>");
    test_simd<stdx::simd<double>>("simd<double>");
    test_simd_mask<stdx::simd_mask<double>>("simd_mask<double>");
    test_simd<stdx::simd<int>>("simd<int>");
    test_simd_mask<stdx::simd_mask<int>>("simd_mask<int>");
    // Testing with bool
    test_simd<stdx::simd<bool>>("simd<bool>");
    test_simd_mask<stdx::simd_mask<bool>>("simd_mask<bool>");
    // Testing with other integral types
    test_simd<stdx::simd<short>>("simd<short>");
    test_simd_mask<stdx::simd_mask<short>>("simd_mask<short>");
    test_simd<stdx::simd<long>>("simd<long>");
    test_simd_mask<stdx::simd_mask<long>>("simd_mask<long>");
    // Testing with non-SIMD compatible types
    test_simd<std::string>("std::string");
    test_simd_mask<std::string>("std::string");
    test_simd<void*>("void*");
    test_simd_mask<void*>("void*");
    return 0;
}

Output:

Output

The type is: int
  is_simd: false
  is_constructible: true
  is_trivially_copyable: true
  alignment: 4

The type is: int
  is_simd_mask: false
  is_constructible: true
  is_trivially_copyable: true
  alignment: 4

The type is: float
  is_simd: false
  is_constructible: true
  is_trivially_copyable: true
  alignment: 4

The type is: float
  is_simd_mask: false
  is_constructible: true
  is_trivially_copyable: true
  alignment: 4

The type is: simd<float>
  is_simd: true
  is_constructible: true
  is_trivially_copyable: true
  alignment: 16

The type is: simd_mask<float>
  is_simd_mask: true
  is_constructible: true
  is_trivially_copyable: true
  alignment: 16

The type is: simd<double>
  is_simd: true
  is_constructible: true
  is_trivially_copyable: true
  alignment: 16

The type is: simd_mask<double>
  is_simd_mask: true
  is_constructible: true
  is_trivially_copyable: true
  alignment: 16

The type is: simd<int>
  is_simd: true
  is_constructible: true
  is_trivially_copyable: true
  alignment: 16

The type is: simd_mask<int>
  is_simd_mask: true
  is_constructible: true
  is_trivially_copyable: true
  alignment: 16

The type is: simd<bool>
  is_simd: true
  is_constructible: false
  is_trivially_copyable: true
  alignment: 1

The type is: simd_mask<bool>
  is_simd_mask: true
  is_constructible: false
  is_trivially_copyable: true
  alignment: 1

The type is: simd<short>
  is_simd: true
  is_constructible: true
  is_trivially_copyable: true
  alignment: 16

The type is: simd_mask<short>
  is_simd_mask: true
  is_constructible: true
  is_trivially_copyable: true
  alignment: 16

The type is: simd<long>
  is_simd: true
  is_constructible: true
  is_trivially_copyable: true
  alignment: 16

The type is: simd_mask<long>
  is_simd_mask: true
  is_constructible: true
  is_trivially_copyable: true
  alignment: 16

The type is: std::string
  is_simd: false
  is_constructible: true
  is_trivially_copyable: false
  alignment: 8

The type is: std::string
  is_simd_mask: false
  is_constructible: true
  is_trivially_copyable: false
  alignment: 8

The type is: void*
  is_simd: false
  is_constructible: true
  is_trivially_copyable: true
  alignment: 8

The type is: void*
  is_simd_mask: false
  is_constructible: true
  is_trivially_copyable: true
  alignment: 8

Explanation:

In this example, the code checks that if different types are compatible with SIMD (Single Instruction, Multiple Data) operations using the std::experimental::simd and std::experimental::simdmask templates. It defines two functions, testsimd, and testsimdmask, that determine and print if a given type is a SIMD type or a SIMD mask type, as well as other traits like constructibility, trivial copyability, and alignment. The main function runs these tests on various fundamental, SIMD, and non-SIMD compatible types, like int, float, simd, and std::string, to show how these traits behave across different types.

Input Required

This code uses input(). Please provide values below: