1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22/* 23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27.ident "%Z%%M% %I% %E% SMI" 28 29 .file "%M%" 30 31/* 32 * strncpy(s1, s2) 33 * 34 * Copy string s2 to s1, truncating or null-padding to always copy n bytes 35 * return s1. 36 * 37 * Fast assembler language version of the following C-program for strncpy 38 * which represents the `standard' for the C-library. 39 * 40 * char * 41 * strncpy(char *s1, const char *s2, size_t n) 42 * { 43 * char *os1 = s1; 44 * 45 * n++; 46 * while ((--n != 0) && ((*s1++ = *s2++) != '\0')) 47 * ; 48 * if (n != 0) 49 * while (--n != 0) 50 * *s1++ = '\0'; 51 * return (os1); 52 * } 53 */ 54 55#include <sys/asm_linkage.h> 56#include "synonyms.h" 57 58 ! strncpy works similarly to strcpy, except that n bytes of s2 59 ! are copied to s1. If a null character is reached in s2 yet more 60 ! bytes remain to be copied, strncpy will copy null bytes into 61 ! the destination string. 62 ! 63 ! This implementation works by first aligning the src ptr and 64 ! performing small copies until it is aligned. Then, the string 65 ! is copied based upon destination alignment. (byte, half-word, 66 ! word, etc.) 67 68 ENTRY(strncpy) 69 70 .align 32 71 nop ! pad to align loop on 16-byte boundary 72 subcc %g0, %o2, %g4 ! n = -n, n == 0 ? 73 bz,pn %ncc, .done ! n == 0, done 74 add %o1, %o2, %o3 ! src = src + n 75 andcc %o1, 7, %o4 ! dword aligned ? 76 bz,pn %ncc, .dwordaligned ! yup 77 add %o0, %o2, %o2 ! dst = dst + n 78 sub %o4, 8, %o4 ! bytes until src aligned 79 80.alignsrc: 81 ldub [%o3 + %g4], %o1 ! src[] 82 stb %o1, [%o2 + %g4] ! dst[] = src[] 83 addcc %g4, 1, %g4 ! src++, dst++, n-- 84 bz,pn %ncc, .done ! n == 0, done 85 tst %o1 ! end of src reached (null byte) ? 86 bz,a %ncc, .bytepad ! yes, at least one byte to pad here 87 add %o2, %g4, %o3 ! need single dest pointer for fill 88 addcc %o4, 1, %o4 ! src aligned now? 89 bnz,a %ncc, .alignsrc ! no, copy another byte 90 nop ! pad 91 nop ! pad 92 93.dwordaligned: 94 sethi %hi(0x01010101), %o4 ! Alan Mycroft's magic1 95 add %o2, %g4, %g5 ! dst 96 or %o4, %lo(0x01010101),%o4! finish loading magic1 97 and %g5, 3, %g1 ! dst<1:0> to examine offset 98 sllx %o4, 32, %o1 ! spread magic1 99 cmp %g1, 1 ! dst offset of 1 or 5 100 or %o4, %o1, %o4 ! to all 64 bits 101 sub %o2, 8, %o2 ! adjust for dest pre-incr in cpy loops 102 be,pn %ncc, .storebyte1241 ! store 1, 2, 4, 1 bytes 103 sllx %o4, 7, %o5 ! Alan Mycroft's magic2 104 cmp %g1, 3 ! dst offset of 3 or 7 105 be,pn %ncc, .storebyte1421 ! store 1, 4, 2, 1 bytes 106 cmp %g1, 2 ! dst halfword aligned ? 107 be,pn %ncc, .storehalfword ! yup, store half-word wise 108 andcc %g5, 7, %g0 ! dst word aligned ? 109 bnz,pn %ncc, .storeword2 ! yup, store word wise 110 nop ! ensure loop is 16-byte aligned 111 112.storedword: 113 ldx [%o3 + %g4], %o1 ! src dword 114 addcc %g4, 8, %g4 ! n += 8, src += 8, dst += 8 115 bcs,pn %ncc,.lastword ! if counter wraps, last word 116 andn %o5, %o1, %g1 ! ~dword & 0x8080808080808080 117 sub %o1, %o4, %g5 ! dword - 0x0101010101010101 118 andcc %g5, %g1, %g0 ! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080) 119 bz,a,pt %ncc, .storedword ! no zero byte if magic expression == 0 120 stx %o1, [%o2 + %g4] ! store word to dst (address pre-incremented) 121 122 ! n has not expired, but src is at the end. we need to push out the 123 ! remaining src bytes and then start padding with null bytes 124 125.zerobyte: 126 add %o2, %g4, %o3 ! pointer to dest string 127 srlx %o1, 56, %g1 ! first byte 128 stb %g1, [%o3] ! store it 129 andcc %g1, 0xff, %g0 ! end of string ? 130 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 131 srlx %o1, 48, %g1 ! second byte 132 stb %g1, [%o3 + 1] ! store it 133 andcc %g1, 0xff, %g0 ! end of string ? 134 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 135 srlx %o1, 40, %g1 ! third byte 136 stb %g1, [%o3 + 2] ! store it 137 andcc %g1, 0xff, %g0 ! end of string ? 138 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 139 srlx %o1, 32, %g1 ! fourth byte 140 stb %g1, [%o3 + 3] ! store it 141 andcc %g1, 0xff, %g0 ! end of string ? 142 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 143 srlx %o1, 24, %g1 ! fifth byte 144 stb %g1, [%o3 + 4] ! store it 145 andcc %g1, 0xff, %g0 ! end of string ? 146 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 147 srlx %o1, 16, %g1 ! sixth byte 148 stb %g1, [%o3 + 5] ! store it 149 andcc %g1, 0xff, %g0 ! end of string ? 150 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 151 srlx %o1, 8, %g1 ! seventh byte 152 stb %g1, [%o3 + 6] ! store it 153 andcc %g1, 0xff, %g0 ! end of string ? 154 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 155 stb %o1, [%o3 + 7] ! store eighth byte 156 addcc %g4, 16, %g0 ! number of pad bytes < 16 ? 157 bcs,pn %ncc, .bytepad ! yes, do simple byte wise fill 158 add %o3, 8, %o3 ! dst += 8 159 andcc %o3, 7, %o4 ! dst offset relative to dword boundary 160 bz,pn %ncc, .fillaligned ! dst already dword aligned 161 162 ! here there is a least one more byte to zero out: otherwise we would 163 ! have exited through label .lastword 164 165 sub %o4, 8, %o4 ! bytes to align dst to dword boundary 166.makealigned: 167 stb %g0, [%o3] ! dst[] = 0 168 addcc %g4, 1, %g4 ! n-- 169 bz,pt %ncc, .done ! n == 0, we are done 170 addcc %o4, 1, %o4 ! any more byte needed to align 171 bnz,pt %ncc, .makealigned ! yup, pad another byte 172 add %o3, 1, %o3 ! dst++ 173 nop ! pad to align copy loop below 174 nop ! pad to align copy loop below 175 176 ! here we know that there at least another 8 bytes to pad, since 177 ! we don't get here unless there were >= 16 bytes to pad to begin 178 ! with, and we have padded at most 7 bytes suring dst aligning 179 180.fillaligned: 181 add %g4, 7, %o2 ! round up to next dword boundary 182 and %o2, -8, %o4 ! pointer to next dword boundary 183 and %o2, 8, %o2 ! dword count odd ? 8 : 0 184 stx %g0, [%o3] ! store first dword 185 addcc %o4, %o2, %o4 ! dword count == 1 ? 186 add %g4, %o2, %g4 ! if dword count odd, n -= 8 187 bz,pt %ncc, .bytepad ! if dword count == 1, pad leftover bytes 188 add %o3, %o2, %o3 ! bump dst if dword count odd 189 190.filldword: 191 addcc %o4, 16, %o4 ! count -= 16 192 stx %g0, [%o3] ! dst[n] = 0 193 stx %g0, [%o3 + 8] ! dst[n+8] = 0 194 add %o3, 16, %o3 ! dst += 16 195 bcc,pt %ncc, .filldword ! fill dwords until count == 0 196 addcc %g4, 16, %g4 ! n -= 16 197 bz,pn %ncc, .done ! if n == 0, we are done 198 199.bytepad: 200 and %g4, 1, %o2 ! byte count odd ? 1 : 0 201 stb %g0, [%o3] ! store first byte 202 addcc %g4, %o2, %g4 ! byte count == 1 ? 203 bz,pt %ncc, .done ! yup, we are done 204 add %o3, %o2, %o3 ! bump pointer if odd 205 206.fillbyte: 207 addcc %g4, 2, %g4 ! n -= 2 208 stb %g0, [%o3] ! dst[n] = 0 209 stb %g0, [%o3 + 1] ! dst[n+1] = 0 210 bnz,pt %ncc, .fillbyte ! fill until n == 0 211 add %o3, 2, %o3 ! dst += 2 212 213.done: 214 retl ! done 215 nop ! pad to align loops below 216 nop ! pad to align loops below 217 218 ! this is the last word. It may contain null bytes. store bytes 219 ! until n == 0. if null byte encountered, continue 220 221.lastword: 222 sub %g4, 8, %g4 ! undo counter pre-increment 223 add %o2, 8, %o2 ! adjust dst for counter un-bumping 224 225 srlx %o1, 56, %g1 ! first byte 226 stb %g1, [%o2 + %g4] ! store it 227 inccc %g4 ! n-- 228 bz .done ! if n == 0, we're done 229 andcc %g1, 0xff, %g0 ! end of src reached ? 230 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 231 srlx %o1, 48, %g1 ! second byte 232 stb %g1, [%o2 + %g4] ! store it 233 inccc %g4 ! n-- 234 bz .done ! if n == 0, we're done 235 andcc %g1, 0xff, %g0 ! end of src reached ? 236 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 237 srlx %o1, 40, %g1 ! third byte 238 stb %g1, [%o2 + %g4] ! store it 239 inccc %g4 ! n-- 240 bz .done ! if n == 0, we're done 241 andcc %g1, 0xff, %g0 ! end of src reached ? 242 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 243 srlx %o1, 32, %g1 ! fourth byte 244 stb %g1, [%o2 + %g4] ! store it 245 inccc %g4 ! n-- 246 bz .done ! if n == 0, we're done 247 andcc %g1, 0xff, %g0 ! end of src reached ? 248 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 249 srlx %o1, 24, %g1 ! fifth byte 250 stb %g1, [%o2 + %g4] ! store it 251 inccc %g4 ! n-- 252 bz .done ! if n == 0, we're done 253 andcc %g1, 0xff, %g0 ! end of src reached ? 254 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 255 srlx %o1, 16, %g1 ! sixth byte 256 stb %g1, [%o2 + %g4] ! store it 257 inccc %g4 ! n-- 258 bz .done ! if n == 0, we're done 259 andcc %g1, 0xff, %g0 ! end of src reached ? 260 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 261 srlx %o1, 8, %g1 ! seventh byte 262 stb %g1, [%o2 + %g4] ! store it 263 inccc %g4 ! n-- 264 bz .done ! if n == 0, we're done 265 andcc %g1, 0xff, %g0 ! end of src reached ? 266 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 267 ba .done ! here n must be zero, we are done 268 stb %o1, [%o2 + %g4] ! store eigth byte 269 nop ! pad to align loops below 270 nop ! pad to align loops below 271 272.storebyte1421: 273 ldx [%o3 + %g4], %o1 ! x = src[] 274 addcc %g4, 8, %g4 ! src += 8, dst += 8 275 bcs,pn %ncc,.lastword ! if counter wraps, last word 276 andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 277 sub %o1, %o4, %g5 ! x - 0x0101010101010101 278 andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 279 bnz,pn %ncc, .zerobyte ! end of src found, may need to pad 280 add %o2, %g4, %g5 ! dst (in pointer form) 281 srlx %o1, 56, %g1 ! %g1<7:0> = first byte; word aligned now 282 stb %g1, [%g5] ! store first byte 283 srlx %o1, 24, %g1 ! %g1<31:0> = bytes 2, 3, 4, 5 284 stw %g1, [%g5 + 1] ! store bytes 2, 3, 4, 5 285 srlx %o1, 8, %g1 ! %g1<15:0> = bytes 6, 7 286 sth %g1, [%g5 + 5] ! store bytes 6, 7 287 ba .storebyte1421 ! next dword 288 stb %o1, [%g5 + 7] ! store eigth byte 289 290.storebyte1241: 291 ldx [%o3 + %g4], %o1 ! x = src[] 292 addcc %g4, 8, %g4 ! src += 8, dst += 8 293 bcs,pn %ncc,.lastword ! if counter wraps, last word 294 andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 295 sub %o1, %o4, %g5 ! x - 0x0101010101010101 296 andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 297 bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 298 add %o2, %g4, %g5 ! dst (in pointer form) 299 srlx %o1, 56, %g1 ! %g1<7:0> = first byte; half-word aligned now 300 stb %g1, [%g5] ! store first byte 301 srlx %o1, 40, %g1 ! %g1<15:0> = bytes 2, 3 302 sth %g1, [%g5 + 1] ! store bytes 2, 3 303 srlx %o1, 8, %g1 ! %g1<31:0> = bytes 4, 5, 6, 7 304 stw %g1, [%g5 + 3] ! store bytes 4, 5, 6, 7 305 ba .storebyte1241 ! next dword 306 stb %o1, [%g5 + 7] ! store eigth byte 307 308.storehalfword: 309 ldx [%o3 + %g4], %o1 ! x = src[] 310 addcc %g4, 8, %g4 ! src += 8, dst += 8 311 bcs,pn %ncc,.lastword ! if counter wraps, last word 312 andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 313 sub %o1, %o4, %g5 ! x - 0x0101010101010101 314 andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 315 bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 316 add %o2, %g4, %g5 ! dst (in pointer form) 317 srlx %o1, 48, %g1 ! %g1<15:0> = bytes 1, 2; word aligned now 318 sth %g1, [%g5] ! store bytes 1, 2 319 srlx %o1, 16, %g1 ! %g1<31:0> = bytes 3, 4, 5, 6 320 stw %g1, [%g5 + 2] ! store bytes 3, 4, 5, 6 321 ba .storehalfword ! next dword 322 sth %o1, [%g5 + 6] ! store bytes 7, 8 323 nop ! align next loop to 16-byte boundary 324 nop ! align next loop to 16-byte boundary 325 326.storeword2: 327 ldx [%o3 + %g4], %o1 ! x = src[] 328 addcc %g4, 8, %g4 ! src += 8, dst += 8 329 bcs,pn %ncc,.lastword ! if counter wraps, last word 330 andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 331 sub %o1, %o4, %g5 ! x - 0x0101010101010101 332 andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 333 bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 334 add %o2, %g4, %g5 ! dst (in pointer form) 335 srlx %o1, 32, %g1 ! %g1<31:0> = bytes 1, 2, 3, 4 336 stw %g1, [%g5] ! store bytes 1, 2, 3, 4 337 ba .storeword2 ! next dword 338 stw %o1, [%g5 + 4] ! store bytes 5, 6, 7, 8 339 340 ! do not remove these pads, loop above may slow down otherwise 341 342 nop ! pad 343 nop ! pad 344 345 SET_SIZE(strncpy) 346