1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2013 ARM Ltd.
4 * Copyright (C) 2013 Linaro.
6 * This code is based on glibc cortex strings work originally authored by Linaro
9 * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
10 * files/head:/src/aarch64/
13 #include <linux/linkage.h>
14 #include <asm/assembler.h>
17 * calculate the length of a string
20 * x0 - const string pointer
22 * x0 - the return length of specific string
25 /* Arguments and results. */
29 /* Locals and temporaries. */
43 #define REP8_01 0x0101010101010101
44 #define REP8_7f 0x7f7f7f7f7f7f7f7f
45 #define REP8_80 0x8080808080808080
48 mov zeroones, #REP8_01
53 * NUL detection works on the principle that (X - 1) & (~X) & 0x80
54 * (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
55 * can be done in parallel across the entire word.
58 * The inner loop deals with two Dwords at a time. This has a
59 * slightly higher start-up cost, but we should win quite quickly,
60 * especially on cores with a high number of issue slots per
61 * cycle, as we get much better parallelism out of the operations.
64 ldp data1, data2, [src], #16
66 sub tmp1, data1, zeroones
67 orr tmp2, data1, #REP8_7f
68 sub tmp3, data2, zeroones
69 orr tmp4, data2, #REP8_7f
70 bic has_nul1, tmp1, tmp2
71 bics has_nul2, tmp3, tmp4
72 ccmp has_nul1, #0, #0, eq /* NZCV = 0000 */
76 cbz has_nul1, .Lnul_in_data2
77 CPU_BE( mov data2, data1 ) /*prepare data to re-calculate the syndrome*/
79 mov has_nul2, has_nul1
82 * For big-endian, carry propagation (if the final byte in the
83 * string is 0x01) means we cannot use has_nul directly. The
84 * easiest way to get the correct byte is to byte-swap the data
85 * and calculate the syndrome a second time.
87 CPU_BE( rev data2, data2 )
88 CPU_BE( sub tmp1, data2, zeroones )
89 CPU_BE( orr tmp2, data2, #REP8_7f )
90 CPU_BE( 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. */
104 /* Big-endian. Early bytes are at MSB. */
105 CPU_BE( lsl tmp2, tmp2, tmp1 ) /* Shift (tmp1 & 63). */
106 /* Little-endian. Early bytes are at LSB. */
107 CPU_LE( lsr tmp2, tmp2, tmp1 ) /* Shift (tmp1 & 63). */
109 orr data1, data1, tmp2
110 orr data2a, data2, tmp2
111 csinv data1, data1, xzr, le
112 csel data2, data2, data2a, le
115 EXPORT_SYMBOL_NOKASAN(strlen)