Blame sysdeps/aarch64/strcmp.S

Packit 6c4009
/* Copyright (C) 2012-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
/* Assumptions:
Packit 6c4009
 *
Packit 6c4009
 * ARMv8-a, AArch64
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
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 result		x0
Packit 6c4009
Packit 6c4009
/* Internal variables.  */
Packit 6c4009
#define data1		x2
Packit 6c4009
#define data1w		w2
Packit 6c4009
#define data2		x3
Packit 6c4009
#define data2w		w3
Packit 6c4009
#define has_nul		x4
Packit 6c4009
#define diff		x5
Packit 6c4009
#define syndrome	x6
Packit 6c4009
#define tmp1		x7
Packit 6c4009
#define tmp2		x8
Packit 6c4009
#define tmp3		x9
Packit 6c4009
#define zeroones	x10
Packit 6c4009
#define pos		x11
Packit 6c4009
Packit 6c4009
	/* Start of performance-critical section  -- one 64B cache line.  */
Packit 6c4009
ENTRY_ALIGN(strcmp, 6)
Packit 6c4009
Packit 6c4009
	DELOUSE (0)
Packit 6c4009
	DELOUSE (1)
Packit 6c4009
	eor	tmp1, src1, src2
Packit 6c4009
	mov	zeroones, #REP8_01
Packit 6c4009
	tst	tmp1, #7
Packit 6c4009
	b.ne	L(misaligned8)
Packit 6c4009
	ands	tmp1, src1, #7
Packit 6c4009
	b.ne	L(mutual_align)
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
L(loop_aligned):
Packit 6c4009
	ldr	data1, [src1], #8
Packit 6c4009
	ldr	data2, [src2], #8
Packit 6c4009
L(start_realigned):
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
	bic	has_nul, tmp1, tmp2	/* Non-zero if NUL terminator.  */
Packit 6c4009
	orr	syndrome, diff, has_nul
Packit 6c4009
	cbz	syndrome, L(loop_aligned)
Packit 6c4009
	/* End of performance-critical section  -- one 64B cache line.  */
Packit 6c4009
Packit 6c4009
L(end):
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 preceed the start point.  */
Packit 6c4009
	bic	src1, src1, #7
Packit 6c4009
	bic	src2, src2, #7
Packit 6c4009
	lsl	tmp1, tmp1, #3		/* Bytes beyond alignment -> bits.  */
Packit 6c4009
	ldr	data1, [src1], #8
Packit 6c4009
	neg	tmp1, tmp1		/* Bits to alignment -64.  */
Packit 6c4009
	ldr	data2, [src2], #8
Packit 6c4009
	mov	tmp2, #~0
Packit 6c4009
#ifdef __AARCH64EB__
Packit 6c4009
	/* Big-endian.  Early bytes are at MSB.  */
Packit 6c4009
	lsl	tmp2, tmp2, tmp1	/* Shift (tmp1 & 63).  */
Packit 6c4009
#else
Packit 6c4009
	/* Little-endian.  Early bytes are at LSB.  */
Packit 6c4009
	lsr	tmp2, tmp2, tmp1	/* Shift (tmp1 & 63).  */
Packit 6c4009
#endif
Packit 6c4009
	orr	data1, data1, tmp2
Packit 6c4009
	orr	data2, data2, tmp2
Packit 6c4009
	b	L(start_realigned)
Packit 6c4009
Packit 6c4009
L(misaligned8):
Packit 6c4009
	/* Align SRC1 to 8 bytes and then compare 8 bytes at a time, always
Packit 6c4009
	   checking to make sure that we don't access beyond page boundary in
Packit 6c4009
	   SRC2.  */
Packit 6c4009
	tst	src1, #7
Packit 6c4009
	b.eq	L(loop_misaligned)
Packit 6c4009
L(do_misaligned):
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
	tst	src1, #7
Packit 6c4009
	b.ne	L(do_misaligned)
Packit 6c4009
Packit 6c4009
L(loop_misaligned):
Packit 6c4009
	/* Test if we are within the last dword of the end of a 4K page.  If
Packit 6c4009
	   yes then jump back to the misaligned loop to copy a byte at a time.  */
Packit 6c4009
	and	tmp1, src2, #0xff8
Packit 6c4009
	eor	tmp1, tmp1, #0xff8
Packit 6c4009
	cbz	tmp1, L(do_misaligned)
Packit 6c4009
	ldr	data1, [src1], #8
Packit 6c4009
	ldr	data2, [src2], #8
Packit 6c4009
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
	bic	has_nul, tmp1, tmp2	/* Non-zero if NUL terminator.  */
Packit 6c4009
	orr	syndrome, diff, has_nul
Packit 6c4009
	cbz	syndrome, L(loop_misaligned)
Packit 6c4009
	b	L(end)
Packit 6c4009
Packit 6c4009
L(done):
Packit 6c4009
	sub	result, data1, data2
Packit 6c4009
	RET
Packit 6c4009
END(strcmp)
Packit 6c4009
libc_hidden_builtin_def (strcmp)