17c478bd9Sstevel@tonic-gate/* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*9a70fc3bSMark J. Nelson * Common Development and Distribution License (the "License"). 6*9a70fc3bSMark J. Nelson * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate/* 227c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 26*9a70fc3bSMark J. Nelson .file "strncat.s" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate/ 297c478bd9Sstevel@tonic-gate/ strncat(s1, s2, n) 307c478bd9Sstevel@tonic-gate/ 317c478bd9Sstevel@tonic-gate/ Concatenates s2 on the end of s1. s1's space must be large enough. 327c478bd9Sstevel@tonic-gate/ At most n characters are moved. 337c478bd9Sstevel@tonic-gate/ Returns s1. 347c478bd9Sstevel@tonic-gate/ 357c478bd9Sstevel@tonic-gate/ Fast assembly language version of the following C-program strncat 367c478bd9Sstevel@tonic-gate/ which represents the `standard' for the C-library. 377c478bd9Sstevel@tonic-gate/ 387c478bd9Sstevel@tonic-gate/ char * 397c478bd9Sstevel@tonic-gate/ strncat(char *s1, const char *s2, size_t n) 407c478bd9Sstevel@tonic-gate/ { 417c478bd9Sstevel@tonic-gate/ char *os1 = s1; 427c478bd9Sstevel@tonic-gate/ 437c478bd9Sstevel@tonic-gate/ n++; 447c478bd9Sstevel@tonic-gate/ while (*s1++) 457c478bd9Sstevel@tonic-gate/ ; 467c478bd9Sstevel@tonic-gate/ --s1; 477c478bd9Sstevel@tonic-gate/ while (*s1++ = *s2++) 487c478bd9Sstevel@tonic-gate/ if (--n == 0) { 497c478bd9Sstevel@tonic-gate/ s1[-1] = '\0'; 507c478bd9Sstevel@tonic-gate/ break; 517c478bd9Sstevel@tonic-gate/ } 527c478bd9Sstevel@tonic-gate/ return (os1); 537c478bd9Sstevel@tonic-gate/ } 547c478bd9Sstevel@tonic-gate/ 557c478bd9Sstevel@tonic-gate/ In this assembly language version, the following expression is used 567c478bd9Sstevel@tonic-gate/ to check if a 32-bit word data contains a null byte or not: 577c478bd9Sstevel@tonic-gate/ (((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080 587c478bd9Sstevel@tonic-gate/ If the above expression geneates a value other than 0x80808080, 597c478bd9Sstevel@tonic-gate/ that means the 32-bit word data contains a null byte. 607c478bd9Sstevel@tonic-gate/ 617c478bd9Sstevel@tonic-gate/ The above has been extended for 64-bit support. 627c478bd9Sstevel@tonic-gate/ 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate#include "SYS.h" 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate ENTRY(strncat) /* (char *, char *, size_t) */ 677c478bd9Sstevel@tonic-gate movq %rdi, %rax / save return value 687c478bd9Sstevel@tonic-gate movabsq $0x7f7f7f7f7f7f7f7f, %r8 / %r8 = 0x7f... 697c478bd9Sstevel@tonic-gate movq %r8, %r9 707c478bd9Sstevel@tonic-gate notq %r9 / %r9 = 0x80... 717c478bd9Sstevel@tonic-gate testq $7, %rdi / if %rdi not quadword aligned 727c478bd9Sstevel@tonic-gate jnz .L1 / goto .L1 737c478bd9Sstevel@tonic-gate .align 4 747c478bd9Sstevel@tonic-gate.L2: 757c478bd9Sstevel@tonic-gate movq (%rdi), %r11 / move 1 quadword from (%rdi) to %r11 767c478bd9Sstevel@tonic-gate movq %r8, %rcx 777c478bd9Sstevel@tonic-gate andq %r11, %rcx / %rcx = %r11 & 0x7f7f7f7f 787c478bd9Sstevel@tonic-gate addq $8, %rdi / next quadword 797c478bd9Sstevel@tonic-gate addq %r8, %rcx / %rcx += 0x7f7f7f7f 807c478bd9Sstevel@tonic-gate orq %r11, %rcx / %rcx |= %r11 817c478bd9Sstevel@tonic-gate andq %r9, %rcx / %rcx &= 0x80808080 827c478bd9Sstevel@tonic-gate cmpq %r9, %rcx / if no null byte in this quadword 837c478bd9Sstevel@tonic-gate je .L2 / goto .L2 847c478bd9Sstevel@tonic-gate subq $8, %rdi / post-incremented 857c478bd9Sstevel@tonic-gate.L1: 867c478bd9Sstevel@tonic-gate cmpb $0, (%rdi) / if a byte in (%rdi) is null 877c478bd9Sstevel@tonic-gate je .L3 / goto .L3 887c478bd9Sstevel@tonic-gate incq %rdi / next byte 897c478bd9Sstevel@tonic-gate testq $7, %rdi / if %rdi not quadword aligned 907c478bd9Sstevel@tonic-gate jnz .L1 / goto .L1 917c478bd9Sstevel@tonic-gate jmp .L2 / goto .L2 (%rdi quadword aligned) 927c478bd9Sstevel@tonic-gate .align 4 937c478bd9Sstevel@tonic-gate.L3: 947c478bd9Sstevel@tonic-gate / %rdi points to a null byte in destination string 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate testq $7, %rsi / if %rsi not quadword aligned 977c478bd9Sstevel@tonic-gate jnz .L4 / goto .L4 987c478bd9Sstevel@tonic-gate cmpq $8, %rdx / if number of bytes < 8 997c478bd9Sstevel@tonic-gate jb .L7 / goto .L7 1007c478bd9Sstevel@tonic-gate .align 4 1017c478bd9Sstevel@tonic-gate.L5: 1027c478bd9Sstevel@tonic-gate movq (%rsi), %r11 / move 1 quadword from (%rsi) to %r11 1037c478bd9Sstevel@tonic-gate movq %r8, %rcx 1047c478bd9Sstevel@tonic-gate andq %r11, %rcx / %rcx = %r11 & 0x7f7f7f7f 1057c478bd9Sstevel@tonic-gate addq $8, %rsi / next quadword 1067c478bd9Sstevel@tonic-gate addq %r8, %rcx / %rcx += 0x7f7f7f7f 1077c478bd9Sstevel@tonic-gate orq %r11, %rcx / %rcx |= %r11 1087c478bd9Sstevel@tonic-gate andq %r9, %rcx / %rcx &= 0x80808080 1097c478bd9Sstevel@tonic-gate cmpq %r9, %rcx / if null byte in this quadword 1107c478bd9Sstevel@tonic-gate jne .L6 / goto .L6 1117c478bd9Sstevel@tonic-gate movq %r11, (%rdi) / copy this quadword to (%rdi) 1127c478bd9Sstevel@tonic-gate subq $8, %rdx / decrement number of bytes by 8 1137c478bd9Sstevel@tonic-gate addq $8, %rdi / next quadword 1147c478bd9Sstevel@tonic-gate cmpq $8, %rdx / if number of bytes >= 8 1157c478bd9Sstevel@tonic-gate jae .L5 / goto .L5 1167c478bd9Sstevel@tonic-gate jmp .L7 / goto .L7 1177c478bd9Sstevel@tonic-gate.L6: 1187c478bd9Sstevel@tonic-gate subq $8, %rsi / post-incremented 1197c478bd9Sstevel@tonic-gate .align 4 1207c478bd9Sstevel@tonic-gate.L7: 1217c478bd9Sstevel@tonic-gate / number of bytes < 8 or a null byte found in the quadword 1227c478bd9Sstevel@tonic-gate cmpq $0, %rdx / if number of bytes == 0 1237c478bd9Sstevel@tonic-gate jz .L8 / goto .L8 (finished) 1247c478bd9Sstevel@tonic-gate movb (%rsi), %r11b / %r11b = a byte in (%rsi) 1257c478bd9Sstevel@tonic-gate decq %rdx / decrement number of bytes by 1 1267c478bd9Sstevel@tonic-gate movb %r11b, (%rdi) / copy %r11b to (%rdi) 1277c478bd9Sstevel@tonic-gate incq %rsi / next byte 1287c478bd9Sstevel@tonic-gate incq %rdi / next byte 1297c478bd9Sstevel@tonic-gate cmpb $0, %r11b / compare %r11b with a null byte 1307c478bd9Sstevel@tonic-gate je .L9 / if %r11b is a null, goto .L9 1317c478bd9Sstevel@tonic-gate jmp .L7 / goto .L7 1327c478bd9Sstevel@tonic-gate .align 4 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate.L4: 1357c478bd9Sstevel@tonic-gate / %rsi not aligned 1367c478bd9Sstevel@tonic-gate cmpq $0, %rdx / if number of bytes == 0 1377c478bd9Sstevel@tonic-gate jz .L8 / goto .L8 (finished) 1387c478bd9Sstevel@tonic-gate movb (%rsi), %r11b / %r11b = a byte in (%rsi) 1397c478bd9Sstevel@tonic-gate decq %rdx / decrement number of bytes by 1 1407c478bd9Sstevel@tonic-gate movb %r11b, (%rdi) / copy %r11b to (%rdi) 1417c478bd9Sstevel@tonic-gate incq %rdi / next byte 1427c478bd9Sstevel@tonic-gate incq %rsi / next byte 1437c478bd9Sstevel@tonic-gate cmpb $0, %r11b / compare %r11b with a null byte 1447c478bd9Sstevel@tonic-gate je .L9 / if %r11b is a null, goto .L9 1457c478bd9Sstevel@tonic-gate jmp .L4 / goto .L4 1467c478bd9Sstevel@tonic-gate .align 4 1477c478bd9Sstevel@tonic-gate.L8: 1487c478bd9Sstevel@tonic-gate movb $0, (%rdi) / null termination 1497c478bd9Sstevel@tonic-gate.L9: 1507c478bd9Sstevel@tonic-gate ret 1517c478bd9Sstevel@tonic-gate SET_SIZE(strncat) 152