Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: some quotes on switch and indirect branches

Author: Gerd Isenberg

Date: 02:22:57 11/22/05

Go up one level in this thread


Also msc6 is able to produce some unconditional assembly for two cases:

int someMargin(int depth) {
 const int fm1 = 200;
 const int fm2 = 300;
 return depth < 0 ? fm1 : fm2;
}

?someMargin@@YIHH@Z PROC NEAR				; someMargin, COMDAT
  00000	33 c0		 xor	 eax, eax
  00002	85 c9		 test	 ecx, ecx
  00004	0f 9d c0	 setge	 al
  00007	48		 dec	 eax
  00008	24 9c		 and	 al, -100		; ffffff9cH
  0000a	05 2c 01 00 00	 add	 eax, 300		; 0000012cH
  0000f	c3		 ret	 0
?someMargin@@YIHH@Z ENDP				; someMargin

but still not that optimal as it could be
(even if the code is only two bytes larger)

int someMargin(int depth) {
 const int fm1 = 200;
 const int fm2 = 300;
 return fm2 +((depth>>31) & (fm1-fm2));
}

?someMargin@@YIHH@Z PROC NEAR				; someMargin, COMDAT
  00000	8b c1		 mov	 eax, ecx
  00002	c1 f8 1f	 sar	 eax, 31		; 0000001fH
  00005	24 9c		 and	 al, -100		; ffffff9cH
  00007	05 2c 01 00 00	 add	 eax, 300		; 0000012cH
  0000c	c3		 ret	 0
?someMargin@@YIHH@Z ENDP				; someMargin


With three cases:

int someMargin(int depth) {
 const int fm1 = 200;
 const int fm2 = 300;
 const int fm3 = 500;
 return depth < -1 ? fm1 : depth < 0 ? fm2 : fm3;
}

?someMargin@@YIHH@Z PROC NEAR				; someMargin, COMDAT
  00000	83 f9 ff	 cmp	 ecx, -1
  00003	7d 06		 jge	 SHORT $L24962
  00005	b8 c8 00 00 00	 mov	 eax, 200		; 000000c8H
  0000a	c3		 ret	 0
$L24962:
  0000b	33 c0		 xor	 eax, eax
  0000d	85 c9		 test	 ecx, ecx
  0000f	0f 9d c0	 setge	 al
  00012	48		 dec	 eax
  00013	24 38		 and	 al, 56			; 00000038H
  00015	05 f4 01 00 00	 add	 eax, 500		; 000001f4H
  0001a	c3		 ret	 0
?someMargin@@YIHH@Z ENDP				; someMargin

The "forced" branchless one:

int someMargin(int depth) {
 const int fm1 = 200;
 const int fm2 = 300;
 const int fm3 = 500;
 return                       fm3
        + ( (depth   >>31) & (fm2-fm3))
        + (((depth+1)>>31) & (fm1-fm2));
}

?someMargin@@YIHH@Z PROC NEAR				; someMargin, COMDAT
  00000	8d 41 01	 lea	 eax, DWORD PTR [ecx+1]
  00003	c1 f8 1f	 sar	 eax, 31		; 0000001fH
  00006	c1 f9 1f	 sar	 ecx, 31		; 0000001fH
  00009	24 9c		 and	 al, -100		; ffffff9cH
  0000b	80 e1 38	 and	 cl, 56			; 00000038H
  0000e	8d 84 08 f4 01
	00 00		 lea	 eax, DWORD PTR [eax+ecx+500]
  00015	c3		 ret	 0
?someMargin@@YIHH@Z ENDP				; someMargin

Cheers,
Gerd



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.