github.com/outcaste-io/ristretto@v0.2.3/z/simd/search.go (about)

     1  // +build !amd64
     2  
     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  
    19  package simd
    20  
    21  // Search uses the Clever search to find the correct key.
    22  func Search(xs []uint64, k uint64) int16 {
    23  	if len(xs) < 8 || (len(xs) % 8 != 0) {
    24  		return Naive(xs, k)
    25  	}
    26  	var twos, pk [4]uint64
    27  	pk[0] = k
    28  	pk[1] = k
    29  	pk[2] = k
    30  	pk[3] = k
    31  	for i := 0; i < len(xs); i += 8 {
    32  		twos[0] = xs[i]
    33  		twos[1] = xs[i+2]
    34  		twos[2] = xs[i+4]
    35  		twos[3] = xs[i+6]
    36  		if twos[0] >= pk[0] {
    37  			return int16(i / 2)
    38  		}
    39  		if twos[1] >= pk[1] {
    40  			return int16((i + 2) / 2)
    41  		}
    42  		if twos[2] >= pk[2] {
    43  			return int16((i + 4) / 2)
    44  		}
    45  		if twos[3] >= pk[3] {
    46  			return int16((i + 6) / 2)
    47  		}
    48  
    49  	}
    50  	return int16(len(xs) / 2)
    51  }