Casting and Datatype Conversion - Casting and Datatype Conversion - 2022.1 English - UG1079

AI Engine Kernel Coding Best Practices Guide (UG1079)

Document_ID
UG1079
Release_Date
2022-05-25
Version
2022.1 English

Casting functions (aie::vector_cast<DstT>(const Vec& v) and aie::vector.cast_to<DstT>()) allow value casting between vector types with the same size in bits. Accumulator vector types have the casting function aie::accum.cast_to<DstT>(). Generally, using the smallest data type possible will reduce register spillage and improve performance. For example, if a 48-bit accumulator (acc48) can meet the design requirements then it is preferable to use that instead of a larger 80-bit accumulator (acc80).

Note: The acc80 vector data type occupies two neighboring 48-bit lanes.
aie::vector<int16,8> iv=window_readincr_v<8>(data0);
aie::vector<cint16,4> cv=iv.cast_to<cint16>();
aie::vector<cint16,4> cv2=aie::vector_cast<cint16>(iv);
aie::accum<cacc48,4> acc=aie::mul(cv,cv2);
aie::accum<acc80,4> acc2=acc.cast_to<acc80>();

Standard C++ casts can be also used. But the recommended ways of reading vectors from a buffer are as follows.

  • Use aie::load_v and increment the scalar pointer by the number of elements in the vector.
  • Using vector iterators.

Additional details about aie::load_v and iterators are covered in the following sections.

int16 coeff_buffer[16];
aie::vector<int32,8> coeff=aie::load_v<8>((int32*)coeff_buffer);//cast to int32 and load
auto it = aie::begin_vector<8>(coeff_buffer);//create vector<int16,8> iterator 
aie::vector<int16,8> vec0=*it++;//read first vector<int16,8>
aie::vector<int16,8> vec1=*it;//read second vector<int16,8>

Hardware support is built-in for floating-point to fixed-point (to_fixed()) and fixed-point to floating-point (to_float()) conversions. For example, the fixed-point square root, inverse square root, and inverse (reciprocal) are implemented with floating-point precision and the to_float() and to_fixed() conversions are used before and after the function.

Only scalar data types are supported by the square root and inverse functions in AI Engine hardware but the API also accepts vectors for those operations.

Note: The AI Engine floating-point is not completely compliant with the IEEE standards. For more information about the exceptions, see Versal ACAP AI Engine Architecture Manual (AM009).

The conversion functions (to_float() and to_fixed()) can be handled by either the vector or scalar engines depending on the function called.

int a=48;
float f1=aie::to_float(a); //f1=48.0
float f2=aie::to_float(a,/*position of input decimal point*/2); //f=12.0
aie::vector<cint32,8> iv_c; 
aie::vector<cfloat,8> fv_c=aie::to_float(iv_c);

int b1=aie::to_fixed(f1);//b1=48
int b2=aie::to_fixed(f,/*position of output decimal point*/2);//b2=192
aie::vector<float,32> fv;
aie::vector<int32,32> iv=aie::to_fixed(fv,/*position of output decimal point*/2);
Note: For to_fixed(), there are two types of implementations, safe (default) and fast implementations, with the safe implementation offering a more strict data type check. You can use --fastmath option (default is false) of aiecompiler to make to_fixed() choose the fast implementation.
Note: aiecompiler provides another two options to enable fast implementation of floating point scalar operations:
--fast-floats
Enables fast implementation for linear floating-point scalar operations such as add, sub, mul, and compare. The default is true.
--fast-nonlinearfloats
Enables fast implementation for non-linear floating-point scalar operations such as, sine/cosine, sqrt, and inv. The default is false.

Data can be moved from vector to accumulator using aie::accum.from_vector() or from accumulator to vector using aie::accum.to_vector<DstT>() with shifting and rounding as shown in the following example.

aie::vector<int16,16> v;
aie::accum<acc32,16> acc; 
acc.from_vector(v, 0);

aie::accum<acc32,16> acc2;
aie::vector<int16,16> v2; 
v2 = acc2.to_vector<int16>(15);