github.com/primecitizens/pcz/std@v0.2.1/core/math/mul.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2018 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package math 9 10 // NOTE: this package MUST be in exact package runtime/internal/math to 11 // get functions replaced by compiler intrinsics. 12 // 13 // See $GOROOT/src/cmd/compile/internal/ssagen/ssa.go#func:InitTables 14 // 15 //pcz:importpath runtime/internal/math 16 17 import ( 18 "github.com/primecitizens/pcz/std/core/arch" 19 ) 20 21 // MulUintptr returns a * b and whether the multiplication overflowed. 22 // On supported platforms this is an intrinsic lowered by the compiler. 23 func MulUintptr(a, b uintptr) (uintptr, bool) { 24 if a|b < 1<<(4*arch.PtrSize) || a == 0 { 25 return a * b, false 26 } 27 overflow := b > MaxUintptr/a 28 return a * b, overflow 29 } 30 31 // Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y 32 // with the product bits' upper half returned in hi and the lower 33 // half returned in lo. 34 // This is a copy from math/bits.Mul64 35 // On supported platforms this is an intrinsic lowered by the compiler. 36 func Mul64(x, y uint64) (hi, lo uint64) { 37 const mask32 = 1<<32 - 1 38 x0 := x & mask32 39 x1 := x >> 32 40 y0 := y & mask32 41 y1 := y >> 32 42 w0 := x0 * y0 43 t := x1*y0 + w0>>32 44 w1 := t & mask32 45 w2 := t >> 32 46 w1 += x0 * y1 47 hi = x1*y1 + w2 + w1>>32 48 lo = x * y 49 return 50 }