Re: Modulo optimizations

Robert Harley <harley@corton.inria.fr>
14 Oct 1999 01:22:48 -0400

          From comp.compilers

Related articles
Modulo optimizations gmarshal@globalnet.co.uk (Graham Marshall) (1999-10-04)
Re: Modulo optimizations jgk@jgk.org (Joe Keane) (1999-10-11)
Re: Modulo optimizations torbenm@diku.dk (1999-10-13)
Re: Modulo optimizations chase@world.std.com (David Chase) (1999-10-13)
Re: Modulo optimizations ger@informatik.uni-bremen.de (George Russell) (1999-10-14)
Re: Modulo optimizations harley@corton.inria.fr (Robert Harley) (1999-10-14)
Re:Modulo optimizations Wilco.Dijkstra@arm.com (Wilco Dijkstra) (1999-10-19)
Re: Modulo optimizations harley@corton.inria.fr (Robert Harley) (1999-10-21)
Re: Modulo optimizations Peter-Lawrence.Montgomery@cwi.nl (1999-10-27)
| List of all articles for this month |

From: Robert Harley <harley@corton.inria.fr>
Newsgroups: comp.compilers
Date: 14 Oct 1999 01:22:48 -0400
Organization: I.N.R.I.A Rocquencourt
References: 99-10-017 99-10-056
Keywords: arithmetic, optimize

torbenm@diku.dk (Torben AEgidius Mogensen) writes:
> After this (if I haven't made any mistakes along the way), sum
> contains x%3.


It looks like it is in 0..3 so you need a final step to reduce to 0..2.
Here is another variant (for 32-bit unsigned ints):


static unsigned mod3(unsigned x) {
    static const unsigned tab[18] = { 0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2 };


    x = (x & 0xFFFF)+(x>>16);
    x = (x & 255)+(x>>8);
    x = (x & 15)+(x>>4);
    x = (x & 3)+(x>>2);


    return tab[x];
} /* end function mod3 */




Bye,
    Rob.


Post a followup to this message

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