2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A .file "strlen.s"
2N/A
2N/A/
2N/A/ strlen(s)
2N/A/
2N/A/ Returns the number of non-NULL bytes in string argument.
2N/A/
2N/A/
2N/A/ Fast assembly language version of the following C-program strlen
2N/A/ which represents the `standard' for the C-library.
2N/A/
2N/A/ size_t
2N/A/ strlen(const char *s)
2N/A/ {
2N/A/ const char *s0 = s + 1;
2N/A/
2N/A/ while (*s++ != '\0')
2N/A/ ;
2N/A/ return (s - s0);
2N/A/ }
2N/A/
2N/A/ In this assembly language version, the following expression is used
2N/A/ to check if a 32-bit word data contains a null byte or not:
2N/A/ (((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080
2N/A/ If the above expression geneates a value other than 0x80808080,
2N/A/ that means the 32-bit word data contains a null byte.
2N/A/
2N/A
2N/A#include "SYS.h"
2N/A
2N/A ENTRY(strlen)
2N/A mov 4(%esp), %edx / src in %edx
2N/A mov %edx, %eax / cpy src to %eax
2N/A
2N/A and $3, %edx / is src aligned?
2N/A jz countbytes
2N/A / work byte-wise until aligned
2N/A cmpb $0, (%eax) / is *src == 0 ?
2N/A jz done
2N/A inc %eax / increment src
2N/A cmp $3, %edx / if aligned, jump to word-wise check
2N/A jz countbytes
2N/A cmpb $0, (%eax)
2N/A jz done
2N/A inc %eax
2N/A cmp $2, %edx
2N/A jz countbytes
2N/A cmpb $0, (%eax)
2N/A jz done
2N/A inc %eax
2N/A
2N/A .align 16
2N/A
2N/Acountbytes:
2N/A mov (%eax), %ecx / load wrd
2N/A add $4, %eax / increment src by 4 (bytes in word)
2N/A lea -0x01010101(%ecx), %edx / (wrd - 0x01010101)
2N/A not %ecx / ~wrd
2N/A and $0x80808080, %ecx / ~wrd & 0x80808080
2N/A and %edx, %ecx / (wrd - 0x01010101) & ~wrd & 0x80808080
2N/A jz countbytes / if zero, no null byte found -- cont
2N/A
2N/Ahas_zero_byte:
2N/A bsfl %ecx, %ecx / find first set bit (null byte)
2N/A shr $3, %ecx / switch bit position to byte posn
2N/A lea -4(%eax, %ecx, 1), %eax / undo pre-increment and count bytes
2N/Adone:
2N/A sub 4(%esp), %eax / return (src - old_src)
2N/A ret
2N/A SET_SIZE(strlen)