arith.go 4.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file provides Go implementations of elementary multi-precision
// arithmetic operations on word vectors. Needed for platforms without
// assembly implementations of these routines.

package big

11 12
import "math/bits"

13
// A Word represents a single digit of a multi-precision unsigned integer.
14
type Word uint
15 16

const (
17
	_S = _W / 8 // word size in bytes
18

19 20 21
	_W = bits.UintSize // word size in bits
	_B = 1 << _W       // digit base
	_M = _B - 1        // digit mask
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

	_W2 = _W / 2   // half word size in bits
	_B2 = 1 << _W2 // half digit base
	_M2 = _B2 - 1  // half digit mask
)

// ----------------------------------------------------------------------------
// Elementary operations on words
//
// These operations are used by the vector operations below.

// z1<<_W + z0 = x+y+c, with c == 0 or 1
func addWW_g(x, y, c Word) (z1, z0 Word) {
	yc := y + c
	z0 = x + yc
	if z0 < x || yc < y {
		z1 = 1
	}
	return
}

// z1<<_W + z0 = x-y-c, with c == 0 or 1
func subWW_g(x, y, c Word) (z1, z0 Word) {
	yc := y + c
	z0 = x - yc
	if z0 > x || yc < y {
		z1 = 1
	}
	return
}

// z1<<_W + z0 = x*y
// Adapted from Warren, Hacker's Delight, p. 132.
func mulWW_g(x, y Word) (z1, z0 Word) {
	x0 := x & _M2
	x1 := x >> _W2
	y0 := y & _M2
	y1 := y >> _W2
	w0 := x0 * y0
	t := x1*y0 + w0>>_W2
	w1 := t & _M2
	w2 := t >> _W2
	w1 += x0 * y1
	z1 = x1*y1 + w2 + w1>>_W2
	z0 = x * y
	return
}

// z1<<_W + z0 = x*y + c
func mulAddWWW_g(x, y, c Word) (z1, z0 Word) {
72
	z1, zz0 := mulWW_g(x, y)
73 74 75 76 77 78
	if z0 = zz0 + c; z0 < zz0 {
		z1++
	}
	return
}

79
// nlz returns the number of leading zeros in x.
80
// Wraps bits.LeadingZeros call for convenience.
81
func nlz(x Word) uint {
82
	return uint(bits.LeadingZeros(uint(x)))
83
}
84

85
// q = (u1<<_W + u0 - r)/y
86 87 88 89 90 91
// Adapted from Warren, Hacker's Delight, p. 152.
func divWW_g(u1, u0, v Word) (q, r Word) {
	if u1 >= v {
		return 1<<_W - 1, 1<<_W - 1
	}

92
	s := nlz(v)
93 94 95 96 97 98 99 100 101 102 103
	v <<= s

	vn1 := v >> _W2
	vn0 := v & _M2
	un32 := u1<<s | u0>>(_W-s)
	un10 := u0 << s
	un1 := un10 >> _W2
	un0 := un10 & _M2
	q1 := un32 / vn1
	rhat := un32 - q1*vn1

104
	for q1 >= _B2 || q1*vn0 > _B2*rhat+un1 {
105 106
		q1--
		rhat += vn1
107 108
		if rhat >= _B2 {
			break
109 110 111 112 113 114 115
		}
	}

	un21 := un32*_B2 + un1 - q1*v
	q0 := un21 / vn1
	rhat = un21 - q0*vn1

116
	for q0 >= _B2 || q0*vn0 > _B2*rhat+un0 {
117 118
		q0--
		rhat += vn1
119 120
		if rhat >= _B2 {
			break
121 122 123 124 125 126
		}
	}

	return q1*_B2 + q0, (un21*_B2 + un0 - q0*v) >> s
}

127 128 129 130 131
// Keep for performance debugging.
// Using addWW_g is likely slower.
const use_addWW_g = false

