Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: optimizing loops question

Author: Omid David Tabibi

Date: 02:50:56 07/21/03

Go up one level in this thread


On July 20, 2003 at 23:30:04, Zach Wegner wrote:

>>switch (n-i) // sample for 0 >= n-i >= 7
>>{
>>case 7:  f(i--);
>>case 6:  f(i--);
>>case 5:  f(i--);
>>case 4:  f(i--);
>>case 3:  f(i--);
>>case 2:	 f(i--);
>>case 1:  f(i);
>>case 0:  break;
>>default: assume (0); // msc specific
>>}
>>
>>Gerd
>
>An uglier, yet less branchy way would be to use a Duff's device:
>
>int j=(n+7)/8;
>switch(n%8)
>{
>  case 0: do{ f(i++);
>  case 7:  f(i++);
>  case 6:  f(i++);
>  case 5:  f(i++);
>  case 4:  f(i++);
>  case 3:  f(i++);
>  case 2:  f(i++);
>  case 1:  f(i++);
>  }while(--j>0);
>}
>
>But you did say more readable...
>

An even better way:

int j = (n + 7) >> 3;
switch (n & 7)
{
  case 0: do{ f(i++);
  case 7:  f(i++);
  case 6:  f(i++);
  case 5:  f(i++);
  case 4:  f(i++);
  case 3:  f(i++);
  case 2:  f(i++);
  case 1:  f(i++);
  }while(--j>0);
}

Find the differences :)

>Zach



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.