Operator Overloading - 2022.1 English

AI Engine Kernel Coding Best Practices Guide (UG1079)

Document ID
UG1079
Release Date
2022-05-25
Version
2022.1 English

The AI Engine API provides operator overloading for many operations. This feature can be used by including the header file aie_api/operators.hpp and using the aie::operators namespace.

The included operators are as follows.

Operator +
Addition operator. It is equivalent to aie::add.
Operator +=
Addition assignment operator.
Operator -
Negation operator. It is equivalent to aie::neg.
Operator -
Subtraction operator. It is equivalent to aie::sub.
Operator -=
Subtraction assignment operator.
Operator !=
Not equal to comparison operator. It is equivalent to aie::neq.
Operator <
Less than comparison operator. It is equivalent to aie::lt.
Operator <=
Less than or equal comparison operator. It is equivalent to aie::le.
Operator ==
Equal to comparison operator. It is equivalent to aie::eq.
Operator >
Greater than comparison operator. It is equivalent to aie::gt.
Operator >=
Greater than or equal comparison operator. It is equivalent to aie::ge.
Operator <<
Bitwise left shift operator. It is equivalent to aie::upshift.
Operator >>
Bitwise right shift operator. It is equivalent to aie::downshift.
Operator &
Bitwise AND operation. It is equivalent to aie::bit_and.
Operator ^
Bitwise XOR operation. It is equivalent to aie::bit_xor.
Operator |
Bitwise OR operation. It is equivalent to aie::bit_or.
Operator ~
Bitwise NEG operation. It is equivalent to aie::bit_not.

An example code of using operators is as follows.

#include <aie_api/aie.hpp>
#include <aie_api/aie_adf.hpp>
#include <aie_api/utils.hpp>
#include <aie_api/operators.hpp>
using namespace aie::operators;

void operator_overload_test(input_window<int32>* __restrict data,output_window<int32>* __restrict out){
  aie::vector<int32,8> va=window_readincr_v<8>(data);
  aie::vector<int32,8> vb=window_readincr_v<8>(data);
  aie::vector<int32,8> vadd=va+vb;
  aie::vector<int32,8> vsub=va-vb;
  aie::vector vneg=-vb;//negation of each element
  vadd+=vneg;
  aie::print(va,true,"va=");
  aie::print(vadd,true,"vadd=");
  //compare each element of the vector correspondingly
  auto msk_neq=(vadd!=va);
  //mask bit equals 1 if vector element not equal
  aie::print(msk_neq,true,"msk_neq=");

  aie::vector vnot_va=~va;//bit not
  aie::vector vones=va ^ vnot_va;//bit xor
  aie::print(vones,true,"vones=");
  //choose vadd if mask bit equals 0
  auto vout=aie::select(vadd,vones,msk_neq);
  aie::print(vout,true,"vout=");
  window_writeincr(out,vadd);
}

One possible output of the previous code is as follows.

va=9 10 11 12 13 14 15 16 
vadd=9 10 11 12 13 14 15 16 
msk_neq=0 0 0 0 0 0 0 0 
vones=-1 -1 -1 -1 -1 -1 -1 -1 
vout=9 10 11 12 13 14 15 16