1 1.8 andvar /* $NetBSD: strlen.S,v 1.8 2024/03/30 22:03:39 andvar Exp $ */ 2 1.5 dsl 3 1.5 dsl /*- 4 1.5 dsl * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 1.5 dsl * All rights reserved. 6 1.5 dsl * 7 1.5 dsl * This code is derived from software contributed to The NetBSD Foundation 8 1.5 dsl * by David Laight. 9 1.5 dsl * 10 1.5 dsl * Redistribution and use in source and binary forms, with or without 11 1.5 dsl * modification, are permitted provided that the following conditions 12 1.5 dsl * are met: 13 1.5 dsl * 1. Redistributions of source code must retain the above copyright 14 1.5 dsl * notice, this list of conditions and the following disclaimer. 15 1.5 dsl * 2. Redistributions in binary form must reproduce the above copyright 16 1.5 dsl * notice, this list of conditions and the following disclaimer in the 17 1.5 dsl * documentation and/or other materials provided with the distribution. 18 1.5 dsl * 19 1.5 dsl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.5 dsl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.5 dsl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.5 dsl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.5 dsl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.5 dsl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.5 dsl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.5 dsl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.5 dsl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.5 dsl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.5 dsl * POSSIBILITY OF SUCH DAMAGE. 30 1.5 dsl */ 31 1.5 dsl 32 1.1 christos /* 33 1.5 dsl * Inspired by a version written by J.T. Conklin <jtc (at) acorntoolworks.com> 34 1.5 dsl * (Only the long comment really remains his work!) 35 1.1 christos */ 36 1.1 christos 37 1.1 christos #include <machine/asm.h> 38 1.1 christos 39 1.1 christos #if defined(LIBC_SCCS) 40 1.8 andvar RCSID("$NetBSD: strlen.S,v 1.8 2024/03/30 22:03:39 andvar Exp $") 41 1.1 christos #endif 42 1.1 christos 43 1.3 dsl /* 44 1.3 dsl * There are many well known branch-free sequences which are used 45 1.3 dsl * for determining whether a zero-byte is contained within a word. 46 1.7 andvar * These sequences are generally much more efficient than loading 47 1.3 dsl * and comparing each byte individually. 48 1.3 dsl * 49 1.3 dsl * The expression [1,2]: 50 1.3 dsl * 51 1.3 dsl * (1) ~(((x & 0x7f....7f) + 0x7f....7f) | (x | 0x7f....7f)) 52 1.3 dsl * 53 1.3 dsl * evaluates to a non-zero value if any of the bytes in the 54 1.3 dsl * original word is zero. 55 1.3 dsl * 56 1.3 dsl * It also has the useful property that bytes in the result word 57 1.3 dsl * that correspond to non-zero bytes in the original word have 58 1.3 dsl * the value 0x00, while bytes corresponding to zero bytes have 59 1.3 dsl * the value 0x80. This allows calculation of the first (and 60 1.3 dsl * last) occurrence of a zero byte within the word (useful for C's 61 1.3 dsl * str* primitives) by counting the number of leading (or 62 1.3 dsl * trailing) zeros and dividing the result by 8. On machines 63 1.3 dsl * without (or with slow) clz() / ctz() instructions, testing 64 1.3 dsl * each byte in the result word for zero is necessary. 65 1.3 dsl * 66 1.3 dsl * This typically takes 4 instructions (5 on machines without 67 1.3 dsl * "not-or") not including those needed to load the constant. 68 1.3 dsl * 69 1.3 dsl * 70 1.3 dsl * The expression: 71 1.3 dsl * 72 1.3 dsl * (2) ((x - 0x01....01) & 0x80....80 & ~x) 73 1.3 dsl * 74 1.3 dsl * evaluates to a non-zero value if any of the bytes in the 75 1.3 dsl * original word is zero. 76 1.3 dsl * 77 1.3 dsl * On little endian machines, the first byte in the result word 78 1.3 dsl * that corresponds to a zero byte in the original byte is 0x80, 79 1.3 dsl * so clz() can be used as above. On big endian machines, and 80 1.3 dsl * little endian machines without (or with a slow) clz() insn, 81 1.3 dsl * testing each byte in the original for zero is necessary. 82 1.3 dsl * 83 1.3 dsl * This typically takes 3 instructions (4 on machines without 84 1.3 dsl * "and with complement") not including those needed to load 85 1.3 dsl * constants. 86 1.3 dsl * 87 1.3 dsl * 88 1.3 dsl * The expression: 89 1.3 dsl * 90 1.3 dsl * (3) ((x - 0x01....01) & 0x80....80) 91 1.3 dsl * 92 1.3 dsl * always evaluates to a non-zero value if any of the bytes in 93 1.3 dsl * the original word is zero or has the top bit set. 94 1.3 dsl * For strings that are likely to only contain 7-bit ascii these 95 1.3 dsl * false positives will be rare. 96 1.3 dsl * 97 1.3 dsl * To account for possible false positives, each byte of the 98 1.3 dsl * original word must be checked when the expression evaluates to 99 1.3 dsl * a non-zero value. However, because it is simpler than those 100 1.3 dsl * presented above, code that uses it will be faster as long as 101 1.3 dsl * the rate of false positives is low. 102 1.3 dsl * 103 1.3 dsl * This is likely, because the the false positive can only occur 104 1.3 dsl * if the most siginificant bit of a byte within the word is set. 105 1.3 dsl * The expression will never fail for typical 7-bit ASCII strings. 106 1.3 dsl * 107 1.3 dsl * This typically takes 2 instructions not including those needed 108 1.3 dsl * to load constants. 109 1.3 dsl * 110 1.3 dsl * 111 1.8 andvar * [1] Henry S. Warren Jr., "Hacker's Delight", Addison-Wesley 2003 112 1.3 dsl * 113 1.3 dsl * [2] International Business Machines, "The PowerPC Compiler Writer's 114 1.3 dsl * Guide", Warthman Associates, 1996 115 1.3 dsl */ 116 1.3 dsl 117 1.3 dsl #ifdef TEST_STRLEN 118 1.3 dsl ENTRY(test_strlen) 119 1.3 dsl #else 120 1.1 christos ENTRY(strlen) 121 1.3 dsl #endif 122 1.3 dsl movabsq $0x0101010101010101,%r8 123 1.3 dsl 124 1.3 dsl test $7,%dil 125 1.3 dsl movq %rdi,%rax /* Buffer, %rdi unchanged */ 126 1.3 dsl movabsq $0x8080808080808080,%r9 127 1.3 dsl jnz 10f /* Jump if misaligned */ 128 1.1 christos 129 1.3 dsl _ALIGN_TEXT 130 1.3 dsl 1: 131 1.3 dsl movq (%rax),%rdx /* get bytes to check */ 132 1.3 dsl 2: 133 1.3 dsl addq $8,%rax 134 1.3 dsl mov %rdx,%rcx /* save for later check */ 135 1.3 dsl subq %r8,%rdx /* alg (3) above first */ 136 1.3 dsl not %rcx /* Invert of data */ 137 1.3 dsl andq %r9,%rdx 138 1.4 dsl je 1b /* jump if all 0x01-0x80 */ 139 1.3 dsl 140 1.4 dsl /* Do check from alg (2) above - loops for 0x81..0xff bytes */ 141 1.3 dsl andq %rcx,%rdx 142 1.3 dsl je 1b 143 1.3 dsl 144 1.3 dsl /* Since we are LE, use bit scan for first 0x80 byte */ 145 1.3 dsl sub %rdi,%rax /* length to next word */ 146 1.3 dsl bsf %rdx,%rdx /* 7, 15, 23 ... 63 */ 147 1.3 dsl shr $3,%rdx /* 0, 1, 2 ... 7 */ 148 1.3 dsl lea -8(%rax,%rdx),%rax 149 1.1 christos ret 150 1.1 christos 151 1.3 dsl /* Misaligned, read aligned word and make low bytes non-zero */ 152 1.1 christos _ALIGN_TEXT 153 1.3 dsl 10: 154 1.5 dsl mov %al,%cl 155 1.3 dsl mov $1,%rsi 156 1.5 dsl and $7,%cl /* offset into word 1..7 */ 157 1.3 dsl and $~7,%al /* start of word with buffer */ 158 1.3 dsl shl $3,%cl /* bit count 8, 16 .. 56 */ 159 1.3 dsl movq (%rax),%rdx /* first data in high bytes */ 160 1.3 dsl shl %cl,%rsi 161 1.3 dsl dec %rsi 162 1.3 dsl or %rsi,%rdx /* low bytes now non-zero */ 163 1.3 dsl jmp 2b 164 1.6 jakllsch #ifdef TEST_STRLEN 165 1.6 jakllsch END(test_strlen) 166 1.6 jakllsch #else 167 1.6 jakllsch END(strlen) 168 1.6 jakllsch #endif 169 1.1 christos 170 1.3 dsl #ifdef TEST_STRLEN 171 1.3 dsl /* trivial implementation when testing above! */ 172 1.3 dsl ENTRY(strlen) 173 1.3 dsl mov %rdi,%rax 174 1.3 dsl 1: 175 1.3 dsl cmpb $0,(%rax) 176 1.3 dsl jz 2f 177 1.3 dsl inc %rax 178 1.3 dsl jmp 1b 179 1.3 dsl 2: sub %rdi,%rax 180 1.1 christos ret 181 1.6 jakllsch END(strlen) 182 1.3 dsl #endif 183