> 
> That's a great explanation Thomas.
> I'm curious though:  how come both compilers produce this 
> same sequence of instructions?  I'd have thought it was a
> rather obscure combination.  Is it perhaps more common
> than I'd suspected, or do GCC and Dignus have some common heritage
> in the back end?

 No common heritage at all.   The IBM compiler produces a similar
 sequence.

 Compiler writers are always looking for ways to do more using
 less.

 It's pretty well known, as are some other surprising sequences.

 Here's one that will surprise people...

 For this source:

   int 
   foo(int x)
   {
     return x / 5;
   }

 the Dignus compiler (generating code for 32-bit z/OS) 
 generates:

* ***     return x / 5;
         L     15,0(0,1)   ; x
         LR    3,15        ;   .
         SRL   15,31(0)    ;   .
         M     2,@lit_153_0 ;   .
         SRA   2,1(0)      ;   .
         ALR   2,15        ;   .
         LR    15,2
   ...
         DS    0D
@lit_153_0 DC  F'1717986919' 0x66666667

 which is correct, and avoids the division instruction.
 It takes advantage of the fact the the M)ultiply instruction
 uses 2 signed 32-bit operands and produces a signed 64-bit
 result.

        - Dave Rivers -

p.s. Even though we don't "know" the timing difference between
     division and multiplication, it's a sure bet that division
     takes a lot more time than multiplication on any hardware.
     So, best to avoid it if you can.

--
riv...@dignus.com                        Work: (919) 676-0847
Get your mainframe programming tools at http://www.dignus.com

Reply via email to