std::experimental::clamp(3)
π NAME π
std::experimental::clamp β std::experimental::clamp
Ok bestie, welcome to the std::experimental::clamp(3) page from the C++ Standard Libary β yes, thatβs the manualβs spelling, not me π. This is the SIMD-flavored clamp, and itβs giving βkeep your lanes in lineβ energy π
.
β¨ Synopsis β¨
- Defined in header
<experimental/simd> - Tagged
(parallelism TS v2)β experimental girlie behavior, handle with care π§
template< class T, class Abi >
simd<T, Abi>
clamp( const simd<T, Abi>& v, const simd<T, Abi>& lo, const
simd<T, Abi>& hi );
π Parameters π
vβ the elements to clamp.lo, hiβ the boundaries to clampvto.
π¦ Return value π¦
The return value is the result of element-wise application of std::clamp(v[i], lo[i], hi[i]) for all i β [0, size()). Literally every lane gets the clamp treatment, no cap π«Ά.
πΈ Example πΈ
// Run this code
#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 π
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β clamps a value between a pair of boundary values(C++17)(function template). Thatβs the function-template one, and sheβs iconic, periodt π
Page tea β: this came from http://cppreference.com, dated 2024.06.10, and itβs the std::experimental::clamp(3) page. Mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms