github.com/apache/arrow/go/v16@v16.1.0/arrow/compute/internal/kernels/scalar_comparison_amd64.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  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  //go:build go1.18 && !noasm
    18  
    19  package kernels
    20  
    21  import (
    22  	"unsafe"
    23  
    24  	"github.com/apache/arrow/go/v16/arrow"
    25  	"golang.org/x/sys/cpu"
    26  )
    27  
    28  var pureGo bool
    29  
    30  type cmpfn func(arrow.Type, []byte, []byte, []byte, int64, int)
    31  
    32  var comparisonMap map[CompareOperator][3]cmpfn
    33  
    34  func genCompareKernel[T arrow.NumericType](op CompareOperator) *CompareData {
    35  	if pureGo {
    36  		return genGoCompareKernel(getCmpOp[T](op))
    37  	}
    38  
    39  	ty := arrow.GetType[T]()
    40  	byteWidth := int(unsafe.Sizeof(T(0)))
    41  	comparisonFns := comparisonMap[op]
    42  	return &CompareData{
    43  		funcAA: func(left, right, out []byte, offset int) {
    44  			length := int64(len(left) / byteWidth)
    45  			comparisonFns[0](ty, left, right, out, length, offset)
    46  		},
    47  		funcAS: func(left, right, out []byte, offset int) {
    48  			length := int64(len(left) / byteWidth)
    49  			comparisonFns[1](ty, left, right, out, length, offset)
    50  		},
    51  		funcSA: func(left, right, out []byte, offset int) {
    52  			length := int64(len(right) / byteWidth)
    53  			comparisonFns[2](ty, left, right, out, length, offset)
    54  		},
    55  	}
    56  }
    57  
    58  func init() {
    59  	if cpu.X86.HasAVX2 {
    60  		comparisonMap = map[CompareOperator][3]cmpfn{
    61  			CmpEQ: {
    62  				comparisonEqualArrArrAvx2,
    63  				comparisonEqualArrScalarAvx2,
    64  				comparisonEqualScalarArrAvx2,
    65  			},
    66  			CmpNE: {
    67  				comparisonNotEqualArrArrAvx2,
    68  				comparisonNotEqualArrScalarAvx2,
    69  				comparisonNotEqualScalarArrAvx2,
    70  			},
    71  			CmpGT: {
    72  				comparisonGreaterArrArrAvx2,
    73  				comparisonGreaterArrScalarAvx2,
    74  				comparisonGreaterScalarArrAvx2,
    75  			},
    76  			CmpGE: {
    77  				comparisonGreaterEqualArrArrAvx2,
    78  				comparisonGreaterEqualArrScalarAvx2,
    79  				comparisonGreaterEqualScalarArrAvx2,
    80  			},
    81  		}
    82  
    83  	} else if cpu.X86.HasSSE42 {
    84  		comparisonMap = map[CompareOperator][3]cmpfn{
    85  			CmpEQ: {
    86  				comparisonEqualArrArrSSE4,
    87  				comparisonEqualArrScalarSSE4,
    88  				comparisonEqualScalarArrSSE4,
    89  			},
    90  			CmpNE: {
    91  				comparisonNotEqualArrArrSSE4,
    92  				comparisonNotEqualArrScalarSSE4,
    93  				comparisonNotEqualScalarArrSSE4,
    94  			},
    95  			CmpGT: {
    96  				comparisonGreaterArrArrSSE4,
    97  				comparisonGreaterArrScalarSSE4,
    98  				comparisonGreaterScalarArrSSE4,
    99  			},
   100  			CmpGE: {
   101  				comparisonGreaterEqualArrArrSSE4,
   102  				comparisonGreaterEqualArrScalarSSE4,
   103  				comparisonGreaterEqualScalarArrSSE4,
   104  			},
   105  		}
   106  	} else {
   107  		pureGo = true
   108  	}
   109  }