Blame sysdeps/aarch64/strncmp.S

Packit 6c4009
/* Copyright (C) 2013-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library.  If not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
Packit 6c4009
/* Assumptions:
Packit 6c4009
 *
Packit 6c4009
 * ARMv8-a, AArch64
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#define REP8_01 0x0101010101010101
Packit 6c4009
#define REP8_7f 0x7f7f7f7f7f7f7f7f
Packit 6c4009
#define REP8_80 0x8080808080808080
Packit 6c4009
Packit 6c4009
/* Parameters and result.  */
Packit 6c4009
#define src1		x0
Packit 6c4009
#define src2		x1
Packit 6c4009
#define limit		x2
Packit 6c4009
#define result		x0
Packit 6c4009
Packit 6c4009
/* Internal variables.  */
Packit 6c4009
#define data1		x3
Packit 6c4009
#define data1w		w3
Packit 6c4009
#define data2		x4
Packit 6c4009
#define data2w		w4
Packit 6c4009
#define has_nul		x5
Packit 6c4009
#define diff		x6
Packit 6c4009
#define syndrome	x7
Packit 6c4009
#define tmp1		x8
Packit 6c4009
#define tmp2		x9
Packit 6c4009
#define tmp3		x10
Packit 6c4009
#define zeroones	x11
Packit 6c4009
#define pos		x12
Packit 6c4009
#define limit_wd	x13
Packit 6c4009
#define mask		x14
Packit 6c4009
#define endloop		x15
Packit 6c4009
#define count		mask
Packit 6c4009
Packit 6c4009
ENTRY_ALIGN_AND_PAD (strncmp, 6, 7)
Packit 6c4009
	DELOUSE (0)
Packit 6c4009
	DELOUSE (1)
Packit 6c4009
	DELOUSE (2)
Packit 6c4009
	cbz	limit, L(ret0)
Packit 6c4009
	eor	tmp1, src1, src2
Packit 6c4009
	mov	zeroones, #REP8_01
Packit 6c4009
	tst	tmp1, #7
Packit 6c4009
	and	count, src1, #7
Packit 6c4009
	b.ne	L(misaligned8)
Packit 6c4009
	cbnz	count, L(mutual_align)
Packit 6c4009
	/* Calculate the number of full and partial words -1.  */
Packit 6c4009
	sub	limit_wd, limit, #1	/* limit != 0, so no underflow.  */
Packit 6c4009
	lsr	limit_wd, limit_wd, #3	/* Convert to Dwords.  */
Packit 6c4009
Packit 6c4009
	/* NUL detection works on the principle that (X - 1) & (~X) & 0x80
Packit 6c4009
	   (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
Packit 6c4009
	   can be done in parallel across the entire word.  */
Packit 6c4009
	/* Start of performance-critical section  -- one 64B cache line.  */
Packit 6c4009
L(loop_aligned):
Packit 6c4009
	ldr	data1, [src1], #8
Packit 6c4009
	ldr	data2, [src2], #8
Packit 6c4009
L(start_realigned):
Packit 6c4009
	subs	limit_wd, limit_wd, #1
Packit 6c4009
	sub	tmp1, data1, zeroones
Packit 6c4009
	orr	tmp2, data1, #REP8_7f
Packit 6c4009
	eor	diff, data1, data2	/* Non-zero if differences found.  */
Packit 6c4009
	csinv	endloop, diff, xzr, pl	/* Last Dword or differences.  */
Packit 6c4009
	bics	has_nul, tmp1, tmp2	/* Non-zero if NUL terminator.  */
Packit 6c4009
	ccmp	endloop, #0, #0, eq
Packit 6c4009
	b.eq	L(loop_aligned)
Packit 6c4009
	/* End of performance-critical section  -- one 64B cache line.  */
Packit 6c4009
Packit 6c4009
	/* Not reached the limit, must have found the end or a diff.  */
Packit 6c4009
	tbz	limit_wd, #63, L(not_limit)
Packit 6c4009
Packit 6c4009
	/* Limit % 8 == 0 => all bytes significant.  */
Packit 6c4009
	ands	limit, limit, #7
Packit 6c4009
	b.eq	L(not_limit)
Packit 6c4009
Packit 6c4009
	lsl	limit, limit, #3	/* Bits -> bytes.  */
Packit 6c4009
	mov	mask, #~0
Packit 6c4009
#ifdef __AARCH64EB__
Packit 6c4009
	lsr	mask, mask, limit
Packit 6c4009
#else
Packit 6c4009
	lsl	mask, mask, limit
Packit 6c4009
#endif
Packit 6c4009
	bic	data1, data1, mask
Packit 6c4009
	bic	data2, data2, mask
Packit 6c4009
Packit 6c4009
	/* Make sure that the NUL byte is marked in the syndrome.  */
Packit 6c4009
	orr	has_nul, has_nul, mask
Packit 6c4009
Packit 6c4009
L(not_limit):
Packit 6c4009
	orr	syndrome, diff, has_nul
