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/>. */
26 /* Arguments and results. */
30 /* Locals and temporaries. */
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
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. */
62 ldp data1, data2, [src], #16
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 */
72 /* End of critical section -- keep to one 64Byte cache line. */
75 cbz has_nul1, L(nul_in_data2)
80 mov has_nul2, has_nul1
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. */
88 sub tmp1, data2, zeroones
89 orr tmp2, data2, #REP8_7f
90 bic has_nul2, tmp1, tmp2
93 rev has_nul2, has_nul2
95 add len, len, pos, lsr #3 /* Bits to bytes. */
101 ldp data1, data2, [src], #16
102 lsl tmp1, tmp1, #3 /* Bytes beyond alignment -> bits. */
105 /* Big-endian. Early bytes are at MSB. */
106 lsl tmp2, tmp2, tmp1 /* Shift (tmp1 & 63). */
108 /* Little-endian. Early bytes are at LSB. */
109 lsr tmp2, tmp2, tmp1 /* Shift (tmp1 & 63). */
111 orr data1, data1, tmp2
112 orr data2a, data2, tmp2
113 csinv data1, data1, xzr, le
114 csel data2, data2, data2a, le
117 libc_hidden_builtin_def (strlen)