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