github.com/etecs-ru/ristretto@v0.9.1/z/simd/search.go (about)

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