Prashant Patil | fcb877a | 2017-03-16 18:07:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Imagination Technologies. |
| 3 | * |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with |
| 15 | * the distribution. |
| 16 | * * Neither the name of Imagination Technologies nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived |
| 18 | * from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #include <string.h> |
| 34 | |
| 35 | #define op_t unsigned long int |
| 36 | #define op_size sizeof (op_t) |
| 37 | |
| 38 | #if __mips64 |
| 39 | typedef struct |
| 40 | { |
| 41 | op_t B0:8, B1:8, B2:8, B3:8, B4:8, B5:8, B6:8, B7:8; |
| 42 | } bits_t; |
| 43 | #else |
| 44 | typedef struct |
| 45 | { |
| 46 | op_t B0:8, B1:8, B2:8, B3:8; |
| 47 | } bits_t; |
| 48 | #endif |
| 49 | |
| 50 | typedef union |
| 51 | { |
| 52 | op_t v; |
| 53 | bits_t b; |
| 54 | } bitfields_t; |
| 55 | |
| 56 | #define DO_BYTE(i) \ |
| 57 | if (a.b.B##i != ch) { \ |
| 58 | if(a.b.B##i == '\0') return 0; \ |
| 59 | p++; \ |
| 60 | } else \ |
| 61 | return (char *)p; |
| 62 | |
| 63 | #define DO_WORD(w, cnt) { \ |
| 64 | op_t val = w[cnt] ^ mask_c; \ |
| 65 | if ((((w[cnt] - mask_1) & ~w[cnt]) & mask_128) || \ |
| 66 | (((val - mask_1) & ~val) & mask_128)) { \ |
| 67 | return do_bytes(w + cnt, ch); \ |
| 68 | } \ |
| 69 | } |
| 70 | |
| 71 | static inline char * __attribute__ ((always_inline)) |
| 72 | do_bytes (const op_t* w, unsigned char ch) |
| 73 | { |
| 74 | bitfields_t a; |
| 75 | unsigned char* p = (unsigned char *) w; |
| 76 | a.v = *w; |
| 77 | #if __mips64 |
| 78 | DO_BYTE(0) |
| 79 | DO_BYTE(1) |
| 80 | DO_BYTE(2) |
| 81 | DO_BYTE(3) |
| 82 | DO_BYTE(4) |
| 83 | DO_BYTE(5) |
| 84 | DO_BYTE(6) |
| 85 | DO_BYTE(7) |
| 86 | #else |
| 87 | DO_BYTE(0) |
| 88 | DO_BYTE(1) |
| 89 | DO_BYTE(2) |
| 90 | DO_BYTE(3) |
| 91 | #endif |
| 92 | return (char *)p; |
| 93 | } |
| 94 | |
George Burgess IV | 9024235 | 2018-02-06 12:51:31 -0800 | [diff] [blame] | 95 | char* strchr(const char* s, int c) |
Prashant Patil | fcb877a | 2017-03-16 18:07:00 +0530 | [diff] [blame] | 96 | { |
| 97 | const op_t *w; |
| 98 | op_t mask_1, mask_128, mask_c; |
| 99 | const unsigned char ch = c; |
| 100 | unsigned char* p = (unsigned char *) s; |
| 101 | |
| 102 | /* |
| 103 | * Check byte by byte till initial alignment |
| 104 | */ |
| 105 | for ( ; *p != ch && ((size_t) p % op_size) != 0; p++) |
| 106 | if (*p == '\0') |
| 107 | return 0; |
| 108 | |
| 109 | if (*p != ch) { |
| 110 | w = (const op_t *) p; |
| 111 | |
| 112 | mask_c = ch | (ch << 8); |
| 113 | mask_c |= mask_c << 16; |
| 114 | __asm__ volatile ( |
| 115 | "li %0, 0x01010101 \n\t" |
| 116 | : "=r" (mask_1) |
| 117 | ); |
| 118 | #if __mips64 |
| 119 | mask_1 |= mask_1 << 32; |
| 120 | mask_c |= mask_c << 32; |
| 121 | #endif |
| 122 | mask_128 = mask_1 << 7; |
| 123 | |
| 124 | /* |
| 125 | * Check word/dword wize after initial alignment till character match |
| 126 | * or end of string |
| 127 | */ |
| 128 | while (1) { |
| 129 | DO_WORD(w, 0) |
| 130 | DO_WORD(w, 1) |
| 131 | DO_WORD(w, 2) |
| 132 | DO_WORD(w, 3) |
| 133 | w += 4; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return (char *)p; |
| 138 | } |