Update ARM ulps for VFPv4 (bug 16600).
[glibc.git] / sysdeps / aarch64 / strlen.S
blob4d2a20a18e7622c048d63fc1bacfbd15c4dea916
1 /* Copyright (C) 2012-2014 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library.  If not, see
17    <http://www.gnu.org/licenses/>.  */
19 #include <sysdep.h>
21 /* Assumptions:
22  *
23  * ARMv8-a, AArch64
24  */
26 /* Arguments and results.  */
27 #define srcin           x0
28 #define len             x0
30 /* Locals and temporaries.  */
31 #define src             x1
32 #define data1           x2
33 #define data2           x3
34 #define data2a          x4
35 #define has_nul1        x5
36 #define has_nul2        x6
37 #define tmp1            x7
38 #define tmp2            x8
39 #define tmp3            x9
40 #define tmp4            x10
41 #define zeroones        x11
42 #define pos             x12
44 #define REP8_01 0x0101010101010101
45 #define REP8_7f 0x7f7f7f7f7f7f7f7f
46 #define REP8_80 0x8080808080808080
48         /* Start of critial section -- keep to one 64Byte cache line.  */
49 ENTRY_ALIGN (strlen, 6)
50         mov     zeroones, #REP8_01
51         bic     src, srcin, #15
52         ands    tmp1, srcin, #15
53         b.ne    L(misaligned)
54         /* NUL detection works on the principle that (X - 1) & (~X) & 0x80
55            (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
56            can be done in parallel across the entire word.  */
57         /* The inner loop deals with two Dwords at a time.  This has a
58            slightly higher start-up cost, but we should win quite quickly,
59            especially on cores with a high number of issue slots per
60            cycle, as we get much better parallelism out of the operations.  */
61 L(loop):
62         ldp     data1, data2, [src], #16
63 L(realigned):
64         sub     tmp1, data1, zeroones
65         orr     tmp2, data1, #REP8_7f
66         sub     tmp3, data2, zeroones
67         orr     tmp4, data2, #REP8_7f
68         bic     has_nul1, tmp1, tmp2
69         bics    has_nul2, tmp3, tmp4
70         ccmp    has_nul1, #0, #0, eq    /* NZCV = 0000  */
71         b.eq    L(loop)
72         /* End of critical section -- keep to one 64Byte cache line.  */
74         sub     len, src, srcin
75         cbz     has_nul1, L(nul_in_data2)
76 #ifdef __AARCH64EB__
77         mov     data2, data1
78 #endif
79         sub     len, len, #8
80         mov     has_nul2, has_nul1
81 L(nul_in_data2):
82 #ifdef __AARCH64EB__
83         /* For big-endian, carry propagation (if the final byte in the
84            string is 0x01) means we cannot use has_nul directly.  The
85            easiest way to get the correct byte is to byte-swap the data
86            and calculate the syndrome a second time.  */
87         rev     data2, data2
88         sub     tmp1, data2, zeroones
89         orr     tmp2, data2, #REP8_7f
90         bic     has_nul2, tmp1, tmp2
91 #endif
92         sub     len, len, #8
93         rev     has_nul2, has_nul2
94         clz     pos, has_nul2
95         add     len, len, pos, lsr #3           /* Bits to bytes.  */
96         RET
98 L(misaligned):
99         cmp     tmp1, #8
100         neg     tmp1, tmp1
101         ldp     data1, data2, [src], #16
102         lsl     tmp1, tmp1, #3          /* Bytes beyond alignment -> bits.  */
103         mov     tmp2, #~0
104 #ifdef __AARCH64EB__
105         /* Big-endian.  Early bytes are at MSB.  */
106         lsl     tmp2, tmp2, tmp1        /* Shift (tmp1 & 63).  */
107 #else
108         /* Little-endian.  Early bytes are at LSB.  */
109         lsr     tmp2, tmp2, tmp1        /* Shift (tmp1 & 63).  */
110 #endif
111         orr     data1, data1, tmp2
112         orr     data2a, data2, tmp2
113         csinv   data1, data1, xzr, le
114         csel    data2, data2, data2a, le
115         b       L(realigned)
116 END (strlen)
117 libc_hidden_builtin_def (strlen)