add go support (#59)

This commit is contained in:
2024-03-01 12:33:38 +08:00
committed by GitHub
parent 8ea0754e3f
commit f40a34c04b
5 changed files with 118 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
module ruapu-go
go 1.20
+15
View File
@@ -0,0 +1,15 @@
package main
import (
"fmt"
"ruapu-go/ruapu"
"strconv"
)
func main() {
ruapu.Init()
avx2Status := ruapu.Supports("avx2")
fmt.Println("avx2:" + strconv.Itoa(avx2Status))
rua := ruapu.Rua()
fmt.Println(rua)
}
+36
View File
@@ -0,0 +1,36 @@
package ruapu
/*
#cgo CFLAGS: -I ../../
#define RUAPU_IMPLEMENTATION
#include "ruapu.h"
#include "stdlib.h"
*/
import "C"
import (
"unsafe"
)
func Init() {
C.ruapu_init()
}
func Supports(isa string) int {
ptr := C.CString(isa)
defer C.free(unsafe.Pointer(ptr))
result := int(C.ruapu_supports(ptr))
return result
}
func Rua() []string {
p := unsafe.Pointer(C.ruapu_rua())
s := make([]string, 0, 1)
for i := 0; ; i++ {
q := *(*unsafe.Pointer)(unsafe.Pointer(uintptr(p) + uintptr(i)*unsafe.Sizeof(p)))
if q == nil {
break
}
s = append(s, C.GoString((*C.char)(q)))
}
return s
}