Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22002

I wonder if my code can be more optimized

$
0
0

I'm student and taking Microprocessor class.

Because of COVID-19, I had gotten a simple mid-term assignment writing inline assembly code of QuickSort.

Today, I got a score of this assignment and the score was almost the lowest because of "operation speed". (scored by rank, that mean my code works much slower than anyone else in class)

I tried to optimize my code before submission. So I have no idea why does my code work slowly.

TA said it is common for 85 ms to come out, but my code has an execution speed of 400ms.

I guess my code load a lot of unnecessary memory.

So my questions are:

  1. Do I load unnecessary memory? (ie. Is my code use ldr, str instruction in unnecessary way?)
  2. Does the number of memory loads affect the actual operation time?

Adding comment on "optimization", We only learned that the access speed of memory is slower then the register in the class, So I only tried to minimize the memory load and reduce unnecessary construction.

That mean someone can use "awesome tricks" to solve this problem but it is not general in my class.

Development environment:

  • GNU C Compiller(GCC) in Arm cortex M0 processor and nano Editor

Here is my code:

void QuickSort(int* arr, int size, int pivot, int end) {    int Lsize, Lstart, Lend, Rsize, Rstart, Rend;//Note that all variables are mean index of Array, excep for temp//so in inline assembly, We have to change them into byteasm(        /*check Condition*/"MOV r10, #2\n\t""CMP r1, r10\n\t""BLT FINISH\n\t"        /*Initialize*/"MOV r10, #4\n\t"           //r10 is temp value        //"LDR r1, %[size]\n\t"     //r1 is size of arr            //"LDR r2, %[pivot]\n\t"    //r2 is pivot's address(start)"MUL r2, r2, r10\n\t"        //"LDR r3, %[end]\n\t"      //r3 is end's address"MUL r3, r3, r10\n\t""MOV r4, r2\n\t"            //r4 is low's address"ADD r4, r4, #4\n\t"        //low = pivot +1  "MOV r5, r3\n\t"            //r5 is high's address         /*Initialize for loop*/"LDR r6, [r0, r4]\n\t"      //let r6 as value of low"LDR r7, [r0, r2]\n\t"      //let r7 as value of pivot"LDR r8, [r0, r5]\n\t"      //let r8 as value of high        /*Start Loop*/"B L6\n\t"                  //Check Condition First"LOOP2:\n\t"                //while(low <= high)"B L7\n\t"                  //check condition First"LOOP3:\n\t"                //while(arr[low]<=arr[pivot])"ADDS r4, r4, #4\n\t"       //low++"LDR r6, [r0,r4]\n\t"           //and update low's value"L7: CMP r6, r7\n\t""BLE LOOP3\n\t""B L8\n\t"                  //check condition First"LOOP4:\n\t"                //while(arr[high]>=arr[pivot])"SUBS r5, r5, #4\n\t"       //high--"LDR r8, [r0,r5]\n\t"       //and update high's value"L8: CMP r8, r7\n\t""BGE LOOP4\n\t""CMP r5, r2\n\t"            //if(high<pivot)"BGE L9\n\t""MOVS r5, r2\n\t"           //high = pivot"L9:\n\t""CMP r4, r5\n\t"            //if(low >= high)"BGE END\n\t"               //break LOOP2 "LDR r9, [r0, r4]\n\t"      //store r9 value of low"LDR r10, [r0, r5]\n\t"     //store r10 value of high"STR r9, [r0, r5]\n\t""STR r10, [r0, r4]\n\t""MOV r6, r10\n\t""MOV r8, r9\n\t""L6: CMP r4, r5\n\t"        //Compare whether low <= high"BLE LOOP2\n\t"             //If so, back to loop2"END: \n\t"                 //This is end of loop2           /*swap pivot and high*/"LDR r9, [r0, r5]\n\t"      //store r9 vlaue of high"LDR r10, [r0, r2]\n\t"     //store r10 value of pivot"STR r9, [r0, r2]\n\t""STR r10, [r0,r5]\n\t"        /*Set variables again*/        //Note that we have to divide them in 4        //Use r9 as quotient and r10 as zero"MOV r9, #0\n\t""MOV r10, #0\n\t""B D1\n\t""sLoop1:\n\t""ADD r9, r9, #1\n\t""SUB r2, r2, #4\n\t""D1: CMP r2, r10\n\t""BGT sLoop1\n\t""MOV r2, r9\n\t"      //pivot"MOV r9, #0\n\t""B D2\n\t""sLoop2:\n\t""ADD r9, r9, #1\n\t""SUB r3, r3, #4\n\t""D2: CMP r3, r10\n\t""BGT sLoop2\n\t""MOV r3, r9\n\t"       //end"MOV r9, #0\n\t""B D4\n\t""sLoop4:\n\t""ADD r9, r9, #1\n\t""SUB r5, r5, #4\n\t""D4: CMP r5, r10\n\t""BGT sLoop4\n\t""MOV r5, r9\n\t"   //high        /*Let r10 a temp of argument*/"SUB r10, r5, r2\n\t"   //Lsize = high-pivot"STR r10, %[Lsize]\n\t""STR r2, %[Lstart]\n\t" //Lstart = pivot"SUB r10, r5, #1\n\t"   //Lend=high-1"STR r10, %[Lend]\n\t""SUB r10, r3, r5\n\t"   //Rsize = end-high"STR r10, %[Rsize]\n\t""ADD r10, r5, #1\n\t"   //Rstart=high+1"STR r10, %[Rstart]\n\t""STR r3, %[Rend]\n\t"   //Rend = end    ://There is no output operands    :[arr] "r"(arr), [size] "m"(size) ,[pivot] "m"(pivot),[end] "m"(end), [Lsize] "m"(Lsize), [Lstart] "m"(Lstart), [Lend] "m"(Lend), [Rsize] "m"(Rsize), [Rstart] "m"(Rstart), [Rend] "m"(Rend)    :"r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10");

Viewing all articles
Browse latest Browse all 22002

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>