/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* strncpy(s1, s2)
*
* Copy string s2 to s1, truncating or null-padding to always copy n bytes
* return s1.
*
* Fast assembler language version of the following C-program for strncpy
* which represents the `standard' for the C-library.
*
* char *
* strncpy(char *s1, const char *s2, size_t n)
* {
* char *os1 = s1;
*
* n++;
* while ((--n != 0) && ((*s1++ = *s2++) != '\0'))
* ;
* if (n != 0)
* while (--n != 0)
* *s1++ = '\0';
* return (os1);
* }
*/
#include <sys/asm_linkage.h>
!
.align 32
.alignsrc:
add %o2, %g4, %g5 ! dst
or %o4, %lo(0x01010101),%o4! finish loading magic1
and %g5, 3, %g1 ! dst<1:0> to examine offset
sllx %o4, 32, %o1 ! spread magic1
cmp %g1, 1 ! dst offset of 1 or 5
or %o4, %o1, %o4 ! to all 64 bits
sub %o2, 8, %o2 ! adjust for dest pre-incr in cpy loops
be,pn %ncc, .storebyte1241 ! store 1, 2, 4, 1 bytes
sllx %o4, 7, %o5 ! Alan Mycroft's magic2
.zerobyte:
! with, and we have padded at most 7 bytes suring dst aligning
.fillaligned:
add %g4, 7, %o2 ! round up to next dword boundary
and %o2, -8, %o4 ! pointer to next dword boundary
and %o2, 8, %o2 ! dword count odd ? 8 : 0
stx %g0, [%o3] ! store first dword
addcc %o4, %o2, %o4 ! dword count == 1 ?
add %g4, %o2, %g4 ! if dword count odd, n -= 8
bz,pt %ncc, .bytepad ! if dword count == 1, pad leftover bytes
add %o3, %o2, %o3 ! bump dst if dword count odd
.filldword:
addcc %o4, 16, %o4 ! count -= 16
stx %g0, [%o3] ! dst[n] = 0
stx %g0, [%o3 + 8] ! dst[n+8] = 0
add %o3, 16, %o3 ! dst += 16
bcc,pt %ncc, .filldword ! fill dwords until count == 0
addcc %g4, 16, %g4 ! n -= 16
bz,pn %ncc, .done ! if n == 0, we are done
.bytepad:
and %g4, 1, %o2 ! byte count odd ? 1 : 0
stb %g0, [%o3] ! store first byte
addcc %g4, %o2, %g4 ! byte count == 1 ?
bz,pt %ncc, .done ! yup, we are done
add %o3, %o2, %o3 ! bump pointer if odd
.fillbyte:
addcc %g4, 2, %g4 ! n -= 2
stb %g0, [%o3] ! dst[n] = 0
stb %g0, [%o3 + 1] ! dst[n+1] = 0
bnz,pt %ncc, .fillbyte ! fill until n == 0
add %o3, 2, %o3 ! dst += 2
.done:
retl ! done
nop ! pad to align loops below
nop ! pad to align loops below
! this is the last word. It may contain null bytes. store bytes
! until n == 0. if null byte encountered, continue
.lastword:
sub %g4, 8, %g4 ! undo counter pre-increment
add %o2, 8, %o2 ! adjust dst for counter un-bumping
srlx %o1, 56, %g1 ! first byte
stb %g1, [%o2 + %g4] ! store it
inccc %g4 ! n--
andcc %g1, 0xff, %g0 ! end of src reached ?
movz %ncc, %g0, %o1 ! if so, start padding with null bytes
srlx %o1, 40, %g1 ! third byte
stb %g1, [%o2 + %g4] ! store it
inccc %g4 ! n--
andcc %g1, 0xff, %g0 ! end of src reached ?
movz %ncc, %g0, %o1 ! if so, start padding with null bytes
srlx %o1, 24, %g1 ! fifth byte
stb %g1, [%o2 + %g4] ! store it
inccc %g4 ! n--
andcc %g1, 0xff, %g0 ! end of src reached ?
movz %ncc, %g0, %o1 ! if so, start padding with null bytes
srlx %o1, 8, %g1 ! seventh byte
stb %g1, [%o2 + %g4] ! store it
inccc %g4 ! n--