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 "strcpy.s"
2N/A
2N/A/*
2N/A * strcpy(s1, s2)
2N/A *
2N/A * Copy string s2 to s1. s1 must be large enough. Return s1.
2N/A *
2N/A * Fast assembler language version of the following C-program strcpy
2N/A * which represents the `standard' for the C-library.
2N/A *
2N/A * char *
2N/A * strcpy(s1, s2)
2N/A * register char *s1;
2N/A * register const char *s2;
2N/A * {
2N/A * char *os1 = s1;
2N/A *
2N/A * while(*s1++ = *s2++)
2N/A * ;
2N/A * return(os1);
2N/A * }
2N/A *
2N/A */
2N/A
2N/A#include <sys/asm_linkage.h>
2N/A
2N/A ! This is a 32-bit implementation of strcpy. It works by
2N/A ! first checking the alignment of its source pointer. And,
2N/A ! if it is not aligned, attempts to copy bytes until it is.
2N/A ! once this has occurred, the copy takes place, while checking
2N/A ! for zero bytes, based upon destination alignment.
2N/A ! Methods exist to handle per-byte, half-word, and word sized
2N/A ! copies.
2N/A
2N/A ENTRY(strcpy)
2N/A
2N/A .align 32
2N/A
2N/A sub %o1, %o0, %o3 ! src - dst
2N/A andcc %o1, 3, %o4 ! src word aligned ?
2N/A bz .srcaligned ! yup
2N/A mov %o0, %o2 ! save dst
2N/A
2N/A cmp %o4, 2 ! src halfword aligned
2N/A be .s2aligned ! yup
2N/A ldub [%o2 + %o3], %o1 ! src[0]
2N/A tst %o1 ! byte zero?
2N/A stb %o1, [%o2] ! store first byte
2N/A bz .done ! yup, done
2N/A cmp %o4, 3 ! only one byte needed to align?
2N/A bz .srcaligned ! yup
2N/A inc %o2 ! src++, dst++
2N/A
2N/A.s2aligned:
2N/A lduh [%o2 + %o3], %o1 ! src[]
2N/A srl %o1, 8, %o4 ! %o4<7:0> = first byte
2N/A tst %o4 ! first byte zero ?
2N/A bz .done ! yup, done
2N/A stb %o4, [%o2] ! store first byte
2N/A andcc %o1, 0xff, %g0 ! second byte zero ?
2N/A bz .done ! yup, done
2N/A stb %o1, [%o2 + 1] ! store second byte
2N/A add %o2, 2, %o2 ! src += 2, dst += 2
2N/A
2N/A.srcaligned:
2N/A sethi %hi(0x01010101), %o4 ! Alan Mycroft's magic1
2N/A sethi %hi(0x80808080), %o5 ! Alan Mycroft's magic2
2N/A or %o4, %lo(0x01010101), %o4
2N/A andcc %o2, 3, %o1 ! destination word aligned?
2N/A bnz .dstnotaligned ! nope
2N/A or %o5, %lo(0x80808080), %o5
2N/A
2N/A.copyword:
2N/A lduw [%o2 + %o3], %o1 ! src word
2N/A add %o2, 4, %o2 ! src += 4, dst += 4
2N/A andn %o5, %o1, %g1 ! ~word & 0x80808080
2N/A sub %o1, %o4, %o1 ! word - 0x01010101
2N/A andcc %o1, %g1, %g0 ! ((word - 0x01010101) & ~word & 0x80808080)
2N/A add %o1, %o4, %o1 ! restore word
2N/A bz,a .copyword ! no zero byte if magic expression == 0
2N/A st %o1, [%o2 - 4] ! store word to dst (address pre-incremented)
2N/A
2N/A.zerobyte:
2N/A set 0xff000000, %o4 ! mask for 1st byte
2N/A srl %o1, 24, %o3 ! %o3<7:0> = first byte
2N/A andcc %o1, %o4, %g0 ! first byte zero?
2N/A bz .done ! yup, done
2N/A stb %o3, [%o2 - 4] ! store first byte
2N/A set 0x00ff0000, %o5 ! mask for 2nd byte
2N/A srl %o1, 16, %o3 ! %o3<7:0> = second byte
2N/A andcc %o1, %o5, %g0 ! second byte zero?
2N/A bz .done ! yup, done
2N/A stb %o3, [%o2 - 3] ! store second byte
2N/A srl %o4, 16, %o4 ! 0x0000ff00 = mask for 3rd byte
2N/A andcc %o1, %o4, %g0 ! third byte zero?
2N/A srl %o1, 8, %o3 ! %o3<7:0> = third byte
2N/A bz .done ! yup, done
2N/A stb %o3, [%o2 - 2] ! store third byte
2N/A stb %o1, [%o2 - 1] ! store fourth byte
2N/A
2N/A.done:
2N/A retl ! done with leaf function
2N/A .empty
2N/A
2N/A.dstnotaligned:
2N/A cmp %o1, 2 ! dst half word aligned?
2N/A be,a .storehalfword2 ! yup, store half word at a time
2N/A lduw [%o2 + %o3], %o1 ! src word
2N/A
2N/A.storebyte:
2N/A lduw [%o2 + %o3], %o1 ! src word
2N/A add %o2, 4, %o2 ! src += 4, dst += 4
2N/A sub %o1, %o4, %g1 ! x - 0x01010101
2N/A andn %g1, %o1, %g1 ! (x - 0x01010101) & ~x
2N/A andcc %g1, %o5, %g0 ! ((x - 0x01010101) & ~x & 0x80808080)
2N/A bnz .zerobyte ! word has zero byte, handle end cases
2N/A srl %o1, 24, %g1 ! %g1<7:0> = first byte
2N/A stb %g1, [%o2 - 4] ! store first byte; half-word aligned now
2N/A srl %o1, 8, %g1 ! %g1<15:0> = byte 2, 3
2N/A sth %g1, [%o2 - 3] ! store bytes 2, 3
2N/A ba .storebyte ! next word
2N/A stb %o1, [%o2 - 1] ! store fourth byte
2N/A
2N/A.storehalfword:
2N/A lduw [%o2 + %o3], %o1 ! src word
2N/A.storehalfword2:
2N/A add %o2, 4, %o2 ! src += 4, dst += 4
2N/A sub %o1, %o4, %g1 ! x - 0x01010101
2N/A andn %g1, %o1, %g1 ! (x - 0x01010101) & ~x
2N/A andcc %g1, %o5, %g0 ! ((x - 0x01010101) & ~x & 0x80808080)
2N/A bnz .zerobyte ! word has zero byte, handle end cases
2N/A srl %o1, 16, %g1 ! get first and second byte
2N/A sth %g1, [%o2 - 4] ! store first and second byte
2N/A ba .storehalfword ! next word
2N/A sth %o1, [%o2 - 2] ! store third and fourth byte
2N/A
2N/A ! DO NOT remove these NOPs. It will slow down the halfword loop by 15%
2N/A
2N/A nop ! padding
2N/A nop ! padding
2N/A
2N/A SET_SIZE(strcpy)
2N/A