The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [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 | #include <machine/cpu-features.h> |
| 31 | |
| 32 | size_t strlen(const char *s) |
| 33 | { |
| 34 | __builtin_prefetch(s); |
| 35 | __builtin_prefetch(s+32); |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 36 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 | union { |
| 38 | const char *b; |
| 39 | const uint32_t *w; |
| 40 | uintptr_t i; |
| 41 | } u; |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 42 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 43 | // these are some scratch variables for the asm code below |
| 44 | uint32_t v, t; |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 45 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 46 | // initialize the string length to zero |
| 47 | size_t l = 0; |
| 48 | |
| 49 | // align the pointer to a 32-bit word boundary |
| 50 | u.b = s; |
| 51 | while (u.i & 0x3) { |
| 52 | if (__builtin_expect(*u.b++ == 0, 0)) { |
| 53 | goto done; |
| 54 | } |
| 55 | l++; |
| 56 | } |
| 57 | |
| 58 | // loop for each word, testing if it contains a zero byte |
| 59 | // if so, exit the loop and update the length. |
| 60 | // We need to process 32 bytes per loop to schedule PLD properly |
| 61 | // and achieve the maximum bus speed. |
| 62 | asm( |
Andrew Hsieh | e8f46e8 | 2013-04-25 15:05:03 +0800 | [diff] [blame] | 63 | "ldr %[v], [%[s]], #4 \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | "sub %[l], %[l], %[s] \n" |
| 65 | "0: \n" |
| 66 | #if __ARM_HAVE_PLD |
Andrew Hsieh | e8f46e8 | 2013-04-25 15:05:03 +0800 | [diff] [blame] | 67 | "pld [%[s], #64] \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 68 | #endif |
| 69 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 70 | "and %[t], %[t], %[mask] \n" |
| 71 | "bics %[t], %[t], %[v] \n" |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 72 | "it eq \n" |
Andrew Hsieh | e8f46e8 | 2013-04-25 15:05:03 +0800 | [diff] [blame] | 73 | "ldreq %[v], [%[s]], #4 \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 74 | #if !defined(__OPTIMIZE_SIZE__) |
| 75 | "bne 1f \n" |
| 76 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 77 | "and %[t], %[t], %[mask] \n" |
| 78 | "bics %[t], %[t], %[v] \n" |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 79 | "it eq \n" |
Andrew Hsieh | e8f46e8 | 2013-04-25 15:05:03 +0800 | [diff] [blame] | 80 | "ldreq %[v], [%[s]], #4 \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 81 | "bne 1f \n" |
| 82 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 83 | "and %[t], %[t], %[mask] \n" |
| 84 | "bics %[t], %[t], %[v] \n" |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 85 | "it eq \n" |
Andrew Hsieh | e8f46e8 | 2013-04-25 15:05:03 +0800 | [diff] [blame] | 86 | "ldreq %[v], [%[s]], #4 \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 87 | "bne 1f \n" |
| 88 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 89 | "and %[t], %[t], %[mask] \n" |
| 90 | "bics %[t], %[t], %[v] \n" |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 91 | "it eq \n" |
Andrew Hsieh | e8f46e8 | 2013-04-25 15:05:03 +0800 | [diff] [blame] | 92 | "ldreq %[v], [%[s]], #4 \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 93 | "bne 1f \n" |
| 94 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 95 | "and %[t], %[t], %[mask] \n" |
| 96 | "bics %[t], %[t], %[v] \n" |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 97 | "it eq \n" |
Andrew Hsieh | e8f46e8 | 2013-04-25 15:05:03 +0800 | [diff] [blame] | 98 | "ldreq %[v], [%[s]], #4 \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 99 | "bne 1f \n" |
| 100 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 101 | "and %[t], %[t], %[mask] \n" |
| 102 | "bics %[t], %[t], %[v] \n" |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 103 | "it eq \n" |
Andrew Hsieh | e8f46e8 | 2013-04-25 15:05:03 +0800 | [diff] [blame] | 104 | "ldreq %[v], [%[s]], #4 \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 105 | "bne 1f \n" |
| 106 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 107 | "and %[t], %[t], %[mask] \n" |
| 108 | "bics %[t], %[t], %[v] \n" |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 109 | "it eq \n" |
Andrew Hsieh | e8f46e8 | 2013-04-25 15:05:03 +0800 | [diff] [blame] | 110 | "ldreq %[v], [%[s]], #4 \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 111 | "bne 1f \n" |
| 112 | "sub %[t], %[v], %[mask], lsr #7\n" |
| 113 | "and %[t], %[t], %[mask] \n" |
| 114 | "bics %[t], %[t], %[v] \n" |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 115 | "it eq \n" |
Andrew Hsieh | e8f46e8 | 2013-04-25 15:05:03 +0800 | [diff] [blame] | 116 | "ldreq %[v], [%[s]], #4 \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 117 | #endif |
| 118 | "beq 0b \n" |
| 119 | "1: \n" |
| 120 | "add %[l], %[l], %[s] \n" |
| 121 | "tst %[v], #0xFF \n" |
| 122 | "beq 2f \n" |
| 123 | "add %[l], %[l], #1 \n" |
| 124 | "tst %[v], #0xFF00 \n" |
| 125 | "beq 2f \n" |
| 126 | "add %[l], %[l], #1 \n" |
| 127 | "tst %[v], #0xFF0000 \n" |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 128 | "it ne \n" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 129 | "addne %[l], %[l], #1 \n" |
| 130 | "2: \n" |
| 131 | : [l]"=&r"(l), [v]"=&r"(v), [t]"=&r"(t), [s]"=&r"(u.b) |
| 132 | : "%[l]"(l), "%[s]"(u.b), [mask]"r"(0x80808080UL) |
| 133 | : "cc" |
| 134 | ); |
Christopher Ferris | 9ad2a73 | 2013-07-15 13:55:45 -0700 | [diff] [blame^] | 135 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 136 | done: |
| 137 | return l; |
| 138 | } |