# Vector Arithmetic Operations - 2022.2 English

## AI Engine Kernel and Graph Programming Guide (UG1079)

Document ID
UG1079
Release Date
2022-10-19
Version
2022.2 English

The AI Engine API supports basic arithmetic operations on two vectors, or on a scalar and a vector (operation on the scalar and each element of the vector). It also supports addition or subtraction of a scalar or a vector on an accumulator. Additionally, it supports multiply-accumulate (MAC). These operations include:

• `aie::mul`

Returns an accumulator with the element-wise multiplication of two vectors, or a value and all the elements of a vector.

• `aie::negmul`

Returns an accumulator with the negate of the element-wise multiplication of two vectors, or a value and all the elements of a vector.

• `aie::mac`

Multiply-add on vectors (or scalar) and accumulator.

• `aie::msc`

Multiply-sub on vectors (or scalar) and accumulator.

• `aie::add`

Returns a vector with the element-wise addition of two vectors, or a value and all the elements of a vector. Or add scalar or vector on accumulator.

• `aie::sub`

Returns a vector with the element-wise subtraction of two vectors, or a value and all the elements of a vector. Or subtract scalar or vector on accumulator.

The vectors and accumulator must have the same size and their types must be compatible. For example:

``````aie::vector<int32,8> va,vb;;
auto vm=aie::mul(va,vb);
auto vm2=aie::mul((int32)10,vb);
aie::vector<int32,8> vsub=aie::sub(va,vb);
aie::vector<int32,8> vsub2=aie::sub(va,(int32)10);//scalar and vector can switch placement

auto vsub_acc=aie::sub(vm,(int32)10);
auto vsub_acc2=aie::sub(vm,va);

auto vmac=aie::mac(vm,va,vb);
auto vmsc=aie::msc(vm,va,vb);
auto vmac2=aie::mac(vm,va,(int32)10);//scalar and vector can switch placement
auto vmsc2=aie::msc(vm,(int32)10,vb);//scalar and vector can switch placement``````

AI Engine API supports arithmetic operations on a vector or accumulation of element-wise square, including:

• `aie::abs`

Computes the absolute value for each element in the given vector.

• `aie::abs_square`

Computes the absolute square of each element in the given complex vector.

• `aie::conj`

Computes the conjugate for each element in the given vector of complex elements.

• `aie::neg`

For vectors with signed types, returns a vector whose elements are the same as in the given vector but with the sign flipped. If the input type is unsigned, the input vector is returned.

• `aie::mul_square`

Returns an accumulator of the requested type with the element-wise square of the input vector.

• `aie::mac_square`

Returns an accumulator with the addition of the given accumulator and the element-wise square of the input vector.

• `aie::msc_square`

Returns an accumulator with the subtraction of the given accumulator and the element-wise square of the input vector.

The vector and the accumulator must have the same size and their types must be compatible. For example:

``````aie::vector<int16,16> va;
aie::vector<cint16,16> ca;
aie::vector<int16,16> va_abs=aie::abs(va);
aie::vector<int32,16> ca_abs=aie::abs_square(ca);
aie::vector<cint16,16> ca_conj=aie::conj(ca);
aie::vector<int16,16> va_neg=aie::neg(va);
auto va_sq=aie::mul_square(va);

aie::vector<int32,8> vc,vd;
auto vm=aie::mul(vc,vd);
auto vmac3=aie::mac_square(vm,vc);//vmac3[i]=vm[i]+vc[i]*vc[i];
auto vmsc3=aie::msc_square(vm,vd);//vmsc3[i]=vm[i]-vd[i]*vd[i];``````

Operands can also be supported pre-multiplication operations. On some AI Engine architectures certain operations can be collapsed with the multiplication into a single instruction. For example:

``````aie::vector<cint16,16> ca,cb;
auto acc=aie::mul(aie::op_conj(ca),aie::op_conj(cb));``````

For details about pre-multiplication operations, see Pre-Multiplication Operations.