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 "strrchr.s" 34#ifdef __i960_BIG_ENDIAN__ 35#error "This does not work in big-endian" 36#endif 37 38#ifdef __PIC 39 .pic 40#endif 41#ifdef __PID 42 .pid 43#endif 44 45/* 46 * (c) copyright 1988,1993 Intel Corp., all rights reserved 47 */ 48 49/* 50 procedure strrchr (optimized assembler version for the 80960K series) 51 52 src_addr = strrchr (src_addr, char) 53 54 return a pointer to the last byte that contains the indicated 55 byte in the source string. Return null if the byte is not found. 56 57 Undefined behavior will occur if the end of the source string (i.e. 58 the terminating null byte) is in the last two words of the program's 59 allocated memory space. This is so because strrchr fetches ahead. 60 Disallowing the fetch ahead would impose a severe performance penalty. 61 62 Strategy: 63 64 Fetch the source string by words and scanbyte the words for the 65 char until either a word with the byte is found or the null byte is 66 encountered. In the former case, move through the word to find the 67 matching byte and save its memory address, then continue the search. 68 In the latter case, return the saved address, or zero (null) if none 69 was ever found to save. 70 71 Tactics: 72 73 1) Do NOT try to fetch the words in a word aligned manner because, 74 in my judgement, the performance degradation experienced due to 75 non-aligned accesses does NOT outweigh the time and complexity added 76 by the preamble that would be necessary to assure alignment. This 77 is supported by the intuition that most source arrays (even more 78 true of most big source arrays) will be word aligned to begin with. 79*/ 80 81 .globl _strrchr 82 .globl __strrchr 83 .leafproc _strrchr, __strrchr 84 .align 2 85_strrchr: 86#ifdef __PIC 87 lda Lrett-(.+8)(ip),g14 88#else 89 lda Lrett,g14 90#endif 91__strrchr: 92 93 ld (g0),g4 # fetch first word 94 lda 0xff,g7 # byte extraction mask 95 and g1,g7,g1 # make char an 8-bit ordinal 96 shlo 8,g1,g2 # broadcast the char to four bytes 97 or g1,g2,g2 98 shlo 16,g2,g5 99 or g2,g5,g3 100 mov g14,g13 # preserve return address 101 addo 4,g0,g2 # post-increment src pointer 102 mov 1,g0 # prepare to return null pointer 103 mov g3,g6 # prepare to return null pointer 104 105Lsearch_for_word_with_char_or_null: 106 mov g4,g5 # copy word 107 scanbyte 0,g5 # check for null byte 108 ld (g2),g4 # fetch next word of src 109 bo Lword_has_null # branch if null found 110 scanbyte g3,g5 # check for byte with char 111 addo 4,g2,g2 # post-increment src pointer 112 bno Lsearch_for_word_with_char_or_null # branch if no copy of char 113 mov g5,g6 # save word that has char in it (at least once) 114 subo 4,g2,g0 # save addr of byte after word with char 115 b Lsearch_for_word_with_char_or_null 116 117Lword_has_null: 118 subo 4,g2,g2 # move src pointer back to word with null 119Lfind_null: 120 addo 1,g2,g2 # advance src pointer to byte after current 121 and g7,g5,g14 # extract next byte 122 cmpo g1,g14 # is current byte char? 123 shro 8,g5,g5 # position next byte for extraction 124 bne 1f # skip if not char sought after 125 mov g2,g0 # save addr of byte after char 126 mov g3,g6 # save word of all char to short circuit search 1271: cmpobne 0,g14,Lfind_null # is current byte null? 128 129Lfind_last_char: 130 rotate 8,g6,g6 # position next highest byte 131 and g7,g6,g5 # extract byte 132 subo 1,g0,g0 # move pointer to that byte (or nullify) 133 cmpobne g5,g1,Lfind_last_char # branch if not at char 134 135 bx (g13) # g0 = addr of char in src (or null); g14 = 0 136Lrett: 137 ret 138 139/* end of strrchr */ 140