zeroshade commented on code in PR #14255: URL: https://github.com/apache/arrow/pull/14255#discussion_r982548697
########## go/arrow/compute/internal/kernels/base_arithmetic_amd64.go: ########## @@ -0,0 +1,83 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !noasm + +package kernels + +import ( + "unsafe" + + "github.com/apache/arrow/go/v10/arrow/compute/internal/exec" + "golang.org/x/exp/constraints" + "golang.org/x/sys/cpu" +) + +func getAvx2ArithmeticBinaryNumeric[T exec.NumericTypes](op ArithmeticOp) binaryOps[T, T, T] { + typ := exec.GetType[T]() + return binaryOps[T, T, T]{ + arrArr: func(_ *exec.KernelCtx, Arg0, Arg1, Out []T) error { + arithmeticAvx2(typ, op, exec.GetBytes(Arg0), exec.GetBytes(Arg1), exec.GetBytes(Out), len(Out)) + return nil + }, + arrScalar: func(_ *exec.KernelCtx, Arg0 []T, Arg1 T, Out []T) error { + arithmeticArrScalarAvx2(typ, op, exec.GetBytes(Arg0), unsafe.Pointer(&Arg1), exec.GetBytes(Out), len(Out)) + return nil + }, + scalarArr: func(_ *exec.KernelCtx, Arg0 T, Arg1, Out []T) error { + arithmeticScalarArrAvx2(typ, op, unsafe.Pointer(&Arg0), exec.GetBytes(Arg1), exec.GetBytes(Out), len(Out)) + return nil + }, + } +} + +func getSSE4ArithmeticBinaryNumeric[T exec.NumericTypes](op ArithmeticOp) binaryOps[T, T, T] { + typ := exec.GetType[T]() + return binaryOps[T, T, T]{ + arrArr: func(_ *exec.KernelCtx, Arg0, Arg1, Out []T) error { + arithmeticSSE4(typ, op, exec.GetBytes(Arg0), exec.GetBytes(Arg1), exec.GetBytes(Out), len(Out)) + return nil + }, + arrScalar: func(_ *exec.KernelCtx, Arg0 []T, Arg1 T, Out []T) error { + arithmeticArrScalarSSE4(typ, op, exec.GetBytes(Arg0), unsafe.Pointer(&Arg1), exec.GetBytes(Out), len(Out)) + return nil + }, + scalarArr: func(_ *exec.KernelCtx, Arg0 T, Arg1, Out []T) error { + arithmeticScalarArrSSE4(typ, op, unsafe.Pointer(&Arg0), exec.GetBytes(Arg1), exec.GetBytes(Out), len(Out)) + return nil + }, + } +} + +func getArithmeticBinaryOpsFloating[T constraints.Float](op ArithmeticOp) binaryOps[T, T, T] { + if cpu.X86.HasAVX2 { + return getAvx2ArithmeticBinaryNumeric[T](op) + } else if cpu.X86.HasSSE42 { + return getSSE4ArithmeticBinaryNumeric[T](op) + } + + return getGoArithmeticBinaryOpsFloating[T](op) Review Comment: The fallback pure-go implementation is needed for various cases and as of Go1.19, Go still can't inline assembly implemented functions. So it's worthwhile to fallback to the pure-go implementations when we aren't benefiting from the SIMD implementations to allow for whatever optimizations Go can do and so on along with portability. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
