Branchless MIN/MAX functions

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
Bo Persson
Posts: 257
Joined: Sat Mar 11, 2006 8:31 am
Location: Malmö, Sweden
Full name: Bo Persson

Re: Branchless MIN/MAX functions

Post by Bo Persson »

Gerd Isenberg wrote:
Michael Sherwin wrote: Sorry but, please forgive me! :oops:

But, how do you know that he did not go back in time and patent the routine before he invented it and then back in the future after having forgot about it, then copied it with out having to invent it? :? I guessed I've watched too many trek episodes! :lol:

Thanks for the link! :D
Intel's revenge on Sun's patent was the P4 ;-)
They made shift so slow that the mentioned sar trick became rather worthless.

As mentioned one may use cdq instead sar 31.
And there are two ways to define Two's Complement by One's Complement.

Code: Select all

int abs(int x) 
{
  int m = (int)( (unsigned __int64)((__int64)x) >> 32 );
  return (x + m) ^ m;
}

?abs@@YIHH@Z PROC NEAR					; abs, COMDAT
  00000	8b c1		 mov	 eax, ecx
  00002	99		 cdq
  00003	8d 04 0a	 lea	 eax, [edx+ecx]
  00006	33 c2		 xor	 eax, edx
  00008	c3		 ret	 0
?abs@@YIHH@Z ENDP					; abs
And like you said in a post above - don't forget to check what the compiler does for the standard function

Code: Select all

int test(int x)
{
   return std::abs(x);
}

?test@@YAHH@Z PROC					; test, COMDAT
; _x$ = eax

; 6    :    return std::abs(x);

  00000	99		 cdq
  00001	33 c2		 xor	 eax, edx
  00003	2b c2		 sub	 eax, edx

; 7    : }

  00005	c3		 ret	 0
?test@@YAHH@Z ENDP					; test
:-)

Bo Persson
Gerd Isenberg
Posts: 2251
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Branchless MIN/MAX functions

Post by Gerd Isenberg »

Bo Persson wrote: And like you said in a post above - don't forget to check what the compiler does for the standard function

Code: Select all

int test(int x)
{
   return std::abs(x);
}

?test@@YAHH@Z PROC					; test, COMDAT
; _x$ = eax

; 6    :    return std::abs(x);

  00000	99		 cdq
  00001	33 c2		 xor	 eax, edx
  00003	2b c2		 sub	 eax, edx

; 7    : }

  00005	c3		 ret	 0
?test@@YAHH@Z ENDP					; test
:-)

Bo Persson
Yes, very nice! Five bytes only ;-)