string-fzi.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* string-fzi.h -- zero byte detection; indices. Alpha version.
  2. Copyright (C) 2023-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _STRING_FZI_H
  16. #define _STRING_FZI_H
  17. #include <stdint.h>
  18. #include <string-optype.h>
  19. #include <string-fza.h>
  20. /* Note that since CMPBGE creates a bit mask rather than a byte mask,
  21. we cannot simply provide a target-specific string-fza.h. */
  22. /* A subroutine for the index_zero functions. Given a bitmask C,
  23. return the index of the first bit set in memory order. */
  24. static __always_inline unsigned int
  25. index_first (find_t c)
  26. {
  27. #ifdef __alpha_cix__
  28. return __builtin_ctzl (c);
  29. #else
  30. c = c & -c;
  31. return (c & 0xf0 ? 4 : 0) + (c & 0xcc ? 2 : 0) + (c & 0xaa ? 1 : 0);
  32. #endif
  33. }
  34. /* Similarly, but return the (memory order) index of the last bit
  35. that is non-zero. Note that only the least 8 bits may be nonzero. */
  36. static __always_inline unsigned int
  37. index_last (find_t x)
  38. {
  39. #ifdef __alpha_cix__
  40. return __builtin_clzl (x) ^ 63;
  41. #else
  42. unsigned r = 0;
  43. if (x & 0xf0)
  44. r += 4;
  45. if (x & (0xc << r))
  46. r += 2;
  47. if (x & (0x2 << r))
  48. r += 1;
  49. return r;
  50. #endif
  51. }
  52. #endif /* _STRING_FZI_H */