bits_compat.go 956 B

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