memcmp.S revision 1.2 1 1.2 skrll /* $NetBSD: memcmp.S,v 1.2 2018/02/04 21:52:16 skrll Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Matt Thomas of 3am Software Foundry.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #include <machine/asm.h>
33 1.1 matt
34 1.2 skrll RCSID("$NetBSD: memcmp.S,v 1.2 2018/02/04 21:52:16 skrll Exp $")
35 1.1 matt
36 1.1 matt ENTRY(memcmp)
37 1.1 matt mov x9, x0
38 1.1 matt mov x10, x1
39 1.1 matt mov x0, xzr
40 1.1 matt cbz x2, .Lmemcmp_ret
41 1.1 matt #ifdef _KERNEL
42 1.1 matt cmp x2, #6
43 1.1 matt b.eq .Lmemcmp_6bytes
44 1.1 matt #endif
45 1.2 skrll cmp x2, #8
46 1.1 matt b.ls .Lmemcmp_lessthan8
47 1.1 matt
48 1.1 matt ands x3, x9, #7
49 1.1 matt b.eq .Lmemcmp_dword_loop
50 1.1 matt
51 1.1 matt /*
52 1.2 skrll * The src1 address is not dword aligned.
53 1.1 matt */
54 1.1 matt add x2, x2, x3 /* add unalignment to length */
55 1.1 matt sub x2, x2, #8 /* now subtract a dword */
56 1.1 matt
57 1.1 matt sub x9, x9, x3 /* dword align src1 */
58 1.1 matt sub x10, x10, x3 /* adjust src2 */
59 1.1 matt
60 1.1 matt lsl x3, x3, #3 /* convert bytes to bits */
61 1.1 matt ldr x4, [x9], #8 /* load dword from src1 */
62 1.1 matt ldr x6, [x10], #8 /* load dword from src2 */
63 1.1 matt #ifdef __AARCH64EB__
64 1.1 matt lsl x4, x4, x3 /* discard leading bytes from data1 */
65 1.1 matt lsl x6, x6, x3 /* discard leading bytes from data2 */
66 1.1 matt #else
67 1.1 matt lsr x4, x4, x3 /* discard leading bytes from data1 */
68 1.1 matt lsr x6, x6, x3 /* discard leading bytes from data2 */
69 1.1 matt #endif
70 1.1 matt subs x0, x4, x6 /* compare data */
71 1.1 matt b.ne .Lmemcmp_last_compare /* difference. find it */
72 1.1 matt
73 1.1 matt .Lmemcmp_dword_loop:
74 1.1 matt subs x2, x2, #8
75 1.1 matt b.mi .Lmemcmp_finish_dword
76 1.1 matt ldr x4, [x9], #8
77 1.1 matt ldr x6, [x10], #8
78 1.1 matt subs x0, x4, x6
79 1.1 matt b.eq .Lmemcmp_dword_loop /* no difference. go to loop */
80 1.1 matt b .Lmemcmp_last_compare /* go find the difference. */
81 1.1 matt
82 1.1 matt .Lmemcmp_finish_dword:
83 1.1 matt /*
84 1.1 matt * we might have gotten here with nothing left. If so, just bail.
85 1.1 matt */
86 1.1 matt tst x2, #7
87 1.1 matt b.eq .Lmemcmp_ret
88 1.2 skrll mov x4, xzr
89 1.2 skrll mov x6, xzr
90 1.1 matt /*
91 1.1 matt *
92 1.1 matt */
93 1.1 matt tbz x2, #2, .Lmemcmp_finish_word
94 1.1 matt ldr w4, [x9], #4
95 1.1 matt ldr w6, [x10], #4
96 1.1 matt #ifdef __AARCH64EB__
97 1.1 matt lsl x4, x4, #32 /* move to MSW */
98 1.1 matt lsl x6, x6, #32 /* move to MSW */
99 1.1 matt #endif
100 1.1 matt
101 1.1 matt .Lmemcmp_finish_word:
102 1.1 matt tbz x2, #1, .Lmemcmp_finish_hword
103 1.1 matt ldrh w5, [x9], #2
104 1.1 matt ldrh w7, [x10], #2
105 1.1 matt #ifdef __AARCH64EB__
106 1.1 matt orr x4, x4, x5, lsl #16
107 1.1 matt orr x6, x6, x7, lsl #16
108 1.1 matt #else
109 1.1 matt orr x4, x4, x5, lsl #32
110 1.1 matt orr x6, x6, x7, lsl #32
111 1.1 matt #endif
112 1.1 matt
113 1.1 matt .Lmemcmp_finish_hword:
114 1.2 skrll tbz x2, #0, .Lmemcmp_last_compare0
115 1.2 skrll
116 1.1 matt ldrb w5, [x9]
117 1.1 matt ldrb w7, [x10]
118 1.2 skrll #ifdef __AARCH64EB__
119 1.2 skrll orr x4, x4, x5, lsl #8
120 1.2 skrll orr x6, x6, x7, lsl #8
121 1.2 skrll #else
122 1.1 matt orr x4, x4, x5, lsl #48
123 1.1 matt orr x6, x6, x7, lsl #48
124 1.2 skrll #endif
125 1.2 skrll b .Lmemcmp_last_compare0 /* go find the difference. */
126 1.1 matt
127 1.1 matt /*
128 1.1 matt * D
129 1.1 matt */
130 1.1 matt .Lmemcmp_lessthan8:
131 1.1 matt sub x2, x2, #1
132 1.1 matt 1: ldrb w4, [x9], #1
133 1.1 matt ldrb w5, [x10], #1
134 1.1 matt subs x2, x2, #1
135 1.1 matt ccmp x4, x5, #0, cs
136 1.1 matt b.eq 1b
137 1.1 matt sub x0, x4, x5
138 1.1 matt
139 1.1 matt .Lmemcmp_ret:
140 1.1 matt ret
141 1.1 matt
142 1.1 matt #ifdef _KERNEL
143 1.1 matt .Lmemcmp_6bytes:
144 1.1 matt ldr w4, [x9], #4
145 1.1 matt ldrh w5, [x9]
146 1.1 matt #if __AARCH64EB__
147 1.1 matt orr x4, x4, x5, lsl #48
148 1.1 matt rev x4, x4
149 1.1 matt #else
150 1.1 matt orr x4, x4, x5, lsl #32
151 1.1 matt #endif
152 1.1 matt ldr w6, [x10], #4
153 1.1 matt ldrh w7, [x10]
154 1.1 matt #if __AARCH64EB__
155 1.1 matt orr x6, x6, x7, lsl #48
156 1.1 matt rev x6, x6
157 1.1 matt #else
158 1.1 matt orr x6, x6, x7, lsl #32
159 1.1 matt #endif
160 1.1 matt #endif /* _KERNEL */
161 1.1 matt
162 1.1 matt /*
163 1.2 skrll * We have loaded the final bytes in x4 and x6 in host-endian. Now we have
164 1.1 matt * to figure what the difference is (if any). First we subtract. Any bytes
165 1.1 matt * that are the same will be 0. So to find the first non-zero byte we byterev
166 1.1 matt * and then use clz to find that byte.
167 1.1 matt * We mask the location to get the start of the byte. We shift both
168 1.1 matt * data dwords left to remove the equal part. Then we shift right to discard
169 1.1 matt * the trailing bytes. Then we subtract and return.
170 1.1 matt */
171 1.2 skrll .Lmemcmp_last_compare0:
172 1.1 matt subs x0, x4, x6
173 1.1 matt b.eq .Lmemcmp_ret
174 1.1 matt .Lmemcmp_last_compare:
175 1.2 skrll #if __AARCH64EB__
176 1.2 skrll clz x1, x0 /* find first non-zero byte */
177 1.2 skrll rev x0, x0
178 1.2 skrll #else
179 1.2 skrll rev x1, x0
180 1.1 matt clz x1, x1 /* find first non-zero byte */
181 1.2 skrll #endif
182 1.2 skrll bfi x1, xzr, #0, #3 /* make it byte aligned */
183 1.2 skrll lsr x1, x0, x1 /* shift to LSB */
184 1.2 skrll #if __AARCH64EL__
185 1.2 skrll rev x4, x4 /* byte reverse */
186 1.2 skrll rev x6, x6 /* byte reverse */
187 1.2 skrll #endif
188 1.2 skrll subs x0, x4, x6
189 1.2 skrll csetm x0, cc /* set mask bits as sign */
190 1.2 skrll bfm x0, x1, #0, #7 /* extend with sign bit */
191 1.1 matt ret
192 1.1 matt END(memcmp)
193