operator+,-,*,/,%,&,|,^,<<,>> (std::simd::basic_vec)
friend constexpr basic_vec operator+( const basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec operator-( const basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec operator*( const basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec operator/( const basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec operator%( const basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec operator&( const basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec operator|( const basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec operator^( const basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec operator<<( const basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec operator>>( const basic_vec& lhs, const basic_vec& rhs ) noexcept;
|
(1) | (since C++26) |
friend constexpr basic_vec operator<<( const basic_vec& v, /*simd-size-type*/ n ) noexcept;
friend constexpr basic_vec operator>>( const basic_vec& v, /*simd-size-type*/ n ) noexcept;
|
(2) | (since C++26) |
Applies a binary operator element-wise.
basic_vec whose ith element is lhs[i] op rhs[i], where op is the operator, converted to T. Each operator participates in overload resolution only if a op b is valid for two values a and b of type T.basic_vec whose ith element is v[i] op n, converted to T: every element is shifted by the same amount. Each operator participates in overload resolution only if a op n is valid for a value a of type T./*simd-size-type*/ is the signed integer type of size().
These functions are not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::simd::basic_vec<T, Abi> is an associated class of the arguments.
If an operation on an element has undefined behavior for scalars, such as signed integer overflow, division by zero, or a shift by a negative amount or by at least the number of bits of the promoted type, the behavior is undefined.
Parameters
| lhs, rhs, v | - | the operands |
| n | - | the shift count for all elements |
Return value
As described above.
Notes
Since both operands of (1) have the type basic_vec, an operand of another type, such as a scalar or a basic_vec with a different element type, is converted implicitly by a constructor. The constructors are implicit only for value-preserving conversions, so the operand types must be chosen accordingly:
f * 2with avec<float>fdoes not compile, becauseinttofloatis not value-preserving;f * 2.forf * std::cw<2>work.s + 1with avec<short>sdoes not compile, becauseinttoshortis not value-preserving;s + short(1)ors + std::cw<1>work.s << 1compiles, because it uses (2).- The sum of a
vec<float>and avec<double>is avec<double>, while the sum of avec<float>and avec<int>does not compile. Convert one operand explicitly, such asf + simd::vec<float, 4>(i)for avec<float, 4>fand avec<int, 4>i.
Unlike for scalars, the elements are not promoted: the result of an operation on two vec<std::uint8_t> is a vec<std::uint8_t>, and the result of each element wraps around.
Most of these operators compile to one instruction. For example, GCC on x86-64 with AVX2 compiles float + to vaddps, int * to vpmulld, & to vpand, (1) << to vpsllvd, (2) << to vpslld, and >> by a constant to vpsrad. x86 has no instructions for some operations, so they take more: 8-bit * uses 16-bit multiplication (vpmullw) and shuffles, int / converts to double and uses vdivpd, and int % uses a scalar idiv for each element.
Example
#include <cstdint>
#include <print>
#include <simd>
#include <utility>
int main()
{
std::simd::vec<int, 4> a([](int i) { return i + 1; });
std::simd::vec<int, 4> b = 10;
std::println("a = {}, b = {}", a, b);
std::println("a + b = {}", a + b);
std::println("b / a = {}", b / a);
std::println("b % a = {}", b % a);
std::println("b | a = {}", b | a);
std::println("a << a = {}", a << a); // a shift count for each element
std::println("a << 2 = {}", a << 2); // the same shift count for all elements
std::simd::vec<float, 4> f = 0.5f;
std::println("f * 2.f = {}", f * 2.f);
std::println("f * std::cw<2> = {}", f * std::cw<2>);
// f * 2; // error: int to float is not value-preserving
// f + a; // error: the same for vec<int> to vec<float>
std::simd::vec<double, 4> d = 0.25;
std::println("f + d = {}", f + d); // vec<double>
std::simd::vec<std::uint8_t, 4> u = std::cw<200>;
std::println("u + u = {}", u + u); // no promotion to int: wraps around
// u + 1; // error: int to uint8_t is not value-preserving
std::println("u + std::cw<1> = {}", u + std::cw<1>);
}
Output:
a = [1, 2, 3, 4], b = [10, 10, 10, 10]
a + b = [11, 12, 13, 14]
b / a = [10, 5, 3, 2]
b % a = [0, 0, 1, 2]
b | a = [11, 10, 11, 14]
a << a = [2, 8, 24, 64]
a << 2 = [4, 8, 12, 16]
f * 2.f = [1, 1, 1, 1]
f * std::cw<2> = [1, 1, 1, 1]
f + d = [0.75, 0.75, 0.75, 0.75]
u + u = [144, 144, 144, 144]
u + std::cw<1> = [201, 201, 201, 201]
See also
| element-wise compound assignment (function) | |
| element-wise unary operators (public member function) | |
element-wise comparison, returning a basic_mask (function) |