xref: /openbsd-src/sys/lib/libkern/arch/amd64/strchr.S (revision 28c675773680115a49ba777d0361775c11aa12d6)
1*28c67577Sguenther/*	$OpenBSD: strchr.S,v 1.7 2022/12/07 19:26:39 guenther Exp $	*/
28c688dc9Sreyk/*	$NetBSD: strchr.S,v 1.7 2014/03/22 19:16:34 jakllsch Exp $	*/
38c688dc9Sreyk
48c688dc9Sreyk/*-
58c688dc9Sreyk * Copyright (c) 2009 The NetBSD Foundation, Inc.
68c688dc9Sreyk * All rights reserved.
78c688dc9Sreyk *
88c688dc9Sreyk * This code is derived from software contributed to The NetBSD Foundation
98c688dc9Sreyk * by David Laight.
108c688dc9Sreyk *
118c688dc9Sreyk * Redistribution and use in source and binary forms, with or without
128c688dc9Sreyk * modification, are permitted provided that the following conditions
138c688dc9Sreyk * are met:
148c688dc9Sreyk * 1. Redistributions of source code must retain the above copyright
158c688dc9Sreyk *    notice, this list of conditions and the following disclaimer.
168c688dc9Sreyk * 2. Redistributions in binary form must reproduce the above copyright
178c688dc9Sreyk *    notice, this list of conditions and the following disclaimer in the
188c688dc9Sreyk *    documentation and/or other materials provided with the distribution.
198c688dc9Sreyk *
208c688dc9Sreyk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
218c688dc9Sreyk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
228c688dc9Sreyk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
238c688dc9Sreyk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
248c688dc9Sreyk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
258c688dc9Sreyk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
268c688dc9Sreyk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
278c688dc9Sreyk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
288c688dc9Sreyk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
298c688dc9Sreyk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
308c688dc9Sreyk * POSSIBILITY OF SUCH DAMAGE.
314d6af78aSderaadt */
32f5df1827Smickey
338c688dc9Sreyk/* See comments in strlen.S about checking words for byte values */
348c688dc9Sreyk
354d6af78aSderaadt#include <machine/asm.h>
364d6af78aSderaadt
378c688dc9SreykSTRONG_ALIAS(index, strchr)
388c688dc9Sreyk
398c688dc9Sreyk/*
408c688dc9Sreyk * On entry %rdi is the buffer and the low byte of %rsi (%sil) the
418c688dc9Sreyk * character to search for.
428c688dc9Sreyk *
438c688dc9Sreyk * Registers %rdx, %rcx, %r8-%r11 and %rax are also usable
448c688dc9Sreyk */
458c688dc9Sreyk
464d6af78aSderaadtENTRY(strchr)
471d66f0a0Smortimer	RETGUARD_SETUP(strchr, r9)
481d66f0a0Smortimer	RETGUARD_PUSH(r9)
498c688dc9Sreyk	movabsq	$0x0101010101010101,%r8
508c688dc9Sreyk
518c688dc9Sreyk	movzbq	%sil,%rdx	/* value to search for (c) */
528c688dc9Sreyk	/* These imul are 'directpath' on athlons, so are fast */
538c688dc9Sreyk	imul	$0x80,%r8,%r9	/* 0x8080808080808080 */
548c688dc9Sreyk	imul	%r8,%rdx	/* (c) copied to all bytes */
558c688dc9Sreyk	test	$7,%dil
568c688dc9Sreyk	jnz	20f		/* jump if misaligned */
578c688dc9Sreyk
588c688dc9Sreyk	_ALIGN_TEXT		/* one byte nop */
598c688dc9Sreyk1:
608c688dc9Sreyk	movq	(%rdi),%rax	/* bytes to check (x) */
618c688dc9Sreyk2:
628c688dc9Sreyk	addq	$8,%rdi
638c688dc9Sreyk	mov	%rax,%r10
648c688dc9Sreyk	mov	%rax,%r11	/* for 'char' check */
658c688dc9Sreyk	not	%r10		/* invert of data (~x) */
668c688dc9Sreyk
678c688dc9Sreyk	xorq	%rdx,%r11	/* convert 'char' test to one for NUL */
688c688dc9Sreyk	subq	%r8,%rax	/* x - 0x10 */
698c688dc9Sreyk	movq	%r10,%rsi	/* ~x */
708c688dc9Sreyk	subq	%r8,%r11	/* (x ^ c) - 0x10 */
718c688dc9Sreyk/*
728c688dc9Sreyk * Here we could check ((x - 0x10) | ((x ^ c) - 0x10)) & 0x80
738c688dc9Sreyk * and short-circuit the case where no top bits are set, and
748c688dc9Sreyk * we continue the loop.
758c688dc9Sreyk * However it needs 3 more clocks that are difficult to interleave
768c688dc9Sreyk * in the existing dependency chain ...
778c688dc9Sreyk */
788c688dc9Sreyk	andq	%r9,%rax	/* (x - 0x10) & 0x80 */
798c688dc9Sreyk	xorq	%rdx,%rsi	/* c ^ ~x == ~(c ^ x) */
808c688dc9Sreyk	andq	%r9,%r11	/* ((x ^ c) - 0x10) & 0x80 */
818c688dc9Sreyk	andq	%r10,%rax	/* (x - 0x10) & 0x80 & ~x */
828c688dc9Sreyk	jne	10f		/* jump if string ends */
838c688dc9Sreyk	andq	%rsi,%r11	/* ((x ^ c) - 0x10) & 0x80 & ~(x ^ c) */
848c688dc9Sreyk	je	1b		/* jump if no match */
858c688dc9Sreyk
868c688dc9Sreyk	/* Found char, since LE can use bit scan */
878c688dc9Sreyk	bsf	%r11,%r11	/* 7, 15, 23 ... 63 */
888c688dc9Sreyk8:	shr	$3,%r11		/* 0, 1, 2 .. 7 */
898c688dc9Sreyk	lea	-8(%r11,%rdi),%rax
901d66f0a0Smortimer	jmp 12f
918c688dc9Sreyk
928c688dc9Sreyk/* End of string, check whether char is before NUL */
93b8dcfd0bSguenther	_ALIGN_TRAPS
948c688dc9Sreyk10:
958c688dc9Sreyk	bsf	%rax,%rax	/* count to NUL */
968c688dc9Sreyk	andq	%rsi,%r11	/* check for char in last 8 bytes */
978c688dc9Sreyk	je	11f
988c688dc9Sreyk	bsf	%r11,%r11	/* NUL and char - see which was first */
998c688dc9Sreyk	cmp	%r11,%rax
1008c688dc9Sreyk	jae	8b		/* return 'found' if same - searching for NUL */
1018c688dc9Sreyk11:	xor	%eax,%eax	/* char not found */
1021d66f0a0Smortimer12:	RETGUARD_POP(r9)
1031d66f0a0Smortimer	RETGUARD_CHECK(strchr, r9)
1048c688dc9Sreyk	ret
105fc541c5dSguenther	lfence
1068c688dc9Sreyk
1078c688dc9Sreyk/* Source misaligned: read aligned word and make low bytes invalid */
108b8dcfd0bSguenther/* I (dsl) think aligning the text here will slow things down! */
1098c688dc9Sreyk20:
1108c688dc9Sreyk	xor	%rcx,%rcx
1118c688dc9Sreyk	sub	%dil,%cl	/* Convert low address values 1..7 ... */
1128c688dc9Sreyk	sbb	%rsi,%rsi	/* carry was set, so %rsi now ~0u! */
1138c688dc9Sreyk	and	$7,%cl		/* ... to 7..1 */
1148c688dc9Sreyk	and	$~7,%dil	/* move address to start of word */
1158c688dc9Sreyk	shl	$3,%cl		/* now 56, 48 ... 16, 8 */
1168c688dc9Sreyk	movq	(%rdi),%rax	/* aligned word containing first data */
1178c688dc9Sreyk	xor	%rdx,%rsi	/* invert of search pattern (~c) */
1188c688dc9Sreyk	je	22f		/* searching for 0xff */
1198c688dc9Sreyk21:	shr	%cl,%rsi	/* ~c in low bytes */
1208c688dc9Sreyk	or	%rsi,%rax	/* set some bits making low bytes invalid */
1218c688dc9Sreyk	jmp	2b
1228c688dc9Sreyk
1238c688dc9Sreyk/* We are searching for 0xff, so can't use ~pattern for invalid value */
1248c688dc9Sreyk22:
1258c688dc9Sreyk	mov	%r8,%r10	/* 0x01 pattern */
1268c688dc9Sreyk	lea	(%r8,%r8),%rsi	/* 0x02 - bits gets set (above) */
1278c688dc9Sreyk	not	%r10		/* now 0xfe */
1288c688dc9Sreyk	sar	%cl,%r10	/* top bytes 0xff */
1298c688dc9Sreyk	and	%r10,%rax	/* clear lsb from unwanted low bytes */
1308c688dc9Sreyk	jmp	21b
131*28c67577SguentherEND(strchr)
132