2 * Copyright (c) 2018 Instituto de Pesquisas Eldorado
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <machine/asm.h>
28 __FBSDID("$FreeBSD$");
31 RCSID("$NetBSD: strcmp.S,v 1.0 2018/05/10 12:33:02 alexandre Exp $")
35 #define STRCMP_MULTI_ALIGNMENT_BYTES 8
36 #define STRCMP_MULTI_ALIGNMENT_MASK (STRCMP_MULTI_ALIGNMENT_BYTES - 1)
39 /* Starting alignment even if aligned, avoiding performance
40 * degradation for short strings.
42 lbz %r5,0(%r3) /* Load chars. */
44 cmpd %r5,%r6 /* Check if chars are different. */
46 cmpdi %r5,0 /* Check if char is zero. */
49 /* Checking if addresses can be aligned, otherwise copy by byte */
51 andi. %r7,%r7,STRCMP_MULTI_ALIGNMENT_MASK
52 bne .Lstrcmp_compare_by_byte_loop
54 .Lstrcmp_param1_align_loop:
55 lbzu %r5,1(%r3) /* Load chars. */
57 cmpd %r5,%r6 /* Check if chars are different. */
59 cmpdi %r5,0 /* Check if char is zero. */
61 andi. %r7,%r3,STRCMP_MULTI_ALIGNMENT_MASK /* Check alignment. */
62 bne .Lstrcmp_param1_align_loop
64 .Lstrcmp_param1_aligned:
65 /* If parameter 2 is aligned compare by qword/word,
66 * else compare by byte. */
67 andi. %r7,%r4,STRCMP_MULTI_ALIGNMENT_MASK
68 beq .Lstrcmp_compare_by_word
69 lbz %r5,0(%r3) /* Load chars. */
71 cmpd %r5,%r6 /* Check if chars are different. */
73 cmpdi %r5,0 /* Check if char is zero. */
76 .Lstrcmp_compare_by_byte_loop:
82 beq .Lstrcmp_compare_by_byte_loop
88 .Lstrcmp_compare_by_word:
89 ld %r5,0(%r3) /* Load double words. */
91 xor %r8,%r8,%r8 /* %r8 <- Zero. */
92 xor %r0,%r5,%r6 /* Check if double words are different. */
93 cmpb %r7,%r5,%r8 /* Check if double words contain zero. */
96 * If double words are different or contain zero,
97 * find what byte is different or contains zero,
98 * else load next double words.
101 bne .Lstrcmp_check_zeros_differences
103 .Lstrcmp_compare_by_word_loop:
104 ldu %r5,8(%r3) /* Load double words. */
106 xor %r0,%r5,%r6 /* Check if double words are different. */
107 cmpb %r7,%r5,%r8 /* Check if double words contain zero. */
110 * If double words are different or contain zero,
111 * find what byte is different or contains zero,
112 * else load next double words.
115 beq .Lstrcmp_compare_by_word_loop
117 .Lstrcmp_check_zeros_differences:
118 /* Find what byte is different or contains zero. */
120 /* Check 1st byte. */
122 bne .Lstrcmp_found_zero_difference_in_1
124 /* Check 2nd byte. */
126 bne .Lstrcmp_found_zero_difference_in_2
128 /* Check 3rd byte. */
130 bne .Lstrcmp_found_zero_difference_in_3
132 /* Check 4th byte. */
134 bne .Lstrcmp_found_zero_difference_in_4
136 /* Check 5th byte. */
137 andis. %r0,%r9,0xff00
138 bne .Lstrcmp_found_zero_difference_in_5
140 /* Check 6th byte. */
142 bne .Lstrcmp_found_zero_difference_in_6
144 /* Check 7th byte. */
146 bne .Lstrcmp_found_zero_difference_in_7
148 .Lstrcmp_found_zero_difference_in_8:
149 /* 8th byte is different or contains zero. */
155 .Lstrcmp_found_zero_difference_in_1:
156 /* 1st byte is different or contains zero. */
162 .Lstrcmp_found_zero_difference_in_2:
163 /* 2nd byte is different or contains zero. */
169 .Lstrcmp_found_zero_difference_in_3:
170 /* 3rd byte is different or contains zero. */
176 .Lstrcmp_found_zero_difference_in_4:
177 /* 4th byte is different or contains zero. */
183 .Lstrcmp_found_zero_difference_in_5:
184 /* 5th byte is different or contains zero. */
190 .Lstrcmp_found_zero_difference_in_6:
191 /* 6th byte is different or contains zero. */
197 .Lstrcmp_found_zero_difference_in_7:
198 /* 7th byte is different or contains zero. */
206 .section .note.GNU-stack,"",%progbits