Re: Compiler optimization of division and remainder

augustss@cs.chalmers.se (Lennart Augustsson)
27 Jan 1996 16:02:10 -0500

          From comp.compilers

Related articles
Compiler optimization of division and remainder hbaker@netcom.com (1996-01-27)
Re: Compiler optimization of division and remainder d.sand@ix.netcom.com (1996-01-27)
Re: Compiler optimization of division and remainder augustss@cs.chalmers.se (1996-01-27)
Re: Compiler optimization of division and remainder richard@atheist.tamu.edu (1996-01-28)
Re: Compiler optimization of division and remainder prener@watson.ibm.com (1996-01-29)
Re: Compiler optimization of division and remainder hbaker@netcom.com (1996-01-29)
Re: Compiler optimization of division and remainder Peter-Lawrence.Montgomery@cwi.nl (1996-01-29)
Re: Compiler optimization of division and remainder johnmce@world.std.com (1996-01-29)
Re: Compiler optimization of division and remainder michael.williams@armltd.co.uk (1996-01-29)
[3 later articles]
| List of all articles for this month |

From: augustss@cs.chalmers.se (Lennart Augustsson)
Newsgroups: comp.compilers
Date: 27 Jan 1996 16:02:10 -0500
Organization: Chalmers University of Technology
References: 96-01-088
Keywords: arithmetic, optimize



hbaker@netcom.com (Henry G. Baker) writes:
      On another newsgroup, a poster claimed that some compilers could
      optimize a program which used both a/b and a%b (both quotient and
      remainder with the _same_ arguments) in such a way that the compiler
      would perform only a single hardware division operation and arrange to
      use both the quotient and remainder from this single operation.


The ASSEMBLER does this for some MIPS platforms.
Here is an example from a DEC box:


C program:
foo(i)
int i;
{
                return i%10 + i/10;
}


Assembly code from 'cc -O2 -S':
foo:
                rem $14, $4, 10 # rem is a pseudo-op handled by as
                div $15, $4, 10 # so is div
                addu $2, $14, $15
                j $31


But look at the disassembled code from 'cc -O2 -c'!
foo:
li $at,10 # load 10 into reg at
div $zero,$a0,$at # a0 ($4) by at, result in special regs
mfhi $t6 # get one half of the result
mflo $t7 # and the other
addu $v0,$t6,$t7
jr $ra


I've not tested how well the assembler handles these
things but it works here.


Dividing and modding with the same values is a quite common
operation when you do base conversion so it is worthwhile,
I think.
--
-- Lennart Augustsson
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.