| 1 | /* Zero byte detection; indexes. Generic C version. |
| 2 | Copyright (C) 2023-2024 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _STRING_FZI_H |
| 20 | #define _STRING_FZI_H 1 |
| 21 | |
| 22 | #include <limits.h> |
| 23 | #include <endian.h> |
| 24 | #include <string-fza.h> |
| 25 | |
| 26 | static __always_inline int |
| 27 | clz (find_t c) |
| 28 | { |
| 29 | if (sizeof (find_t) == sizeof (unsigned long)) |
| 30 | return __builtin_clzl (c); |
| 31 | else |
| 32 | return __builtin_clzll (c); |
| 33 | } |
| 34 | |
| 35 | static __always_inline int |
| 36 | ctz (find_t c) |
| 37 | { |
| 38 | if (sizeof (find_t) == sizeof (unsigned long)) |
| 39 | return __builtin_ctzl (c); |
| 40 | else |
| 41 | return __builtin_ctzll (c); |
| 42 | } |
| 43 | |
| 44 | /* A subroutine for the index_zero functions. Given a test word C, return |
| 45 | the (memory order) index of the first byte (in memory order) that is |
| 46 | non-zero. */ |
| 47 | static __always_inline unsigned int |
| 48 | index_first (find_t c) |
| 49 | { |
| 50 | int r; |
| 51 | if (__BYTE_ORDER == __LITTLE_ENDIAN) |
| 52 | r = ctz (c); |
| 53 | else |
| 54 | r = clz (c); |
| 55 | return r / CHAR_BIT; |
| 56 | } |
| 57 | |
| 58 | /* Similarly, but return the (memory order) index of the last byte that is |
| 59 | non-zero. */ |
| 60 | static __always_inline unsigned int |
| 61 | index_last (find_t c) |
| 62 | { |
| 63 | int r; |
| 64 | if (__BYTE_ORDER == __LITTLE_ENDIAN) |
| 65 | r = clz (c); |
| 66 | else |
| 67 | r = ctz (c); |
| 68 | return sizeof (find_t) - 1 - (r / CHAR_BIT); |
| 69 | } |
| 70 | |
| 71 | #endif /* STRING_FZI_H */ |
| 72 | |