From 070f52dda3dc7e82d3446acca07718efd9c890b6 Mon Sep 17 00:00:00 2001 From: Yoh <550856122@qq.com> Date: Thu, 29 Feb 2024 21:36:35 +0800 Subject: [PATCH] Enhance Python API to support retrieval of all available ISAs (#51) * Enhance Python API to support retrieval of all available ISAs * add rua! ci * migrate to ruapu_rua() --- .github/workflows/ci.yml | 4 ++++ README.md | 3 +++ python/ruapu-binding.c | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 654efa3..2fbb4fb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,6 +38,7 @@ jobs: cd python pip3 install . python3 -c 'import ruapu; print(ruapu.supports("neon")); print(ruapu.supports(isa="avx"))' + python3 -c 'import ruapu; print(ruapu.rua())' - name: build-test-rust run: | rustup update stable @@ -61,6 +62,7 @@ jobs: cd python pip3 install . python3 -c 'import ruapu; print(ruapu.supports("neon")); print(ruapu.supports(isa="avx"))' + python3 -c 'import ruapu; print(ruapu.rua())' - name: build-test-rust run: | rustup update stable @@ -84,6 +86,7 @@ jobs: cd python pip3 install . python3 -c 'import ruapu; print(ruapu.supports("neon")); print(ruapu.supports(isa="avx"))' + python3 -c 'import ruapu; print(ruapu.rua())' - name: build-test-rust run: | rustup update stable @@ -113,6 +116,7 @@ jobs: cd python pip3 install . python3 -c 'import ruapu; print(ruapu.supports("neon")); print(ruapu.supports(isa="avx"))' + python3 -c 'import ruapu; print(ruapu.rua())' - name: build-test-rust run: | rustup update stable diff --git a/README.md b/README.md index d214cff..f5f8e02 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,9 @@ ruapu.supports("avx2") ruapu.supports(isa="avx2") # True + +ruapu.rua() +#(mmx', 'sse', 'sse2', 'sse3', 'ssse3', 'sse41', 'sse42', 'avx', 'f16c', 'fma', 'avx2') ``` diff --git a/python/ruapu-binding.c b/python/ruapu-binding.c index 03c2712..e738fdd 100644 --- a/python/ruapu-binding.c +++ b/python/ruapu-binding.c @@ -15,9 +15,33 @@ static PyObject *ruapu_supports_py(PyObject *self, PyObject *args, PyObject *kwa Py_RETURN_FALSE; } +static PyObject *get_isa_items_py(PyObject *self, PyObject *args, PyObject *kwargs) +{ + const char* const* isa_supported = ruapu_rua(); + int total = 0; + while(*isa_supported) + { + total++; + isa_supported++; + } + + isa_supported = ruapu_rua(); + PyObject* supported_isa_py = PyTuple_New(total); + + int tuple_idx = 0; + while(*isa_supported) + { + PyTuple_SetItem(supported_isa_py, tuple_idx++, PyUnicode_FromString(*isa_supported)); + isa_supported++; + } + + return supported_isa_py; +} + static PyMethodDef ruapu_methods[] = { {"supports", ruapu_supports_py, METH_VARARGS | METH_KEYWORDS, "Check if the CPU supports an instruction set"}, + {"rua", get_isa_items_py, METH_VARARGS | METH_KEYWORDS, "Get the instruction sets supported by the current CPU"}, {NULL, NULL, 0, NULL} };