/*1* arch/xtensa/lib/strnlen_user.S2*3* This file is subject to the terms and conditions of the GNU General4* Public License. See the file "COPYING" in the main directory of5* this archive for more details.6*7* Returns strnlen, including trailing zero terminator.8* Zero indicates error.9*10* Copyright (C) 2002 Tensilica Inc.11*/1213#include <variant/core.h>1415/* Load or store instructions that may cause exceptions use the EX macro. */1617#define EX(insn,reg1,reg2,offset,handler) \189: insn reg1, reg2, offset; \19.section __ex_table, "a"; \20.word 9b, handler; \21.previous2223/*24* size_t __strnlen_user(const char *s, size_t len)25*/2627#ifdef __XTENSA_EB__28# define MASK0 0xff00000029# define MASK1 0x00ff000030# define MASK2 0x0000ff0031# define MASK3 0x000000ff32#else33# define MASK0 0x000000ff34# define MASK1 0x0000ff0035# define MASK2 0x00ff000036# define MASK3 0xff00000037#endif3839# Register use:40# a2/ src41# a3/ len42# a4/ tmp43# a5/ mask044# a6/ mask145# a7/ mask246# a8/ mask347# a9/ tmp48# a10/ tmp4950.text51.align 452.global __strnlen_user53.type __strnlen_user,@function54__strnlen_user:55entry sp, 16 # minimal stack frame56# a2/ s, a3/ len57addi a4, a2, -4 # because we overincrement at the end;58# we compensate with load offsets of 459movi a5, MASK0 # mask for byte 060movi a6, MASK1 # mask for byte 161movi a7, MASK2 # mask for byte 262movi a8, MASK3 # mask for byte 363bbsi.l a2, 0, .L1mod2 # if only 8-bit aligned64bbsi.l a2, 1, .L2mod4 # if only 16-bit aligned6566/*67* String is word-aligned.68*/69.Laligned:70srli a10, a3, 2 # number of loop iterations with 4B per loop71#if XCHAL_HAVE_LOOPS72loopnez a10, .Ldone73#else74beqz a10, .Ldone75slli a10, a10, 276add a10, a10, a4 # a10 = end of last 4B chunk77#endif /* XCHAL_HAVE_LOOPS */78.Loop:79EX(l32i, a9, a4, 4, lenfixup) # get next word of string80addi a4, a4, 4 # advance string pointer81bnone a9, a5, .Lz0 # if byte 0 is zero82bnone a9, a6, .Lz1 # if byte 1 is zero83bnone a9, a7, .Lz2 # if byte 2 is zero84bnone a9, a8, .Lz3 # if byte 3 is zero85#if !XCHAL_HAVE_LOOPS86blt a4, a10, .Loop87#endif8889.Ldone:90EX(l32i, a9, a4, 4, lenfixup) # load 4 bytes for remaining checks9192bbci.l a3, 1, .L10093# check two more bytes (bytes 0, 1 of word)94addi a4, a4, 2 # advance string pointer95bnone a9, a5, .Lz0 # if byte 0 is zero96bnone a9, a6, .Lz1 # if byte 1 is zero97.L100:98bbci.l a3, 0, .L10199# 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 have102# exhausted the original len parameter.103addi a4, a4, 1 # advance string pointer104.L101:105sub a2, a4, a2 # compute length106retw107108# NOTE that in several places below, we point to the byte just after109# the zero byte in order to include the NULL terminator in the count.110111.Lz3: # byte 3 is zero112addi a4, a4, 3 # point to zero byte113.Lz0: # byte 0 is zero114addi a4, a4, 1 # point just beyond zero byte115sub a2, a4, a2 # subtract to get length116retw117.Lz1: # byte 1 is zero118addi a4, a4, 1+1 # point just beyond zero byte119sub a2, a4, a2 # subtract to get length120retw121.Lz2: # byte 2 is zero122addi a4, a4, 2+1 # point just beyond zero byte123sub a2, a4, a2 # subtract to get length124retw125126.L1mod2: # address is odd127EX(l8ui, a9, a4, 4, lenfixup) # get byte 0128addi a4, a4, 1 # advance string pointer129beqz a9, .Lz3 # if byte 0 is zero130bbci.l a4, 1, .Laligned # if string pointer is now word-aligned131132.L2mod4: # address is 2 mod 4133addi a4, a4, 2 # advance ptr for aligned access134EX(l32i, a9, a4, 0, lenfixup) # get word with first two bytes of string135bnone a9, a7, .Lz2 # if byte 2 (of word, not string) is zero136bany a9, a8, .Laligned # if byte 3 (of word, not string) is nonzero137# byte 3 is zero138addi a4, a4, 3+1 # point just beyond zero byte139sub a2, a4, a2 # subtract to get length140retw141142.section .fixup, "ax"143.align 4144lenfixup:145movi a2, 0146retw147148149150