Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Tilera Corporation. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation, version 2. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 11 | * NON INFRINGEMENT. See the GNU General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/module.h> |
| 18 | |
| 19 | void *memchr(const void *s, int c, size_t n) |
| 20 | { |
Chris Metcalf | 3edabee | 2010-11-24 13:57:42 -0500 | [diff] [blame] | 21 | const uint32_t *last_word_ptr; |
| 22 | const uint32_t *p; |
| 23 | const char *last_byte_ptr; |
| 24 | uintptr_t s_int; |
| 25 | uint32_t goal, before_mask, v, bits; |
| 26 | char *ret; |
| 27 | |
| 28 | if (__builtin_expect(n == 0, 0)) { |
| 29 | /* Don't dereference any memory if the array is empty. */ |
| 30 | return NULL; |
| 31 | } |
| 32 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 33 | /* Get an aligned pointer. */ |
Chris Metcalf | 3edabee | 2010-11-24 13:57:42 -0500 | [diff] [blame] | 34 | s_int = (uintptr_t) s; |
| 35 | p = (const uint32_t *)(s_int & -4); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 36 | |
| 37 | /* Create four copies of the byte for which we are looking. */ |
Chris Metcalf | 3edabee | 2010-11-24 13:57:42 -0500 | [diff] [blame] | 38 | goal = 0x01010101 * (uint8_t) c; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 39 | |
| 40 | /* Read the first word, but munge it so that bytes before the array |
| 41 | * will not match goal. |
| 42 | * |
| 43 | * Note that this shift count expression works because we know |
| 44 | * shift counts are taken mod 32. |
| 45 | */ |
Chris Metcalf | 3edabee | 2010-11-24 13:57:42 -0500 | [diff] [blame] | 46 | before_mask = (1 << (s_int << 3)) - 1; |
| 47 | v = (*p | before_mask) ^ (goal & before_mask); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 48 | |
| 49 | /* Compute the address of the last byte. */ |
Chris Metcalf | 3edabee | 2010-11-24 13:57:42 -0500 | [diff] [blame] | 50 | last_byte_ptr = (const char *)s + n - 1; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 51 | |
| 52 | /* Compute the address of the word containing the last byte. */ |
Chris Metcalf | 3edabee | 2010-11-24 13:57:42 -0500 | [diff] [blame] | 53 | last_word_ptr = (const uint32_t *)((uintptr_t) last_byte_ptr & -4); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 54 | |
| 55 | while ((bits = __insn_seqb(v, goal)) == 0) { |
| 56 | if (__builtin_expect(p == last_word_ptr, 0)) { |
| 57 | /* We already read the last word in the array, |
| 58 | * so give up. |
| 59 | */ |
| 60 | return NULL; |
| 61 | } |
| 62 | v = *++p; |
| 63 | } |
| 64 | |
| 65 | /* We found a match, but it might be in a byte past the end |
| 66 | * of the array. |
| 67 | */ |
| 68 | ret = ((char *)p) + (__insn_ctz(bits) >> 3); |
| 69 | return (ret <= last_byte_ptr) ? ret : NULL; |
| 70 | } |
| 71 | EXPORT_SYMBOL(memchr); |