1 /*******************************************************************************
3 * Copyright (c) 1993 Intel Corporation
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.
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.
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.
29 ******************************************************************************/
39 * (c) copyright 1989,1993 Intel Corp., all rights reserved
43 procedure memccpy (optimized assembler version for the 80960CA)
45 dest_addr = memccpy (dest_addr, src_addr, char, len)
47 copy len bytes pointed to by src_addr to the space pointed to by
48 dest_addr, stopping if char is copied. If char is copied,
49 return address of byte after char in dest string; else null.
51 Undefined behavior will occur if the end of the source array is in
52 the last two words of the program's allocated memory space. This
53 is so because the routine fetches ahead. Disallowing the fetch
54 ahead would impose a severe performance penalty.
56 Undefined behavior will also occur if the source and destination
60 This program handles five cases:
62 1) both arguments start on a word boundary
63 2) neither are word aligned, but they are offset by the same amount
64 3) source is word aligned, destination is not
65 4) destination is word aligned, source is not
66 5) neither is word aligned, and they are offset by differing amounts
68 At the time of this writing, only g0 thru g7 and g13 are available
69 for use in this leafproc; other registers would have to be saved and
70 restored. These nine registers, plus tricky use of g14 are sufficient
71 to implement the routine.
74 #if __i960_BIG_ENDIAN__
83 .leafproc _memccpy, __memccpy
89 lda Lrett-(.+8)(ip),g14
92 notand g1,3,g5 # extract word addr of start of src
93 lda (g14),g13 # preserve return address
94 cmpibge.f 0,g3,Lexit_char_not_found # Lexit if # of bytes to move is <= 0
95 cmpo g5,g1 # check alignment of src
96 ld (g5),LSW # fetch word containing at least first byte
97 notand g0,3,g4 # extract word addr of start of dest
98 lda 4(g5),g5 # advance src word addr
99 shlo 24,g2,g2 # reduce char to single byte
100 bne.f Lcase_245 # branch if src is NOT word aligned
103 cmpobe.t g0,g4,Lcase_1_setup # branch if dest word aligned
105 Lcase_3: # src is word aligned; dest is not
106 mov LSW,MSW # make copy of first word of src
107 addo 4,g4,g1 # move dest word ptr to first word boundary
108 lda 32,g14 # initialize shift count to zero
111 Lcase_3_cloop_at_start: # character copying loop for start of dest str
112 cmpdeci 0,g3,g3 # is max_bytes exhausted?
113 #if __i960_BIG_ENDIAN__
114 lda -8(g14),g14 # augment the shift counter
116 lda 8(g14),g14 # augment the shift counter
118 be.f Lexit_char_not_found # Lexit if max_bytes is exhausted
119 #if __i960_BIG_ENDIAN__
120 rotate 8,MSW,MSW # move next byte into position for extraction
123 stob MSW,(g0) # store the byte in dest
125 lda 1(g0),g0 # post-increment dest ptr
126 #if ! __i960_BIG_ENDIAN__
127 shro 8,MSW,MSW # move next byte into position for extraction
129 be.f Lexit_char_found # Lexit if char found
130 cmpobne.t g1,g0,Lcase_3_cloop_at_start # branch if reached word boundary
132 ld (g5),MSW # fetch msw of operand for double shift
140 #if __i960_BIG_ENDIAN__
141 cmpobne 0,g14,Lcase_3_wloop
144 cmpi g3,4 # less than four bytes to move?
145 lda 4(g5),g5 # post-increment src word addr
146 mov LSW,g1 # extract 4 bytes of src
147 bl.f Lcase_13_cloop_setup # branch if < four bytes left to move
148 scanbyte g4,g1 # branch if word has char in it
149 bo.f Lcase_13_cloop_setup
150 mov MSW,LSW # move msw to lsw
151 ld (g5),MSW # pre-fetch msw of operand for double shift
152 subi 4,g3,g3 # decrease max_byte count by the 4 bytes moved
153 st g1,(g0) # store 4 bytes to dest
154 addo 4,g0,g0 # post-increment dest ptr
159 cmpi g3,4 # less than four bytes to move?
160 lda 4(g5),g5 # post-increment src word addr
161 eshro g14,g6,g1 # extract 4 bytes of src
162 bl.f Lcase_13_cloop_setup # branch if < four bytes left to move
163 scanbyte g4,g1 # branch if word has char in it
164 bo.f Lcase_13_cloop_setup
165 mov MSW,LSW # move msw to lsw
166 ld (g5),MSW # pre-fetch msw of operand for double shift
167 subi 4,g3,g3 # decrease max_byte count by the 4 bytes moved
168 st g1,(g0) # store 4 bytes to dest
169 addo 4,g0,g0 # post-increment dest ptr
173 subo 4,g0,g0 # store is pre-incrementing; back up dest addr
179 Lcase_1_wloop: # word copying loop
180 subi 4,g3,g3 # decrease max_byte count by the 4 bytes moved
181 ld (g5),LSW # pre-fetch next word of src
182 addo 4,g5,g5 # post-increment src addr
183 st g1,(g0) # store word in dest string
184 Lcase_1: # src and dest are word aligned
185 cmpi g3,4 # check for fewer than four bytes to move
186 addo 4,g0,g0 # pre-increment dest addr
187 lda (LSW),g1 # keep a copy of the src word
188 bl.f Lcase_13_cloop_setup # branch if less than four bytes to copy
189 scanbyte LSW,g4 # branch if char is not in foursome
191 Lcase_13_cloop_setup:
192 cmpibe.f 0,g3,Lexit_char_not_found # Lexit if max_bytes is exhausted
195 #if __i960_BIG_ENDIAN__
196 rotate 8,g1,g1 # move next byte into position for extraction
199 stob g1,(g0) # store the byte in dest
201 lda 1(g0),g0 # post-increment dest byte addr
203 be.f Lexit_char_found # Lexit if char reached
205 #if ! __i960_BIG_ENDIAN__
206 shro 8,g1,g1 # move next byte into position for extraction
208 bne.t Lcase_1_cloop # continue if len not exhausted
210 Lexit_char_not_found:
214 bx (g13) # g0 = dest array address; g14 = 0
220 cmpo g0,g4 # check alignment of dest
221 ld (g5),MSW # pre-fetch second half
222 and 3,g1,g1 # compute shift count
224 #if __i960_BIG_ENDIAN__
225 subo g14,0,g14 # adjust shift count for big endian
227 be.t Lcase_4 # branch if dest is word aligned
228 or g4,g1,g1 # is src earlier in word, later, or sync w/ dst
229 cmpo g0,g1 # < indicates first word of dest has more bytes
230 /* than first word of source. */
231 eshro g14,g6,g4 # extract four bytes
232 lda 4(g0),g1 # move dest word addr to first word boundary
233 #if __i960_BIG_ENDIAN__
239 lda 4(g5),g5 # move src word addr to second word boundary