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 "strncpy.s"
2N/A
2N/A/*
2N/A * strncpy(s1, s2)
2N/A *
2N/A * Copy string s2 to s1, truncating or null-padding to always copy n bytes
2N/A * return s1.
2N/A *
2N/A * Fast assembler language version of the following C-program for strncpy
2N/A * which represents the `standard' for the C-library.
2N/A *
2N/A * char *
2N/A * strncpy(char *s1, const char *s2, size_t n)
2N/A * {
2N/A * char *os1 = s1;
2N/A *
2N/A * n++;
2N/A * while ((--n != 0) && ((*s1++ = *s2++) != '\0'))
2N/A * ;
2N/A * if (n != 0)
2N/A * while (--n != 0)
2N/A * *s1++ = '\0';
2N/A * return (os1);
2N/A * }
2N/A */
2N/A
2N/A#include <sys/asm_linkage.h>
2N/A
2N/A ! strncpy works similarly to strcpy, except that n bytes of s2
2N/A ! are copied to s1. If a null character is reached in s2 yet more
2N/A ! bytes remain to be copied, strncpy will copy null bytes into
2N/A ! the destination string.
2N/A !
2N/A ! This implementation works by first aligning the src ptr and
2N/A ! performing small copies until it is aligned. Then, the string
2N/A ! is copied based upon destination alignment. (byte, half-word,
2N/A ! word, etc.)
2N/A
2N/A ENTRY(strncpy)
2N/A
2N/A .align 32
2N/A nop ! pad to align loop on 16-byte boundary
2N/A subcc %g0, %o2, %g4 ! n = -n, n == 0 ?
2N/A bz,pn %ncc, .done ! n == 0, done
2N/A add %o1, %o2, %o3 ! src = src + n
2N/A andcc %o1, 7, %o4 ! dword aligned ?
2N/A bz,pn %ncc, .dwordaligned ! yup
2N/A add %o0, %o2, %o2 ! dst = dst + n
2N/A sub %o4, 8, %o4 ! bytes until src aligned
2N/A
2N/A.alignsrc:
2N/A ldub [%o3 + %g4], %o1 ! src[]
2N/A stb %o1, [%o2 + %g4] ! dst[] = src[]
2N/A addcc %g4, 1, %g4 ! src++, dst++, n--
2N/A bz,pn %ncc, .done ! n == 0, done
2N/A tst %o1 ! end of src reached (null byte) ?
2N/A bz,a %ncc, .bytepad ! yes, at least one byte to pad here
2N/A add %o2, %g4, %o3 ! need single dest pointer for fill
2N/A addcc %o4, 1, %o4 ! src aligned now?
2N/A bnz,a %ncc, .alignsrc ! no, copy another byte
2N/A nop ! pad
2N/A nop ! pad
2N/A
2N/A.dwordaligned:
2N/A sethi %hi(0x01010101), %o4 ! Alan Mycroft's magic1
2N/A add %o2, %g4, %g5 ! dst
2N/A or %o4, %lo(0x01010101),%o4! finish loading magic1
2N/A and %g5, 3, %g1 ! dst<1:0> to examine offset
2N/A sllx %o4, 32, %o1 ! spread magic1
2N/A cmp %g1, 1 ! dst offset of 1 or 5
2N/A or %o4, %o1, %o4 ! to all 64 bits
2N/A sub %o2, 8, %o2 ! adjust for dest pre-incr in cpy loops
2N/A be,pn %ncc, .storebyte1241 ! store 1, 2, 4, 1 bytes
2N/A sllx %o4, 7, %o5 ! Alan Mycroft's magic2
2N/A cmp %g1, 3 ! dst offset of 3 or 7
2N/A be,pn %ncc, .storebyte1421 ! store 1, 4, 2, 1 bytes
2N/A cmp %g1, 2 ! dst halfword aligned ?
2N/A be,pn %ncc, .storehalfword ! yup, store half-word wise
2N/A andcc %g5, 7, %g0 ! dst word aligned ?
2N/A bnz,pn %ncc, .storeword2 ! yup, store word wise
2N/A nop ! ensure loop is 16-byte aligned
2N/A
2N/A.storedword:
2N/A ldx [%o3 + %g4], %o1 ! src dword
2N/A addcc %g4, 8, %g4 ! n += 8, src += 8, dst += 8
2N/A bcs,pn %ncc,.lastword ! if counter wraps, last word
2N/A andn %o5, %o1, %g1 ! ~dword & 0x8080808080808080
2N/A sub %o1, %o4, %g5 ! dword - 0x0101010101010101
2N/A andcc %g5, %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 + %g4] ! store word to dst (address pre-incremented)
2N/A
2N/A ! n has not expired, but src is at the end. we need to push out the
2N/A ! remaining src bytes and then start padding with null bytes
2N/A
2N/A.zerobyte:
2N/A add %o2, %g4, %o3 ! pointer to dest string
2N/A srlx %o1, 56, %g1 ! first byte
2N/A stb %g1, [%o3] ! store it
2N/A andcc %g1, 0xff, %g0 ! end of string ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 48, %g1 ! second byte
2N/A stb %g1, [%o3 + 1] ! store it
2N/A andcc %g1, 0xff, %g0 ! end of string ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 40, %g1 ! third byte
2N/A stb %g1, [%o3 + 2] ! store it
2N/A andcc %g1, 0xff, %g0 ! end of string ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 32, %g1 ! fourth byte
2N/A stb %g1, [%o3 + 3] ! store it
2N/A andcc %g1, 0xff, %g0 ! end of string ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 24, %g1 ! fifth byte
2N/A stb %g1, [%o3 + 4] ! store it
2N/A andcc %g1, 0xff, %g0 ! end of string ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 16, %g1 ! sixth byte
2N/A stb %g1, [%o3 + 5] ! store it
2N/A andcc %g1, 0xff, %g0 ! end of string ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 8, %g1 ! seventh byte
2N/A stb %g1, [%o3 + 6] ! store it
2N/A andcc %g1, 0xff, %g0 ! end of string ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A stb %o1, [%o3 + 7] ! store eighth byte
2N/A addcc %g4, 16, %g0 ! number of pad bytes < 16 ?
2N/A bcs,pn %ncc, .bytepad ! yes, do simple byte wise fill
2N/A add %o3, 8, %o3 ! dst += 8
2N/A andcc %o3, 7, %o4 ! dst offset relative to dword boundary
2N/A bz,pn %ncc, .fillaligned ! dst already dword aligned
2N/A
2N/A ! here there is a least one more byte to zero out: otherwise we would
2N/A ! have exited through label .lastword
2N/A
2N/A sub %o4, 8, %o4 ! bytes to align dst to dword boundary
2N/A.makealigned:
2N/A stb %g0, [%o3] ! dst[] = 0
2N/A addcc %g4, 1, %g4 ! n--
2N/A bz,pt %ncc, .done ! n == 0, we are done
2N/A addcc %o4, 1, %o4 ! any more byte needed to align
2N/A bnz,pt %ncc, .makealigned ! yup, pad another byte
2N/A add %o3, 1, %o3 ! dst++
2N/A nop ! pad to align copy loop below
2N/A nop ! pad to align copy loop below
2N/A
2N/A ! here we know that there at least another 8 bytes to pad, since
2N/A ! we don't get here unless there were >= 16 bytes to pad to begin
2N/A ! with, and we have padded at most 7 bytes suring dst aligning
2N/A
2N/A.fillaligned:
2N/A add %g4, 7, %o2 ! round up to next dword boundary
2N/A and %o2, -8, %o4 ! pointer to next dword boundary
2N/A and %o2, 8, %o2 ! dword count odd ? 8 : 0
2N/A stx %g0, [%o3] ! store first dword
2N/A addcc %o4, %o2, %o4 ! dword count == 1 ?
2N/A add %g4, %o2, %g4 ! if dword count odd, n -= 8
2N/A bz,pt %ncc, .bytepad ! if dword count == 1, pad leftover bytes
2N/A add %o3, %o2, %o3 ! bump dst if dword count odd
2N/A
2N/A.filldword:
2N/A addcc %o4, 16, %o4 ! count -= 16
2N/A stx %g0, [%o3] ! dst[n] = 0
2N/A stx %g0, [%o3 + 8] ! dst[n+8] = 0
2N/A add %o3, 16, %o3 ! dst += 16
2N/A bcc,pt %ncc, .filldword ! fill dwords until count == 0
2N/A addcc %g4, 16, %g4 ! n -= 16
2N/A bz,pn %ncc, .done ! if n == 0, we are done
2N/A
2N/A.bytepad:
2N/A and %g4, 1, %o2 ! byte count odd ? 1 : 0
2N/A stb %g0, [%o3] ! store first byte
2N/A addcc %g4, %o2, %g4 ! byte count == 1 ?
2N/A bz,pt %ncc, .done ! yup, we are done
2N/A add %o3, %o2, %o3 ! bump pointer if odd
2N/A
2N/A.fillbyte:
2N/A addcc %g4, 2, %g4 ! n -= 2
2N/A stb %g0, [%o3] ! dst[n] = 0
2N/A stb %g0, [%o3 + 1] ! dst[n+1] = 0
2N/A bnz,pt %ncc, .fillbyte ! fill until n == 0
2N/A add %o3, 2, %o3 ! dst += 2
2N/A
2N/A.done:
2N/A retl ! done
2N/A nop ! pad to align loops below
2N/A nop ! pad to align loops below
2N/A
2N/A ! this is the last word. It may contain null bytes. store bytes
2N/A ! until n == 0. if null byte encountered, continue
2N/A
2N/A.lastword:
2N/A sub %g4, 8, %g4 ! undo counter pre-increment
2N/A add %o2, 8, %o2 ! adjust dst for counter un-bumping
2N/A
2N/A srlx %o1, 56, %g1 ! first byte
2N/A stb %g1, [%o2 + %g4] ! store it
2N/A inccc %g4 ! n--
2N/A bz .done ! if n == 0, we're done
2N/A andcc %g1, 0xff, %g0 ! end of src reached ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 48, %g1 ! second byte
2N/A stb %g1, [%o2 + %g4] ! store it
2N/A inccc %g4 ! n--
2N/A bz .done ! if n == 0, we're done
2N/A andcc %g1, 0xff, %g0 ! end of src reached ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 40, %g1 ! third byte
2N/A stb %g1, [%o2 + %g4] ! store it
2N/A inccc %g4 ! n--
2N/A bz .done ! if n == 0, we're done
2N/A andcc %g1, 0xff, %g0 ! end of src reached ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 32, %g1 ! fourth byte
2N/A stb %g1, [%o2 + %g4] ! store it
2N/A inccc %g4 ! n--
2N/A bz .done ! if n == 0, we're done
2N/A andcc %g1, 0xff, %g0 ! end of src reached ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 24, %g1 ! fifth byte
2N/A stb %g1, [%o2 + %g4] ! store it
2N/A inccc %g4 ! n--
2N/A bz .done ! if n == 0, we're done
2N/A andcc %g1, 0xff, %g0 ! end of src reached ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 16, %g1 ! sixth byte
2N/A stb %g1, [%o2 + %g4] ! store it
2N/A inccc %g4 ! n--
2N/A bz .done ! if n == 0, we're done
2N/A andcc %g1, 0xff, %g0 ! end of src reached ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A srlx %o1, 8, %g1 ! seventh byte
2N/A stb %g1, [%o2 + %g4] ! store it
2N/A inccc %g4 ! n--
2N/A bz .done ! if n == 0, we're done
2N/A andcc %g1, 0xff, %g0 ! end of src reached ?
2N/A movz %ncc, %g0, %o1 ! if so, start padding with null bytes
2N/A ba .done ! here n must be zero, we are done
2N/A stb %o1, [%o2 + %g4] ! store eigth byte
2N/A nop ! pad to align loops below
2N/A nop ! pad to align loops below
2N/A
2N/A.storebyte1421:
2N/A ldx [%o3 + %g4], %o1 ! x = src[]
2N/A addcc %g4, 8, %g4 ! src += 8, dst += 8
2N/A bcs,pn %ncc,.lastword ! if counter wraps, last word
2N/A andn %o5, %o1, %g1 ! ~x & 0x8080808080808080
2N/A sub %o1, %o4, %g5 ! x - 0x0101010101010101
2N/A andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2N/A bnz,pn %ncc, .zerobyte ! end of src found, may need to pad
2N/A add %o2, %g4, %g5 ! dst (in pointer form)
2N/A srlx %o1, 56, %g1 ! %g1<7:0> = first byte; word aligned now
2N/A stb %g1, [%g5] ! store first byte
2N/A srlx %o1, 24, %g1 ! %g1<31:0> = bytes 2, 3, 4, 5
2N/A stw %g1, [%g5 + 1] ! store bytes 2, 3, 4, 5
2N/A srlx %o1, 8, %g1 ! %g1<15:0> = bytes 6, 7
2N/A sth %g1, [%g5 + 5] ! store bytes 6, 7
2N/A ba .storebyte1421 ! next dword
2N/A stb %o1, [%g5 + 7] ! store eigth byte
2N/A
2N/A.storebyte1241:
2N/A ldx [%o3 + %g4], %o1 ! x = src[]
2N/A addcc %g4, 8, %g4 ! src += 8, dst += 8
2N/A bcs,pn %ncc,.lastword ! if counter wraps, last word
2N/A andn %o5, %o1, %g1 ! ~x & 0x8080808080808080
2N/A sub %o1, %o4, %g5 ! x - 0x0101010101010101
2N/A andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2N/A bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases
2N/A add %o2, %g4, %g5 ! dst (in pointer form)
2N/A srlx %o1, 56, %g1 ! %g1<7:0> = first byte; half-word aligned now
2N/A stb %g1, [%g5] ! store first byte
2N/A srlx %o1, 40, %g1 ! %g1<15:0> = bytes 2, 3
2N/A sth %g1, [%g5 + 1] ! store bytes 2, 3
2N/A srlx %o1, 8, %g1 ! %g1<31:0> = bytes 4, 5, 6, 7
2N/A stw %g1, [%g5 + 3] ! store bytes 4, 5, 6, 7
2N/A ba .storebyte1241 ! next dword
2N/A stb %o1, [%g5 + 7] ! store eigth byte
2N/A
2N/A.storehalfword:
2N/A ldx [%o3 + %g4], %o1 ! x = src[]
2N/A addcc %g4, 8, %g4 ! src += 8, dst += 8
2N/A bcs,pn %ncc,.lastword ! if counter wraps, last word
2N/A andn %o5, %o1, %g1 ! ~x & 0x8080808080808080
2N/A sub %o1, %o4, %g5 ! x - 0x0101010101010101
2N/A andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2N/A bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases
2N/A add %o2, %g4, %g5 ! dst (in pointer form)
2N/A srlx %o1, 48, %g1 ! %g1<15:0> = bytes 1, 2; word aligned now
2N/A sth %g1, [%g5] ! store bytes 1, 2
2N/A srlx %o1, 16, %g1 ! %g1<31:0> = bytes 3, 4, 5, 6
2N/A stw %g1, [%g5 + 2] ! store bytes 3, 4, 5, 6
2N/A ba .storehalfword ! next dword
2N/A sth %o1, [%g5 + 6] ! store bytes 7, 8
2N/A nop ! align next loop to 16-byte boundary
2N/A nop ! align next loop to 16-byte boundary
2N/A
2N/A.storeword2:
2N/A ldx [%o3 + %g4], %o1 ! x = src[]
2N/A addcc %g4, 8, %g4 ! src += 8, dst += 8
2N/A bcs,pn %ncc,.lastword ! if counter wraps, last word
2N/A andn %o5, %o1, %g1 ! ~x & 0x8080808080808080
2N/A sub %o1, %o4, %g5 ! x - 0x0101010101010101
2N/A andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2N/A bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases
2N/A add %o2, %g4, %g5 ! dst (in pointer form)
2N/A srlx %o1, 32, %g1 ! %g1<31:0> = bytes 1, 2, 3, 4
2N/A stw %g1, [%g5] ! store bytes 1, 2, 3, 4
2N/A ba .storeword2 ! next dword
2N/A stw %o1, [%g5 + 4] ! store bytes 5, 6, 7, 8
2N/A
2N/A ! do not remove these pads, loop above may slow down otherwise
2N/A
2N/A nop ! pad
2N/A nop ! pad
2N/A
2N/A SET_SIZE(strncpy)