Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Sign of Integer Operands: int vs unsigned int

Author: Dieter Buerssner

Date: 13:24:47 09/27/03

Go up one level in this thread


On September 27, 2003 at 13:34:57, Omid David Tabibi wrote:

>Switching "assume no aliasing" on in MSVC 6 resulted in some considerable
>speedup for me.

Perhaps, you are using too many references? I remember, that some time ago you
gave one example with a function with one output parameter, where you used a
reference (the alternative would be returning). I read the suggestions in the
other post about references vs. pointers. They also may be slightly misleading.
It may depend very much on the compiler. Also on the way, one writes code.

void foo(int *p [, ...])
{
  *p = ...
  /* interleaving stuff */
  *p += ...
}

vs.

void foo(int *p [,...])
{
  int r = ...
  /* interleaving stuff */
  r += ...
  *p = r;
}

The later will probably be easier to optimize. Actually, using the former way in
reference style might be more likely than in pointer style. At least I would
feel uncomfortable to see various *p in that function, and would "cache" it by a
local var. With references those "*" won't hit the eye.

Using a non alias option, will restrict the coder to use a language, can yield
in subtle bugs.

One example, a bignum package may have an array of 16 unsigned chars as the type
(it probably will use a wider data type than unsigned char, but the argument is
the same).

void shift_right_8(unsigned char *a, unsigned char *result)
{
  int i;
  /* By purpose, code it in a way, that a and result can be the same,
     to give the caller a more flexible way to use it */
  for (i=14; i>=0; i--)
    result[i+1] = a[i];
  result[0] = 0;
}

With no aliasing, the compiler can reorder everything, for example, he could
rearrange this to

  result[0] = 0;
  for (i=0; i<=14; i++)
    result[i+1] = a[i];


called with

  unsigned char a[8], b[8];
  shift_right_8(a, b); /* OK */
  shift_right_8(b, b); /* Can fail, but correct in Standard C */

Of course, one could use memmove ...

Regards,
Dieter



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.