# std::experimental::clamp

> C++ Atlas: `cppatlas.com/w/cpp/experimental/simd/clamp` | Source: https://en.cppreference.com/w/cpp/experimental/simd/clamp | Domain: Technical specifications | Standards: C++17

The result of element-wise application of std::clamp(v[i], lo[i], hi[i]) for all i ∈ [ 0 , size()).

## Official Specification Summary (cppreference)

```cpp
// Defined in header <experimental/simd>

template< class T, class Abi >
simd<T, Abi>
clamp( const simd<T, Abi>& v, const simd<T, Abi>& lo, const simd<T, Abi>& hi ); // (parallelism TS v2)
```

### Parameters

- `v` - the elements to clamp
- `lo, hi` - the boundaries to clamp `v` to

### Return value

The result of element-wise application of `std::clamp(v[i], lo[i], hi[i])` for all i ∈ `[​0​,` `size())`.

### Example

```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()
{
    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);
}
```

Output:

```text
a: -129 -128   -1    0   42  127  128  255
b: -128 -128   -1    0   42  127  127  127
c:    0    0    0    0   42  127  128  255
```

### See also

- [`clamp`](https://en.cppreference.com/w/cpp/algorithm/clamp) (C++17): clamps a value between a pair of boundary values (function template)

---
Attribution: content from https://en.cppreference.com/w/cpp/experimental/simd/clamp (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.