Packit 6c4009
Packit 6c4009
#ifndef	__AARCH64EB__
Packit 6c4009
	rev	syndrome, syndrome
Packit 6c4009
	rev	data1, data1
Packit 6c4009
	/* The MS-non-zero bit of the syndrome marks either the first bit
Packit 6c4009
	   that is different, or the top bit of the first zero byte.
Packit 6c4009
	   Shifting left now will bring the critical information into the
Packit 6c4009
	   top bits.  */
Packit 6c4009
	clz	pos, syndrome
Packit 6c4009
	rev	data2, data2
Packit 6c4009
	lsl	data1, data1, pos
Packit 6c4009
	lsl	data2, data2, pos
Packit 6c4009
	/* But we need to zero-extend (char is unsigned) the value and then
Packit 6c4009
	   perform a signed 32-bit subtraction.  */
Packit 6c4009
	lsr	data1, data1, #56
Packit 6c4009
	sub	result, data1, data2, lsr #56
Packit 6c4009
	RET
Packit 6c4009
#else
Packit 6c4009
	/* For big-endian we cannot use the trick with the syndrome value
Packit 6c4009
	   as carry-propagation can corrupt the upper bits if the trailing
Packit 6c4009
	   bytes in the string contain 0x01.  */
Packit 6c4009
	/* However, if there is no NUL byte in the dword, we can generate
Packit 6c4009
	   the result directly.  We can't just subtract the bytes as the
Packit 6c4009
	   MSB might be significant.  */
Packit 6c4009
	cbnz	has_nul, 1f
Packit 6c4009
	cmp	data1, data2
Packit 6c4009
	cset	result, ne
Packit 6c4009
	cneg	result, result, lo
Packit 6c4009
	RET
Packit 6c4009
1:
Packit 6c4009
	/* Re-compute the NUL-byte detection, using a byte-reversed value.  */
Packit 6c4009
	rev	tmp3, data1
Packit 6c4009
	sub	tmp1, tmp3, zeroones
Packit 6c4009
	orr	tmp2, tmp3, #REP8_7f
Packit 6c4009
	bic	has_nul, tmp1, tmp2
Packit 6c4009
	rev	has_nul, has_nul
Packit 6c4009
	orr	syndrome, diff, has_nul
Packit 6c4009
	clz	pos, syndrome
Packit 6c4009
	/* The MS-non-zero bit of the syndrome marks either the first bit
Packit 6c4009
	   that is different, or the top bit of the first zero byte.
Packit 6c4009
	   Shifting left now will bring the critical information into the
Packit 6c4009
	   top bits.  */
Packit 6c4009
	lsl	data1, data1, pos
Packit 6c4009
	lsl	data2, data2, pos
Packit 6c4009
	/* But we need to zero-extend (char is unsigned) the value and then
Packit 6c4009
	   perform a signed 32-bit subtraction.  */
Packit 6c4009
	lsr	data1, data1, #56
Packit 6c4009
	sub	result, data1, data2, lsr #56
Packit 6c4009
	RET
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
L(mutual_align):
Packit 6c4009
	/* Sources are mutually aligned, but are not currently at an
Packit 6c4009
	   alignment boundary.  Round down the addresses and then mask off
Packit 6c4009
	   the bytes that precede the start point.
Packit 6c4009
	   We also need to adjust the limit calculations, but without
Packit 6c4009
	   overflowing if the limit is near ULONG_MAX.  */
Packit 6c4009
	bic	src1, src1, #7
Packit 6c4009
	bic	src2, src2, #7
Packit 6c4009
	ldr	data1, [src1], #8
Packit 6c4009
	neg	tmp3, count, lsl #3	/* 64 - bits(bytes beyond align). */
Packit 6c4009
	ldr	data2, [src2], #8
Packit 6c4009
	mov	tmp2, #~0
Packit 6c4009
	sub	limit_wd, limit, #1	/* limit != 0, so no underflow.  */
Packit 6c4009
#ifdef __AARCH64EB__
Packit 6c4009
	/* Big-endian.  Early bytes are at MSB.  */
Packit 6c4009
	lsl	tmp2, tmp2, tmp3	/* Shift (count & 63).  */
Packit 6c4009
#else
Packit 6c4009
	/* Little-endian.  Early bytes are at LSB.  */
Packit 6c4009
	lsr	tmp2, tmp2, tmp3	/* Shift (count & 63).  */
Packit 6c4009
#endif
Packit 6c4009
	and	tmp3, limit_wd, #7
Packit 6c4009
	lsr	limit_wd, limit_wd, #3
Packit 6c4009
	/* Adjust the limit. Only low 3 bits used, so overflow irrelevant.  */
Packit 6c4009
	add	limit, limit, count
Packit 6c4009
	add	tmp3, tmp3, count
Packit 6c4009
	orr	data1, data1, tmp2
Packit 6c4009
	orr	data2, data2, tmp2
Packit 6c4009
	add	limit_wd, limit_wd, tmp3, lsr #3
