[pypy-dev] Re: OT: abs(x) with 4 assembly insns
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
> 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
You should also check out IBM's PowerPC Compiler Writer's Guide, if you
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 :)
More information about the Pypy-dev