1249ac17eSChris Zankel/* 2249ac17eSChris Zankel * arch/xtensa/lib/strnlen_user.S 3249ac17eSChris Zankel * 4249ac17eSChris Zankel * This file is subject to the terms and conditions of the GNU General 5249ac17eSChris Zankel * Public License. See the file "COPYING" in the main directory of 6249ac17eSChris Zankel * this archive for more details. 7249ac17eSChris Zankel * 8249ac17eSChris Zankel * Returns strnlen, including trailing zero terminator. 9249ac17eSChris Zankel * Zero indicates error. 10249ac17eSChris Zankel * 11249ac17eSChris Zankel * Copyright (C) 2002 Tensilica Inc. 12249ac17eSChris Zankel */ 13249ac17eSChris Zankel 14367b8112SChris Zankel#include <variant/core.h> 150013acebSMax Filippov#include <asm/asmmacro.h> 16249ac17eSChris Zankel 17249ac17eSChris Zankel/* 18249ac17eSChris Zankel * size_t __strnlen_user(const char *s, size_t len) 19249ac17eSChris Zankel */ 20a0bb46baSChris Zankel 21a0bb46baSChris Zankel#ifdef __XTENSA_EB__ 22a0bb46baSChris Zankel# define MASK0 0xff000000 23a0bb46baSChris Zankel# define MASK1 0x00ff0000 24a0bb46baSChris Zankel# define MASK2 0x0000ff00 25a0bb46baSChris Zankel# define MASK3 0x000000ff 26a0bb46baSChris Zankel#else 27a0bb46baSChris Zankel# define MASK0 0x000000ff 28a0bb46baSChris Zankel# define MASK1 0x0000ff00 29a0bb46baSChris Zankel# define MASK2 0x00ff0000 30a0bb46baSChris Zankel# define MASK3 0xff000000 31a0bb46baSChris Zankel#endif 32249ac17eSChris Zankel 33249ac17eSChris Zankel# Register use: 34249ac17eSChris Zankel# a2/ src 35249ac17eSChris Zankel# a3/ len 36249ac17eSChris Zankel# a4/ tmp 37249ac17eSChris Zankel# a5/ mask0 38249ac17eSChris Zankel# a6/ mask1 39249ac17eSChris Zankel# a7/ mask2 40249ac17eSChris Zankel# a8/ mask3 41249ac17eSChris Zankel# a9/ tmp 42249ac17eSChris Zankel# a10/ tmp 43249ac17eSChris Zankel 44a0bb46baSChris Zankel.text 45249ac17eSChris Zankel.align 4 46249ac17eSChris Zankel.global __strnlen_user 47249ac17eSChris Zankel.type __strnlen_user,@function 48249ac17eSChris Zankel__strnlen_user: 49249ac17eSChris Zankel entry sp, 16 # minimal stack frame 50249ac17eSChris Zankel # a2/ s, a3/ len 51249ac17eSChris Zankel addi a4, a2, -4 # because we overincrement at the end; 52249ac17eSChris Zankel # we compensate with load offsets of 4 53a0bb46baSChris Zankel movi a5, MASK0 # mask for byte 0 54a0bb46baSChris Zankel movi a6, MASK1 # mask for byte 1 55a0bb46baSChris Zankel movi a7, MASK2 # mask for byte 2 56a0bb46baSChris Zankel movi a8, MASK3 # mask for byte 3 57249ac17eSChris Zankel bbsi.l a2, 0, .L1mod2 # if only 8-bit aligned 58249ac17eSChris Zankel bbsi.l a2, 1, .L2mod4 # if only 16-bit aligned 59249ac17eSChris Zankel 60249ac17eSChris Zankel/* 61249ac17eSChris Zankel * String is word-aligned. 62249ac17eSChris Zankel */ 63249ac17eSChris Zankel.Laligned: 64249ac17eSChris Zankel srli a10, a3, 2 # number of loop iterations with 4B per loop 65249ac17eSChris Zankel#if XCHAL_HAVE_LOOPS 66249ac17eSChris Zankel loopnez a10, .Ldone 67249ac17eSChris Zankel#else 68249ac17eSChris Zankel beqz a10, .Ldone 69249ac17eSChris Zankel slli a10, a10, 2 70249ac17eSChris Zankel add a10, a10, a4 # a10 = end of last 4B chunk 71249ac17eSChris Zankel#endif /* XCHAL_HAVE_LOOPS */ 72249ac17eSChris Zankel.Loop: 730013acebSMax FilippovEX(10f) l32i a9, a4, 4 # get next word of string 74249ac17eSChris Zankel addi a4, a4, 4 # advance string pointer 75249ac17eSChris Zankel bnone a9, a5, .Lz0 # if byte 0 is zero 76249ac17eSChris Zankel bnone a9, a6, .Lz1 # if byte 1 is zero 77249ac17eSChris Zankel bnone a9, a7, .Lz2 # if byte 2 is zero 78249ac17eSChris Zankel bnone a9, a8, .Lz3 # if byte 3 is zero 79249ac17eSChris Zankel#if !XCHAL_HAVE_LOOPS 80249ac17eSChris Zankel blt a4, a10, .Loop 81249ac17eSChris Zankel#endif 82249ac17eSChris Zankel 83249ac17eSChris Zankel.Ldone: 840013acebSMax FilippovEX(10f) l32i a9, a4, 4 # load 4 bytes for remaining checks 85249ac17eSChris Zankel 86249ac17eSChris Zankel bbci.l a3, 1, .L100 87249ac17eSChris Zankel # check two more bytes (bytes 0, 1 of word) 88249ac17eSChris Zankel addi a4, a4, 2 # advance string pointer 89249ac17eSChris Zankel bnone a9, a5, .Lz0 # if byte 0 is zero 90249ac17eSChris Zankel bnone a9, a6, .Lz1 # if byte 1 is zero 91249ac17eSChris Zankel.L100: 92249ac17eSChris Zankel bbci.l a3, 0, .L101 93249ac17eSChris Zankel # check one more byte (byte 2 of word) 94249ac17eSChris Zankel # Actually, we don't need to check. Zero or nonzero, we'll add one. 95249ac17eSChris Zankel # Do not add an extra one for the NULL terminator since we have 96249ac17eSChris Zankel # exhausted the original len parameter. 97249ac17eSChris Zankel addi a4, a4, 1 # advance string pointer 98249ac17eSChris Zankel.L101: 99249ac17eSChris Zankel sub a2, a4, a2 # compute length 100249ac17eSChris Zankel retw 101249ac17eSChris Zankel 102249ac17eSChris Zankel# NOTE that in several places below, we point to the byte just after 103249ac17eSChris Zankel# the zero byte in order to include the NULL terminator in the count. 104249ac17eSChris Zankel 105249ac17eSChris Zankel.Lz3: # byte 3 is zero 106249ac17eSChris Zankel addi a4, a4, 3 # point to zero byte 107249ac17eSChris Zankel.Lz0: # byte 0 is zero 108249ac17eSChris Zankel addi a4, a4, 1 # point just beyond zero byte 109249ac17eSChris Zankel sub a2, a4, a2 # subtract to get length 110249ac17eSChris Zankel retw 111249ac17eSChris Zankel.Lz1: # byte 1 is zero 112249ac17eSChris Zankel addi a4, a4, 1+1 # point just beyond zero byte 113249ac17eSChris Zankel sub a2, a4, a2 # subtract to get length 114249ac17eSChris Zankel retw 115249ac17eSChris Zankel.Lz2: # byte 2 is zero 116249ac17eSChris Zankel addi a4, a4, 2+1 # point just beyond zero byte 117249ac17eSChris Zankel sub a2, a4, a2 # subtract to get length 118249ac17eSChris Zankel retw 119249ac17eSChris Zankel 120249ac17eSChris Zankel.L1mod2: # address is odd 1210013acebSMax FilippovEX(10f) l8ui a9, a4, 4 # get byte 0 122249ac17eSChris Zankel addi a4, a4, 1 # advance string pointer 123249ac17eSChris Zankel beqz a9, .Lz3 # if byte 0 is zero 124249ac17eSChris Zankel bbci.l a4, 1, .Laligned # if string pointer is now word-aligned 125249ac17eSChris Zankel 126249ac17eSChris Zankel.L2mod4: # address is 2 mod 4 127249ac17eSChris Zankel addi a4, a4, 2 # advance ptr for aligned access 1280013acebSMax FilippovEX(10f) l32i a9, a4, 0 # get word with first two bytes of string 129249ac17eSChris Zankel bnone a9, a7, .Lz2 # if byte 2 (of word, not string) is zero 130249ac17eSChris Zankel bany a9, a8, .Laligned # if byte 3 (of word, not string) is nonzero 131249ac17eSChris Zankel # byte 3 is zero 132249ac17eSChris Zankel addi a4, a4, 3+1 # point just beyond zero byte 133249ac17eSChris Zankel sub a2, a4, a2 # subtract to get length 134249ac17eSChris Zankel retw 135249ac17eSChris Zankel 136249ac17eSChris Zankel .section .fixup, "ax" 137249ac17eSChris Zankel .align 4 1380013acebSMax Filippov10: 139249ac17eSChris Zankel movi a2, 0 140249ac17eSChris Zankel retw 141