The following code example shows some basic arithmetic operations being performed.
#include "types_standard.h"
void types_standard(din_A inA, din_B inB, din_C inC, din_D inD,
dout_1 *out1, dout_2 *out2, dout_3 *out3, dout_4 *out4
) {
// Basic arithmetic operations
*out1 = inA * inB;
*out2 = inB + inA;
*out3 = inC / inA;
*out4 = inD % inA;
}
The data types in the example above are defined in the header file types_standard.h shown in the following code example. They show how the
following types can be used:
- Standard signed types
- Unsigned types
- Exact-width integer types (with the inclusion of header file
stdint.h)#include <stdio.h> #include <stdint.h> #define N 9 typedef char din_A; typedef short din_B; typedef int din_C; typedef long long din_D; typedef int dout_1; typedef unsigned char dout_2; typedef int32_t dout_3; typedef int64_t dout_4; void types_standard(din_A inA,din_B inB,din_C inC,din_D inD,dout_1 *out1,dout_2 *out2,dout_3 *out3,dout_4 *out4);
These different types result in the following operator and port sizes after synthesis:
- The multiplier used to calculate result
out1is a 24-bit multiplier. An 8-bitchartype multiplied by a 16-bitshorttype requires a 24-bit multiplier. The result is sign-extended to 32-bit to match the output port width. - The adder used for
out2is 8-bit. Because the output is an 8-bitunsigned chartype, only the bottom 8-bits ofinB(a 16-bitshort) are added to 8-bitchartypeinA. - For output
out3(32-bit exact width type), 8-bitchartypeinAis sign-extended to 32-bit value and a 32-bit division operation is performed with the 32-bit (inttype)inCinput. - A 64-bit modulus operation is performed using the 64-bit
long longtypeinDand 8-bitchartypeinAsign-extended to 64-bit, to create a 64-bit output resultout4.
As the result of out1 indicates, Vitis HLS uses the smallest operator it can and extends the result to match
the required output bit-width. For result out2, even though one
of the inputs is 16-bit, an 8-bit adder can be used because only an 8-bit output is required. As
the results for out3 and out4
show, if all bits are required, a full sized operator is synthesized.