Go Alignment

So, I use c2go to link C code with Go. C code requires specific arguments to a function called Go to be 256-bit aligned (function arguments are all pointers to Go variables). Is there a way to achieve this - Go (i.e. Specify 256 alignments for the variable in Go)?

In Go, "unsafe.Alignof (f)" shows how 8 bytes are aligned (for "var f [8] float32"), that is, f is guaranteed by Go to only be 8 bytes aligned. I need it to be somehow 32 bytes.

For the curious: C code uses SIMD instructions (specific AVX). I used the "vmovaps" command, which requires 256-bit alignment of the operands. I can get away using "vmovups", which does not require alignment, but I suspect it has a performance penalty.

+4
source share
2 answers

For example, trading more memory in less processor time,

package main

import (
    "fmt"
    "unsafe"
)

// Float32Align32 returns make([]float32, n) 32-byte aligned.
func Float32Align32(n int) []float32 {
    // align >= size && align%size == 0
    const align = 32 // SIMD AVX byte alignment
    const size = unsafe.Sizeof(float32(0))
    const pad = int(align/size - 1)
    if n <= 0 {
        return nil
    }
    s := make([]float32, n+pad)
    p := uintptr(unsafe.Pointer(&s[0]))
    i := int(((p+align-1)/align*align - p) / size)
    j := i + n
    return s[i:j:j]
}

func main() {
    f := Float32Align32(8) // SIMD AVX
    fmt.Printf(
        "SIMD AVX: %T %d %d %p %g\n",
        f, len(f), cap(f), &f[0], f,
    )
    CFuncArg := &f[0]
    fmt.Println("CFuncArg:", CFuncArg)
}

Playground: https://play.golang.org/p/mmFnHEwGKt

Conclusion:

SIMD AVX: []float32 8 8 0x10436080 [0 0 0 0 0 0 0 0]
CFuncArg: 0x10436080
0
source

The only reasonable way to achieve this is to prototype the functions in go, and then write the assembly (go) as directives BYTEand WORD, as was done in the golang libraries themselves, as described in glang-1.9.1 documentation

Unsupported Operation Codes

, : , , . , . - , , , , . , , BYTE WORD TEXT.

,

blake2b 115 AVX2

0

Source: https://habr.com/ru/post/1689517/


All Articles