xref: /minix3/common/lib/libc/arch/x86_64/string/strchr.S (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc/*	$NetBSD: strchr.S,v 1.7 2014/03/22 19:16:34 jakllsch Exp $	*/
2b6cbf720SGianluca Guida
3b6cbf720SGianluca Guida/*-
4b6cbf720SGianluca Guida * Copyright (c) 2009 The NetBSD Foundation, Inc.
5b6cbf720SGianluca Guida * All rights reserved.
6b6cbf720SGianluca Guida *
7b6cbf720SGianluca Guida * This code is derived from software contributed to The NetBSD Foundation
8b6cbf720SGianluca Guida * by David Laight.
9b6cbf720SGianluca Guida *
10b6cbf720SGianluca Guida * Redistribution and use in source and binary forms, with or without
11b6cbf720SGianluca Guida * modification, are permitted provided that the following conditions
12b6cbf720SGianluca Guida * are met:
13b6cbf720SGianluca Guida * 1. Redistributions of source code must retain the above copyright
14b6cbf720SGianluca Guida *    notice, this list of conditions and the following disclaimer.
15b6cbf720SGianluca Guida * 2. Redistributions in binary form must reproduce the above copyright
16b6cbf720SGianluca Guida *    notice, this list of conditions and the following disclaimer in the
17b6cbf720SGianluca Guida *    documentation and/or other materials provided with the distribution.
18b6cbf720SGianluca Guida *
19b6cbf720SGianluca Guida * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b6cbf720SGianluca Guida * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b6cbf720SGianluca Guida * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b6cbf720SGianluca Guida * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b6cbf720SGianluca Guida * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b6cbf720SGianluca Guida * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b6cbf720SGianluca Guida * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b6cbf720SGianluca Guida * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b6cbf720SGianluca Guida * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b6cbf720SGianluca Guida * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b6cbf720SGianluca Guida * POSSIBILITY OF SUCH DAMAGE.
30b6cbf720SGianluca Guida */
31b6cbf720SGianluca Guida
32b6cbf720SGianluca Guida/* See comments in strlen.S about checking words for byte values */
33b6cbf720SGianluca Guida
34b6cbf720SGianluca Guida#include <machine/asm.h>
35b6cbf720SGianluca Guida
36b6cbf720SGianluca Guida#if defined(LIBC_SCCS)
37*0a6a1f1dSLionel Sambuc	RCSID("$NetBSD: strchr.S,v 1.7 2014/03/22 19:16:34 jakllsch Exp $")
38b6cbf720SGianluca Guida#endif
39b6cbf720SGianluca Guida
40b6cbf720SGianluca Guida/*
41b6cbf720SGianluca Guida * On entry %rdi is the buffer and the low byte of %rsi (%sil) the
42b6cbf720SGianluca Guida * character to search for.
43b6cbf720SGianluca Guida *
44b6cbf720SGianluca Guida * Registers %rdx, %rcx, %r8-%r11 and %rax are also usable
45b6cbf720SGianluca Guida */
46b6cbf720SGianluca Guida
47b6cbf720SGianluca Guida/* Uncomment below to get regression test to run this version but
48b6cbf720SGianluca Guida * have everything else use the trivial one below. */
49b6cbf720SGianluca Guida/* #define TEST_STRCHR */
50b6cbf720SGianluca Guida
51b6cbf720SGianluca Guida#ifdef TEST_STRCHR
52b6cbf720SGianluca GuidaENTRY(test_strchr)
53b6cbf720SGianluca Guida#else
54b6cbf720SGianluca GuidaENTRY(strchr)
55b6cbf720SGianluca Guida#endif
56b6cbf720SGianluca Guida	movabsq	$0x0101010101010101,%r8
57b6cbf720SGianluca Guida
58b6cbf720SGianluca Guida	movzbq	%sil,%rdx	/* value to search for (c) */
59b6cbf720SGianluca Guida	/* These imul are 'directpath' on athlons, so are fast */
60b6cbf720SGianluca Guida	imul	$0x80,%r8,%r9	/* 0x8080808080808080 */
61b6cbf720SGianluca Guida	imul	%r8,%rdx	/* (c) copied to all bytes */
62b6cbf720SGianluca Guida	test	$7,%dil
63b6cbf720SGianluca Guida	jnz	20f		/* jump if misaligned */
64b6cbf720SGianluca Guida
65b6cbf720SGianluca Guida	_ALIGN_TEXT		/* one byte nop */
66b6cbf720SGianluca Guida1:
67b6cbf720SGianluca Guida	movq	(%rdi),%rax	/* bytes to check (x) */
68b6cbf720SGianluca Guida2:
69b6cbf720SGianluca Guida	addq	$8,%rdi
70b6cbf720SGianluca Guida	mov	%rax,%r10
71b6cbf720SGianluca Guida	mov	%rax,%r11	/* for 'char' check */
72b6cbf720SGianluca Guida	not	%r10		/* invert of data (~x) */
73b6cbf720SGianluca Guida
74b6cbf720SGianluca Guida	xorq	%rdx,%r11	/* convert 'char' test to one for NUL */
75b6cbf720SGianluca Guida	subq	%r8,%rax	/* x - 0x10 */
76b6cbf720SGianluca Guida	movq	%r10,%rsi	/* ~x */
77b6cbf720SGianluca Guida	subq	%r8,%r11	/* (x ^ c) - 0x10 */
78b6cbf720SGianluca Guida/*
79b6cbf720SGianluca Guida * Here we could check ((x - 0x10) | ((x ^ c) - 0x10)) & 0x80
80b6cbf720SGianluca Guida * and short-circuit the case where no top bits are set, and
81b6cbf720SGianluca Guida * we continue the loop.
82b6cbf720SGianluca Guida * However it needs 3 more clocks that are difficult to interleave
83b6cbf720SGianluca Guida * in the existing dependency chain ...
84b6cbf720SGianluca Guida */
85b6cbf720SGianluca Guida	andq	%r9,%rax	/* (x - 0x10) & 0x80 */
86b6cbf720SGianluca Guida	xorq	%rdx,%rsi	/* c ^ ~x == ~(c ^ x) */
87b6cbf720SGianluca Guida	andq	%r9,%r11	/* ((x ^ c) - 0x10) & 0x80 */
88b6cbf720SGianluca Guida	andq	%r10,%rax	/* (x - 0x10) & 0x80 & ~x */
89b6cbf720SGianluca Guida	jne	10f		/* jump if string ends */
90b6cbf720SGianluca Guida	andq	%rsi,%r11	/* ((x ^ c) - 0x10) & 0x80 & ~(x ^ c) */
91b6cbf720SGianluca Guida	je	1b		/* jump if no match */
92b6cbf720SGianluca Guida
93b6cbf720SGianluca Guida	/* Found char, since LE can use bit scan */
94b6cbf720SGianluca Guida	bsf	%r11,%r11	/* 7, 15, 23 ... 63 */
95b6cbf720SGianluca Guida8:	shr	$3,%r11		/* 0, 1, 2 .. 7 */
96b6cbf720SGianluca Guida	lea	-8(%r11,%rdi),%rax
97b6cbf720SGianluca Guida	ret
98b6cbf720SGianluca Guida
99b6cbf720SGianluca Guida/* End of string, check whether char is before NUL */
100b6cbf720SGianluca Guida	_ALIGN_TEXT		/* adds three byte nop */
101b6cbf720SGianluca Guida10:
102b6cbf720SGianluca Guida	bsf	%rax,%rax	/* count to NUL */
103b6cbf720SGianluca Guida	andq	%rsi,%r11	/* check for char in last 8 bytes */
104b6cbf720SGianluca Guida	je	11f
105b6cbf720SGianluca Guida	bsf	%r11,%r11	/* NUL and char - see which was first */
106b6cbf720SGianluca Guida	cmp	%r11,%rax
107b6cbf720SGianluca Guida	jae	8b		/* return 'found' if same - searching for NUL */
108b6cbf720SGianluca Guida11:	xor	%eax,%eax	/* char not found */
109b6cbf720SGianluca Guida	ret
110b6cbf720SGianluca Guida
111b6cbf720SGianluca Guida/* Source misaligned: read aligned word and make low bytes invalid */
112b6cbf720SGianluca Guida/* I (dsl) think a _ALIGN_TEXT here will slow things down! */
113b6cbf720SGianluca Guida20:
114b6cbf720SGianluca Guida	xor	%rcx,%rcx
115b6cbf720SGianluca Guida	sub	%dil,%cl	/* Convert low address values 1..7 ... */
116b6cbf720SGianluca Guida	sbb	%rsi,%rsi	/* carry was set, so %rsi now ~0u! */
117b6cbf720SGianluca Guida	and	$7,%cl		/* ... to 7..1 */
118b6cbf720SGianluca Guida	and	$~7,%dil	/* move address to start of word */
119b6cbf720SGianluca Guida	shl	$3,%cl		/* now 56, 48 ... 16, 8 */
120b6cbf720SGianluca Guida	movq	(%rdi),%rax	/* aligned word containing first data */
121b6cbf720SGianluca Guida	xor	%rdx,%rsi	/* invert of search pattern (~c) */
122b6cbf720SGianluca Guida	je	22f		/* searching for 0xff */
123b6cbf720SGianluca Guida21:	shr	%cl,%rsi	/* ~c in low bytes */
124b6cbf720SGianluca Guida	or	%rsi,%rax	/* set some bits making low bytes invalid */
125b6cbf720SGianluca Guida	jmp	2b
126b6cbf720SGianluca Guida
127b6cbf720SGianluca Guida/* We are searching for 0xff, so can't use ~pattern for invalid value */
128b6cbf720SGianluca Guida22:
129b6cbf720SGianluca Guida	mov	%r8,%r10	/* 0x01 pattern */
130b6cbf720SGianluca Guida	lea	(%r8,%r8),%rsi	/* 0x02 - bits gets set (above) */
131b6cbf720SGianluca Guida	not	%r10		/* now 0xfe */
132b6cbf720SGianluca Guida	sar	%cl,%r10	/* top bytes 0xff */
133b6cbf720SGianluca Guida	and	%r10,%rax	/* clear lsb from unwanted low bytes */
134b6cbf720SGianluca Guida	jmp	21b
135*0a6a1f1dSLionel Sambuc#ifdef TEST_STRCHR
136*0a6a1f1dSLionel SambucEND(test_strchr)
137*0a6a1f1dSLionel Sambuc#else
138*0a6a1f1dSLionel SambucEND(strchr)
139*0a6a1f1dSLionel Sambuc#endif
140b6cbf720SGianluca Guida
141b6cbf720SGianluca Guida#ifdef TEST_STRCHR
142b6cbf720SGianluca Guida/* Trivial version for bug-fixing above */
143b6cbf720SGianluca GuidaENTRY(strchr)
144b6cbf720SGianluca Guida	movq	%rsi,%rdx
145b6cbf720SGianluca Guida	movq	%rdi,%rsi
146b6cbf720SGianluca Guida1:
147b6cbf720SGianluca Guida	lodsb
148b6cbf720SGianluca Guida	cmp	%al,%dl
149b6cbf720SGianluca Guida	je	2f
150b6cbf720SGianluca Guida	test	%al,%al
151b6cbf720SGianluca Guida	jne	1b
152b6cbf720SGianluca Guida	xor	%eax,%eax
153b6cbf720SGianluca Guida	ret
154b6cbf720SGianluca Guida2:	lea	-1(%rsi),%rax
155b6cbf720SGianluca Guida	ret
156*0a6a1f1dSLionel SambucEND(strchr)
157b6cbf720SGianluca Guida#endif
158b6cbf720SGianluca Guida
159b6cbf720SGianluca GuidaSTRONG_ALIAS(index,strchr)
160