changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > ventivac / changeset: use "% mod" instead of "& (mod-1)", no real difference in performance.

changeset 100: a332f10eb972
parent 99: a1ab045f1634
child 101: b3487ae9e237
author: Mechiel Lukkien <mechiel@ueber.net>
date: Mon, 06 Aug 2007 17:56:04 +0200
files: appl/lib/rabin.b
description: use "% mod" instead of "& (mod-1)", no real difference in performance.
     1.1--- a/appl/lib/rabin.b	Mon Aug 06 17:09:06 2007 +0200
     1.2+++ b/appl/lib/rabin.b	Mon Aug 06 17:56:04 2007 +0200
     1.3@@ -19,21 +19,19 @@
     1.4 {
     1.5 	power := 1;
     1.6 	for(i := 0; i < n; i++)
     1.7-		power = (power * base) & (mod-1);
     1.8+		power = (power * base) % mod;
     1.9 	return power;
    1.10 }
    1.11 
    1.12 Rcfg.mk(prime, width, mod: int): (ref Rcfg, string)
    1.13 {
    1.14 	rcfg := ref Rcfg(prime, width, mod, array[256] of int);
    1.15-	if((mod & (mod-1)) != 0)
    1.16-		return (nil, "mod is not power of 2");
    1.17 
    1.18 	power := modpower(prime, width, mod);
    1.19 if(debug) say(sprint("power=%d", power));
    1.20 
    1.21 	for(i := 0; i < 256; i++) {
    1.22-		rcfg.tab[i] = (i * power) & (mod-1);
    1.23+		rcfg.tab[i] = (i * power) % mod;
    1.24 if(debug) say(sprint("tab[%d] = %d", i, rcfg.tab[i]));
    1.25 	}
    1.26 if(debug) say(sprint("mod=%d prime=%d width=%d", rcfg.mod, rcfg.prime, rcfg.width));
    1.27@@ -60,7 +58,7 @@
    1.28 			break;
    1.29 		c.buf[c.n] = byte ch;
    1.30 if(debug) say(sprint("i=%d prev=%d width=%d data[i]=%d prime=%d mod=%d", c.n, c.state, width, int c.buf[c.n], prime, mod));
    1.31-		c.state = (prime*c.state + ch) & (mod-1);
    1.32+		c.state = (prime*c.state + ch) % mod;
    1.33 		c.n++;
    1.34 	}
    1.35 if(debug) say(sprint("initial state=%d", c.state));
    1.36@@ -82,7 +80,7 @@
    1.37 			return (d, off, nil);
    1.38 		}
    1.39 		c.buf[c.n] = byte ch;
    1.40-		c.state = (mod+prime*c.state + ch - c.rcfg.tab[int c.buf[c.n-width]]) & (mod-1);
    1.41+		c.state = (mod+prime*c.state + ch - c.rcfg.tab[int c.buf[c.n-width]]) % mod;
    1.42 		c.n++;
    1.43 		if(c.n-width >= c.max || (c.n-width >= c.min && c.state == mod-1)) {
    1.44 			d := array[c.n-width] of byte;