Return Absolute value of number. core: @@ : neg eax js @B Possible realisation: macro release --------------------------------------- abs macro number @@ neg number js @B endm ------------------------------------- Proc release (goung with macro call to make it faster) abs macro number push number call ABS endm ;return abs num value in eax ABS proc mov eax,dword ptr @@: neg eax js @B ret 4 ABS endp usage: abs ecx ;eax = abs(ecx) .data? someinteger dd ? .code abs someinteger ;eax= abs(someinteger) The Svin
Posted on 2001-04-20 07:25:00 by The Svin
Agner Fog has this method, which does it without branching, this extract is taken from his document "How to optimize for the pentium family of microprocessors" 22.4. Avoiding conditional jumps by using flags (all processors) The most important jumps to eliminate are conditional jumps, especially if they are poorly predictable. Sometimes it is possible to obtain the same effect as a branch by ingenious manipulation of bits and flags. For example you may calculate the absolute value of a signed number without branching:
``````
CDQ
XOR EAX,EDX
SUB EAX,EDX
(On PPlain and PMMX, use MOV EDX,EAX / SAR EDX,31 instead of CDQ).

The carry flag is particularly useful for this kind of tricks:
Setting carry if a value is zero: CMP ,1
Setting carry if a value is not zero: XOR EAX,EAX / CMP EAX,
Incrementing a counter if carry: ADC EAX,0
Setting a bit for each time the carry is set: RCL EAX,1
Generating a bit mask if carry is set: SBB EAX,EAX
Setting a bit on an arbitrary condition: SETcond AL
Setting all bits on an arbitrary condition: XOR EAX,EAX / SETNcond AL / DEC EAX
(remember to reverse the condition in the last example)

The following example finds the minimum of two unsigned numbers: if (b < a) a = b;

SUB EBX,EAX
SBB ECX,ECX
AND ECX,EBX
The next example chooses between two numbers: if (a != 0) a = b; else a = c;

CMP EAX,1
SBB EAX,EAX
XOR ECX,EBX
AND EAX,ECX
XOR EAX,EBX
Whether or not such tricks are worth the extra code depends on how predictable a conditional jump would be, whether the extra pairing or scheduling opportunities of the branch-free code can be utilized, and whether there are other jumps following immediately after which could suffer the penalties of consecutive jumps.
``````
umbongo This message was edited by umbongo, on 4/20/2001 9:47:10 AM
Posted on 2001-04-20 09:46:00 by umbongo
Thanx for recall the Anger version. I like both. The more interesting methods, the better. We'll find right place for all of them :) The Svin.
Posted on 2001-04-20 14:06:00 by The Svin
``````
umbongo,

where is that document located, do you know?
``````
Posted on 2001-05-20 07:17:00 by disease_2000
Somewhere at www.agner.org , iirc.
Posted on 2001-05-20 17:21:00 by f0dder