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 implementation of strcpy works by first checking the
2N/A ! source alignment and copying byte, half byte, or word
2N/A ! quantities until the source ptr is aligned at an extended
2N/A ! word boundary. Once this has occurred, the string is copied,
2N/A ! checking for zero bytes, depending upon its dst ptr alignment.
2N/A ! (methods for xword, word, half-word, and byte copies are present)
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, 7, %o4 ! dword aligned ?
2N/A bz,pn %ncc, .srcaligned ! yup
2N/A mov %o0, %o2 ! save dst
2N/A
2N/A.chkbyte:
2N/A andcc %o1, 1, %g0 ! need to copy byte ?
2N/A bz,pn %ncc, .chkhalfword ! nope, maybe halfword
2N/A sub %g0, %o1, %g1 ! %g1<2:0> = # of unaligned bytes
2N/A ldub [%o2 + %o3], %o5 ! src[0]
2N/A tst %o5 ! src[0] == 0 ?
2N/A stb %o5, [%o2] ! dst[0] = src[0]
2N/A bz,pn %ncc, .done ! yup, done
2N/A inc %o2 ! src++, dst++
2N/A
2N/A.chkhalfword:
2N/A andcc %g1, 2, %g0 ! need to copy half-word ?
2N/A bz,pn %ncc, .chkword ! nope, maybe word
2N/A nop !
2N/A lduh [%o2 + %o3], %o5 ! load src halfword
2N/A srl %o5, 8, %o4 ! extract first byte
2N/A tst %o4 ! first byte == 0 ?
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o4, [%o2] ! store first byte
2N/A andcc %o5, 0xff, %g0 ! extract second byte
2N/A stb %o5, [%o2 + 1] ! store second byte
2N/A bz,pn %ncc, .done ! yup, 2nd byte zero, done
2N/A add %o2, 2, %o2 ! src += 2
2N/A
2N/A.chkword:
2N/A andcc %g1, 4, %g0 ! need to copy word ?
2N/A bz,pn %ncc, .srcaligned ! nope
2N/A nop !
2N/A lduw [%o2 + %o3], %o5 ! load src word
2N/A srl %o5, 24, %o4 ! extract first byte
2N/A tst %o4 ! is first byte zero ?
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o4, [%o2] ! store first byte
2N/A srl %o5, 16, %o4 ! extract second byte
2N/A andcc %o4, 0xff, %g0 ! is second byte zero ?
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o4, [%o2 + 1] ! store second byte
2N/A srl %o5, 8, %o4 ! extract third byte
2N/A andcc %o4, 0xff, %g0 ! third byte zero ?
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o4, [%o2 + 2] ! store third byte
2N/A andcc %o5, 0xff, %g0 ! fourth byte zero ?
2N/A stb %o5, [%o2 + 3] ! store fourth byte
2N/A bz,pn %ncc, .done ! yup, fourth byte zero, done
2N/A add %o2, 4, %o2 ! src += 2
2N/A
2N/A.srcaligned:
2N/A sethi %hi(0x01010101), %o4 ! Alan Mycroft's magic1
2N/A or %o4, %lo(0x01010101),%o4! finish loading magic1
2N/A sllx %o4, 32, %o1 ! spread magic1
2N/A and %o2, 3, %g4 ! dst<1:0> to examine offset
2N/A or %o4, %o1, %o4 ! to all 64 bits
2N/A cmp %g4, 1 ! dst offset of 1 or 5
2N/A sllx %o4, 7, %o5 ! Alan Mycroft's magic2
2N/A be,pn %ncc, .storebyte1241 ! store 1, 2, 4, 1 bytes
2N/A cmp %g4, 3 ! dst offset of 3 or 7
2N/A be,pn %ncc, .storebyte1421 ! store 1, 4, 2, 1 bytes
2N/A cmp %g4, 2 ! dst halfword aligned ?
2N/A be,pn %ncc, .storehalfword ! yup, store half-word wise
2N/A andcc %o2, 7, %g0 ! dst word aligned ?
2N/A bnz,pn %ncc, .storeword2 ! yup, store word wise
2N/A .empty
2N/A
2N/A.storedword:
2N/A ldx [%o2 + %o3], %o1 ! src dword
2N/A add %o2, 8, %o2 ! src += 8, dst += 8
2N/A andn %o5, %o1, %g1 ! ~dword & 0x8080808080808080
2N/A sub %o1, %o4, %g4 ! dword - 0x0101010101010101
2N/A andcc %g4, %g1, %g0 ! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080)
2N/A bz,a,pt %ncc, .storedword ! no zero byte if magic expression == 0
2N/A stx %o1, [%o2 - 8] ! store word to dst (address pre-incremented)
2N/A
2N/A.zerobyte:
2N/A orn %o4, %g0, %o4 ! 0xffffffffffffffff
2N/A sllx %o4, 56, %o4 ! 0xff00000000000000
2N/A srlx %o1, 56, %o3 ! %o3<7:0> = first byte
2N/A andcc %o1, %o4, %g0 ! first byte zero?
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o3, [%o2 - 8] ! store first byte
2N/A srlx %o4, 8, %o4 ! 0x00ff000000000000
2N/A srlx %o1, 48, %o3 ! %o3<7:0> = second byte
2N/A andcc %o1, %o4, %g0 ! second byte zero?
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o3, [%o2 - 7] ! store second byte
2N/A srlx %o4, 8, %o4 ! 0x0000ff0000000000
2N/A srlx %o1, 40, %o3 ! %o3<7:0> = third byte
2N/A andcc %o1, %o4, %g0 ! third byte zero?
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o3, [%o2 - 6] ! store third byte
2N/A srlx %o4, 8, %o4 ! 0x000000ff00000000
2N/A srlx %o1, 32, %o3 ! %o3<7:0> = fourth byte
2N/A andcc %o1, %o4, %g0 ! fourth byte zero?
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o3, [%o2 - 5] ! store fourth byte
2N/A srlx %o4, 8, %o4 ! 0x00000000ff000000
2N/A srlx %o1, 24, %o3 ! %o3<7:0> = fifth byte
2N/A andcc %o1, %o4, %g0 ! fifth byte zero?
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o3, [%o2 - 4] ! store fifth byte
2N/A srlx %o4, 8, %o4 ! 0x0000000000ff0000
2N/A srlx %o1, 16, %o3 ! %o3<7:0> = sixth byte
2N/A andcc %o1, %o4, %g0 ! sixth byte zero?
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o3, [%o2 - 3] ! store sixth byte
2N/A srlx %o4, 8, %o4 ! 0x000000000000ff00
2N/A andcc %o1, %o4, %g0 ! seventh byte zero?
2N/A srlx %o1, 8, %o3 ! %o3<7:0> = seventh byte
2N/A bz,pn %ncc, .done ! yup, done
2N/A stb %o3, [%o2 - 2] ! store seventh byte
2N/A stb %o1, [%o2 - 1] ! store eigth byte
2N/A.done:
2N/A retl ! done with leaf function
2N/A
2N/A nop ! ensure following loop 16-byte aligned
2N/A
2N/A.storebyte1421:
2N/A ldx [%o2 + %o3], %o1 ! x = src[]
2N/A add %o2, 8, %o2 ! src += 8, dst += 8
2N/A andn %o5, %o1, %g1 ! ~x & 0x8080808080808080
2N/A sub %o1, %o4, %g4 ! x - 0x0101010101010101
2N/A andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2N/A bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases
2N/A srlx %o1, 56, %g1 ! %g1<7:0> = first byte; word aligned now
2N/A stb %g1, [%o2 - 8] ! store first byte
2N/A srlx %o1, 24, %g1 ! %g1<31:0> = bytes 2, 3, 4, 5
2N/A stw %g1, [%o2 - 7] ! store bytes 2, 3, 4, 5
2N/A srlx %o1, 8, %g1 ! %g1<15:0> = bytes 6, 7
2N/A sth %g1, [%o2 - 3] ! store bytes 6, 7
2N/A ba .storebyte1421 ! next dword
2N/A stb %o1, [%o2 - 1] ! store eigth byte
2N/A
2N/A nop ! ensure following loop 16-byte aligned
2N/A nop ! ensure following loop 16-byte aligned
2N/A
2N/A.storebyte1241:
2N/A ldx [%o2 + %o3], %o1 ! x = src[]
2N/A add %o2, 8, %o2 ! src += 8, dst += 8
2N/A andn %o5, %o1, %g1 ! ~x & 0x8080808080808080
2N/A sub %o1, %o4, %g4 ! x - 0x0101010101010101
2N/A andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2N/A bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases
2N/A srlx %o1, 56, %g1 ! %g1<7:0> = first byte; word aligned now
2N/A stb %g1, [%o2 - 8] ! store first byte
2N/A srlx %o1, 40, %g1 ! %g1<15:0> = bytes 2, 3
2N/A sth %g1, [%o2 - 7] ! store bytes 2, 3
2N/A srlx %o1, 8, %g1 ! %g1<31:0> = bytes 4, 5, 6, 7
2N/A stw %g1, [%o2 - 5] ! store bytes 4, 5, 6, 7
2N/A ba .storebyte1241 ! next dword
2N/A stb %o1, [%o2 - 1] ! store eigth byte
2N/A
2N/A nop ! ensure following loop 16-byte aligned
2N/A nop ! ensure following loop 16-byte aligned
2N/A
2N/A.storehalfword:
2N/A ldx [%o2 + %o3], %o1 ! x = src[]
2N/A add %o2, 8, %o2 ! src += 8, dst += 8
2N/A andn %o5, %o1, %g1 ! ~x & 0x8080808080808080
2N/A sub %o1, %o4, %g4 ! x - 0x0101010101010101
2N/A andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2N/A bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases
2N/A srlx %o1, 48, %g1 ! get first and second byte
2N/A sth %g1, [%o2 - 8] ! store first and second byte; word aligned now
2N/A srlx %o1, 16, %g1 ! %g1<31:0> = bytes 3, 4, 5, 6
2N/A stw %g1, [%o2 - 6] ! store bytes 3, 4, 5, 6
2N/A ba .storehalfword ! next word
2N/A sth %o1, [%o2 - 2] ! store seventh and eigth byte
2N/A
2N/A.storeword:
2N/A ldx [%o2 + %o3], %o1 ! x = src[]
2N/A.storeword2:
2N/A add %o2, 8, %o2 ! src += 8, dst += 8
2N/A andn %o5, %o1, %g1 ! ~x & 0x0x8080808080808080
2N/A sub %o1, %o4, %g4 ! x - 0x0101010101010101
2N/A andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2N/A bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases
2N/A srlx %o1, 32, %g1 ! get bytes 1,2,3,4
2N/A stw %g1, [%o2 - 8] ! store bytes 1,2,3,4 (address is pre-incremented)
2N/A ba .storeword ! no zero byte if magic expression == 0
2N/A stw %o1, [%o2 - 4] ! store bytes 5,6,7,8
2N/A
2N/A nop ! padding, do not remove!!!
2N/A nop ! padding, do not remove!!!
2N/A SET_SIZE(strcpy)
2N/A