diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 372bce8..ac0c709 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,6 +55,12 @@ jobs: cd erlang rebar3 compile rebar3 eunit + - name: build-test-golang + run: | + cd go + go build -o ruapu-go + ./ruapu-go + macos: runs-on: macos-latest @@ -79,6 +85,11 @@ jobs: cd rust cargo build --verbose cargo test --verbose + - name: build-test-golang + run: | + cd go + go build -o ruapu-go + ./ruapu-go macos-arm64: runs-on: macos-14 @@ -103,6 +114,14 @@ jobs: cd rust cargo build --verbose cargo test --verbose + - uses: actions/setup-go@v2 + with: + go-version: 1.21.x + - name: build-test-golang + run: | + cd go + go build -o ruapu-go + ./ruapu-go windows: runs-on: windows-latest @@ -142,6 +161,11 @@ jobs: cd erlang rebar3 compile rebar3 eunit + - name: build-test-golang + run: | + cd go + go build -o ruapu-go.exe + ./ruapu-go.exe qemu: runs-on: ubuntu-latest diff --git a/README.md b/README.md index 993cac8..be36c1a 100644 --- a/README.md +++ b/README.md @@ -264,6 +264,46 @@ end program main +### ruapu with Golang + + + + + + +
+ +Compile ruapu library + +```shell +cd go +go build -o ruapu-go +``` + + + +Use ruapu in Golang + +```go +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) +} +``` + +
+
Github-hosted runner result (Linux) diff --git a/go/go.mod b/go/go.mod new file mode 100644 index 0000000..01ff96d --- /dev/null +++ b/go/go.mod @@ -0,0 +1,3 @@ +module ruapu-go + +go 1.20 diff --git a/go/main.go b/go/main.go new file mode 100644 index 0000000..7f9eb2f --- /dev/null +++ b/go/main.go @@ -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) +} diff --git a/go/ruapu/ruapu-binding.go b/go/ruapu/ruapu-binding.go new file mode 100644 index 0000000..a8a6a51 --- /dev/null +++ b/go/ruapu/ruapu-binding.go @@ -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 +}