◆ clamp()

Value boost::simd::clamp ( Value const &  x,
Value const &  lo,
Value const &  hi 
)

This function object clamps x between lo and hi.

If x is less than hi, returns the larger of x and lo, otherwise returns the smaller of x and hi.

Header <boost/simd/function/clamp.hpp>
Decorators
  • pedantic_ ensures standard conformity for a Nan first parameter.
  • std_ is not available yet as the stlibc++ version is only defined since C++17.
Example:
#include <boost/simd/arithmetic.hpp>
#include <boost/simd/pack.hpp>
#include <boost/simd/function/enumerate.hpp>
#include <iostream>
namespace bs = boost::simd;
using pack_ft = bs::pack <float, 8>;
int main()
{
pack_ft pf = bs::enumerate<pack_ft>(-1, 1.2);
pack_ft qf = bs::enumerate<pack_ft>( 0, 0.5);
pack_ft rf = bs::enumerate<pack_ft>( 2, 0.7);
std::cout
<< "---- simd" << '\n'
<< " <- pf = " << pf << '\n'
<< " <- qf = " << qf << '\n'
<< " <- rf = " << rf << '\n'
<< " -> bs::clamp(pf, qf, rf) = " << bs::clamp(pf, qf, rf) << '\n';
float xf = 3.0f, yf = -3.0f, zf = 2.0f;
std::cout
<< "---- scalar" << '\n'
<< " <- xf = " << xf << '\n'
<< " <- yf = " << yf << '\n'
<< " <- zf = " << zf << '\n'
<< " -> bs::clamp(xf, yf, zf) = " << bs::clamp(xf, yf, zf) << '\n';
return 0;
}
Possible output:
---- simd
<- pf = (-1, 0.2, 1.4, 2.6, 3.8, 5, 6.2, 7.4)
<- qf = (0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5)
<- rf = (2, 2.7, 3.4, 4.1, 4.8, 5.5, 6.2, 6.9)
-> bs::clamp(pf, qf, rf) = (0, 0.5, 1.4, 2.6, 3.8, 5, 6.2, 6.9)
---- scalar
<- xf = 3
<- yf = -3
<- zf = 2
-> bs::clamp(xf, yf, zf) = 2