SPRUJG0C December 2024 – October 2026 F29H850DM , F29H850DU , F29H850TU , F29H859TU-Q1 , F29P329SM-Q1 , F29P580DM , F29P589DM-Q1 , F29P589DU-Q1
Many operations in embedded systems involve consecutive data accesses (reads or writes) to memory. Since data buses on the F29x are 64-bit wide, the architecture allows 64-bit data reads and writes. However, most user code is limited to 32-bit data, and therefore accesses are limited to 32-bits. In some cases, especially array accesses, significant performance benefits can be achieved by re-writing the code to perform 64-bit accesses instead of 32-bit accesses. For example, the first code block below represents a simple memory buffer read operation, through 32-bit accesses.
uint32_t mem_read_16k_cn(uint32_t *src)
{
uint32_t i =0;
uint32_t x =0;
for (i=0;i<LEN_16K;i++)
{
x +=*src++;
}
return x;
}The next code block represents the identical operation, implemented using 64-bit accesses, which is twice as efficient.
uint32_t mem_read_16k_opt(uint32_t *src)
{
uint32_t i =0;
uint64_t *s2 = (uint64_t*) src;
uint32_t x =0;
uint32_t x2 =0;
for (i=0;i<LEN_16K>>1;i++)
{
uint64_t temp =*s2++;
x += (temp>>32);
x2 += (temp&0xFFFFFFFF);
}
return x+x2;
}In many cases, the compiler is able to provide this performance boost without explicitly rewriting the code, but by applying specific attributes to the underlying data. If an array is aligned, use __attribute__((aligned(val)) to indicate so to the compiler. For example, __attribute__((aligned(8)) on an array indicates 8-byte alignment, and allows the compiler to potentially load 64-bits at a time instead of 32-bits. This can lead to performance benefits for operations like matrix multiplication. If the array is global and is accessed directly in a function, then the alignment specification above is sufficient (see first code block below). On the other hand, if the aligned array is passed into a function as a pointer, then the __builtin_assume_aligned attribute needs to be applied to the pointer to inform the compiler about alignment of that object (see second code block below).
__attribute__((aligned(8))) float A[100];
__attribute__((aligned(8))) float B[100];
__attribute__((aligned(8))) float C[100];
void matrix_mpy(void)
{
int32_T i;
int32_T i_0;
int32_T i_1;
for (i_0 =0; i_0 <10; i_0++)
{
for (i =0; i <10; i++)
{
int32_T C_tmp, tmp;
C[10* i_0 + i] =0.0f;
for (i_1 =0; i_1 <10; i_1++)
{
C[10* i_0 + i] +=A[10* i_1 + i] *B[10* i_0 + i_1];
}
}
}void matrix_mpy_f32_4by4(float (*restrict Ma_f32) [4], float (*restrict Mb_f32)[4], float (*restrict Mc_f32)[4])
{
int32_t i,j,k;
// Use __builtin_assume_aligned to inform the compiler about alignment Ma_f32 = __builtin_assume_aligned(Ma_f32, 8);
Mb_f32 = __builtin_assume_aligned(Mb_f32, 8);
Mc_f32 = __builtin_assume_aligned(Mc_f32, 8);
..
}At present, the compiler is not able to discern alignment based on the physical address of an object i.e. just based on a location attribute.
This "vectorization" i.e. combining accesses of smaller data types to larger data types is also possible with 8-bit and 16-bit data, and is planned in a future release of the compiler.