memchr.S revision 1.4 1 1.4 christos /* $NetBSD: memchr.S,v 1.4 2009/07/20 15:21:00 christos Exp $ */
2 1.4 christos
3 1.4 christos /*-
4 1.4 christos * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 1.4 christos * All rights reserved.
6 1.4 christos *
7 1.4 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.4 christos * by David Laight.
9 1.4 christos *
10 1.4 christos * Redistribution and use in source and binary forms, with or without
11 1.4 christos * modification, are permitted provided that the following conditions
12 1.4 christos * are met:
13 1.4 christos * 1. Redistributions of source code must retain the above copyright
14 1.4 christos * notice, this list of conditions and the following disclaimer.
15 1.4 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.4 christos * notice, this list of conditions and the following disclaimer in the
17 1.4 christos * documentation and/or other materials provided with the distribution.
18 1.4 christos *
19 1.4 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.4 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.4 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.4 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.4 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.4 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.4 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.4 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.4 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.4 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.4 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <machine/asm.h>
33 1.1 christos
34 1.1 christos #if defined(LIBC_SCCS)
35 1.4 christos RCSID("$NetBSD: memchr.S,v 1.4 2009/07/20 15:21:00 christos Exp $")
36 1.1 christos #endif
37 1.1 christos
38 1.4 christos /*
39 1.4 christos * The instruction sequences used try to avoid data dependencies
40 1.4 christos * between adjacent instructions (to allow parallel execution).
41 1.4 christos * The 'imul' for %r9 could be put into the delay following the
42 1.4 christos * memory read (ie inside the loop) at no obvious cost - except
43 1.4 christos * that the loop is currently exactly 32 bytes - 2 fetch blocks!.
44 1.4 christos *
45 1.4 christos * I don't think aligning any of the other branch targets is useful.
46 1.4 christos */
47 1.4 christos
48 1.3 christos ENTRY(memchr)
49 1.4 christos movabsq $0x0101010101010101,%r8
50 1.4 christos lea (%rdi,%rdx),%r10 /* limit of buffer to scan */
51 1.4 christos movzbq %sil,%rsi /* mask high bits! */
52 1.3 christos
53 1.4 christos /* 'directpath' imuls can execute 3 at a time ... (amd) */
54 1.4 christos imul %r8,%rsi /* search byte replicated in word */
55 1.4 christos imul $0x80,%r8,%r9 /* 0x8080808080808080 */
56 1.4 christos test $7,%dil
57 1.4 christos jnz 20f /* jump if misaligned */
58 1.4 christos jmp 1f /* jump to avoid 4 nops (13 bytes) in gap */
59 1.4 christos
60 1.4 christos _ALIGN_TEXT /* entire loop now in 32 aligned bytes */
61 1.4 christos 1:
62 1.4 christos cmpq %r10,%rdi /* end of buffer ? */
63 1.4 christos jae 30f /* jump if so */
64 1.2 dsl
65 1.4 christos movq (%rdi),%rax /* value to check */
66 1.4 christos 2:
67 1.3 christos addq $8,%rdi
68 1.4 christos xorq %rsi,%rax /* now looking for zeros */
69 1.4 christos mov %rax,%rcx
70 1.4 christos subq %r8,%rax /* x - 0x01 */
71 1.4 christos not %rcx
72 1.4 christos andq %r9,%rax /* (x - 0x01) & 0x80 */
73 1.4 christos andq %rcx,%rax /* ((x - 0x01) & 0x80) & ~x */
74 1.4 christos je 1b /* jump if not found */
75 1.4 christos
76 1.4 christos /* Found byte in word, get its address */
77 1.4 christos bsf %rax,%rax
78 1.4 christos shr $3,%eax
79 1.4 christos lea -8(%rax,%rdi),%rax
80 1.4 christos cmpq %r10,%rax /* need to check not beyond buffer */
81 1.4 christos jae 30f
82 1.4 christos rep
83 1.4 christos ret /* amd - no ret after jmp */
84 1.4 christos
85 1.4 christos /* Input misaligned, read aligned and kill low bits */
86 1.4 christos /* (Getting a -1 is surprisingly hard work!) */
87 1.4 christos 20:
88 1.4 christos xor %eax,%eax /* zeros all 64 bits */
89 1.4 christos mov %dil,%cl /* misalignment amount 1..7 (+high bits )*/
90 1.4 christos test %rdx,%rdx /* zero length, don't read */
91 1.4 christos jz 30f
92 1.4 christos
93 1.4 christos and $~7,%dil /* %rdi now start of word */
94 1.4 christos lea -1(%rax),%r11 /* all 0xff */
95 1.4 christos and $7,%cl /* 1..7 */
96 1.4 christos
97 1.4 christos mov (%rdi),%rax /* word containing first byte */
98 1.4 christos shl $3,%cl /* 8..56 */
99 1.4 christos cmp %r11,%rsi /* searching for 0xff */
100 1.4 christos jz 25f
101 1.4 christos
102 1.4 christos /* Searching for other than 0xff, set low bytes */
103 1.4 christos shl %cl,%r11 /* 0xff in high (wanted) bytes */
104 1.4 christos not %r11 /* 0xff in low (unwanted) bytes */
105 1.4 christos or %r11,%rax /* low bytes now set */
106 1.4 christos jmp 2b
107 1.4 christos
108 1.4 christos 25: /* Searching for 0xff, clear low bytes */
109 1.4 christos shl %cl,%r11 /* 0xff in high (wanted) bytes */
110 1.4 christos and %r11,%rax /* low bytes now zero */
111 1.4 christos jmp 2b
112 1.1 christos
113 1.4 christos /* Not found */
114 1.4 christos 30: xorq %rax,%rax
115 1.1 christos ret
116