E.V.E  0.1-beta

◆ convert

eve::convert = {}
inlineconstexpr

Callable object converting a value to another type.

Required header: #include <eve/function/convert.hpp>

Members Functions

Member Effect
operator() converts a value to a value of another type

template<value T, scalar_value Target>
auto operator()( T const& x, as_<Target> const& t) const noexcept;

Parameters

x: value to convert.

t: Type wrapper instance embedding the type to convert x to.

Return value

For a value x and any type Target the expression:

auto r = convert(x, as_<Target>{});
constexpr callable_convert_ convert
Callable object converting a value to another type.
Definition: convert.hpp:87

is semantically equivalent to:

Target r;
if constexpr( scalar_value<T> )
{
r = static_cast<Target>(x);
}
else if constexpr( simd_value<T> )
{
for(std::size_t i=;i<x.size();++i)
r[i] = static_cast<Target>(x[i]);
}
Specify that a type represents a scalar value.
Definition: vectorizable.hpp:70
Warning
Conversion operated by eve::convert follows the regular rules of C++ type conversion, including the cases leading to Undefined Behaviors.

Supported decorators

  • eve::saturated

    Required header: #include <eve/function/saturated/convert.hpp>

    The expression saturated(convert)(x,t) computes a saturated conversion of x to the type wrapped by t.

Example

See it live on Compiler Explorer

#include <eve/function/convert.hpp>
#include <eve/function/saturated/convert.hpp>
#include <eve/function/converter.hpp>
#include <eve/constant/valmax.hpp>
#include <eve/wide.hpp>
#include <iostream>
using int_16 = eve::as<std::int16_t>;
using int_64 = eve::as<std::int64_t>;
int main()
{
wide_ft pf = {-1.0f, 2.3f, 45000.7f, -64768.6f};
wide_it pi = {-1, 2, -3, eve::valmax(eve::as<std::int64_t>())};
std::cout << "---- simd" << '\n'
<< "<- pf = " << pf << '\n'
<< "-> convert(pf, int_64()_) = " << eve::convert(pf, int_64()) << '\n'
<< "-> saturated(convert)(pf, int_16()) = " << eve::saturated(eve::convert)(pf, int_16()) << '\n'
<< "-> convert(pf, int_16()) = = " << eve::convert(pf, int_16()) << '\n'
<< "<- pi = " << pi << '\n'
<< "-> convert(pi, as(eve::as<double>())) = " << eve::convert(pi, eve::as<double>()) << '\n';
double xf = -64768.4f;
std::int64_t xi = -64768;
std::cout << "---- scalar" << '\n'
<< "<- xf = " << xf << '\n'
<< "-> convert(xf, int_64()) = " << eve::convert(xf, int_64()) << '\n'
<< "<- xi = " << xi << '\n'
<< "-> convert(xi, eve::as<double>()) = " << eve::convert(xi, eve::as<double>()) << '\n';
return 0;
}
constexpr callable_valmax_ valmax
Callable object computing the greatest representable value.
Definition: valmax.hpp:53
constexpr callable_pi_ pi
Callable object computing the value.
Definition: pi.hpp:54
constexpr saturated_type const saturated
Higher-order Callable Object imbuing saturation semantic onto other Callable Objects.
Definition: saturated.hpp:68
Lightweight type-wrapper.
Definition: as.hpp:29
Wrapper for SIMD registers.
Definition: wide.hpp:65