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

Michael Hudson mwh at python.net
Mon Sep 29 13:15:23 CEST 2003

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


  There are two kinds of large software systems: those that evolved
  from small systems and those that don't work.
                           -- Seen on slashdot.org, then quoted by amk

More information about the Pypy-dev mailing list