Re: [RFC PATCH 0/2] Add RISC-V vector math library support
Pincheng Wang <[email protected]> Thu, 26 Feb 2026 23:04:58 +0800
| Newsgroups | gmane.comp.lib.newlib |
|---|---|
| Message-ID | <[email protected]> |
Hi Brian, Apologies for the delayed response. This email somehow didn't appear in my inbox, and I only came across it while browsing the mailing list archive. Regarding benefits and performance, the primary benefit of this patch manifests when code contains loops (or similar patterns recognizable by the compiler for auto-vectorization, e.g, consecutive math function calls within a basic block. Please see the gcc patch logic for details) that invoke math functions. In such cases, multiple scalar function calls can be consolidated into a single vectorized call, resulting measurable performance gains. On the Spacemit X60 CPU, our benchmarks for functions like sin() and cos() showed up to 5.40x speedup compared to the scalar implementation. For Accuracy, the vectorized implementations are adapted from the SLEEF project with target-specifir tuning for RISC-V. Through extensive random sampling tests, we have verified that the error remains within 1ULP across the tests. For reference, glibc's vector math library on x86 targets typically guarantees accuracy within 3ULP. While nelib primarily targets embedded systems, we believe this optimization benefits with less friction. First, applications can benefit automatically with the patched gcc toolchain and appropriate flags without rewriting application codes. If vector math is not applicable or desired, the disabled by default configure option ensures no unintended side effects. Second, certail embedded workloads such as signal processing and sensor fusion do involve high throughput math computations, where vectorized math functions can meaningfully improve efficiency. Thank you very much for your questions. We welcome any further feedback or suggestions from the community! Best regards, Pincheng Wang On 2026/2/5 17:52, Brian Inglis wrote: > Benefits? - Performance? Accuracy? - numbers to back that up? > > On 2026-02-04 11:15, Pincheng Wang wrote: >> This RFC patch series propose adding RISC-V Vector Extension (RVV) >> optimized math functions to newlib, starting with a vectorized >> implementation of sin(). >> >> The motivation is to enable automatic vectorization of math functions on >> RISC-V platforms with vector extensions, which can provide significant >> performance improvements for numerical applications. >> >> After applying the gcc vecmath patch[1], compiler supports automatic >> vectorzation using the vector ABI[2](also in RFC). When compiling with >> auto-vectorization enabled (-O3 -ffast-math), the compiler can replace >> scalar math function calls with vector variants if they are available in >> the math library. >> >> This patch series adds infrastructure support for building vector math >> functions conditionally via a newly added "--enable-vecmath" configure >> option (disabled by default), and a vectorized sin() implementation for >> RISC-V using intrinsics from the RISC-V Vector Extension. >> >> While adding the above option, I encountered an automake compatibility >> issue during math object copying. Therefore, commit 1 is dedicated to >> fixing that issue and does not involve any vector math changes. >> >> The vector sin implementation is derived from the SLEEF library (Boost >> licensed) and adapted for newlib's RISC-V target. It uses RVV 1.0 >> intrinsics and provides performance-optimized computation with proper >> handling of edge cases. >> >> To configure and build, use below command. >> configure --target=riscv64-unknown-elf \ >> --prefix=/opt/vecmath-newlib \ >> CFLAGS_FOR_TARGET="-O2 -march=rv64gcv -mabi=lp64d" \ >> --enable-vecmath >> >> To compile a program that can take >> benefit of this patch, use the patched[1] gcc and run command like >> below. >> /opt/vecmath-gcc/bin/riscv64-unknown-elf-gcc \ >> -O3 -ffast-math -march=rv64gcv \ >> --sysroot=/opt/vecmath-newlib \ >> -L/opt/vecmath-newlib/riscv64-unknown-elf/lib \ >> -I/opt/vecmath-newlib/riscv64-unknown-elf/include \ >> test_sin.c -o test_sin -lm >> >> Comments and suggestions are welcome! >> >> [1] https://gcc.gnu.org/pipermail/gcc-patches/2024-November/667352.html >> [2] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/455 >> >> Thanks, >> Pincheng Wang >> >> Pincheng Wang (2): >> newlib: Fix automake 1.16 compatibility for math object copying >> newlib: Add RISC-V vector math library support >> >> newlib/Makefile.am | 26 +- >> newlib/configure.ac | 10 + >> newlib/libc/include/math.h | 1 + >> newlib/libm/machine/riscv/Makefile.inc | 5 + >> newlib/libm/machine/riscv/riscv-vec-math.h | 1461 ++++++++++++++++++++ >> newlib/libm/machine/riscv/vec_sin.c | 79 ++ >> 6 files changed, 1569 insertions(+), 13 deletions(-) >> create mode 100644 newlib/libm/machine/riscv/riscv-vec-math.h >> create mode 100644 newlib/libm/machine/riscv/vec_sin.c >> > >