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 <asm/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)
31 .byte 0xff, 0x00, 0x00, 0x00
33 .byte 0x00, 0xff, 0x00, 0x00
35 .byte 0x00, 0x00, 0xff, 0x00
37 .byte 0x00, 0x00, 0x00, 0xff
52 .global __strnlen_user
53 .type __strnlen_user,@function
55 entry sp, 16 # minimal stack frame
57 addi a4, a2, -4 # because we overincrement at the end;
58 # we compensate with load offsets of 4
59 l32r a5, .Lmask0 # mask for byte 0
60 l32r a6, .Lmask1 # mask for byte 1
61 l32r a7, .Lmask2 # mask for byte 2
62 l32r a8, .Lmask3 # mask for byte 3
63 bbsi.l a2, 0, .L1mod2 # if only 8-bit aligned
64 bbsi.l a2, 1, .L2mod4 # if only 16-bit aligned
67 * String is word-aligned.
70 srli a10, a3, 2 # number of loop iterations with 4B per loop
76 add a10, a10, a4 # a10 = end of last 4B chunk
77 #endif /* XCHAL_HAVE_LOOPS */
79 EX(l32i, a9, a4, 4, lenfixup) # get next word of string
80 addi a4, a4, 4 # advance string pointer
81 bnone a9, a5, .Lz0 # if byte 0 is zero
82 bnone a9, a6, .Lz1 # if byte 1 is zero
83 bnone a9, a7, .Lz2 # if byte 2 is zero
84 bnone a9, a8, .Lz3 # if byte 3 is zero
90 EX(l32i, a9, a4, 4, lenfixup) # load 4 bytes for remaining checks
93 # check two more bytes (bytes 0, 1 of word)
94 addi a4, a4, 2 # advance string pointer
95 bnone a9, a5, .Lz0 # if byte 0 is zero
96 bnone a9, a6, .Lz1 # if byte 1 is zero
99 # check one more byte (byte 2 of word)
100 # Actually, we don't need to check. Zero or nonzero, we'll add one.
101 # Do not add an extra one for the NULL terminator since we have
102 # exhausted the original len parameter.
103 addi a4, a4, 1 # advance string pointer
105 sub a2, a4, a2 # compute length
108 # NOTE that in several places below, we point to the byte just after
109 # the zero byte in order to include the NULL terminator in the count.
111 .Lz3: # byte 3 is zero
112 addi a4, a4, 3 # point to zero byte
113 .Lz0: # byte 0 is zero
114 addi a4, a4, 1 # point just beyond zero byte
115 sub a2, a4, a2 # subtract to get length
117 .Lz1: # byte 1 is zero
118 addi a4, a4, 1+1 # point just beyond zero byte
119 sub a2, a4, a2 # subtract to get length
121 .Lz2: # byte 2 is zero
122 addi a4, a4, 2+1 # point just beyond zero byte
123 sub a2, a4, a2 # subtract to get length
126 .L1mod2: # address is odd
127 EX(l8ui, a9, a4, 4, lenfixup) # get byte 0
128 addi a4, a4, 1 # advance string pointer
129 beqz a9, .Lz3 # if byte 0 is zero
130 bbci.l a4, 1, .Laligned # if string pointer is now word-aligned
132 .L2mod4: # address is 2 mod 4
133 addi a4, a4, 2 # advance ptr for aligned access
134 EX(l32i, a9, a4, 0, lenfixup) # get word with first two bytes of string
135 bnone a9, a7, .Lz2 # if byte 2 (of word, not string) is zero
136 bany a9, a8, .Laligned # if byte 3 (of word, not string) is nonzero
138 addi a4, a4, 3+1 # point just beyond zero byte
139 sub a2, a4, a2 # subtract to get length
142 .section .fixup, "ax"