Packit 6c4009
	b	L(start_realigned)
Packit 6c4009
Packit 6c4009
	.p2align 6
Packit 6c4009
	/* Don't bother with dwords for up to 16 bytes.  */
Packit 6c4009
L(misaligned8):
Packit 6c4009
	cmp	limit, #16
Packit 6c4009
	b.hs	L(try_misaligned_words)
Packit 6c4009
Packit 6c4009
L(byte_loop):
Packit 6c4009
	/* Perhaps we can do better than this.  */
Packit 6c4009
	ldrb	data1w, [src1], #1
Packit 6c4009
	ldrb	data2w, [src2], #1
Packit 6c4009
	subs	limit, limit, #1
Packit 6c4009
	ccmp	data1w, #1, #0, hi	/* NZCV = 0b0000.  */
Packit 6c4009
	ccmp	data1w, data2w, #0, cs	/* NZCV = 0b0000.  */
Packit 6c4009
	b.eq	L(byte_loop)
Packit 6c4009
L(done):
Packit 6c4009
	sub	result, data1, data2
Packit 6c4009
	RET
Packit 6c4009
Packit 6c4009
	/* Align the SRC1 to a dword by doing a bytewise compare and then do
Packit 6c4009
	   the dword loop.  */
Packit 6c4009
L(try_misaligned_words):
Packit 6c4009
	lsr	limit_wd, limit, #3
Packit 6c4009
	cbz	count, L(do_misaligned)
Packit 6c4009
Packit 6c4009
	neg	count, count
Packit 6c4009
	and	count, count, #7
Packit 6c4009
	sub	limit, limit, count
Packit 6c4009
	lsr	limit_wd, limit, #3
Packit 6c4009
Packit 6c4009
L(page_end_loop):
Packit 6c4009
	ldrb	data1w, [src1], #1
Packit 6c4009
	ldrb	data2w, [src2], #1
Packit 6c4009
	cmp	data1w, #1
Packit 6c4009
	ccmp	data1w, data2w, #0, cs	/* NZCV = 0b0000.  */
Packit 6c4009
	b.ne	L(done)
Packit 6c4009
	subs	count, count, #1
Packit 6c4009
	b.hi	L(page_end_loop)
Packit 6c4009
Packit 6c4009
L(do_misaligned):
Packit 6c4009
	/* Prepare ourselves for the next page crossing.  Unlike the aligned
Packit 6c4009
	   loop, we fetch 1 less dword because we risk crossing bounds on
Packit 6c4009
	   SRC2.  */
Packit 6c4009
	mov	count, #8
Packit 6c4009
	subs	limit_wd, limit_wd, #1
Packit 6c4009
	b.lo	L(done_loop)
Packit 6c4009
L(loop_misaligned):
Packit 6c4009
	and	tmp2, src2, #0xff8
Packit 6c4009
	eor	tmp2, tmp2, #0xff8
Packit 6c4009
	cbz	tmp2, L(page_end_loop)
Packit 6c4009
Packit 6c4009
	ldr	data1, [src1], #8
Packit 6c4009
	ldr	data2, [src2], #8
Packit 6c4009
	sub	tmp1, data1, zeroones
Packit 6c4009
	orr	tmp2, data1, #REP8_7f
Packit 6c4009
	eor	diff, data1, data2	/* Non-zero if differences found.  */
Packit 6c4009
	bics	has_nul, tmp1, tmp2	/* Non-zero if NUL terminator.  */
Packit 6c4009
	ccmp	diff, #0, #0, eq
Packit 6c4009
	b.ne	L(not_limit)
Packit 6c4009
	subs	limit_wd, limit_wd, #1
Packit 6c4009
	b.pl	L(loop_misaligned)
Packit 6c4009
Packit 6c4009
L(done_loop):
Packit 6c4009
	/* We found a difference or a NULL before the limit was reached.  */
Packit 6c4009
	and	limit, limit, #7
Packit 6c4009
	cbz	limit, L(not_limit)
Packit 6c4009
	/* Read the last word.  */
Packit 6c4009
	sub	src1, src1, 8
Packit 6c4009
	sub	src2, src2, 8
Packit 6c4009
	ldr	data1, [src1, limit]
Packit 6c4009
	ldr	data2, [src2, limit]
Packit 6c4009
	sub	tmp1, data1, zeroones
Packit 6c4009
	orr	tmp2, data1, #REP8_7f
Packit 6c4009
	eor	diff, data1, data2	/* Non-zero if differences found.  */
Packit 6c4009
	bics	has_nul, tmp1, tmp2	/* Non-zero if NUL terminator.  */
Packit 6c4009
	ccmp	diff, #0, #0, eq
Packit 6c4009
	b.ne	L(not_limit)
Packit 6c4009
Packit 6c4009
L(ret0):
Packit 6c4009
	mov	result, #0
Packit 6c4009
	RET
Packit 6c4009
Packit 6c4009
END (strncmp)
Packit 6c4009
libc_hidden_builtin_def (strncmp)