strlen_neon.S revision 1.2 1 1.1 matt /*-
2 1.1 matt * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 1.1 matt * All rights reserved.
4 1.1 matt *
5 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
6 1.1 matt * by Matt Thomas of 3am Software Foundry.
7 1.1 matt *
8 1.1 matt * Redistribution and use in source and binary forms, with or without
9 1.1 matt * modification, are permitted provided that the following conditions
10 1.1 matt * are met:
11 1.1 matt * 1. Redistributions of source code must retain the above copyright
12 1.1 matt * notice, this list of conditions and the following disclaimer.
13 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer in the
15 1.1 matt * documentation and/or other materials provided with the distribution.
16 1.1 matt *
17 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
28 1.1 matt */
29 1.1 matt
30 1.1 matt #include <machine/asm.h>
31 1.1 matt
32 1.2 matt RCSID("$NetBSD: strlen_neon.S,v 1.2 2012/12/15 22:23:31 matt Exp $")
33 1.1 matt .text
34 1.1 matt
35 1.1 matt ENTRY(strlen)
36 1.1 matt mov ip, r0 /* we r0 for return value */
37 1.1 matt ands r1, r0, #15 /* verify qword alignment */
38 1.1 matt neg r0, r1 /* subtract misalignment from count */
39 1.1 matt veor q2, q2, q2 /* clear mask */
40 1.1 matt mov r3, #7 /* NBBY - 1 */
41 1.1 matt vdup.32 q3, r3 /* dup throughout q3 */
42 1.2 matt mov r3, #0x04 /* magic since there are 4 bytes per U32 */
43 1.2 matt orr r3, r3, lsl #8 /* copy to next 8 bits */
44 1.2 matt orr r3, r3, lsl #16 /* copy to upper 16 bits */
45 1.1 matt beq .Lmain_loop
46 1.1 matt veor q0, q0, q0 /* clear q0 */
47 1.1 matt vmvn q2, q2 /* set all 16 bytes of mask to all 1s */
48 1.1 matt bic ip, ip, #15 /* qword align string address */
49 1.1 matt lsl r2, r1, #3 /* convert to bits */
50 1.1 matt neg r2, r2 /* make negative since we are shifting right */
51 1.1 matt tst r1, #8 /* do we need skip the first 8? */
52 1.1 matt bne 1f /* yes, we need to skip */
53 1.1 matt veor d4, d4, d4 /* clear lower 8 bytes (upper is set) */
54 1.1 matt vmov s2, r2 /* set shift amount for upper half */
55 1.1 matt b 2f
56 1.1 matt 1: vmov s0, r2 /* set shift amount for lower half */
57 1.1 matt 2: vshl.u64 q2, q2, q0 /* shift */
58 1.1 matt /*
59 1.1 matt * Main loop. Load 16 bytes, do a clz,
60 1.1 matt */
61 1.1 matt .Lmain_loop:
62 1.1 matt vld1.64 {d0, d1}, [ip:128]! /* load qword */
63 1.1 matt #ifdef __ARMEL__
64 1.1 matt vrev64.8 q0, q0 /* convert to BE for clz */
65 1.1 matt #endif
66 1.1 matt vswp d0, d1 /* swap dwords to get BE qword */
67 1.1 matt vorr q0, q0, q2 /* or "in" leading byte mask */
68 1.1 matt veor q2, q2, q2 /* clear byte mask */
69 1.1 matt vceq.i8 q1, q0, #0 /* test each byte for 0 */
70 1.2 matt /* Why couldn't there be a 64-bit CLZ? */
71 1.1 matt vclz.i32 q1, q1 /* count leading zeroes to find the 0 byte */
72 1.1 matt vadd.i32 q1, q1, q3 /* round up to byte bounary */
73 1.1 matt vshr.u32 q1, q1, #3 /* convert to bytes */
74 1.2 matt vmovn.i32 d0, q1 /* 4 I32 -> 4 I16 */
75 1.2 matt vmovn.i16 d0, q0 /* 4 I16 -> 4 I8 */
76 1.2 matt vmov r2, s0 /* get counts */
77 1.2 matt cmp r2, r3 /* count eq 4 in each byte? */
78 1.2 matt addeq r0, #16 /* no NULs */
79 1.2 matt beq .Lmain_loop /* get next qword */
80 1.2 matt /* r2[31:24] already has 1st word byte count */
81 1.2 matt tst r2, #(4 << 24) /* first word has 4 non-NUL? */
82 1.2 matt addne r2, r2, r2, lsl #8 /* add second word byte-count */
83 1.2 matt tstne r2, #(4 << 16) /* second word has 4 non-NUL? */
84 1.2 matt addne r2, r2, r2, lsl #16 /* add thirs word byte-count */
85 1.2 matt tstne r2, #(4 << 8) /* third has 4 non-NULL? */
86 1.2 matt addne r2, r2, r2, lsl #24 /* add fourth word byte-count */
87 1.2 matt add r0, r0, r2, lsr #24 /* add accumulated byte-count to length */
88 1.2 matt RET /* and return. */
89 1.1 matt END(strlen)
90