github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/vector/hnsw/distancer/asm/dot_stub_arm64.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package asm
    13  
    14  // To generate the asm code, run:
    15  //   go install github.com/gorse-io/goat@v0.1.0
    16  //   go generate
    17  
    18  //go:generate goat ../c/dot_arm64.c -O3 -e="-mfpu=neon-fp-armv8" -e="-mfloat-abi=hard" -e="--target=arm64" -e="-march=armv8-a+simd+fp"
    19  
    20  import (
    21  	"reflect"
    22  	"unsafe"
    23  )
    24  
    25  // Dot calculates the dot product between two vectors
    26  // using SIMD instructions.
    27  func Dot(x []float32, y []float32) float32 {
    28  	switch len(x) {
    29  	case 2:
    30  		return dot2(x, y)
    31  	case 4:
    32  		return dot4(x, y)
    33  	case 6:
    34  		return dot6(x, y)
    35  	case 8:
    36  		// manually inlined dot8(x, y)
    37  		sum := x[7]*y[7] + x[6]*y[6]
    38  		return dot6(x, y) + sum
    39  	case 10:
    40  		// manually inlined dot10(x, y)
    41  		sum := x[9]*y[9] + x[8]*y[8] + x[7]*y[7] + x[6]*y[6]
    42  		return dot6(x, y) + sum
    43  	case 12:
    44  		// manually inlined dot12(x, y)
    45  		sum := x[11]*y[11] + x[10]*y[10] + x[9]*y[9] + x[8]*y[8] + x[7]*y[7] + x[6]*y[6]
    46  		return dot6(x, y) + sum
    47  	}
    48  
    49  	var res float32
    50  
    51  	// The C function expects pointers to the underlying array, not slices.
    52  	hdrx := (*reflect.SliceHeader)(unsafe.Pointer(&x))
    53  	hdry := (*reflect.SliceHeader)(unsafe.Pointer(&y))
    54  
    55  	l := len(x)
    56  	dot(
    57  		// The slice header contains the address of the underlying array.
    58  		// We only need to cast it to a pointer.
    59  		unsafe.Pointer(hdrx.Data),
    60  		unsafe.Pointer(hdry.Data),
    61  		// The C function expects pointers to the result and the length of the arrays.
    62  		unsafe.Pointer(&res),
    63  		unsafe.Pointer(&l))
    64  
    65  	return res
    66  }