search.go 1.2 KB

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