Namespaces
Variants

std::simd::clamp

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< class T, class Abi >
constexpr basic_vec<T, Abi> clamp( const basic_vec<T, Abi>& v,
                                   const basic_vec<T, Abi>& lo,
                                   const basic_vec<T, Abi>& hi );
(1) (since C++26)
namespace std {
    using simd::clamp;
}
(2) (since C++26)
1) Clamps each element of v to the range given by the corresponding elements of lo and hi: returns a std::simd::basic_vec whose element i is std::clamp(v[i], lo[i], hi[i]) for every i in [0, v.size()).
2) Makes (1) available as std::clamp too, in the same overload set as std::clamp for scalars.

The overload (1) participates in overload resolution only if T models totally_ordered, which excludes the std::complex element types.

If an element of lo is greater than the corresponding element of hi, the behavior is undefined.

Parameters

v - the basic_vec whose elements are clamped
lo, hi - the lower and upper bounds for each element

Return value

As described above.

Notes

All three arguments have the same basic_vec type, because T and Abi are deduced from each of them, so a scalar bound, as in simd::clamp(v, 0, 255), does not compile. A bound that is the same for every element is written as a basic_vec with that value in every element, such as V(255) for a basic_vec type V.

Because of (2), std::clamp(x, lo, hi) works both for scalars and for basic_vec, and code that is generic over both can call it that way. For a basic_vec, it returns a value, not a reference as std::clamp does for scalars.

As with std::clamp, floating-point elements must not be NaN: floating-point types model totally_ordered only for values other than NaN. For a NaN element of v, GCC returns the element of lo, while its std::clamp returns a NaN for a scalar NaN.

GCC checks the precondition on the bounds in constant evaluation and when _GLIBCXX_ASSERTIONS is defined.

GCC on x86-64 takes the minimum with hi and then the maximum with lo, with two instructions for each register:

  • with SSE2, pminub and pmaxub for std::uint8_t, while float and int elements are compared and blended with bitwise operations;
  • with SSE4.1, minps and maxps for float, and pminsd and pmaxsd for int;
  • with AVX2, the same instructions with the v prefix, such as vminps and vmaxps;
  • with AVX-512, vminps and vmaxps for float, while integer elements are compared into a mask register and blended (vpcmpd, vmovdqa32, and vpblendmd for int).
Feature-test macro Value Std Feature
__cpp_lib_simd 202411L (C++26) simd::clamp

Example

#include <algorithm>
#include <cstdint>
#include <print>
#include <simd>

namespace simd = std::simd;
using V = simd::vec<int, 8>;

// this works for float and vec<float>
template<class T>
T saturate(T x)
{
    return std::clamp(x, T(0.f), T(1.f));
}

int main()
{
    // brighten 8 pixels, keeping the values in [0, 255]
    V pixels([](int i) { return i * 36; });
    V brighter = simd::clamp(pixels + 40, V(0), V(255));
    simd::vec<std::uint8_t, 8> bytes(brighter); // explicit: int to uint8_t
    std::println("pixels   = {}", pixels);
    std::println("brighter = {}", bytes);

    // the bounds can differ from element to element
    V lo([](int i) { return i * 30; });
    std::println("lo       = {}", lo);
    std::println("clamp(pixels, lo, lo + 20) = {}",
                 simd::clamp(pixels, lo, lo + 20));

    simd::vec<float, 4> f([](int i) { return i * 0.75f - 0.5f; });
    std::println("saturate({}) = {}", f, saturate(f));
    std::println("saturate(1.5f) = {}", saturate(1.5f));
}

Output:

pixels   = [0, 36, 72, 108, 144, 180, 216, 252]
brighter = [40, 76, 112, 148, 184, 220, 255, 255]
lo       = [0, 30, 60, 90, 120, 150, 180, 210]
clamp(pixels, lo, lo + 20) = [0, 36, 72, 108, 140, 170, 200, 230]
saturate([-0.5, 0.25, 1, 1.75]) = [0, 0.25, 1, 1]
saturate(1.5f) = 1

See also

(C++26)(C++26)(C++26)
element-wise min/max operations for basic_vec
(function template) [edit]
element-wise selection using conditional operator
(function template) [edit]
(C++17)
clamps a value between a pair of boundary values
(function template & algorithm function object)[edit]