1
0
mirror of https://github.com/golang/go synced 2024-11-18 05:44:47 -07:00

math/big: reduce allocations in Karatsuba case of sqr

For #23221.

Change-Id: If55dcf2e0706d6658f4a0863e3740437e008706c
Reviewed-on: https://go-review.googlesource.com/114335
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Alexander Döring 2018-05-24 00:25:12 +02:00 committed by Robert Griesemer
parent 3f2039e28d
commit 1a5d0f83c9

View File

@ -512,8 +512,7 @@ func karatsubaSqr(z, x nat) {
n := len(x) n := len(x)
if n&1 != 0 || n < karatsubaSqrThreshold || n < 2 { if n&1 != 0 || n < karatsubaSqrThreshold || n < 2 {
z = z[:2*n] basicSqr(z[:2*n], x)
basicSqr(z, x)
return return
} }
@ -562,13 +561,14 @@ func (z nat) sqr(x nat) nat {
if alias(z, x) { if alias(z, x) {
z = nil // z is an alias for x - cannot reuse z = nil // z is an alias for x - cannot reuse
} }
z = z.make(2 * n)
if n < basicSqrThreshold { if n < basicSqrThreshold {
z = z.make(2 * n)
basicMul(z, x, x) basicMul(z, x, x)
return z.norm() return z.norm()
} }
if n < karatsubaSqrThreshold { if n < karatsubaSqrThreshold {
z = z.make(2 * n)
basicSqr(z, x) basicSqr(z, x)
return z.norm() return z.norm()
} }