Optimizing ARM Cortex M3 code

前端 未结 3 1500
忘了有多久
忘了有多久 2021-01-07 05:14

I have a C Function which tries to copy a framebuffer to FSMC RAM.

The functions eats the frame rate of the game loop to 10FPS. I would like to know how to analyze

相关标签:
3条回答
  • 2021-01-07 05:21

    You should start by compiling the C code with speed optimizations enabled. The disassembled code you provide appears to be storing the i and j counters on the stack, which adds 3 load/store operations to the inner loop. You might also want to inline LCD_WriteData in the inner loop.

    On the other hand, if you are really writing to the LCD in the inner loop then the performance may be limited by that interface.

    0 讨论(0)
  • 2021-01-07 05:28

    Not exactly answering your question, but I see you aspire for fast execution of the loops.

    Here are some tips from the book: 'ARM System Developer's Guide: Designing and Optimizing System Software (The Morgan Kaufmann Series in Computer Architecture and Design)' http://www.amazon.com/ARM-System-Developers-Guide-Architecture/dp/1558608745

    Chapter 5 contains section named 'C looping structures'. Here is the summary of the section:

    Writing Loops Efficiently

    • Use loops that count down to zero. Then the compiler does not need to allocate a register to hold the termination value, and the comparison with zero is free.
    • Use unsigned loop counters by default and the continuation condition i!=0 rather than i>0. This will ensure that the loop overhead is only two instructions.
    • Use do-while loops rather than for loops when you know the loop will iterate at least once. This saves the compiler checking to see if the loop count is zero.
    • Unroll important loops to reduce the loop overhead. Do not overunroll. If the loop overhead is small as a proportion of the total, then unrolling will increase code size and hurt the performance of the cache.
    • Try to arrange that the number of elements in arrays are multiples of four or eight. You can then unroll loops easily by two, four, or eight times without worrying about the leftover array elements.

    Based on the summary, your inner loop might look as below.

    uinsigned int i = 240/4;  // Use unsigned loop counters by default
                              // and the continuation condition i!=0
    
    do
    {
        // Unroll important loops to reduce the loop overhead
        LCD_WriteData( (u16)frameBuffer[ (i--) + (j*fbWidth) ] );
        LCD_WriteData( (u16)frameBuffer[ (i--) + (j*fbWidth) ] );
        LCD_WriteData( (u16)frameBuffer[ (i--) + (j*fbWidth) ] );
        LCD_WriteData( (u16)frameBuffer[ (i--) + (j*fbWidth) ] );
    }
    while ( i != 0 )  // Use do-while loops rather than for
                      // loops when you know the loop will
                      // iterate at least once
    

    You might want to experiment also with 'pragmas', e.g. :

    #pragma Otime
    

    http://www.keil.com/support/man/docs/armcc/armcc_chr1359124989673.htm

    #pragma unroll(n)
    

    http://www.keil.com/support/man/docs/armcc/armcc_chr1359124992247.htm

    And as it is Cortex-M3 try to find out if MCU hardware gives you chance to arrange the code/data to take advantage of its Harvard architecture (I experienced 30% speed increase).

    see here my other answer

    Maybe not everything may be applicable in your application (filling a buffer in reverse order). I just wanted to draw your attention to the book and possible points for optimization.

    0 讨论(0)
  • 2021-01-07 05:29

    Just to purely reduce the number of looped operations, you could do something like so. I did make some assumptions which may not be accurate: You had a loop that went from i=0:239, and I am assuming that fbWidth is the same as 240. If this isn't true then the loop would have to be more complicated.

    void LCD_Flip()
    {
        u16 i,limit = fbHeight+fbWidth;
        // We will use a precalculated limit and one single loop
    
        LCD_SetCursor(0x00, 0x0000);
        LCD_WriteRegister(0x0050,0x00);//GRAM horizontal start position
        LCD_WriteRegister(0x0051,239);//GRAM horizontal end position
        LCD_WriteRegister(0x0052,0);//Vertical GRAM Start position
        LCD_WriteRegister(0x0053,319);//Vertical GRAM end position
        LCD_WriteIndex(0x0022);
    
        // Single loop from 0:limit-1 takes care of having to do an
        // x,y conversion each iteration.
        for(i=0;i<limit;j++)
        {
            u16 color = frameBuffer[i];
            LCD_WriteData(color);
        }
    }
    

    This strips out the two loops in favor of a single for loop with only one conditional test per iteration. On top of that, the indexing into frameBuffer is now linear, so we don't need to multiply out the width to go from x,y to linear storage. Your loop iterations won't have been reduced (i.e. it is still O(N) with N = height*width), but the number of instructions should have been reduced.

    As @Joe Hass noted in his answer, this may not actually help at all if you are really limited by the LCD interface. Depending on which STM32 you're using, the FSMC may not be particularly fast, and I can't imagine the LCD controller would be very fast either.

    0 讨论(0)
提交回复
热议问题