Data-Parallel Types: Algorithms

The data-parallel types library has four special algorithms for SIMD vectors.

The four special algorithms are min, max, minmax, and clamp.

min, max, and minmax

The two algorithms min and max have in common that they each accept two SIMD vectors and return a SIMD vector. This contains the element-wise minimum or maximum of the input vectors. The minmax algorithm also takes two SIMD vectors and returns a pair of SIMD vectors. The first vector in the pair contains the element-wise minimum, the second the element-wise maximum of the input vectors.

The following example shows the three algorithms in action:

// minmax.cpp

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

namespace stdx = std::experimental;
 
void println(auto rem, auto const v) {
    std::cout << rem << ": ";
    for (std::size_t i = 0; i != v.size(); ++i)
        std::cout << std::setw(2) << v[i] << ' ';
    std::cout << '\n';
}

void printPairs(auto rem, auto const v1) {
    std::cout << rem << ": ";
    for (std::size_t i = 0; i != v1.first.size(); ++i)
        std::cout << '(' << v1.first[i] << ", " << v1.second[i] << ')' << ' ';
    std::cout << '\n';
}

int main() {

    stdx::fixed_size_simd<int, 8> a{[](int i) {
        static constexpr auto c = {10, 9, 8, 7, 6, 5, 4, 3};
        return c.begin()[i];
    }};
    println("a", a);
    
    stdx::fixed_size_simd<int, 8> b{[](int i) {
        static constexpr auto c = {3, 4, 5, 6, 7, 8, 9, 10,};
        return c.begin()[i];
    }};
    println("b", b);

    std::cout << '\n';

   auto minimum = stdx::min(a, b);
   println("minimum", minimum);

   auto maximum = stdx::max(a, b);
   println("maximum", maximum);

   /*
   auto minmax = stdx::minmax(a, b);
   printPairs("minmax", minmax);
   */

}

I use the SIMD vectors a and b as input vectors. These are initialized specially. To do this, I create an initialization list c in the lambda function that returns an iterator to it.
The following screenshot shows the output of the program:

You may be wondering why I commented out the use of the minmax algorithm. The reason is simple. I couldn’t compile the line stdx::minmax(a, b) with either the GCC or clang compilers.

clamp

std::datapar::clamp applies the std::clamp function to the SIMD vector element by element. Each element is clamped to a minimum and maximum limit.
The following program is based on an example from cppreference:

// clamp.cpp

#include <cstddef>
#include <cstdint>
#include <experimental/simd>
#include <iomanip>
#include <iostream>

namespace stdx = std::experimental;
 
void println(auto rem, auto const v) {
    std::cout << rem << ": ";
    for (std::size_t i = 0; i != v.size(); ++i)
        std::cout << std::setw(4) << v[i] << ' ';
    std::cout << '\n';
}
 
int main() {

    std::cout << "INT8_MIN: " << INT8_MIN << '\n';
    std::cout << "INT8_MAX: " << INT8_MAX << '\n';
    std::cout << "UINT8_MAX: " << UINT8_MAX << '\n';

    std::cout << '\n';

    stdx::fixed_size_simd<int, 8> a{[](int i) {
        static constexpr auto c = {-129, -128, -1, 0, 42, 127, 128, 255};
        return c.begin()[i];
    }};
    println("a", a);
 
    stdx::fixed_size_simd<int, 8> lo1{INT8_MIN};
    stdx::fixed_size_simd<int, 8> hi1{INT8_MAX};
    const auto b = stdx::clamp(a, lo1, hi1);
    println("b", b);
 
    stdx::fixed_size_simd<int, 8> lo2{0};
    stdx::fixed_size_simd<int, 8> hi2{UINT8_MAX};
    const auto c = stdx::clamp(a, lo2, hi2);
    println("c", c);

}

It is nice to see in the output of the SIMD vector b how the values of the SIMD vector a are mapped to the boundary values INT8_MIN and INT8_MAX. In SIMD vector c, on the other hand, the boundary values 0 and UINT8_MAX are used.

What’s Next?

Now it’s time for my second iteration through the new C++26 standard. I will focus primarily on the features that I did not cover in detail in my first iteration. I will start with contracts.

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery, Matt Godbolt, Honey Sukesan, bruce_lee_wayne, Silviu Ardelean, schnapper79, Seeker, and Sundareswaran Senthilvel.

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (open)
  • "Embedded Programming with Modern C++": (open)
  • "Generic Programming (Templates) with C++": (open)
  • "Clean Code: Best Practices for Modern C++": September 2025
  • Do you want to stay informed: Subscribe.

     

    Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS

    Modernes C++ GmbH

    Modernes C++ Mentoring (English)

    Do you want to stay informed about my mentoring programs? Subscribe Here

    Rainer Grimm
    Yalovastraße 20
    72108 Rottenburg

    Mobil: +49 15561 737372
    Mail: schulung@ModernesCpp.de
    Mentoring: www.ModernesCpp.org