Conversion

convert

template<typename R, size_t N, RoundingMode M = RoundingMode::ANY, typename V>
inline vector<R, extent<N>> kernel_float::convert(const V &input, extent<N> new_size = {})

Cast the values of the given input vector to type R and then broadcast the result to the given size N.

Example

int a = 5;
vec<float, 3> x = convert<float, 3>(a);  // returns [5.0f, 5.0f, 5.0f]

float b = 5.0f;
vec<float, 3> x = convert<float, 3>(b);  // returns [5.0f, 5.0f, 5.0f]

vec<int, 3> c = {1, 2, 3};
vec<float, 3> x = convert<float, 3>(c);  // returns [1.0f, 2.0f, 3.0f]

cast

template<typename R, RoundingMode Mode = RoundingMode::ANY, typename V>
inline vector<R, vector_extent_type<V>> kernel_float::cast(const V &input)

Cast the elements of the given vector input to a different type R.

This function casts each element of the input vector to a different data type specified by template parameter R.

Optionally, the rounding mode can be set using the Mode template parameter. The default mode is ANY, which uses the fastest rounding mode available.

Example

vec<float, 4> input {1.2f, 2.7f, 3.5f, 4.9f};
auto casted = cast<int>(input); // [1, 2, 3, 4]

cast_to

template<typename T, RoundingMode M = RoundingMode::ANY>
inline AssignConversionProxy<T, M> kernel_float::cast_to(T &input)

Takes a vector reference and gives back a helper object. This object allows you to assign a vector of a different type to another vector while perofrming implicit type converion.

For example, if x = expression; does not compile because x and expression are different vector types, you can use cast_to(x) = expression; to make it work.

Example

vec<float, 2> x;
vec<double, 2> y = {1.0, 2.0};
cast_to(x) = y;  // Normally, `x = y;` would give an error, but `cast_to` fixes that.

broadcast

template<size_t N, typename V>
inline vector<vector_value_type<V>, extent<N>> kernel_float::broadcast(const V &input, extent<N> new_size = {})

Takes the given vector input and extends its size to a length of N. This is only valid if the size of input is 1 or N.

Example

vec<float, 1> a = {1.0f};
vec<float, 5> x = broadcast<5>(a);  // Returns [1.0f, 1.0f, 1.0f, 1.0f, 1.0f]

vec<float, 5> b = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
vec<float, 5> y = broadcast<5>(b);  // Returns [1.0f, 2.0f, 3.0f, 4.0f, 5.0f]