The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | #include <string.h> |
| 29 | #include <stdint.h> |
| 30 | |
| 31 | size_t strlen(const char *s) |
| 32 | { |
| 33 | __builtin_prefetch(s); |
| 34 | __builtin_prefetch(s+32); |
| 35 | |
| 36 | union { |
| 37 | const char *b; |
| 38 | const uint32_t *w; |
| 39 | uintptr_t i; |
| 40 | } u; |
| 41 | |
| 42 | // these are some scratch variables for the asm code below |
| 43 | uint32_t v, t; |
| 44 | |
| 45 | // initialize the string length to zero |
| 46 | size_t l = 0; |
| 47 | |
| 48 | // align the pointer to a 32-bit word boundary |
| 49 | u.b = s; |
| 50 | while (u.i & 0x3) { |
| 51 | if (__builtin_expect(*u.b++ == 0, 0)) { |
| 52 | goto done; |
| 53 | } |
| 54 | l++; |
| 55 | } |
| 56 | |
| 57 | // loop for each word, testing if it contains a zero byte |
| 58 | // if so, exit the loop and update the length. |
| 59 | // We need to process 32 bytes per loop to schedule PLD properly |
| 60 | // and achieve the maximum bus speed. |
| 61 | asm( |
| 62 | "ldr %[v], [ %[s] ], #4 \n" |
| 63 | "sub %[l], %[l], %[s] \n" |
| 64 | "0: \n" |
| 65 | "pld [ %[s], #64 ] \n" |
| 66 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 67 | "and %[t], %[t], %[mask] \n" |
| 68 | "bics %[t], %[t], %[v] \n" |
| 69 | "ldreq %[v], [ %[s] ], #4 \n" |
| 70 | #if !defined(__OPTIMIZE_SIZE__) |
| 71 | "bne 1f \n" |
| 72 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 73 | "and %[t], %[t], %[mask] \n" |
| 74 | "bics %[t], %[t], %[v] \n" |
| 75 | "ldreq %[v], [ %[s] ], #4 \n" |
| 76 | "bne 1f \n" |
| 77 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 78 | "and %[t], %[t], %[mask] \n" |
| 79 | "bics %[t], %[t], %[v] \n" |
| 80 | "ldreq %[v], [ %[s] ], #4 \n" |
| 81 | "bne 1f \n" |
| 82 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 83 | "and %[t], %[t], %[mask] \n" |
| 84 | "bics %[t], %[t], %[v] \n" |
| 85 | "ldreq %[v], [ %[s] ], #4 \n" |
| 86 | "bne 1f \n" |
| 87 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 88 | "and %[t], %[t], %[mask] \n" |
| 89 | "bics %[t], %[t], %[v] \n" |
| 90 | "ldreq %[v], [ %[s] ], #4 \n" |
| 91 | "bne 1f \n" |
| 92 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 93 | "and %[t], %[t], %[mask] \n" |
| 94 | "bics %[t], %[t], %[v] \n" |
| 95 | "ldreq %[v], [ %[s] ], #4 \n" |
| 96 | "bne 1f \n" |
| 97 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 98 | "and %[t], %[t], %[mask] \n" |
| 99 | "bics %[t], %[t], %[v] \n" |
| 100 | "ldreq %[v], [ %[s] ], #4 \n" |
| 101 | "bne 1f \n" |
| 102 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 103 | "and %[t], %[t], %[mask] \n" |
| 104 | "bics %[t], %[t], %[v] \n" |
| 105 | "ldreq %[v], [ %[s] ], #4 \n" |
| 106 | #endif |
| 107 | "beq 0b \n" |
| 108 | "1: \n" |
| 109 | "add %[l], %[l], %[s] \n" |
| 110 | "tst %[v], #0xFF \n" |
| 111 | "beq 2f \n" |
| 112 | "add %[l], %[l], #1 \n" |
| 113 | "tst %[v], #0xFF00 \n" |
| 114 | "beq 2f \n" |
| 115 | "add %[l], %[l], #1 \n" |
| 116 | "tst %[v], #0xFF0000 \n" |
| 117 | "addne %[l], %[l], #1 \n" |
| 118 | "2: \n" |
| 119 | : [l]"=&r"(l), [v]"=&r"(v), [t]"=&r"(t), [s]"=&r"(u.b) |
| 120 | : "%[l]"(l), "%[s]"(u.b), [mask]"r"(0x80808080UL) |
| 121 | : "cc" |
| 122 | ); |
| 123 | |
| 124 | done: |
| 125 | return l; |
| 126 | } |