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 1988,1993 Intel Corp., all rights reserved
43 procedure memchr (optimized assembler version for the 80960K series)
45 src_addr = memchr (src_addr, char, max_bytes)
47 searching from src_addr for a span of max_bytes bytes, return a
48 pointer to the first byte in the source array that contains the
49 indicated char. Return null if the char is not found.
51 Undefined behavior will occur if the last byte of the source array
52 is in the last two words of the program's allocated memory space.
53 This is so because memchr fetches ahead. Disallowing the fetch
54 ahead would impose a severe performance penalty.
58 Fetch the source array by words and scanbyte the words for the
59 char until either a word with the byte is found or max_bytes is
60 exhausted. In the former case, move through the word to find the
61 matching byte and return its memory address. In the latter case,
66 1) Do NOT try to fetch the words in a word aligned manner because,
67 in my judgement, the performance degradation experienced due to
68 non-aligned accesses does NOT outweigh the time and complexity added
69 by the preamble that would be necessary to assure alignment. This
70 is supported by the intuition that most source arrays (even more
71 true of most big source arrays) will be word aligned to begin with.
73 2) Rather than decrementing max_bytes to zero, I calculate the
74 address of the byte after the last byte of the source array, and
75 quit when the source byte pointer passes that. Refining, actually
76 I calculate the address of the fifth byte after the last byte of
77 the source array, because the source byte pointer is ahead of the
78 actual examination point due to fetch ahead.
83 .leafproc _memchr, __memchr
89 lda Lrett-(.+8)(ip),g14
93 mov g14,g13 # preserve return address
94 lda 0xff,g7 # byte extraction mask
95 and g1,g7,g1 # make char an 8-bit ordinal
96 mov 0,g14 # conform to register linkage standard
97 cmpibge 0,g2,Lnot_found # do nothing if max_bytes <= 0
98 addo 4,g0,g6 # post-increment src word pointer
99 addo g2,g6,g2 # compute ending address from start and len
100 ld (g0),g4 # fetch first word
101 shlo 8,g1,g3 # broadcast the char to four bytes
106 Lsearch_for_word_with_char:
107 mov g4,g5 # keep a copy of word
108 scanbyte g3,g5 # check for byte with char
109 ld (g6),g4 # fetch next word of src
110 bo Lsearch_for_char # branch if null found
111 addo 4,g6,g6 # post-increment src word pointer
112 cmpobge g2,g6,Lsearch_for_word_with_char # branch if max_bytes > 3
115 mov 0,g0 # char not found. Return null
116 bx (g13) # g0 = addr of char in src (or null); g14 = 0
121 cmpobe.f g6,g2,Lnot_found # quit if max_bytes exhausted
122 and g5,g7,g0 # extract byte
123 cmpo g1,g0 # is it char?
124 addo 1,g6,g6 # bump src byte ptr
125 shro 8,g5,g5 # shift word to position next byte
126 bne.t Lsearch_for_char
127 subo 5,g6,g0 # back up the byte pointer