1 /* 2 * Copyright (c) 2008 ARM Ltd 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the company may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 22 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "arm_asm.h" 30 #include <_ansi.h> 31 #include <string.h> 32 #include <limits.h> 33 34 #if defined (__OPTIMIZE_SIZE__) || defined (PREFER_SIZE_OVER_SPEED) || \ 35 (defined (__thumb__) && !defined (__thumb2__)) 36 37 # if !defined (PREFER_SIZE_OVER_SPEED) && !defined (__OPTIMIZE_SIZE__) 38 /* Thumb1 only variant. 39 If speed is preferred, the strlen() function in ../../string/strlen.c 40 will be used. 41 42 Leave this field blank. So the strlen() is not defined, and this will 43 automatically pull in the default C definition of strlen() from 44 ../../string/strlen.c. No need to include this file explicitely. 45 The lib_a-strlen.o will not be generated, so it won't replace the default 46 lib_a-strlen.o which is generated by ../../string/strlen.c. See the 47 commands in configure.in and Makefile.am for more details. 48 49 However, if we need to rewrite this function to be more efficient, 50 we can add the corresponding assembly code into this field and change 51 the commands in configure.in and Makefile.am to allow the corresponding 52 lib_a-strlen.o to be generated. 53 */ 54 # else 55 size_t 56 strlen (const char* str) 57 { 58 int scratch; 59 #if defined (__thumb__) && !defined (__thumb2__) 60 size_t len; 61 asm ("mov %0, #0\n" 62 "1:\n\t" 63 "ldrb %1, [%2, %0]\n\t" 64 "add %0, %0, #1\n\t" 65 "cmp %1, #0\n\t" 66 "bne 1b" 67 : "=&r" (len), "=&r" (scratch) : "r" (str) : "memory", "cc"); 68 return len - 1; 69 #else 70 const char* end; 71 asm ("1:\n\t" 72 "ldrb %1, [%0], #1\n\t" 73 "cmp %1, #0\n\t" 74 "bne 1b" 75 : "=&r" (end), "=&r" (scratch) : "0" (str) : "memory", "cc"); 76 return end - str - 1; 77 #endif 78 } 79 #endif 80 #else 81 82 #if !(defined(_ISA_ARM_7) || defined(__ARM_ARCH_6T2__)) 83 84 size_t __attribute__((naked)) 85 strlen (const char* str) 86 { 87 (void)str; /* disable unused argument warning */ 88 asm ("len .req r0\n\t" 89 "data .req r3\n\t" 90 "addr .req r1\n\t" 91 92 "optpld r0\n\t" 93 /* Word-align address */ 94 "bic addr, r0, #3\n\t" 95 /* Get adjustment for start ... */ 96 "ands len, r0, #3\n\t" 97 "neg len, len\n\t" 98 /* First word of data */ 99 "ldr data, [addr], #4\n\t" 100 /* Ensure bytes preceeding start ... */ 101 "add ip, len, #4\n\t" 102 "mov ip, ip, asl #3\n\t" 103 "mvn r2, #0\n\t" 104 /* ... are masked out */ 105 #ifdef __thumb__ 106 "itt ne\n\t" 107 # ifdef __ARMEB__ 108 "lslne r2, ip\n\t" 109 # else 110 "lsrne r2, ip\n\t" 111 # endif 112 "orrne data, data, r2\n\t" 113 #else 114 "it ne\n\t" 115 # ifdef __ARMEB__ 116 "orrne data, data, r2, lsl ip\n\t" 117 # else 118 "orrne data, data, r2, lsr ip\n\t" 119 # endif 120 #endif 121 /* Magic const 0x01010101 */ 122 #ifdef _ISA_ARM_7 123 "movw ip, #0x101\n\t" 124 #else 125 "mov ip, #0x1\n\t" 126 "orr ip, ip, ip, lsl #8\n\t" 127 #endif 128 "orr ip, ip, ip, lsl #16\n" 129 130 /* This is the main loop. We subtract one from each byte in 131 the word: the sign bit changes iff the byte was zero or 132 0x80 -- we eliminate the latter case by anding the result 133 with the 1-s complement of the data. */ 134 "1:\n\t" 135 /* test (data - 0x01010101) */ 136 "sub r2, data, ip\n\t" 137 /* ... & ~data */ 138 "bic r2, r2, data\n\t" 139 /* ... & 0x80808080 == 0? */ 140 "ands r2, r2, ip, lsl #7\n\t" 141 #ifdef _ISA_ARM_7 142 /* yes, get more data... */ 143 "itt eq\n\t" 144 "ldreq data, [addr], #4\n\t" 145 /* and 4 more bytes */ 146 "addeq len, len, #4\n\t" 147 /* If we have PLD, then unroll the loop a bit. */ 148 "optpld addr, #8\n\t" 149 /* test (data - 0x01010101) */ 150 "ittt eq\n\t" 151 "subeq r2, data, ip\n\t" 152 /* ... & ~data */ 153 "biceq r2, r2, data\n\t" 154 /* ... & 0x80808080 == 0? */ 155 "andeqs r2, r2, ip, lsl #7\n\t" 156 #endif 157 "itt eq\n\t" 158 /* yes, get more data... */ 159 "ldreq data, [addr], #4\n\t" 160 /* and 4 more bytes */ 161 "addeq len, len, #4\n\t" 162 "beq 1b\n\t" 163 #ifdef __ARMEB__ 164 "tst data, #0xff000000\n\t" 165 "itttt ne\n\t" 166 "addne len, len, #1\n\t" 167 "tstne data, #0xff0000\n\t" 168 "addne len, len, #1\n\t" 169 "tstne data, #0xff00\n\t" 170 "it ne\n\t" 171 "addne len, len, #1\n\t" 172 #else 173 # ifdef _ISA_ARM_5 174 /* R2 is the residual sign bits from the above test. All we 175 need to do now is establish the position of the first zero 176 byte... */ 177 /* Little-endian is harder, we need the number of trailing 178 zeros / 8 */ 179 # ifdef _ISA_ARM_7 180 "rbit r2, r2\n\t" 181 "clz r2, r2\n\t" 182 # else 183 "rsb r1, r2, #0\n\t" 184 "and r2, r2, r1\n\t" 185 "clz r2, r2\n\t" 186 "rsb r2, r2, #31\n\t" 187 # endif 188 "add len, len, r2, lsr #3\n\t" 189 # else /* No CLZ instruction */ 190 "tst data, #0xff\n\t" 191 "itttt ne\n\t" 192 "addne len, len, #1\n\t" 193 "tstne data, #0xff00\n\t" 194 "addne len, len, #1\n\t" 195 "tstne data, #0xff0000\n\t" 196 "it ne\n\t" 197 "addne len, len, #1\n\t" 198 # endif 199 #endif 200 "RETURN"); 201 } 202 #endif 203 #endif 204