1*631951aaSmortimer/* $OpenBSD: strchr.S,v 1.9 2018/07/03 23:14:05 mortimer Exp $ */ 2c2a49701Sreyk/* $NetBSD: strchr.S,v 1.7 2014/03/22 19:16:34 jakllsch Exp $ */ 3c2a49701Sreyk 4c2a49701Sreyk/*- 5c2a49701Sreyk * Copyright (c) 2009 The NetBSD Foundation, Inc. 6c2a49701Sreyk * All rights reserved. 7c2a49701Sreyk * 8c2a49701Sreyk * This code is derived from software contributed to The NetBSD Foundation 9c2a49701Sreyk * by David Laight. 10c2a49701Sreyk * 11c2a49701Sreyk * Redistribution and use in source and binary forms, with or without 12c2a49701Sreyk * modification, are permitted provided that the following conditions 13c2a49701Sreyk * are met: 14c2a49701Sreyk * 1. Redistributions of source code must retain the above copyright 15c2a49701Sreyk * notice, this list of conditions and the following disclaimer. 16c2a49701Sreyk * 2. Redistributions in binary form must reproduce the above copyright 17c2a49701Sreyk * notice, this list of conditions and the following disclaimer in the 18c2a49701Sreyk * documentation and/or other materials provided with the distribution. 19c2a49701Sreyk * 20c2a49701Sreyk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21c2a49701Sreyk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22c2a49701Sreyk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23c2a49701Sreyk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24c2a49701Sreyk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25c2a49701Sreyk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26c2a49701Sreyk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27c2a49701Sreyk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28c2a49701Sreyk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29c2a49701Sreyk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30c2a49701Sreyk * POSSIBILITY OF SUCH DAMAGE. 315b859c19Sderaadt */ 325b859c19Sderaadt 33c2a49701Sreyk/* See comments in strlen.S about checking words for byte values */ 34c2a49701Sreyk 35b4551fdeSguenther#include "DEFS.h" 365b859c19Sderaadt 37cb39b413SmillertWEAK_ALIAS(index, strchr) 38c2a49701Sreyk 39c2a49701Sreyk/* 40c2a49701Sreyk * On entry %rdi is the buffer and the low byte of %rsi (%sil) the 41c2a49701Sreyk * character to search for. 42c2a49701Sreyk * 43c2a49701Sreyk * Registers %rdx, %rcx, %r8-%r11 and %rax are also usable 44c2a49701Sreyk */ 45c2a49701Sreyk 465b859c19SderaadtENTRY(strchr) 47*631951aaSmortimer RETGUARD_SETUP(strchr, r8) 48*631951aaSmortimer RETGUARD_PUSH(r8) 49c2a49701Sreyk movabsq $0x0101010101010101,%r8 50c2a49701Sreyk 51c2a49701Sreyk movzbq %sil,%rdx /* value to search for (c) */ 52c2a49701Sreyk /* These imul are 'directpath' on athlons, so are fast */ 53c2a49701Sreyk imul $0x80,%r8,%r9 /* 0x8080808080808080 */ 54c2a49701Sreyk imul %r8,%rdx /* (c) copied to all bytes */ 55c2a49701Sreyk test $7,%dil 56c2a49701Sreyk jnz 20f /* jump if misaligned */ 57c2a49701Sreyk 58c2a49701Sreyk _ALIGN_TEXT /* one byte nop */ 59c2a49701Sreyk1: 60c2a49701Sreyk movq (%rdi),%rax /* bytes to check (x) */ 61c2a49701Sreyk2: 62c2a49701Sreyk addq $8,%rdi 63c2a49701Sreyk mov %rax,%r10 64c2a49701Sreyk mov %rax,%r11 /* for 'char' check */ 65c2a49701Sreyk not %r10 /* invert of data (~x) */ 66c2a49701Sreyk 67c2a49701Sreyk xorq %rdx,%r11 /* convert 'char' test to one for NUL */ 68c2a49701Sreyk subq %r8,%rax /* x - 0x10 */ 69c2a49701Sreyk movq %r10,%rsi /* ~x */ 70c2a49701Sreyk subq %r8,%r11 /* (x ^ c) - 0x10 */ 71c2a49701Sreyk/* 72c2a49701Sreyk * Here we could check ((x - 0x10) | ((x ^ c) - 0x10)) & 0x80 73c2a49701Sreyk * and short-circuit the case where no top bits are set, and 74c2a49701Sreyk * we continue the loop. 75c2a49701Sreyk * However it needs 3 more clocks that are difficult to interleave 76c2a49701Sreyk * in the existing dependency chain ... 77c2a49701Sreyk */ 78c2a49701Sreyk andq %r9,%rax /* (x - 0x10) & 0x80 */ 79c2a49701Sreyk xorq %rdx,%rsi /* c ^ ~x == ~(c ^ x) */ 80c2a49701Sreyk andq %r9,%r11 /* ((x ^ c) - 0x10) & 0x80 */ 81c2a49701Sreyk andq %r10,%rax /* (x - 0x10) & 0x80 & ~x */ 82c2a49701Sreyk jne 10f /* jump if string ends */ 83c2a49701Sreyk andq %rsi,%r11 /* ((x ^ c) - 0x10) & 0x80 & ~(x ^ c) */ 84c2a49701Sreyk je 1b /* jump if no match */ 85c2a49701Sreyk 86c2a49701Sreyk /* Found char, since LE can use bit scan */ 87c2a49701Sreyk bsf %r11,%r11 /* 7, 15, 23 ... 63 */ 88c2a49701Sreyk8: shr $3,%r11 /* 0, 1, 2 .. 7 */ 89c2a49701Sreyk lea -8(%r11,%rdi),%rax 90*631951aaSmortimer jmp 12f 91c2a49701Sreyk 92c2a49701Sreyk/* End of string, check whether char is before NUL */ 93c2a49701Sreyk _ALIGN_TEXT /* adds three byte nop */ 94c2a49701Sreyk10: 95c2a49701Sreyk bsf %rax,%rax /* count to NUL */ 96c2a49701Sreyk andq %rsi,%r11 /* check for char in last 8 bytes */ 97c2a49701Sreyk je 11f 98c2a49701Sreyk bsf %r11,%r11 /* NUL and char - see which was first */ 99c2a49701Sreyk cmp %r11,%rax 100c2a49701Sreyk jae 8b /* return 'found' if same - searching for NUL */ 101c2a49701Sreyk11: xor %eax,%eax /* char not found */ 102*631951aaSmortimer12: RETGUARD_POP(r11) 103*631951aaSmortimer RETGUARD_CHECK(strchr, r11) 104c2a49701Sreyk ret 105c2a49701Sreyk 106c2a49701Sreyk/* Source misaligned: read aligned word and make low bytes invalid */ 107c2a49701Sreyk/* I (dsl) think a _ALIGN_TEXT here will slow things down! */ 108c2a49701Sreyk20: 109c2a49701Sreyk xor %rcx,%rcx 110c2a49701Sreyk sub %dil,%cl /* Convert low address values 1..7 ... */ 111c2a49701Sreyk sbb %rsi,%rsi /* carry was set, so %rsi now ~0u! */ 112c2a49701Sreyk and $7,%cl /* ... to 7..1 */ 113c2a49701Sreyk and $~7,%dil /* move address to start of word */ 114c2a49701Sreyk shl $3,%cl /* now 56, 48 ... 16, 8 */ 115c2a49701Sreyk movq (%rdi),%rax /* aligned word containing first data */ 116c2a49701Sreyk xor %rdx,%rsi /* invert of search pattern (~c) */ 117c2a49701Sreyk je 22f /* searching for 0xff */ 118c2a49701Sreyk21: shr %cl,%rsi /* ~c in low bytes */ 119c2a49701Sreyk or %rsi,%rax /* set some bits making low bytes invalid */ 120c2a49701Sreyk jmp 2b 121c2a49701Sreyk 122c2a49701Sreyk/* We are searching for 0xff, so can't use ~pattern for invalid value */ 123c2a49701Sreyk22: 124c2a49701Sreyk mov %r8,%r10 /* 0x01 pattern */ 125c2a49701Sreyk lea (%r8,%r8),%rsi /* 0x02 - bits gets set (above) */ 126c2a49701Sreyk not %r10 /* now 0xfe */ 127c2a49701Sreyk sar %cl,%r10 /* top bytes 0xff */ 128c2a49701Sreyk and %r10,%rax /* clear lsb from unwanted low bytes */ 129c2a49701Sreyk jmp 21b 1309b9d2a55SguentherEND_STRONG(strchr) 131