◆ abs()

Value boost::simd::abs ( Value const &  x)

The function object abs computes the absolute value of its parameter.

If the result cannot be represented by the return type the return is equal to the input.

Header <boost/simd/function/abs.hpp>
Notes
  • Be aware that for signed integers the absolute value of Valmin is not representable in the input type and the result is undefined.

    To avoid this, you may use the saturated_ decorator (See Decorators.)

  • abs is a also a standard library function name and there possibly exists a C macro version which may be called instead of the boost simd version. To avoid this you may prefix abs using boost::simd::abs notation.
Decorators
  • std_ (See Decorators) for floating entries results in a call to std::abs, in this case the behavior is also undefined if the result cannot be represented by the return type.
  • saturated_ (See Decorators) garanties that saturated_(abs)(x) will never be strictly less than 0. In fact the only change if that for any signed type T saturated_(abs)(Valmin<T>()) will be Valmax<T>()) which is already true for boost::simd::abs with floating types but not for integral signed types.
See also
sqr_abs, sqr
Example:
#include <boost/simd/arithmetic.hpp>
#include <boost/simd/pack.hpp>
#include <iostream>
namespace bs = boost::simd;
using pack_ft = bs::pack <float, 4>;
using pack_it = bs::pack <std::int16_t,4>;
int main()
{
pack_ft pf = {-1.0f, 2.0f, -3.0f, -32768.0f};
pack_it pi = {-1, 2, -3, -32768 };
std::cout
<< "---- simd" << '\n'
<< "<- pf = " << pf << '\n'
<< "-> bs::abs(pf) = " << bs::abs(pf) << '\n'
<< "<- pi = " << pi << '\n'
<< "-> bs::abs(pi) = " << bs::abs(pi) << '\n'
<< "-> bs::saturated_(bs::abs(pi)) = " << bs::saturated_(bs::abs)(pi) << '\n'
<< "-> bs::std_(bs::abs)(pi) = " << bs::std_(bs::abs)(pi) << '\n';
float xf = -32768.0f;
std::int16_t xi = -32768;
std::cout
<< "---- scalar" << '\n'
<< "<- xf = " << xf << '\n'
<< "-> bs::abs(xf) = " << bs::abs(xf) << '\n'
<< "<- xi = " << xi << '\n'
<< "-> bs::abs(xi) = " << bs::abs(xi) << '\n'
<< "-> bs::saturated_(bs::abs(xi)) = " << bs::saturated_(bs::abs)(xi) << '\n'
<< "-> bs::std_(bs::abs)(xi) = " << bs::std_(bs::abs)(xi) << '\n';
return 0;
}
Possible output:
---- simd
<- pf = (-1, 2, -3, -32768)
-> bs::abs(pf) = (1, 2, 3, 32768)
<- pi = (-1, 2, -3, -32768)
-> bs::abs(pi) = (1, 2, 3, -32768)
-> bs::saturated_(bs::abs(pi)) = (1, 2, 3, 32767)
-> bs::std_(bs::abs)(pi) = (1, 2, 3, 32767)
---- scalar
<- xf = -32768
-> bs::abs(xf) = 32768
<- xi = -32768
-> bs::abs(xi) = -32768
-> bs::saturated_(bs::abs(xi)) = 32767
-> bs::std_(bs::abs)(xi) = 32767