diff options
| author | Russ Cox <rsc@golang.org> | 2023-08-07 15:52:18 -0400 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-08-16 16:03:04 +0000 |
| commit | 53895bf993f0822fcf8910b80b20a6e0c684c60d (patch) | |
| tree | b1eb1d8703633aac46d97c967164e33e7eff4a8f /src/runtime/internal | |
| parent | 98c9f271d67b501ecf2ce995539abd2cdc81d505 (diff) | |
| download | go-53895bf993f0822fcf8910b80b20a6e0c684c60d.tar.xz | |
runtime/internal/math: add Add64
This makes the intrinsic available on 64-bit platforms,
since the runtime cannot import math/bits.
Change-Id: I5296cc6a97d1cb4756ab369d96dc9605df9f8247
Reviewed-on: https://go-review.googlesource.com/c/go/+/516861
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
TryBot-Bypass: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/runtime/internal')
| -rw-r--r-- | src/runtime/internal/math/math.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/runtime/internal/math/math.go b/src/runtime/internal/math/math.go index c3fac366be..b2e5508651 100644 --- a/src/runtime/internal/math/math.go +++ b/src/runtime/internal/math/math.go @@ -38,3 +38,18 @@ func Mul64(x, y uint64) (hi, lo uint64) { lo = x * y return } + +// Add64 returns the sum with carry of x, y and carry: sum = x + y + carry. +// The carry input must be 0 or 1; otherwise the behavior is undefined. +// The carryOut output is guaranteed to be 0 or 1. +// +// This function's execution time does not depend on the inputs. +// On supported platforms this is an intrinsic lowered by the compiler. +func Add64(x, y, carry uint64) (sum, carryOut uint64) { + sum = x + y + carry + // The sum will overflow if both top bits are set (x & y) or if one of them + // is (x | y), and a carry from the lower place happened. If such a carry + // happens, the top bit will be 1 + 0 + 1 = 0 (&^ sum). + carryOut = ((x & y) | ((x | y) &^ sum)) >> 63 + return +} |
