1*0a6a1f1dSLionel Sambuc/* $NetBSD: strlen.S,v 1.6 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/* 33b6cbf720SGianluca Guida * Inspired by a version written by J.T. Conklin <jtc@acorntoolworks.com> 34b6cbf720SGianluca Guida * (Only the long comment really remains his work!) 35b6cbf720SGianluca Guida */ 36b6cbf720SGianluca Guida 37b6cbf720SGianluca Guida#include <machine/asm.h> 38b6cbf720SGianluca Guida 39b6cbf720SGianluca Guida#if defined(LIBC_SCCS) 40*0a6a1f1dSLionel Sambuc RCSID("$NetBSD: strlen.S,v 1.6 2014/03/22 19:16:34 jakllsch Exp $") 41b6cbf720SGianluca Guida#endif 42b6cbf720SGianluca Guida 43b6cbf720SGianluca Guida/* 44b6cbf720SGianluca Guida * There are many well known branch-free sequences which are used 45b6cbf720SGianluca Guida * for determining whether a zero-byte is contained within a word. 46b6cbf720SGianluca Guida * These sequences are generally much more efficent than loading 47b6cbf720SGianluca Guida * and comparing each byte individually. 48b6cbf720SGianluca Guida * 49b6cbf720SGianluca Guida * The expression [1,2]: 50b6cbf720SGianluca Guida * 51b6cbf720SGianluca Guida * (1) ~(((x & 0x7f....7f) + 0x7f....7f) | (x | 0x7f....7f)) 52b6cbf720SGianluca Guida * 53b6cbf720SGianluca Guida * evaluates to a non-zero value if any of the bytes in the 54b6cbf720SGianluca Guida * original word is zero. 55b6cbf720SGianluca Guida * 56b6cbf720SGianluca Guida * It also has the useful property that bytes in the result word 57b6cbf720SGianluca Guida * that correspond to non-zero bytes in the original word have 58b6cbf720SGianluca Guida * the value 0x00, while bytes corresponding to zero bytes have 59b6cbf720SGianluca Guida * the value 0x80. This allows calculation of the first (and 60b6cbf720SGianluca Guida * last) occurrence of a zero byte within the word (useful for C's 61b6cbf720SGianluca Guida * str* primitives) by counting the number of leading (or 62b6cbf720SGianluca Guida * trailing) zeros and dividing the result by 8. On machines 63b6cbf720SGianluca Guida * without (or with slow) clz() / ctz() instructions, testing 64b6cbf720SGianluca Guida * each byte in the result word for zero is necessary. 65b6cbf720SGianluca Guida * 66b6cbf720SGianluca Guida * This typically takes 4 instructions (5 on machines without 67b6cbf720SGianluca Guida * "not-or") not including those needed to load the constant. 68b6cbf720SGianluca Guida * 69b6cbf720SGianluca Guida * 70b6cbf720SGianluca Guida * The expression: 71b6cbf720SGianluca Guida * 72b6cbf720SGianluca Guida * (2) ((x - 0x01....01) & 0x80....80 & ~x) 73b6cbf720SGianluca Guida * 74b6cbf720SGianluca Guida * evaluates to a non-zero value if any of the bytes in the 75b6cbf720SGianluca Guida * original word is zero. 76b6cbf720SGianluca Guida * 77b6cbf720SGianluca Guida * On little endian machines, the first byte in the result word 78b6cbf720SGianluca Guida * that corresponds to a zero byte in the original byte is 0x80, 79b6cbf720SGianluca Guida * so clz() can be used as above. On big endian machines, and 80b6cbf720SGianluca Guida * little endian machines without (or with a slow) clz() insn, 81b6cbf720SGianluca Guida * testing each byte in the original for zero is necessary. 82b6cbf720SGianluca Guida * 83b6cbf720SGianluca Guida * This typically takes 3 instructions (4 on machines without 84b6cbf720SGianluca Guida * "and with complement") not including those needed to load 85b6cbf720SGianluca Guida * constants. 86b6cbf720SGianluca Guida * 87b6cbf720SGianluca Guida * 88b6cbf720SGianluca Guida * The expression: 89b6cbf720SGianluca Guida * 90b6cbf720SGianluca Guida * (3) ((x - 0x01....01) & 0x80....80) 91b6cbf720SGianluca Guida * 92b6cbf720SGianluca Guida * always evaluates to a non-zero value if any of the bytes in 93b6cbf720SGianluca Guida * the original word is zero or has the top bit set. 94b6cbf720SGianluca Guida * For strings that are likely to only contain 7-bit ascii these 95b6cbf720SGianluca Guida * false positives will be rare. 96b6cbf720SGianluca Guida * 97b6cbf720SGianluca Guida * To account for possible false positives, each byte of the 98b6cbf720SGianluca Guida * original word must be checked when the expression evaluates to 99b6cbf720SGianluca Guida * a non-zero value. However, because it is simpler than those 100b6cbf720SGianluca Guida * presented above, code that uses it will be faster as long as 101b6cbf720SGianluca Guida * the rate of false positives is low. 102b6cbf720SGianluca Guida * 103b6cbf720SGianluca Guida * This is likely, because the the false positive can only occur 104b6cbf720SGianluca Guida * if the most siginificant bit of a byte within the word is set. 105b6cbf720SGianluca Guida * The expression will never fail for typical 7-bit ASCII strings. 106b6cbf720SGianluca Guida * 107b6cbf720SGianluca Guida * This typically takes 2 instructions not including those needed 108b6cbf720SGianluca Guida * to load constants. 109b6cbf720SGianluca Guida * 110b6cbf720SGianluca Guida * 111b6cbf720SGianluca Guida * [1] Henry S. Warren Jr., "Hacker's Delight", Addison-Westley 2003 112b6cbf720SGianluca Guida * 113b6cbf720SGianluca Guida * [2] International Business Machines, "The PowerPC Compiler Writer's 114b6cbf720SGianluca Guida * Guide", Warthman Associates, 1996 115b6cbf720SGianluca Guida */ 116b6cbf720SGianluca Guida 117b6cbf720SGianluca Guida#ifdef TEST_STRLEN 118b6cbf720SGianluca GuidaENTRY(test_strlen) 119b6cbf720SGianluca Guida#else 120b6cbf720SGianluca GuidaENTRY(strlen) 121b6cbf720SGianluca Guida#endif 122b6cbf720SGianluca Guida movabsq $0x0101010101010101,%r8 123b6cbf720SGianluca Guida 124b6cbf720SGianluca Guida test $7,%dil 125b6cbf720SGianluca Guida movq %rdi,%rax /* Buffer, %rdi unchanged */ 126b6cbf720SGianluca Guida movabsq $0x8080808080808080,%r9 127b6cbf720SGianluca Guida jnz 10f /* Jump if misaligned */ 128b6cbf720SGianluca Guida 129b6cbf720SGianluca Guida _ALIGN_TEXT 130b6cbf720SGianluca Guida1: 131b6cbf720SGianluca Guida movq (%rax),%rdx /* get bytes to check */ 132b6cbf720SGianluca Guida2: 133b6cbf720SGianluca Guida addq $8,%rax 134b6cbf720SGianluca Guida mov %rdx,%rcx /* save for later check */ 135b6cbf720SGianluca Guida subq %r8,%rdx /* alg (3) above first */ 136b6cbf720SGianluca Guida not %rcx /* Invert of data */ 137b6cbf720SGianluca Guida andq %r9,%rdx 138b6cbf720SGianluca Guida je 1b /* jump if all 0x01-0x80 */ 139b6cbf720SGianluca Guida 140b6cbf720SGianluca Guida /* Do check from alg (2) above - loops for 0x81..0xff bytes */ 141b6cbf720SGianluca Guida andq %rcx,%rdx 142b6cbf720SGianluca Guida je 1b 143b6cbf720SGianluca Guida 144b6cbf720SGianluca Guida /* Since we are LE, use bit scan for first 0x80 byte */ 145b6cbf720SGianluca Guida sub %rdi,%rax /* length to next word */ 146b6cbf720SGianluca Guida bsf %rdx,%rdx /* 7, 15, 23 ... 63 */ 147b6cbf720SGianluca Guida shr $3,%rdx /* 0, 1, 2 ... 7 */ 148b6cbf720SGianluca Guida lea -8(%rax,%rdx),%rax 149b6cbf720SGianluca Guida ret 150b6cbf720SGianluca Guida 151b6cbf720SGianluca Guida/* Misaligned, read aligned word and make low bytes non-zero */ 152b6cbf720SGianluca Guida _ALIGN_TEXT 153b6cbf720SGianluca Guida10: 154b6cbf720SGianluca Guida mov %al,%cl 155b6cbf720SGianluca Guida mov $1,%rsi 156b6cbf720SGianluca Guida and $7,%cl /* offset into word 1..7 */ 157b6cbf720SGianluca Guida and $~7,%al /* start of word with buffer */ 158b6cbf720SGianluca Guida shl $3,%cl /* bit count 8, 16 .. 56 */ 159b6cbf720SGianluca Guida movq (%rax),%rdx /* first data in high bytes */ 160b6cbf720SGianluca Guida shl %cl,%rsi 161b6cbf720SGianluca Guida dec %rsi 162b6cbf720SGianluca Guida or %rsi,%rdx /* low bytes now non-zero */ 163b6cbf720SGianluca Guida jmp 2b 164*0a6a1f1dSLionel Sambuc#ifdef TEST_STRLEN 165*0a6a1f1dSLionel SambucEND(test_strlen) 166*0a6a1f1dSLionel Sambuc#else 167*0a6a1f1dSLionel SambucEND(strlen) 168*0a6a1f1dSLionel Sambuc#endif 169b6cbf720SGianluca Guida 170b6cbf720SGianluca Guida#ifdef TEST_STRLEN 171b6cbf720SGianluca Guida/* trivial implementation when testing above! */ 172b6cbf720SGianluca GuidaENTRY(strlen) 173b6cbf720SGianluca Guida mov %rdi,%rax 174b6cbf720SGianluca Guida1: 175b6cbf720SGianluca Guida cmpb $0,(%rax) 176b6cbf720SGianluca Guida jz 2f 177b6cbf720SGianluca Guida inc %rax 178b6cbf720SGianluca Guida jmp 1b 179b6cbf720SGianluca Guida2: sub %rdi,%rax 180b6cbf720SGianluca Guida ret 181*0a6a1f1dSLionel SambucEND(strlen) 182b6cbf720SGianluca Guida#endif 183