◆ toint()

as_integer_t<Value> boost::simd::toint ( Value const &  x)

This function object converts its parameter to integer by truncation.

Header <boost/simd/function/toint.hpp>
Notes

toint cast a floating value to the signed integer value of the same bit size.

This is done by C casting for scalars and corresponding intrinsic in simd (if available).

Peculiarly, that implies that the behaviour of this function on invalid entries is not defined and quite unpredictable.

(For instance it is quite frequent that the test: toint(Inf<double>()) == toint(1.0/0.0) will return False whilst the test: Inf<double>() == 1.0/0.0 returns True !)

If you intend to use Nan and Inf entries, consider using saturated_(toint) instead or its alias ifix.

Decorators
  • saturated_ (See Decorators) as stated above ensures good behaviour on limiting values.
See also
touint, tofloat
Example:
#include <boost/simd/arithmetic.hpp>
#include <boost/simd/pack.hpp>
#include <boost/simd/constant/inf.hpp>
#include <iostream>
namespace bs = boost::simd;
using pack_ft = bs::pack <float, 4>;
int main()
{
pack_ft pf = {-1.0f, 2.6f, -3.4f, bs::Inf<float>()};
pack_it pi = { 1, 2, 3, bs::Inf<std::uint16_t>() };
std::cout
<< "---- simd" << '\n'
<< "<- pf = " << pf << '\n'
<< "-> bs::toint(pf) = " << bs::toint(pf) << '\n'
<< "-> saturated_(bs::toint)(pf) = " << bs::saturated_(bs::toint)(pf) << '\n'
<< "<- pi = " << pi << '\n'
<< "-> bs::toint(pi) = " << bs::toint(pi) << '\n'
<< "-> saturated_(bs::toint)(pi) = " << bs::saturated_(bs::toint)(pi) << '\n';
float xf = -2.5f;
std::uint16_t xi = 65535;
std::cout
<< "---- scalar" << '\n'
<< "<- xf = " << xf << '\n'
<< "-> bs::toint(xf) = " << bs::toint(xf) << '\n'
<< "-> saturated_(bs::toint)(xf) = " << bs::saturated_(bs::toint)(xf) << '\n'
<< "<- xi = " << xi << '\n'
<< "-> bs::toint(xi) = " << bs::toint(xi) << '\n'
<< "-> saturated_(bs::toint)(xi) = " << bs::saturated_(bs::toint)(xi) << '\n';
return 0;
}
Possible output:
icitte
---- simd
<- pf = (-1, 2.6, -3.4, inf)
-> bs::toint(pf) = (-1, 2, -3, -2147483648)
-> saturated_(bs::toint)(pf) = (-1, 2, -3, 2147483647)
<- pi = (1, 2, 3, 65535)
-> bs::toint(pi) = (1, 2, 3, -1)
-> saturated_(bs::toint)(pi) = (1, 2, 3, 32767)
---- scalar
<- xf = -2.5
-> bs::toint(xf) = -2
-> saturated_(bs::toint)(xf) = -2
<- xi = 65535
-> bs::toint(xi) = -1
-> saturated_(bs::toint)(xi) = 32767