bits_compat.go 938 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2019 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. //go:build !go1.13
  5. package poly1305
  6. // Generic fallbacks for the math/bits intrinsics, copied from
  7. // src/math/bits/bits.go. They were added in Go 1.12, but Add64 and Sum64 had
  8. // variable time fallbacks until Go 1.13.
  9. func bitsAdd64(x, y, carry uint64) (sum, carryOut uint64) {
  10. sum = x + y + carry
  11. carryOut = ((x & y) | ((x | y) &^ sum)) >> 63
  12. return
  13. }
  14. func bitsSub64(x, y, borrow uint64) (diff, borrowOut uint64) {
  15. diff = x - y - borrow
  16. borrowOut = ((^x & y) | (^(x ^ y) & diff)) >> 63
  17. return
  18. }
  19. func bitsMul64(x, y uint64) (hi, lo uint64) {
  20. const mask32 = 1<<32 - 1
  21. x0 := x & mask32
  22. x1 := x >> 32
  23. y0 := y & mask32
  24. y1 := y >> 32
  25. w0 := x0 * y0
  26. t := x1*y0 + w0>>32
  27. w1 := t & mask32
  28. w2 := t >> 32
  29. w1 += x0 * y1
  30. hi = x1*y1 + w2 + w1>>32
  31. lo = x * y
  32. return
  33. }