From b42485d56ec646bd1b77091feac03f3f50641d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=9A=E6=9C=A8=20=E9=89=89?= Date: Sat, 2 Mar 2024 10:52:39 +0800 Subject: [PATCH] add vlang binding (#67) --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ vlang/ruapu.v | 13 +++++++++++++ vlang/ruapu/wrapper.c.v | 33 +++++++++++++++++++++++++++++++++ vlang/v.mod | 5 +++++ 4 files changed, 89 insertions(+) create mode 100644 vlang/ruapu.v create mode 100644 vlang/ruapu/wrapper.c.v create mode 100644 vlang/v.mod diff --git a/README.md b/README.md index 4996520..27607af 100644 --- a/README.md +++ b/README.md @@ -331,6 +331,44 @@ main = do +### ruapu with Vlang + + + + + + +
+ +Compile ruapu library + +```shell +cd vlang +v . +``` + + + +Use ruapu in Vlang + +```go +module main + +import ruapu + +fn main() { + ruapu.ruapu_init() + mut avx2_status := ruapu.ruapu_supports('avx2') + if avx2_status { + println('avx2: ' + avx2_status.str()) + } + + println(ruapu.ruapu_rua()) +} +``` + +
+
Github-hosted runner result (Linux) diff --git a/vlang/ruapu.v b/vlang/ruapu.v new file mode 100644 index 0000000..7b7c2a3 --- /dev/null +++ b/vlang/ruapu.v @@ -0,0 +1,13 @@ +module main + +import ruapu + +fn main() { + ruapu.ruapu_init() + mut avx2_status := ruapu.ruapu_supports('avx2') + if avx2_status { + println('avx2: ' + avx2_status.str()) + } + + println(ruapu.ruapu_rua()) +} \ No newline at end of file diff --git a/vlang/ruapu/wrapper.c.v b/vlang/ruapu/wrapper.c.v new file mode 100644 index 0000000..e398d14 --- /dev/null +++ b/vlang/ruapu/wrapper.c.v @@ -0,0 +1,33 @@ +module ruapu + +#flag -I @VMODROOT/../ +#flag -D RUAPU_IMPLEMENTATION +#include "ruapu.h" + +fn C.ruapu_init() + +fn C.ruapu_supports(&char) int + +fn C.ruapu_rua() &&char + +pub fn ruapu_init() { + C.ruapu_init() +} + +pub fn ruapu_supports(isa string) bool { + return C.ruapu_supports(unsafe { isa.str }) == 1 +} + +pub fn ruapu_rua() []string { + mut cstr_array := C.ruapu_rua() + mut v_strings := []string{} + + unsafe { + for i := 0; cstr_array[i] != 0; i++ { + str := cstring_to_vstring(cstr_array[i]) + v_strings << str + } + } + + return v_strings +} diff --git a/vlang/v.mod b/vlang/v.mod new file mode 100644 index 0000000..a72127c --- /dev/null +++ b/vlang/v.mod @@ -0,0 +1,5 @@ +Module { + name: 'ruapu', + description: 'Detect CPU ISA features with single-file.', + dependencies: [] +} \ No newline at end of file