operator+=,-=,*=,/=,%=,&=,|=,^=,<<=,>>= (std::simd::basic_vec)
friend constexpr basic_vec& operator+=( basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec& operator-=( basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec& operator*=( basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec& operator/=( basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec& operator%=( basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec& operator&=( basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec& operator|=( basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec& operator^=( basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec& operator<<=( basic_vec& lhs, const basic_vec& rhs ) noexcept;
friend constexpr basic_vec& operator>>=( basic_vec& lhs, const basic_vec& rhs ) noexcept;
|
(1) | (since C++26) |
friend constexpr basic_vec& operator<<=( basic_vec& lhs, /*simd-size-type*/ n ) noexcept;
friend constexpr basic_vec& operator>>=( basic_vec& lhs, /*simd-size-type*/ n ) noexcept;
|
(2) | (since C++26) |
Applies a compound assignment operator element-wise.
ith element of lhs with lhs[i] op rhs[i], where op is the corresponding binary 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.lhs by n. 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 | - | the basic_vec to modify
|
| rhs | - | the right operand |
| n | - | the shift count for all elements |
Return value
lhs
Notes
As with the binary operators, a right operand of another type, such as a scalar or a basic_vec with a different element type, is converted implicitly to basic_vec only if the conversion is value-preserving. Since the result is stored in lhs, the type of lhs decides: d += f compiles for a vec<double> d and a vec<float> f, while f += d and f += 1.0 do not. s += 1 does not compile for a vec<short> s, while s <<= 1 uses (2) and compiles.
These operators compile to the same instructions as the corresponding binary operators, such as vaddps for += on float elements with GCC on x86-64 with AVX2.
Example
#include <print>
#include <simd>
int main()
{
const std::simd::vec<int, 4> w([](int i) { return i + 1; });
std::simd::vec<int, 4> v = w;
std::println("v = {}", v);
v += 10;
std::println("v += 10: {}", v);
v *= w;
std::println("v *= w: {}", v);
v >>= 1; // the same shift count for all elements
std::println("v >>= 1: {}", v);
v <<= w; // a shift count for each element
std::println("v <<= w: {}", v);
(v %= 7) |= 8; // returns a reference to v
std::println("(v %= 7) |= 8: {}", v);
std::simd::vec<double, 4> d = 0.5;
std::simd::vec<float, 4> f = 0.25f;
d += f; // float to double is value-preserving
// f += d; // error: double to float is not
std::println("d += f: {}", d);
}
Output:
v = [1, 2, 3, 4]
v += 10: [11, 12, 13, 14]
v *= w: [11, 24, 39, 56]
v >>= 1: [5, 12, 19, 28]
v <<= w: [10, 48, 152, 448]
(v %= 7) |= 8: [11, 14, 13, 8]
d += f: [0.75, 0.75, 0.75, 0.75]
See also
| element-wise binary operators (function) | |
| element-wise increment and decrement (public member function) |