2 * arch/xtensa/lib/strnlen_user.S
4 * This file is subject to the terms and conditions of the GNU General
5 * Public License. See the file "COPYING" in the main directory of
6 * this archive for more details.
8 * Returns strnlen, including trailing zero terminator.
9 * Zero indicates error.
11 * Copyright (C) 2002 Tensilica Inc.
14 #include <variant/core.h>
16 /* Load or store instructions that may cause exceptions use the EX macro. */
18 #define EX(insn,reg1,reg2,offset,handler) \
19 9: insn reg1, reg2, offset; \
20 .section __ex_table, "a"; \
25 * size_t __strnlen_user(const char *s, size_t len)
29 # define MASK0 0xff000000
30 # define MASK1 0x00ff0000
31 # define MASK2 0x0000ff00
32 # define MASK3 0x000000ff
34 # define MASK0 0x000000ff
35 # define MASK1 0x0000ff00
36 # define MASK2 0x00ff0000
37 # define MASK3 0xff000000
53 .global __strnlen_user
54 .type __strnlen_user,@function
56 entry sp, 16 # minimal stack frame
58 addi a4, a2, -4 # because we overincrement at the end;
59 # we compensate with load offsets of 4
60 movi a5, MASK0 # mask for byte 0
61 movi a6, MASK1 # mask for byte 1
62 movi a7, MASK2 # mask for byte 2
63 movi a8, MASK3 # mask for byte 3
64 bbsi.l a2, 0, .L1mod2 # if only 8-bit aligned
65 bbsi.l a2, 1, .L2mod4 # if only 16-bit aligned
68 * String is word-aligned.
71 srli a10, a3, 2 # number of loop iterations with 4B per loop
77 add a10, a10, a4 # a10 = end of last 4B chunk
78 #endif /* XCHAL_HAVE_LOOPS */
80 EX(l32i, a9, a4, 4, lenfixup) # get next word of string
81 addi a4, a4, 4 # advance string pointer
82 bnone a9, a5, .Lz0 # if byte 0 is zero
83 bnone a9, a6, .Lz1 # if byte 1 is zero
84 bnone a9, a7, .Lz2 # if byte 2 is zero
85 bnone a9, a8, .Lz3 # if byte 3 is zero
91 EX(l32i, a9, a4, 4, lenfixup) # load 4 bytes for remaining checks
94 # check two more bytes (bytes 0, 1 of word)
95 addi a4, a4, 2 # advance string pointer
96 bnone a9, a5, .Lz0 # if byte 0 is zero
97 bnone a9, a6, .Lz1 # if byte 1 is zero
100 # check one more byte (byte 2 of word)
101 # Actually, we don't need to check. Zero or nonzero, we'll add one.
102 # Do not add an extra one for the NULL terminator since we have
103 # exhausted the original len parameter.
104 addi a4, a4, 1 # advance string pointer
106 sub a2, a4, a2 # compute length
109 # NOTE that in several places below, we point to the byte just after
110 # the zero byte in order to include the NULL terminator in the count.
112 .Lz3: # byte 3 is zero
113 addi a4, a4, 3 # point to zero byte
114 .Lz0: # byte 0 is zero
115 addi a4, a4, 1 # point just beyond zero byte
116 sub a2, a4, a2 # subtract to get length
118 .Lz1: # byte 1 is zero
119 addi a4, a4, 1+1 # point just beyond zero byte
120 sub a2, a4, a2 # subtract to get length
122 .Lz2: # byte 2 is zero
123 addi a4, a4, 2+1 # point just beyond zero byte
124 sub a2, a4, a2 # subtract to get length
127 .L1mod2: # address is odd
128 EX(l8ui, a9, a4, 4, lenfixup) # get byte 0
129 addi a4, a4, 1 # advance string pointer
130 beqz a9, .Lz3 # if byte 0 is zero
131 bbci.l a4, 1, .Laligned # if string pointer is now word-aligned
133 .L2mod4: # address is 2 mod 4
134 addi a4, a4, 2 # advance ptr for aligned access
135 EX(l32i, a9, a4, 0, lenfixup) # get word with first two bytes of string
136 bnone a9, a7, .Lz2 # if byte 2 (of word, not string) is zero
137 bany a9, a8, .Laligned # if byte 3 (of word, not string) is nonzero
139 addi a4, a4, 3+1 # point just beyond zero byte
140 sub a2, a4, a2 # subtract to get length
143 .section .fixup, "ax"