blob: 811e1e0b182268f2aab51ab2832f6464f0ed1159 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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
32size_t strlen(const char *s)
33{
34 __builtin_prefetch(s);
35 __builtin_prefetch(s+32);
Christopher Ferris9ad2a732013-07-15 13:55:45 -070036
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037 union {
38 const char *b;
39 const uint32_t *w;
40 uintptr_t i;
41 } u;
Christopher Ferris9ad2a732013-07-15 13:55:45 -070042
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043 // these are some scratch variables for the asm code below
44 uint32_t v, t;
Christopher Ferris9ad2a732013-07-15 13:55:45 -070045
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046 // 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 Hsiehe8f46e82013-04-25 15:05:03 +080063 "ldr %[v], [%[s]], #4 \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064 "sub %[l], %[l], %[s] \n"
65 "0: \n"
Andrew Hsiehe8f46e82013-04-25 15:05:03 +080066 "pld [%[s], #64] \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067 "sub %[t], %[v], %[mask], lsr #7\n"
68 "and %[t], %[t], %[mask] \n"
69 "bics %[t], %[t], %[v] \n"
Christopher Ferris9ad2a732013-07-15 13:55:45 -070070 "it eq \n"
Andrew Hsiehe8f46e82013-04-25 15:05:03 +080071 "ldreq %[v], [%[s]], #4 \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072#if !defined(__OPTIMIZE_SIZE__)
73 "bne 1f \n"
74 "sub %[t], %[v], %[mask], lsr #7\n"
75 "and %[t], %[t], %[mask] \n"
76 "bics %[t], %[t], %[v] \n"
Christopher Ferris9ad2a732013-07-15 13:55:45 -070077 "it eq \n"
Andrew Hsiehe8f46e82013-04-25 15:05:03 +080078 "ldreq %[v], [%[s]], #4 \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079 "bne 1f \n"
80 "sub %[t], %[v], %[mask], lsr #7\n"
81 "and %[t], %[t], %[mask] \n"
82 "bics %[t], %[t], %[v] \n"
Christopher Ferris9ad2a732013-07-15 13:55:45 -070083 "it eq \n"
Andrew Hsiehe8f46e82013-04-25 15:05:03 +080084 "ldreq %[v], [%[s]], #4 \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085 "bne 1f \n"
86 "sub %[t], %[v], %[mask], lsr #7\n"
87 "and %[t], %[t], %[mask] \n"
88 "bics %[t], %[t], %[v] \n"
Christopher Ferris9ad2a732013-07-15 13:55:45 -070089 "it eq \n"
Andrew Hsiehe8f46e82013-04-25 15:05:03 +080090 "ldreq %[v], [%[s]], #4 \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091 "bne 1f \n"
92 "sub %[t], %[v], %[mask], lsr #7\n"
93 "and %[t], %[t], %[mask] \n"
94 "bics %[t], %[t], %[v] \n"
Christopher Ferris9ad2a732013-07-15 13:55:45 -070095 "it eq \n"
Andrew Hsiehe8f46e82013-04-25 15:05:03 +080096 "ldreq %[v], [%[s]], #4 \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097 "bne 1f \n"
98 "sub %[t], %[v], %[mask], lsr #7\n"
99 "and %[t], %[t], %[mask] \n"
100 "bics %[t], %[t], %[v] \n"
Christopher Ferris9ad2a732013-07-15 13:55:45 -0700101 "it eq \n"
Andrew Hsiehe8f46e82013-04-25 15:05:03 +0800102 "ldreq %[v], [%[s]], #4 \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103 "bne 1f \n"
104 "sub %[t], %[v], %[mask], lsr #7\n"
105 "and %[t], %[t], %[mask] \n"
106 "bics %[t], %[t], %[v] \n"
Christopher Ferris9ad2a732013-07-15 13:55:45 -0700107 "it eq \n"
Andrew Hsiehe8f46e82013-04-25 15:05:03 +0800108 "ldreq %[v], [%[s]], #4 \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109 "bne 1f \n"
110 "sub %[t], %[v], %[mask], lsr #7\n"
111 "and %[t], %[t], %[mask] \n"
112 "bics %[t], %[t], %[v] \n"
Christopher Ferris9ad2a732013-07-15 13:55:45 -0700113 "it eq \n"
Andrew Hsiehe8f46e82013-04-25 15:05:03 +0800114 "ldreq %[v], [%[s]], #4 \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115#endif
116 "beq 0b \n"
117 "1: \n"
118 "add %[l], %[l], %[s] \n"
119 "tst %[v], #0xFF \n"
120 "beq 2f \n"
121 "add %[l], %[l], #1 \n"
122 "tst %[v], #0xFF00 \n"
123 "beq 2f \n"
124 "add %[l], %[l], #1 \n"
125 "tst %[v], #0xFF0000 \n"
Christopher Ferris9ad2a732013-07-15 13:55:45 -0700126 "it ne \n"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127 "addne %[l], %[l], #1 \n"
128 "2: \n"
129 : [l]"=&r"(l), [v]"=&r"(v), [t]"=&r"(t), [s]"=&r"(u.b)
130 : "%[l]"(l), "%[s]"(u.b), [mask]"r"(0x80808080UL)
131 : "cc"
132 );
Christopher Ferris9ad2a732013-07-15 13:55:45 -0700133
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134done:
135 return l;
136}