search.go 764 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //go:build !amd64
  2. // +build !amd64
  3. /*
  4. * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. package simd
  8. // Search uses the Clever search to find the correct key.
  9. func Search(xs []uint64, k uint64) int16 {
  10. if len(xs) < 8 || (len(xs)%8 != 0) {
  11. return Naive(xs, k)
  12. }
  13. var twos, pk [4]uint64
  14. pk[0] = k
  15. pk[1] = k
  16. pk[2] = k
  17. pk[3] = k
  18. for i := 0; i < len(xs); i += 8 {
  19. twos[0] = xs[i]
  20. twos[1] = xs[i+2]
  21. twos[2] = xs[i+4]
  22. twos[3] = xs[i+6]
  23. if twos[0] >= pk[0] {
  24. return int16(i / 2)
  25. }
  26. if twos[1] >= pk[1] {
  27. return int16((i + 2) / 2)
  28. }
  29. if twos[2] >= pk[2] {
  30. return int16((i + 4) / 2)
  31. }
  32. if twos[3] >= pk[3] {
  33. return int16((i + 6) / 2)
  34. }
  35. }
  36. return int16(len(xs) / 2)
  37. }