Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about optimizing code

Author: Reinhard Scharnagl

Date: 21:56:37 07/06/03

Go up one level in this thread


On July 06, 2003 at 23:25:00, Uri Blass wrote:

>In movei I have a number between 0 and 255 and I want to do some functions for
>every 1 that it has in its binary representation(number of functions to
>implement is the same as the number of 1's in the number and there is a
>different function based on the value of the 1)
>
>I do not like to call functions because functions use local varaibles and
>generating a copy of the local varaibles only to forget them may take computer
>time and I also do not use the functions more than once.
>
>I thought to use switch(see the code later in this post) but the problem is that
>switch continue to do everything without break and I want to have switch that
>does not continue to do everything but does not quit and simply forget about the
>fact that I called switch.
>
>Here is the type of the code that I plan to have now.
>I do not like all the if (number&2) or if (number&4) and the question is if I
>can do it faster.

How about:

_work:
switch (number) {
case [all numbers matching 0x01]:
  /* do list of 0-commands */;
  if ((number &= ~0x01) != 0) goto _work;
  break;
case [all numbers left matching 0x02]:
  /* do list of 1-commands */;
  if ((number &= ~0x02) != 0) goto _work;
  break;
case [all numbers left matching 0x04]:
  /* do list of 2-commands */;
  if ((number &= ~0x04) != 0) goto _work;
  break;
case [all numbers left matching 0x08]:
  /* do list of 3-commands */;
  if ((number &= ~0x08) != 0) goto _work;
  break;
case 0x10: case 0x30: case 0x50: case 0x70:
case 0x90: case 0xB0: case 0xD0: case 0xF0:
  /* do list of 4-commands */;
  if ((number &= ~0x10) != 0) goto _work;
  break;
case 0x20: case 0x60: case 0xA0: case 0xE0:
  /* do list of 5-commands */;
  if ((number &= ~0x20) != 0) goto _work;
  break;
case 0x40: case 0xc0:
  /* do list of 6-commands */;
  if ((number &= ~0x40) == 0) break;
case 0x80:
  /* do list of 7-commands */;
  break;
}

Or alternative by doubling code:

_work:
switch (number) {
case 0x01:
  /* do list of 0-commands */;
  break;
case [all numbers left matching 0x01 except 0x01]:
  /* do list of 0-commands */;
  number &= ~0x01; goto _work;

case 0x02:
  /* do list of 1-commands */;
  break;
case [all numbers left matching 0x02 except 0x02]:
  /* do list of 1-commands */;
  number &= ~0x02; goto _work;

case 0x04:
  /* do list of 2-commands */;
  break;
case [all numbers left matching 0x04 except 0x04]:
  /* do list of 2-commands */;
  number &= ~0x04; goto _work;

case 0x08:
  /* do list of 3-commands */;
  break;
case [all numbers left matching 0x08 except 0x08]:
  /* do list of 3-commands */;
  number &= ~0x08; goto _work;

case 0x10:
  /* do list of 4-commands */;
  break;
case 0x30: case 0x50: case 0x70:
case 0x90: case 0xB0: case 0xD0: case 0xF0:
  /* do list of 4-commands */;
  number &= ~0x10; goto _work;

case 0x20:
  /* do list of 5-commands */;
  break;
case 0x60: case 0xA0: case 0xE0:
  /* do list of 5-commands */;
  number &= ~0x20; goto _work;

case 0x40:
  /* do list of 6-commands */;
  break;
case 0xc0:
  /* do list of 6-commands */;

case 0x80:
  /* do list of 7-commands */;
  break;
}

Regards, Reinhard



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.