mirror of
https://github.com/golang/go
synced 2024-11-22 06:34:40 -07:00
- use in-place bignum operations where available
- runs approx. 30% faster R=r DELTA=24 (10 added, 2 deleted, 12 changed) OCL=32984 CL=33005
This commit is contained in:
parent
8db8682453
commit
0dbd8971a2
@ -44,8 +44,6 @@ import (
|
||||
)
|
||||
|
||||
var n = flag.Int("n", 27, "number of digits");
|
||||
|
||||
// TODO for easier profiling, remove eventually
|
||||
var silent = flag.Bool("s", false, "don't print result");
|
||||
|
||||
var (
|
||||
@ -61,13 +59,16 @@ func extract_digit() int64 {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Compute (numer * 3 + accum) / denom */
|
||||
tmp1, tmp2 = numer.MulNat(bignum.Nat(3)).Add(accum).QuoRem(denom);
|
||||
// Compute (numer * 3 + accum) / denom
|
||||
tmp1 = numer.Shl(1);
|
||||
bignum.Iadd(tmp1, tmp1, numer);
|
||||
bignum.Iadd(tmp1, tmp1, accum);
|
||||
tmp1, tmp2 := tmp1.QuoRem(denom);
|
||||
|
||||
/* Now, if (numer * 4 + accum) % denom... */
|
||||
tmp2 = tmp2.Add(numer);
|
||||
// Now, if (numer * 4 + accum) % denom...
|
||||
bignum.Iadd(tmp2, tmp2, numer);
|
||||
|
||||
/* ... is normalized, then the two divisions have the same result. */
|
||||
// ... is normalized, then the two divisions have the same result.
|
||||
if tmp2.Cmp(denom) >= 0 {
|
||||
return -1;
|
||||
}
|
||||
@ -79,16 +80,16 @@ func next_term(k int64) {
|
||||
y2 := k*2 + 1;
|
||||
|
||||
tmp1 = numer.Shl(1);
|
||||
accum = accum.Add(tmp1);
|
||||
accum = accum.Mul1(y2);
|
||||
numer = numer.Mul1(k);
|
||||
denom = denom.Mul1(y2);
|
||||
bignum.Iadd(accum, accum, tmp1);
|
||||
bignum.Iscale(accum, y2);
|
||||
bignum.Iscale(numer, k);
|
||||
bignum.Iscale(denom, y2);
|
||||
}
|
||||
|
||||
func eliminate_digit(d int64) {
|
||||
accum = accum.Sub(denom.Mul1(d));
|
||||
accum = accum.Mul1(10);
|
||||
numer = numer.Mul1(10);
|
||||
bignum.Isub(accum, accum, denom.Mul1(d));
|
||||
bignum.Iscale(accum, 10);
|
||||
bignum.Iscale(numer, 10);
|
||||
}
|
||||
|
||||
func printf(s string, arg ...) {
|
||||
|
@ -231,3 +231,10 @@ chameneos 6000000
|
||||
gcc -O2 chameneosredux.c -lpthread 17.93u 323.65s 88.47r
|
||||
gc chameneosredux 21.72u 0.00s 21.73r
|
||||
|
||||
August 10 2009
|
||||
|
||||
# In-place versions for some bignum operations.
|
||||
pidigits 10000
|
||||
gcc -O2 pidigits.c -lgmp 2.56u 0.00s 2.57r
|
||||
gc pidigits 55.22u 0.04s 55.29r # *** -23%
|
||||
gc_B pidigits 55.49u 0.02s 55.60r # *** -23%
|
||||
|
Loading…
Reference in New Issue
Block a user