search.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // +build !amd64
  2. /*
  3. * Copyright 2020 Dgraph Labs, Inc. and Contributors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package simd
  18. // Search uses the Clever search to find the correct key.
  19. func Search(xs []uint64, k uint64) int16 {
  20. if len(xs) < 8 {
  21. return Naive(xs, k)
  22. }
  23. var twos, pk [4]uint64
  24. pk[0] = k
  25. pk[1] = k
  26. pk[2] = k
  27. pk[3] = k
  28. for i := 0; i < len(xs); i += 8 {
  29. twos[0] = xs[i]
  30. twos[1] = xs[i+2]
  31. twos[2] = xs[i+4]
  32. twos[3] = xs[i+6]
  33. if twos[0] >= pk[0] {
  34. return int16(i / 2)
  35. }
  36. if twos[1] >= pk[1] {
  37. return int16((i + 2) / 2)
  38. }
  39. if twos[2] >= pk[2] {
  40. return int16((i + 4) / 2)
  41. }
  42. if twos[3] >= pk[3] {
  43. return int16((i + 6) / 2)
  44. }
  45. }
  46. return int16(len(xs) / 2)
  47. }