◆ average()

Value boost::simd::average ( Value const &  x,
Value const &  y 
)

This function object computes the arithmetic mean of its parameters.

Header <boost/simd/abs.hpp>
Notes

Using average(x, y) for floating entries is similar to (x+y)/2

for integer types, it returns a rounded value at a distance guaranteed to be less than or equal to 0.5 of the average floating value, but may differ by unity from the truncation given by (x+y)/2.

Note:
This function does not overflow.
See also
meanof
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, 32700.0f};
pack_ft qf = { 2.0f, -1.0f, -3.0f, 32721 };
pack_it pi = { 1, -2, -2, 32700 };
pack_it qi = { 2, -1, -3, 32721 };
std::cout
<< "---- simd" << '\n'
<< " <- pf = " << pf << '\n'
<< " <- qf = " << qf << '\n'
<< " -> bs::average(pf, qf) = " << bs::average(pf, qf) << '\n'
<< " <- pi = " << pi << '\n'
<< " <- qi = " << qi << '\n'
<< " -> bs::average(pi, qi) = " << bs::average(pi, qi) << '\n';
float xf = 32767.0f, yf = -32000.0f;
std::int16_t xi = 32767, yi = -32000;
std::cout
<< "---- scalar" << '\n'
<< " <- xf = " << xf << '\n'
<< "<- ,yf = " << yf << '\n'
<< " -> bs::average(xf, yf) = " << bs::average(xf, yf) << '\n'
<< " <- xi = " << xi << '\n'
<< " <- yi = " << yi << '\n'
<< " -> bs::average(xi, yi) = " << bs::average(xi, yi) << '\n';
return 0;
}
Possible output:
---- simd
<- pf = (1, -2, -3, 32700)
<- qf = (2, -1, -3, 32721)
-> bs::average(pf, qf) = (1.5, -1.5, -3, 32710.5)
<- pi = (1, -2, -2, 32700)
<- qi = (2, -1, -3, 32721)
-> bs::average(pi, qi) = (1, -2, -3, 32710)
---- scalar
xf = 32767
,yf = -32000
-> bs::average(xf, yf) = 383.5
xi = 32767
yi = -32000
-> bs::average(xi, yi) = 383