Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Thoughts about board representations...

Author: Eugene Nalimov

Date: 15:38:51 02/10/00

Go up one level in this thread


And here is result of using MSVC:

#include <stdlib.h>

int type (int i)
{
  return abs (i);
}

Assembly code is:

	mov	eax, DWORD PTR [esp-4]
	cdq
	xor	eax, edx
	sub	eax, edx
	ret

It also lacks the branch, but it is also shorter than GCC output, and will run
on any 32-bit x86 processor.

It is based in the following observation: abs (x) == (x ^ (x >> (sizeof (x) -
1))) - (x >> (sizeof (x) - 1)).

That is an example when good compiler can get rid of branch remaining compatible
with old processors.

Eugene

On February 10, 2000 at 18:01:14, Robert Hyatt wrote:

>On February 10, 2000 at 17:50:54, Robert Hyatt wrote:
>
>Thought a sample bit of code would be even better for the TYPE issue
>in Tom's question.  Here is a simple piece of C code to compute the
>absolute value of an integer and return it:
>
>abs(int i) {
>  int j;
>  j=(i > 0) ? i : -i;
>  return(j);
>}
>
>Here is what gcc produces to accomplish that:
>
>abs:
>        pushl %ebp
>        movl %esp,%ebp
>        movl 8(%ebp),%eax
>        movl %eax,%edx
>        negl %edx
>        cmpl $-1,%eax
>        cmovle %edx,%eax
>        movl %ebp,%esp
>        popl %ebp
>        ret
>
>Which nicely avoids any branch penalties (except for the ret, but
>this doesn't have to be a function of course, it could be inlined
>easily.)



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.