| 1 |
// Copyright 2018 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package math |
| 6 |
|
| 7 |
import "internal/goarch" |
| 8 |
|
| 9 |
const MaxUintptr = ^uintptr(0) |
| 10 |
|
| 11 |
// MulUintptr returns a * b and whether the multiplication overflowed. |
| 12 |
// On supported platforms this is an intrinsic lowered by the compiler. |
| 13 |
func MulUintptr(a, b uintptr) (uintptr, bool) { |
| 14 |
if a|b < 1<<(4*goarch.PtrSize) || a == 0 { |
| 15 |
return a * b, false |
| 16 |
} |
| 17 |
overflow := b > MaxUintptr/a |
| 18 |
return a * b, overflow |
| 19 |
} |
| 20 |
|
| 21 |
// Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y |
| 22 |
// with the product bits' upper half returned in hi and the lower |
| 23 |
// half returned in lo. |
| 24 |
// This is a copy from math/bits.Mul64 |
| 25 |
// On supported platforms this is an intrinsic lowered by the compiler. |
| 26 |
func Mul64(x, y uint64) (hi, lo uint64) { |
| 27 |
const mask32 = 1<<32 - 1 |
| 28 |
x0 := x & mask32 |
| 29 |
x1 := x >> 32 |
| 30 |
y0 := y & mask32 |
| 31 |
y1 := y >> 32 |
| 32 |
w0 := x0 * y0 |
| 33 |
t := x1*y0 + w0>>32 |
| 34 |
w1 := t & mask32 |
| 35 |
w2 := t >> 32 |
| 36 |
w1 += x0 * y1 |
| 37 |
hi = x1*y1 + w2 + w1>>32 |
| 38 |
lo = x * y |
| 39 |
return |
| 40 |
} |