Chris Zankel | 249ac17 | 2005-06-23 22:01:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/xtensa/lib/strnlen_user.S |
| 3 | * |
| 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. |
| 7 | * |
| 8 | * Returns strnlen, including trailing zero terminator. |
| 9 | * Zero indicates error. |
| 10 | * |
| 11 | * Copyright (C) 2002 Tensilica Inc. |
| 12 | */ |
| 13 | |
Chris Zankel | 367b811 | 2008-11-06 06:40:46 -0800 | [diff] [blame] | 14 | #include <variant/core.h> |
Chris Zankel | 249ac17 | 2005-06-23 22:01:20 -0700 | [diff] [blame] | 15 | |
| 16 | /* Load or store instructions that may cause exceptions use the EX macro. */ |
| 17 | |
| 18 | #define EX(insn,reg1,reg2,offset,handler) \ |
| 19 | 9: insn reg1, reg2, offset; \ |
| 20 | .section __ex_table, "a"; \ |
| 21 | .word 9b, handler; \ |
| 22 | .previous |
| 23 | |
| 24 | /* |
| 25 | * size_t __strnlen_user(const char *s, size_t len) |
| 26 | */ |
Chris Zankel | a0bb46b | 2007-05-31 17:44:31 -0700 | [diff] [blame] | 27 | |
| 28 | #ifdef __XTENSA_EB__ |
| 29 | # define MASK0 0xff000000 |
| 30 | # define MASK1 0x00ff0000 |
| 31 | # define MASK2 0x0000ff00 |
| 32 | # define MASK3 0x000000ff |
| 33 | #else |
| 34 | # define MASK0 0x000000ff |
| 35 | # define MASK1 0x0000ff00 |
| 36 | # define MASK2 0x00ff0000 |
| 37 | # define MASK3 0xff000000 |
| 38 | #endif |
Chris Zankel | 249ac17 | 2005-06-23 22:01:20 -0700 | [diff] [blame] | 39 | |
| 40 | # Register use: |
| 41 | # a2/ src |
| 42 | # a3/ len |
| 43 | # a4/ tmp |
| 44 | # a5/ mask0 |
| 45 | # a6/ mask1 |
| 46 | # a7/ mask2 |
| 47 | # a8/ mask3 |
| 48 | # a9/ tmp |
| 49 | # a10/ tmp |
| 50 | |
Chris Zankel | a0bb46b | 2007-05-31 17:44:31 -0700 | [diff] [blame] | 51 | .text |
Chris Zankel | 249ac17 | 2005-06-23 22:01:20 -0700 | [diff] [blame] | 52 | .align 4 |
| 53 | .global __strnlen_user |
| 54 | .type __strnlen_user,@function |
| 55 | __strnlen_user: |
| 56 | entry sp, 16 # minimal stack frame |
| 57 | # a2/ s, a3/ len |
| 58 | addi a4, a2, -4 # because we overincrement at the end; |
| 59 | # we compensate with load offsets of 4 |
Chris Zankel | a0bb46b | 2007-05-31 17:44:31 -0700 | [diff] [blame] | 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 |
Chris Zankel | 249ac17 | 2005-06-23 22:01:20 -0700 | [diff] [blame] | 64 | bbsi.l a2, 0, .L1mod2 # if only 8-bit aligned |
| 65 | bbsi.l a2, 1, .L2mod4 # if only 16-bit aligned |
| 66 | |
| 67 | /* |
| 68 | * String is word-aligned. |
| 69 | */ |
| 70 | .Laligned: |
| 71 | srli a10, a3, 2 # number of loop iterations with 4B per loop |
| 72 | #if XCHAL_HAVE_LOOPS |
| 73 | loopnez a10, .Ldone |
| 74 | #else |
| 75 | beqz a10, .Ldone |
| 76 | slli a10, a10, 2 |
| 77 | add a10, a10, a4 # a10 = end of last 4B chunk |
| 78 | #endif /* XCHAL_HAVE_LOOPS */ |
| 79 | .Loop: |
| 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 |
| 86 | #if !XCHAL_HAVE_LOOPS |
| 87 | blt a4, a10, .Loop |
| 88 | #endif |
| 89 | |
| 90 | .Ldone: |
| 91 | EX(l32i, a9, a4, 4, lenfixup) # load 4 bytes for remaining checks |
| 92 | |
| 93 | bbci.l a3, 1, .L100 |
| 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 |
| 98 | .L100: |
| 99 | bbci.l a3, 0, .L101 |
| 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 |
| 105 | .L101: |
| 106 | sub a2, a4, a2 # compute length |
| 107 | retw |
| 108 | |
| 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. |
| 111 | |
| 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 |
| 117 | retw |
| 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 |
| 121 | retw |
| 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 |
| 125 | retw |
| 126 | |
| 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 |
| 132 | |
| 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 |
| 138 | # byte 3 is zero |
| 139 | addi a4, a4, 3+1 # point just beyond zero byte |
| 140 | sub a2, a4, a2 # subtract to get length |
| 141 | retw |
| 142 | |
| 143 | .section .fixup, "ax" |
| 144 | .align 4 |
| 145 | lenfixup: |
| 146 | movi a2, 0 |
| 147 | retw |
| 148 | |