2
* Copyright (C) 2008 The Android Open Source Project
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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
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
30
#include <machine/cpu-features.h>
32
size_t strlen(const char *s)
34
__builtin_prefetch(s);
35
__builtin_prefetch(s+32);
43
// these are some scratch variables for the asm code below
46
// initialize the string length to zero
49
// align the pointer to a 32-bit word boundary
52
if (__builtin_expect(*u.b++ == 0, 0)) {
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.
63
"ldr %[v], [ %[s] ], #4 \n"
64
"sub %[l], %[l], %[s] \n"
67
"pld [ %[s], #64 ] \n"
69
"sub %[t], %[v], %[mask], lsr #7\n"
70
"and %[t], %[t], %[mask] \n"
71
"bics %[t], %[t], %[v] \n"
72
"ldreq %[v], [ %[s] ], #4 \n"
73
#if !defined(__OPTIMIZE_SIZE__)
75
"sub %[t], %[v], %[mask], lsr #7\n"
76
"and %[t], %[t], %[mask] \n"
77
"bics %[t], %[t], %[v] \n"
78
"ldreq %[v], [ %[s] ], #4 \n"
80
"sub %[t], %[v], %[mask], lsr #7\n"
81
"and %[t], %[t], %[mask] \n"
82
"bics %[t], %[t], %[v] \n"
83
"ldreq %[v], [ %[s] ], #4 \n"
85
"sub %[t], %[v], %[mask], lsr #7\n"
86
"and %[t], %[t], %[mask] \n"
87
"bics %[t], %[t], %[v] \n"
88
"ldreq %[v], [ %[s] ], #4 \n"
90
"sub %[t], %[v], %[mask], lsr #7\n"
91
"and %[t], %[t], %[mask] \n"
92
"bics %[t], %[t], %[v] \n"
93
"ldreq %[v], [ %[s] ], #4 \n"
95
"sub %[t], %[v], %[mask], lsr #7\n"
96
"and %[t], %[t], %[mask] \n"
97
"bics %[t], %[t], %[v] \n"
98
"ldreq %[v], [ %[s] ], #4 \n"
100
"sub %[t], %[v], %[mask], lsr #7\n"
101
"and %[t], %[t], %[mask] \n"
102
"bics %[t], %[t], %[v] \n"
103
"ldreq %[v], [ %[s] ], #4 \n"
105
"sub %[t], %[v], %[mask], lsr #7\n"
106
"and %[t], %[t], %[mask] \n"
107
"bics %[t], %[t], %[v] \n"
108
"ldreq %[v], [ %[s] ], #4 \n"
112
"add %[l], %[l], %[s] \n"
115
"add %[l], %[l], #1 \n"
116
"tst %[v], #0xFF00 \n"
118
"add %[l], %[l], #1 \n"
119
"tst %[v], #0xFF0000 \n"
120
"addne %[l], %[l], #1 \n"
122
: [l]"=&r"(l), [v]"=&r"(v), [t]"=&r"(t), [s]"=&r"(u.b)
123
: "%[l]"(l), "%[s]"(u.b), [mask]"r"(0x80808080UL)