-
Notifications
You must be signed in to change notification settings - Fork 12
/
memchr.s
26 lines (23 loc) · 815 Bytes
/
memchr.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 2020, Georg Sauthoff <[email protected]>, LGPLv3+
.text
# .balign 4
# void *memchr(const void *src, int c, size_t n);
#
# a0 = src, a1 = c, a2 = n
#
.global memchr
memchr:
vsetvli a3, a2, e8, m8 # switch to 8 bit element size,
# i.e. 4 groups of 8 registers
vlb.v v8, (a0) # load a3 bytes
vmseq.vx v0, v8, a1 # set mask bit if equal to scalar c
vfirst.m a4, v0 # find lowest index of set mask bit
bgez a4, 1f # branch if greater-or-equal to zero
add a0, a0, a3 # increment src
sub a2, a2, a3 # decrement n
bnez a2, memchr # branch if not-equal to zero, i.e. continue loop
li a0, 0
ret
1:
add a0, a0, a4 # increment src by match offset
ret