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/*
2N/A * Copyright 2008 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 * Given string s, return length (not including the terminating null).
2N/A *
2N/A * Fast assembler 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(s)
2N/A * register const char *s;
2N/A * {
2N/A * register 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
2N/A#include <sys/asm_linkage.h>
2N/A
2N/A ! The object of strlen is to, as quickly as possible, find the
2N/A ! null byte. To this end, we attempt to get our string aligned
2N/A ! and then blast across it using Alan Mycroft's algorithm for
2N/A ! finding null bytes. If we are not aligned, the string is
2N/A ! checked a byte at a time until it is. Once this occurs,
2N/A ! we can proceed word-wise across it. Once a word with a
2N/A ! zero byte has been found, we then check the word a byte
2N/A ! at a time until we've located the zero byte, and return
2N/A ! the proper length.
2N/A
2N/A .align 32
2N/A ENTRY(strlen)
2N/A andcc %o0, 3, %o4 ! is src word aligned
2N/A bz,pt %icc, .nowalgnd
2N/A mov %o0, %o2
2N/A
2N/A cmp %o4, 2 ! is src half-word aligned
2N/A be,a,pn %icc, .s2algn
2N/A lduh [%o2], %o1
2N/A
2N/A ldub [%o2], %o1
2N/A tst %o1 ! byte zero?
2N/A bz,pn %icc, .done
2N/A cmp %o4, 3 ! src is byte aligned
2N/A
2N/A be,pn %icc, .nowalgnd
2N/A inc 1, %o2
2N/A
2N/A lduh [%o2], %o1
2N/A
2N/A.s2algn:
2N/A srl %o1, 8, %o4
2N/A tst %o4
2N/A bz,pn %icc, .done
2N/A andcc %o1, 0xff, %g0
2N/A
2N/A bz,pn %icc, .done
2N/A inc 1, %o2
2N/A
2N/A inc 1, %o2
2N/A
2N/A.nowalgnd:
2N/A ld [%o2], %o1
2N/A sethi %hi(0x01010101), %o4
2N/A sethi %hi(0x80808080), %o5
2N/A or %o4, %lo(0x01010101), %o4
2N/A or %o5, %lo(0x80808080), %o5
2N/A
2N/A andn %o5, %o1, %o3
2N/A sub %o1, %o4, %g1
2N/A andcc %o3, %g1, %g0
2N/A bnz,a,pn %icc, .nullfound
2N/A sethi %hi(0xff000000), %o4
2N/A
2N/A ld [%o2+4], %o1
2N/A inc 4, %o2
2N/A
2N/A.loop: ! this should be aligned to 32
2N/A inc 4, %o2
2N/A andn %o5, %o1, %o3 ! %o5 = ~word & 0x80808080
2N/A sub %o1, %o4, %g1 ! %g1 = word - 0x01010101
2N/A andcc %o3, %g1, %g0
2N/A bz,a,pt %icc, .loop
2N/A ld [%o2], %o1
2N/A
2N/A dec 4, %o2
2N/A sethi %hi(0xff000000), %o4
2N/A.nullfound:
2N/A andcc %o1, %o4, %g0
2N/A bz,pn %icc, .done ! first byte zero
2N/A srl %o4, 8, %o4
2N/A
2N/A andcc %o1, %o4, %g0
2N/A bz,pn %icc, .done ! second byte zero
2N/A inc 1, %o2
2N/A
2N/A srl %o4, 8, %o4
2N/A andcc %o1, %o4, %g0
2N/A bz,pn %icc, .done ! thrid byte zero
2N/A inc 1, %o2
2N/A
2N/A inc 1, %o2 ! fourth byte zero
2N/A.done:
2N/A retl
2N/A sub %o2, %o0, %o0
2N/A SET_SIZE(strlen)
2N/A