[pypy-dev] Re: OT: abs(x) with 4 assembly insns

Bob Ippolito bob at redivi.com
Mon Sep 29 13:33:56 CEST 2003


On Monday, Sep 29, 2003, at 07:15 America/New_York, Michael Hudson  
wrote:

> Christian Tismer <tismer at tismer.com> writes:
>
>> Hi friends,
>>
>> today, Armin presented me a simple brain-teaser:
>>
>> You have X86 assembly, you have only 4 insns,
>> and you don't want to use a jump.
>> You have a register, loaded with a value, and
>> you should produce its abs, in another register,
>> while preserving the argument register.
>>
>> Hmm. 4 insns.
>
> Or you can cheat, and remember that you saw something like this in
> some psyco header file somewhere...
>
>   /* as you can check the following takes the absolute value of (say)  
> EAX:      \
>        ADD EAX, EAX                                                     
>          \
>        SBB EAX, sourcecopy                                              
>          \
>        SBB EDX, EDX                                                     
>          \
>        XOR EAX, EDX                                                     
>          \
>     (note: although the idea is not original, the above code might be   
>          \
>      original as it has been found by an exhaustive search on *all*     
>          \
>      short codes :-)                                                    
>          \
>   */                                                                    
>          \
>
> I don't know x86 assembler, so I have no idea which of the two code
> sequences are actually likely to run quicker on a modern processor.
>
> I've been reading the PPC architecture manual, though, so I can tell
> you that Bob's 3 instructions are all single cycle instructions on a
> G3...

You should also check out IBM's PowerPC Compiler Writer's Guide, if you  
haven't already:
http://www-3.ibm.com/chips/techlib/techlib.nsf/techdocs/ 
852569B20050FF7785256996007558C6

Hard copies of this document can be obtained by calling IBM at (800)  
879-2755 and requesting document number SA14-2094, I hear that they  
used to (maybe still do) charge $1.00 for the book :)

-bob



More information about the Pypy-dev mailing list