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 14*5cf97ebdSMax Filippov#include <linux/linkage.h> 15367b8112SChris Zankel#include <variant/core.h> 160013acebSMax Filippov#include <asm/asmmacro.h> 17249ac17eSChris Zankel 18249ac17eSChris Zankel/* 19249ac17eSChris Zankel * size_t __strnlen_user(const char *s, size_t len) 20249ac17eSChris Zankel */ 21a0bb46baSChris Zankel 22a0bb46baSChris Zankel#ifdef __XTENSA_EB__ 23a0bb46baSChris Zankel# define MASK0 0xff000000 24a0bb46baSChris Zankel# define MASK1 0x00ff0000 25a0bb46baSChris Zankel# define MASK2 0x0000ff00 26a0bb46baSChris Zankel# define MASK3 0x000000ff 27a0bb46baSChris Zankel#else 28a0bb46baSChris Zankel# define MASK0 0x000000ff 29a0bb46baSChris Zankel# define MASK1 0x0000ff00 30a0bb46baSChris Zankel# define MASK2 0x00ff0000 31a0bb46baSChris Zankel# define MASK3 0xff000000 32a0bb46baSChris Zankel#endif 33249ac17eSChris Zankel 34249ac17eSChris Zankel# Register use: 35249ac17eSChris Zankel# a2/ src 36249ac17eSChris Zankel# a3/ len 37249ac17eSChris Zankel# a4/ tmp 38249ac17eSChris Zankel# a5/ mask0 39249ac17eSChris Zankel# a6/ mask1 40249ac17eSChris Zankel# a7/ mask2 41249ac17eSChris Zankel# a8/ mask3 42249ac17eSChris Zankel# a9/ tmp 43249ac17eSChris Zankel# a10/ tmp 44249ac17eSChris Zankel 45a0bb46baSChris Zankel.text 46*5cf97ebdSMax FilippovENTRY(__strnlen_user) 47*5cf97ebdSMax Filippov 48249ac17eSChris Zankel entry sp, 16 # minimal stack frame 49249ac17eSChris Zankel # a2/ s, a3/ len 50249ac17eSChris Zankel addi a4, a2, -4 # because we overincrement at the end; 51249ac17eSChris Zankel # we compensate with load offsets of 4 52a0bb46baSChris Zankel movi a5, MASK0 # mask for byte 0 53a0bb46baSChris Zankel movi a6, MASK1 # mask for byte 1 54a0bb46baSChris Zankel movi a7, MASK2 # mask for byte 2 55a0bb46baSChris Zankel movi a8, MASK3 # mask for byte 3 56249ac17eSChris Zankel bbsi.l a2, 0, .L1mod2 # if only 8-bit aligned 57249ac17eSChris Zankel bbsi.l a2, 1, .L2mod4 # if only 16-bit aligned 58249ac17eSChris Zankel 59249ac17eSChris Zankel/* 60249ac17eSChris Zankel * String is word-aligned. 61249ac17eSChris Zankel */ 62249ac17eSChris Zankel.Laligned: 63249ac17eSChris Zankel srli a10, a3, 2 # number of loop iterations with 4B per loop 64249ac17eSChris Zankel#if XCHAL_HAVE_LOOPS 65249ac17eSChris Zankel loopnez a10, .Ldone 66249ac17eSChris Zankel#else 67249ac17eSChris Zankel beqz a10, .Ldone 68249ac17eSChris Zankel slli a10, a10, 2 69249ac17eSChris Zankel add a10, a10, a4 # a10 = end of last 4B chunk 70249ac17eSChris Zankel#endif /* XCHAL_HAVE_LOOPS */ 71249ac17eSChris Zankel.Loop: 720013acebSMax FilippovEX(10f) l32i a9, a4, 4 # get next word of string 73249ac17eSChris Zankel addi a4, a4, 4 # advance string pointer 74249ac17eSChris Zankel bnone a9, a5, .Lz0 # if byte 0 is zero 75249ac17eSChris Zankel bnone a9, a6, .Lz1 # if byte 1 is zero 76249ac17eSChris Zankel bnone a9, a7, .Lz2 # if byte 2 is zero 77249ac17eSChris Zankel bnone a9, a8, .Lz3 # if byte 3 is zero 78249ac17eSChris Zankel#if !XCHAL_HAVE_LOOPS 79249ac17eSChris Zankel blt a4, a10, .Loop 80249ac17eSChris Zankel#endif 81249ac17eSChris Zankel 82249ac17eSChris Zankel.Ldone: 830013acebSMax FilippovEX(10f) l32i a9, a4, 4 # load 4 bytes for remaining checks 84249ac17eSChris Zankel 85249ac17eSChris Zankel bbci.l a3, 1, .L100 86249ac17eSChris Zankel # check two more bytes (bytes 0, 1 of word) 87249ac17eSChris Zankel addi a4, a4, 2 # advance string pointer 88249ac17eSChris Zankel bnone a9, a5, .Lz0 # if byte 0 is zero 89249ac17eSChris Zankel bnone a9, a6, .Lz1 # if byte 1 is zero 90249ac17eSChris Zankel.L100: 91249ac17eSChris Zankel bbci.l a3, 0, .L101 92249ac17eSChris Zankel # check one more byte (byte 2 of word) 93249ac17eSChris Zankel # Actually, we don't need to check. Zero or nonzero, we'll add one. 94249ac17eSChris Zankel # Do not add an extra one for the NULL terminator since we have 95249ac17eSChris Zankel # exhausted the original len parameter. 96249ac17eSChris Zankel addi a4, a4, 1 # advance string pointer 97249ac17eSChris Zankel.L101: 98249ac17eSChris Zankel sub a2, a4, a2 # compute length 99249ac17eSChris Zankel retw 100249ac17eSChris Zankel 101249ac17eSChris Zankel# NOTE that in several places below, we point to the byte just after 102249ac17eSChris Zankel# the zero byte in order to include the NULL terminator in the count. 103249ac17eSChris Zankel 104249ac17eSChris Zankel.Lz3: # byte 3 is zero 105249ac17eSChris Zankel addi a4, a4, 3 # point to zero byte 106249ac17eSChris Zankel.Lz0: # byte 0 is zero 107249ac17eSChris Zankel addi a4, a4, 1 # point just beyond zero byte 108249ac17eSChris Zankel sub a2, a4, a2 # subtract to get length 109249ac17eSChris Zankel retw 110249ac17eSChris Zankel.Lz1: # byte 1 is zero 111249ac17eSChris Zankel addi a4, a4, 1+1 # point just beyond zero byte 112249ac17eSChris Zankel sub a2, a4, a2 # subtract to get length 113249ac17eSChris Zankel retw 114249ac17eSChris Zankel.Lz2: # byte 2 is zero 115249ac17eSChris Zankel addi a4, a4, 2+1 # point just beyond zero byte 116249ac17eSChris Zankel sub a2, a4, a2 # subtract to get length 117249ac17eSChris Zankel retw 118249ac17eSChris Zankel 119249ac17eSChris Zankel.L1mod2: # address is odd 1200013acebSMax FilippovEX(10f) l8ui a9, a4, 4 # get byte 0 121249ac17eSChris Zankel addi a4, a4, 1 # advance string pointer 122249ac17eSChris Zankel beqz a9, .Lz3 # if byte 0 is zero 123249ac17eSChris Zankel bbci.l a4, 1, .Laligned # if string pointer is now word-aligned 124249ac17eSChris Zankel 125249ac17eSChris Zankel.L2mod4: # address is 2 mod 4 126249ac17eSChris Zankel addi a4, a4, 2 # advance ptr for aligned access 1270013acebSMax FilippovEX(10f) l32i a9, a4, 0 # get word with first two bytes of string 128249ac17eSChris Zankel bnone a9, a7, .Lz2 # if byte 2 (of word, not string) is zero 129249ac17eSChris Zankel bany a9, a8, .Laligned # if byte 3 (of word, not string) is nonzero 130249ac17eSChris Zankel # byte 3 is zero 131249ac17eSChris Zankel addi a4, a4, 3+1 # point just beyond zero byte 132249ac17eSChris Zankel sub a2, a4, a2 # subtract to get length 133249ac17eSChris Zankel retw 134249ac17eSChris Zankel 135*5cf97ebdSMax FilippovENDPROC(__strnlen_user) 136*5cf97ebdSMax Filippov 137249ac17eSChris Zankel .section .fixup, "ax" 138249ac17eSChris Zankel .align 4 1390013acebSMax Filippov10: 140249ac17eSChris Zankel movi a2, 0 141249ac17eSChris Zankel retw 142