Page MenuHomeFreeBSD

libc: scalar strrchr() in RISC-V assembly
ClosedPublic

Authored by strajabot on Oct 24 2024, 6:31 PM.
Tags
None
Referenced Files
Unknown Object (File)
Fri, Oct 31, 11:36 AM
Unknown Object (File)
Wed, Oct 29, 9:05 AM
Unknown Object (File)
Wed, Oct 29, 9:05 AM
Unknown Object (File)
Wed, Oct 29, 9:05 AM
Unknown Object (File)
Wed, Oct 29, 8:42 AM
Unknown Object (File)
Mon, Oct 27, 9:31 AM
Unknown Object (File)
Mon, Oct 27, 9:31 AM
Unknown Object (File)
Mon, Oct 27, 9:31 AM
Subscribers

Details

Summary

Implements strrchr in RISC-V assembly, leading to the following
improvements (performance measured on SiFive HF105-001)

os: FreeBSD
arch: riscv
        │ strrchr_baseline │             strrchr_scalar             │
        │      sec/op      │   sec/op     vs base                   │
Short          837.2µ ± 1%   574.6µ ± 1%  -31.37% (p=0.000 n=20+21)
Mid            639.7µ ± 0%   269.7µ ± 0%  -57.84% (p=0.000 n=20+21)
Long           589.1µ ± 0%   176.7µ ± 0%  -70.01% (p=0.000 n=20+21)
geomean        680.8µ        301.4µ       -55.73%
 
        │ strrchr_baseline │             strrchr_scalar             │
        │      MiB/s       │   MiB/s     vs base                    │
Short           149.3 ± 1%   217.6 ± 1%   +45.71% (p=0.000 n=20+21)
Mid             195.4 ± 0%   463.6 ± 0%  +137.22% (p=0.000 n=20+21)
Long            212.2 ± 0%   707.4 ± 0%  +233.40% (p=0.000 n=20+21)
geomean         183.6        414.7       +125.88%
Test Plan

Tested using in-tree test suite, and my custom test.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

This looks pretty reasonable.

We have discussed this function a bunch while @strajabot designed the algorithm and came to the conclusion that it's not easy to do better than a linear search on the tail.

mhorne added a subscriber: mhorne.
mhorne added inline comments.
lib/libc/riscv/string/strrchr.S
11
15–26

I am nitpicking.

This revision is now accepted and ready to land.Aug 19 2025, 7:00 PM
  • fixup: address comments on strrchr
This revision now requires review to proceed.Sep 22 2025, 8:48 PM

Addressed the comments on the diff

This revision is now accepted and ready to land.Tue, Oct 21, 7:16 PM