2 * Copyright (C) 2013 ARM Ltd.
3 * Copyright (C) 2013 Linaro.
5 * This code is based on glibc cortex strings work originally authored by Linaro
6 * and re-licensed under GPLv2 for the Linux kernel. The original code can
9 * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
10 * files/head:/src/aarch64/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <linux/linkage.h>
26 #include <asm/assembler.h>
29 * calculate the length of a string
32 * x0 - const string pointer
34 * x0 - the return length of specific string
37 /* Arguments and results. */
41 /* Locals and temporaries. */
55 #define REP8_01 0x0101010101010101
56 #define REP8_7f 0x7f7f7f7f7f7f7f7f
57 #define REP8_80 0x8080808080808080
60 mov zeroones, #REP8_01
65 * NUL detection works on the principle that (X - 1) & (~X) & 0x80
66 * (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
67 * can be done in parallel across the entire word.
70 * The inner loop deals with two Dwords at a time. This has a
71 * slightly higher start-up cost, but we should win quite quickly,
72 * especially on cores with a high number of issue slots per
73 * cycle, as we get much better parallelism out of the operations.
76 ldp data1, data2, [src], #16
78 sub tmp1, data1, zeroones
79 orr tmp2, data1, #REP8_7f
80 sub tmp3, data2, zeroones
81 orr tmp4, data2, #REP8_7f
82 bic has_nul1, tmp1, tmp2
83 bics has_nul2, tmp3, tmp4
84 ccmp has_nul1, #0, #0, eq /* NZCV = 0000 */
88 cbz has_nul1, .Lnul_in_data2
89 CPU_BE( mov data2, data1 ) /*prepare data to re-calculate the syndrome*/
91 mov has_nul2, has_nul1
94 * For big-endian, carry propagation (if the final byte in the
95 * string is 0x01) means we cannot use has_nul directly. The
96 * easiest way to get the correct byte is to byte-swap the data
97 * and calculate the syndrome a second time.
99 CPU_BE( rev data2, data2 )
100 CPU_BE( sub tmp1, data2, zeroones )
101 CPU_BE( orr tmp2, data2, #REP8_7f )
102 CPU_BE( bic has_nul2, tmp1, tmp2 )
105 rev has_nul2, has_nul2
107 add len, len, pos, lsr #3 /* Bits to bytes. */
113 ldp data1, data2, [src], #16
114 lsl tmp1, tmp1, #3 /* Bytes beyond alignment -> bits. */
116 /* Big-endian. Early bytes are at MSB. */
117 CPU_BE( lsl tmp2, tmp2, tmp1 ) /* Shift (tmp1 & 63). */
118 /* Little-endian. Early bytes are at LSB. */
119 CPU_LE( lsr tmp2, tmp2, tmp1 ) /* Shift (tmp1 & 63). */
121 orr data1, data1, tmp2
122 orr data2a, data2, tmp2
123 csinv data1, data1, xzr, le
124 csel data2, data2, data2a, le