Other Parts Discussed in Thread: TEST2
Hi Team,
There's an issue from the customer need your help:
Compiler version: TI v22.6.0.LTS
I need to clear a large array or structure. The previous solution was to directly use the memset function to clear, for example:
memset((void *)&test1,0,sizeof(test1));
However, I found that the compiler will use the RPT instruction to generate more efficient assembly code when it is within 256 words, and directly call the memset library function when it is more than 256 words. For example, I defined three float arrays for testing, with sizes of 127, 128 and 129 and clear them:
volatile float test1[127]; volatile float test2[128]; volatile float test3[129]; memset((void *)&test1,0,sizeof(test1)); //line 209 memset((void *)&test2,0,sizeof(test2)); //line 210 memset((void *)&test3,0,sizeof(test3)); //line 211
The generated assembly codes are:
.dwpsn file "../main.c",line 209,column 5,is_stmt,isa 0
MOVL XAR4,#||test1|| ; [CPU_ARAU] |209|
RPT #253
|| MOV *XAR4++,#0 ; [CPU_ALU] |209|
.dwpsn file "../main.c",line 210,column 5,is_stmt,isa 0
MOVL XAR4,#||test2|| ; [CPU_ARAU] |210|
RPT #255
|| MOV *XAR4++,#0 ; [CPU_ALU] |210|
.dwpsn file "../main.c",line 211,column 5,is_stmt,isa 0
MOV ACC,#258 ; [CPU_ALU] |211|
MOVB XAR5,#0 ; [CPU_ALU] |211|
MOVL XAR4,#||test3|| ; [CPU_ARAU] |211|
$C$DW$325 .dwtag DW_TAG_TI_branch
.dwattr $C$DW$325, DW_AT_low_pc(0x00)
.dwattr $C$DW$325, DW_AT_name("memset")
.dwattr $C$DW$325, DW_AT_TI_call
LCR #||memset|| ; [CPU_ALU] |211|
; call occurs [#||memset||] ; [] |211|
It can be observed that the clearing of test1 and test2 both uses the RPT instruction, which takes 257 and 259 clock cycles respectively, with an average of about 1 clock cycle per byte; while the clearing of test3 calls the memset library function, which takes 4914 clock cycles, taking an average of 19 clock cycles per byte. The C code and assembly code of the library file are as follows:


Even if I turn on level2 compiler optimization, the compiler's processing of memset remains unchanged. So is there any other better way to clear arrays or structures with more than 256 words?
Thanks & Regards,
Ben