1/******************************************************************************* 2 * 3 * Copyright (c) 1993 Intel Corporation 4 * 5 * Intel hereby grants you permission to copy, modify, and distribute this 6 * software and its documentation. Intel grants this permission provided 7 * that the above copyright notice appears in all copies and that both the 8 * copyright notice and this permission notice appear in supporting 9 * documentation. In addition, Intel grants this permission provided that 10 * you prominently mark as "not part of the original" any modifications 11 * made to this software or documentation, and that the name of Intel 12 * Corporation not be used in advertising or publicity pertaining to 13 * distribution of the software or the documentation without specific, 14 * written prior permission. 15 * 16 * Intel Corporation provides this AS IS, WITHOUT ANY WARRANTY, EXPRESS OR 17 * IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY 18 * OR FITNESS FOR A PARTICULAR PURPOSE. Intel makes no guarantee or 19 * representations regarding the use of, or the results of the use of, 20 * the software and documentation in terms of correctness, accuracy, 21 * reliability, currentness, or otherwise; and you rely on the software, 22 * documentation and results solely at your own risk. 23 * 24 * IN NO EVENT SHALL INTEL BE LIABLE FOR ANY LOSS OF USE, LOSS OF BUSINESS, 25 * LOSS OF PROFITS, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES 26 * OF ANY KIND. IN NO EVENT SHALL INTEL'S TOTAL LIABILITY EXCEED THE SUM 27 * PAID TO INTEL FOR THE PRODUCT LICENSED HEREUNDER. 28 * 29 ******************************************************************************/ 30 31#include <picolibc.h> 32 33 .file "strncpy.s" 34#ifdef __PIC 35 .pic 36#endif 37#ifdef __PID 38 .pid 39#endif 40/* 41 * (c) copyright 1988,1993 Intel Corp., all rights reserved 42 */ 43/* 44 procedure strncpy (optimized assembler version for the 80960K Series) 45 46 dest_addr = strncpy (dest_addr, src_addr, max_bytes) 47 48 copy the null terminated string pointed to by src_addr to the 49 string pointed to by dest_addr. Return the original dest_addr. 50 If the source string is shorter than max_bytes, then null-pad 51 the destination string. If it is longer than max_bytes, the 52 copy stops at max_bytes bytes (and no terminating null appears 53 in the destination string). 54 55 This routine will fail if the source and destination string 56 overlap (in particular, if the end of the source is overlapped 57 by the beginning of the destination). The behavior is undefined. 58 This is acceptable according to the draft C standard. 59 60 Undefined behavior will also occur if the end of the source string 61 (i.e. the terminating null byte) is in the last two words of the 62 program's allocated memory space. This is so because strncpy fetches 63 ahead. Disallowing the fetch ahead would impose a severe performance 64 penalty. 65 66 Strategy: 67 68 Fetch and store the strings by words and go to a character move loop 69 as soon as a null byte is encountered. If max_bytes is exhausted 70 first, then terminate after moving only max_bytes (with the last 71 0, 1, 2, or 3 bytes moved as single bytes, not as a word). 72 Otherwise, the character move loop moves the last bytes or the 73 source string, and then null-pads the destination string until 74 max_bytes is exhausted. 75 76 Tactics: 77 78 1) Do NOT try to fetch the words in a word aligned manner because, 79 in my judgement, the performance degradation experienced due to 80 non-aligned accesses does NOT outweigh the time and complexity added 81 by the preamble and convoluted body that would be necessary to assure 82 alignment. 83 84 2) When the null byte is encountered in a source word, null out the 85 higher-numbered bytes in that word, store the word in the destination, 86 and go to the word null-padder, which may eventually go to the byte 87 null-padder. 88*/ 89 90 .globl _strncpy 91 .globl __strncpy 92 .leafproc _strncpy,__strncpy 93 .align 2 94_strncpy: 95#ifndef __PIC 96 lda Lrett,g14 97#else 98 lda Lrett-(.+8)(ip),g14 99#endif 100__strncpy: 101 mov g14, g13 102 cmpibge 0,g2,Lexit # quit early if max_bytes <= 0 103 ld (g1), g7 # fetch the first word of the source 104 mov g0, g5 105 lda 0xff, g3 # byte extraction mask 106 addo g1, g2, g6 107 addo g2, g5, g2 108Lwloop: # word copying loop 109 addo 4, g1, g1 # post-increment source ptr 110 cmpo g6, g1 # max_bytes < 4 ? 111 mov g7, g4 # keep a copy of source word 112 bl Lcloop.a # if less than four bytes to go, go to char loop 113 scanbyte 0, g4 # null byte found? 114 ld (g1), g7 # pre-fetch next word of the source 115 be Lcloop.c # go to char loop if null encountered 116 st g4, (g5) # store current word 117 addo 4, g5, g5 # post-increment destination ptr 118 b Lwloop 119 120Lcloop.a: # character copying loop (max_bytes < 3) 121 and g3, g4, g14 # extract byte 122Lcloop.b: 123 cmpo g2, g5 # max_bytes <= 0 ? 124 shro 8, g4, g4 # position word to extract next byte 125 be Lexit # exit if max_bytes exhausted 126 cmpo 0, g14 # is it null? 127 stob g14, (g5) # store it 128 addo 1, g5, g5 # post-increment dest ptr 129 bne Lcloop.a # branch if we are NOT null padding 130 b Lcloop.b # branch if we are null padding 131 132Lexit: 133 mov 0, g14 134 bx (g13) # g0 = dest string address; g14 = 0 135Lrett: 136 ret 137 138Lcloop.c: # character copying loop 139 and g3, g4, g14 # extract byte 140 cmpo 0, g14 # is it null? 141 mov g3, g7 # save mask 142 shlo 8, g3, g3 # shift mask to next byte position 143 bne Lcloop.c # loop until null found 144 subo 1, g7, g3 # mask to null pad after null byte 145 and g3, g4, g4 # null-out stuff after null byte 146 st g4, (g5) # store last part of src and first of null-pad 147 subo 8,g2,g6 # adjust max_byte counter 148 149Lzwloop: 150 cmpo g5, g6 # max_bytes < 4 ? 151 addo 4, g5, g5 152 bg Lcloop.b # if so, goto character loop 153 st g14, (g5) # store four null bytes 154 b Lzwloop 155 156/* end of strncpy */ 157