Vector Arithmetic Operations - 2021.2 English

AI Engine Kernel Coding Best Practices Guide (UG1079)

Document ID
UG1079
Release Date
2021-11-10
Version
2021.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 accumulation on multiplication. 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=window_readincr_v<8>(data1);
aie::vector<int32,8> vb=window_readincr_v<8>(data2);
aie::accum<acc80,8> vm=aie::mul(va,vb);
aie::accum<acc80,8> vm2=aie::mul((int32)10,vb);
aie::vector<int32,8> vsub=aie::sub(va,vb);
aie::vector<int32,8> vadd=aie::add(va,vb);
aie::vector<int32,8> vsub2=aie::sub(va,(int32)10);//scalar and vector can switch placement
aie::vector<int32,8> vadd2=aie::add((int32)10,va);//scalar and vector can switch placement

aie::accum<acc80,8> vsub_acc=aie::sub(vm,(int32)10);
aie::accum<acc80,8> vsub_acc2=aie::sub(vm,va);
aie::accum<acc80,8> vadd_acc=aie::add(vm,(int32)10);
aie::accum<acc80,8> vadd_acc2=aie::add(vm,vb);

aie::accum<acc80,8> vmac=aie::mac(vm,va,vb);
aie::accum<acc80,8> vmsc=aie::msc(vm,va,vb);
aie::accum<acc80,8> vmac2=aie::mac(vm,va,(int32)10);//scalar and vector can switch placement
aie::accum<acc80,8> 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<cfloat,16> cf;
aie::vector<int16,16> va_abs=aie::abs(va);
aie::vector<int32,16> ca_abs=aie::abs_square(ca);
aie::vector<float,16> cf_abs=aie::abs_square(cf);
aie::vector<cint16,16> ca_conj=aie::conj(ca);
aie::vector<int16,16> va_neg=aie::neg(va);
aie::accum<acc48,16> va_sq=aie::mul_square(va);

aie::vector<int32,8> vc;
aie::vector<int32,8> vd;
aie::accum<acc80,8> vm;
aie::accum<acc80,8> vmac3=aie::mac_square(vm,vc);//vmac3[i]=vm[i]+vc[i]*vc[i];
aie::accum<acc80,8> 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;
aie::accum<cacc48,16> acc=aie::mul(aie::op_conj(ca),aie::op_conj(cb));

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