[M3devel] FW: optimizing range checks?

Jay K jay.krell at cornell.edu
Tue Mar 2 14:16:03 CET 2010


Hey that's pretty clever.

It costs a register, but given:

 

    if (b >= constantX|| b <= -constantY)
        a = 0;

 

The C compiler instead does something like:

  if ((b + constantY - 1) > (constantX + constantY - 1))

    a = 0;

 

This is something the front end could do in many cases.?

Adding a constant to eliminate one of the compares and branches is a win.

If an x86 compiler will give up a register for this, then it is probably a win everywhere.

 

Granted, it probably requires silent overflow. Oh well.

 

 - Jay

 
> Date: Tue, 2 Mar 2010 13:59:31 +0000
> To: m3commit at elegosoft.com
> From: jkrell at elego.de
> Subject: [M3commit] CVS Update: cm3
> 
> CVSROOT: /usr/cvs
> Changes by: jkrell at birch. 10/03/02 13:59:31
> 
> Modified files:
> cm3/m3-libs/m3core/src/Csupport/Common/: hand.c 
> 
> Log message:
> remove m3_shift64, which for the record was:
> uint64 __stdcall m3_shift64(uint64 a, int b)
> {
> if (b >= 64 || b <= -64)
> a = 0;
> else if (b > 0)
> a <<= b;
> else if (b < 0)
> a >>= -b;
> return a;
> }
> 
> _m3_shift64 at 12:
> 00000000: 55 push ebp
> 00000001: 8B EC mov ebp,esp
> 00000003: 8B 4D 10 mov ecx,dword ptr [ebp+10h]
> 00000006: 8D 41 3F lea eax,[ecx+3Fh]
> 00000009: 83 F8 7E cmp eax,7Eh
> 0000000C: 77 1C ja 0000002A
> 0000000E: 8B 45 08 mov eax,dword ptr [ebp+8]
> 00000011: 8B 55 0C mov edx,dword ptr [ebp+0Ch]
> 00000014: 85 C9 test ecx,ecx
> 00000016: 7E 07 jle 0000001F
> 00000018: E8 00 00 00 00 call __allshl
> 0000001D: EB 0F jmp 0000002E
> 0000001F: 7D 0D jge 0000002E
> 00000021: F7 D9 neg ecx
> 00000023: E8 00 00 00 00 call __aullshr
> 00000028: EB 04 jmp 0000002E
> 0000002A: 33 C0 xor eax,eax
> 0000002C: 33 D2 xor edx,edx
> 0000002E: 5D pop ebp
> 0000002F: C2 0C 00 ret 0Ch
> 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20100302/60d21eee/attachment-0001.html>


More information about the M3devel mailing list