// The resulting carry c is either 0 or 1.
132
func addVV_g(z, x, y []Word) (c Word) {
133 134 135 136 137 138 139 140 141 142 143 144 145
	if use_addWW_g {
		for i := range z {
			c, z[i] = addWW_g(x[i], y[i], c)
		}
		return
	}

	for i, xi := range x[:len(z)] {
		yi := y[i]
		zi := xi + yi + c
		z[i] = zi
		// see "Hacker's Delight", section 2-12 (overflow detection)
		c = (xi&yi | (xi|yi)&^zi) >> (_W - 1)
146 147 148 149
	}
	return
}

150
// The resulting carry c is either 0 or 1.
151
func subVV_g(z, x, y []Word) (c Word) {
152 153 154 155 156 157 158 159 160 161 162 163 164
	if use_addWW_g {
		for i := range z {
			c, z[i] = subWW_g(x[i], y[i], c)
		}
		return
	}

	for i, xi := range x[:len(z)] {
		yi := y[i]
		zi := xi - yi - c
		z[i] = zi
		// see "Hacker's Delight", section 2-12 (overflow detection)
		c = (yi&^xi | (yi|^xi)&zi) >> (_W - 1)
165 166 167 168
	}
	return
}

169
// The resulting carry c is either 0 or 1.
170
func addVW_g(z, x []Word, y Word) (c Word) {
171 172 173 174 175 176 177 178
	if use_addWW_g {
		c = y
		for i := range z {
			c, z[i] = addWW_g(x[i], c, 0)
		}
		return
	}

179
	c = y
180 181 182 183
	for i, xi := range x[:len(z)] {
		zi := xi + c
		z[i] = zi
		c = xi &^ zi >> (_W - 1)
184 185 186 187 188
	}
	return
}

func subVW_g(z, x []Word, y Word) (c Word) {
189 190 191 192 193 194 195 196
	if use_addWW_g {
		c = y
		for i := range z {
			c, z[i] = subWW_g(x[i], c, 0)
		}
		return
	}

197
	c = y
198 199 200 201
	for i, xi := range x[:len(z)] {
		zi := xi - c
		z[i] = zi
		c = (zi &^ xi) >> (_W - 1)
202 203 204 205
	}
	return
}

206
func shlVU_g(z, x []Word, s uint) (c Word) {
207 208 209 210 211 212 213 214 215 216 217 218 219 220
	if n := len(z); n > 0 {
		ŝ := _W - s
		w1 := x[n-1]
		c = w1 >> ŝ
		for i := n - 1; i > 0; i-- {
			w := w1
			w1 = x[i-1]
			z[i] = w<<s | w1>>ŝ
		}
		z[0] = w1 << s
	}
	return
}

221
func shrVU_g(z, x []Word, s uint) (c Word) {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	if n := len(z); n > 0 {
		ŝ := _W - s
		w1 := x[0]
		c = w1 << ŝ
		for i := 0; i < n-1; i++ {
			w := w1
			w1 = x[i+1]
			z[i] = w>>s | w1<<ŝ
		}
		z[n-1] = w1 >> s
	}
	return
}

func mulAddVWW_g(z, x []Word, y, r Word) (c Word) {
	c = r
	for i := range z {
		c, z[i] = mulAddWWW_g(x[i], y, c)
	}
	return
}

244
// TODO(gri) Remove use of addWW_g here and then we can remove addWW_g and subWW_g.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
func addMulVVW_g(z, x []Word, y Word) (c Word) {
	for i := range z {
		z1, z0 := mulAddWWW_g(x[i], y, z[i])
		c, z[i] = addWW_g(z0, c, 0)
		c += z1
	}
	return
}

func divWVW_g(z []Word, xn Word, x []Word, y Word) (r Word) {
	r = xn
	for i := len(z) - 1; i >= 0; i-- {
		z[i], r = divWW_g(r, x[i], y)
	}
	return
}