[RFC,0/2] Add RISC-V vector math library support

Message ID 20260204181533.1498013-1-pincheng.plct@isrc.iscas.ac.cn
Headers
Series Add RISC-V vector math library support |

Message

Pincheng Wang Feb. 4, 2026, 6:15 p.m. UTC
  Hi all,

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
  

Comments

Brian Inglis Feb. 5, 2026, 9:52 a.m. UTC | #1
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
>
  
Joel Sherrill Feb. 5, 2026, 2:31 p.m. UTC | #2
On Thu, Feb 5, 2026, 3:53 AM Brian Inglis <Brian.Inglis@systematicsw.ab.ca>
wrote:

> Benefits? - Performance? Accuracy? - numbers to back that up?
>

Are the vector extensions enabled by any multilib variant?

At least for RTEMS, we do not build single variant tool chains. Our risc-v
tool chain supports 32 and 64 variants. The OS source uses the compiler
predefines to tailor to which cpu variant is being compiled to. What
multilib variant would your configuration correspond to?

When did GCC start having the builtins used?

--joel
RTEMS

>
>
> 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
> >
>
>
> --
> Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada
>
> La perfection est atteinte                   Perfection is achieved
> non pas lorsqu'il n'y a plus rien à ajouter  not when there is no more to
> add
> mais lorsqu'il n'y a plus rien à retrancher  but when there is no more to
> cut
>                                  -- Antoine de Saint-Exupéry
>
  
Pincheng Wang Feb. 26, 2026, 3:04 p.m. UTC | #3
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
>>
> 
>
  
Pincheng Wang Feb. 26, 2026, 4:45 p.m. UTC | #4
Hi Joel,

Thank you for the thoughtful questions regarding multilib configureation 
and gcc builtins. I 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.

For multilib variants, currently, neither gcc nor newlib defines a 
dedicated multilib variant solely for RISC-V vector extension (e.g a 
seperate rv64gcv). As noted in the cover letter, the current workflow 
requires both --enable-vecmath at configure time and -march=rv64gcv at 
compile time to enable vector math support.

This may indeed an area that may benefit from upstream toolchain 
coordination, especially given that the vector extension has now become 
a mandatory extension of the RVA23 profile. If the community sees value 
in introducing RVV-aware multilib vairiants like rv64gcv_lp64d, I would 
be happy to collaborate on adapting the build infrasturcture accordingly.

Regarding gcc builtins. If you refers to RVV intrinsics: intrinsics 
based on the v0.11 spec have been supported since gcc 13 and clang 16;
Ratified v1.0 intrinsics became available starting with gcc 14 and clang 
19. Our implementation uses v1.0 intrinsics, so gcc 14+ is required.
If you refers to vectorized math function symbols: The naming convention 
and ABI for these vector math functions are currently under review in 
the RISC-V psABI document [1]. The corresponding gcc patch to recognize 
and emit these calls has not yet been merged upstream. This is also why 
this patch series is send as an RFC to gather feedback before the ABI 
solidifies.

Thank you again for valubale insights from a practical application and 
development perspective. I look forward to further discussion and 
suggestions from the community.

Best regards,
Pincheng Wang

[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/455
On 2026/2/5 22:31, Joel Sherrill wrote:
> 
> 
> On Thu, Feb 5, 2026, 3:53 AM Brian Inglis 
> <Brian.Inglis@systematicsw.ab.ca 
> <mailto:Brian.Inglis@systematicsw.ab.ca>> wrote:
> 
>     Benefits? - Performance? Accuracy? - numbers to back that up?
> 
> 
> Are the vector extensions enabled by any multilib variant?
> 
> At least for RTEMS, we do not build single variant tool chains. Our 
> risc-v tool chain supports 32 and 64 variants. The OS source uses the 
> compiler predefines to tailor to which cpu variant is being compiled to. 
> What multilib variant would your configuration correspond to?
> 
> When did GCC start having the builtins used?
> 
> --joel
> RTEMS
> 
> 
> 
>     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 <https://gcc.gnu.org/pipermail/gcc-
>     patches/2024-November/667352.html>
>      > [2] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/455
>     <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 <http://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
>      >
> 
> 
>     -- 
>     Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada
> 
>     La perfection est atteinte                   Perfection is achieved
>     non pas lorsqu'il n'y a plus rien à ajouter  not when there is no
>     more to add
>     mais lorsqu'il n'y a plus rien à retrancher  but when there is no
>     more to cut
>                                       -- Antoine de Saint-Exupéry
>