Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My question is can we avoid the if conditions?

I have a code condition such as following

for(int i=0;i<Number;i++)
{     
     int* pIn = pInputArr[i];
     int* pOut = pOutputArr[i];
      
     for(int Input_number =0;Input_number<100;Input_number++)
         {
            Some_fun(pIn,pOut );
            if (Input_number % 2 == 0)
            {
               pIn = pOutputArr[i];
               pOut = pInputArr[i];
            }
            else
           {
               pOut =  pOutputArr[i];
               pIn = pInputArr[i];
           }
      }
 }    

I wanted to replace it with a more efficient way in embedded programming since I was told that branch operations are costly in embedded programming. Is there a cleaner way to achieve this using bit operations and without the if conditions?. Also without using any built-in functions such as swap and others.

Based on the odd and even condition I am swapping the role of the buffers that are being as arguments in the Some_func. I checked similar queries in several posts but didn't find them useful. Any suggestions will be highly appreciated.

like image 559
Saurav Rai Avatar asked Nov 28 '25 03:11

Saurav Rai


1 Answers

You don't need to check the evenness if you want to alternate – just swap.

for(int Input_number =0;Input_number<3;Input_number++)
{
    Some_fun(pIn, pOut);
    std::swap(pIn, pOut);
}
like image 90
molbdnilo Avatar answered Nov 30 '25 17:11

molbdnilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!