xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /* Copyright (C) 2011-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011. |
| 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 | #include <endian.h> |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | /* Provide a set of macros to help keep endianness #ifdefs out of |
| 23 | the string functions. |
| 24 | |
| 25 | MASK: Provide a mask based on the pointer alignment that |
| 26 | sets up non-zero bytes before the beginning of the string. |
| 27 | The MASK expression works because shift counts are taken mod 64. |
| 28 | |
| 29 | NULMASK: Clear bytes beyond a given point in the string. |
| 30 | |
| 31 | CFZ: Find the first zero bit in the 8 string bytes in a long. |
| 32 | |
| 33 | REVCZ: Find the last zero bit in the 8 string bytes in a long. |
| 34 | |
| 35 | STRSHIFT: Shift N bits towards the start of the string. */ |
| 36 | |
| 37 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 38 | #define MASK(x) (__insn_shl(1ULL, (x << 3)) - 1) |
| 39 | #define NULMASK(x) ((2ULL << x) - 1) |
| 40 | #define CFZ(x) __insn_ctz(x) |
| 41 | #define REVCZ(x) __insn_clz(x) |
| 42 | #define STRSHIFT(x,n) ((x) >> n) |
| 43 | #else |
| 44 | #define MASK(x) (__insn_shl(-2LL, ((-x << 3) - 1))) |
| 45 | #define NULMASK(x) (-2LL << (63 - x)) |
| 46 | #define CFZ(x) __insn_clz(x) |
| 47 | #define REVCZ(x) __insn_ctz(x) |
| 48 | #define STRSHIFT(x,n) ((x) << n) |
| 49 | #endif |
| 50 | |
| 51 | /* Create eight copies of the byte in a uint64_t. Byte Shuffle uses |
| 52 | the bytes of srcB as the index into the dest vector to select a |
| 53 | byte. With all indices of zero, the first byte is copied into all |
| 54 | the other bytes. */ |
| 55 | static inline uint64_t copy_byte(uint8_t byte) |
| 56 | { |
| 57 | return __insn_shufflebytes(byte, 0, 0); |
| 58 | } |