string-fzc.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* string-fzc.h -- zero byte detection with indexes. HPPA 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_FZC_H
  16. #define _STRING_FZC_H 1
  17. #include <string-optype.h>
  18. _Static_assert (sizeof (op_t) == 4, "64-bit not supported");
  19. /* Given a word X that is known to contain a zero byte, return the
  20. index of the first such within the long in memory order. */
  21. static __always_inline unsigned int
  22. index_first_zero (op_t x)
  23. {
  24. unsigned int ret;
  25. /* Since we have no clz insn, direct tests of the bytes is faster
  26. than loading up the constants to do the masking. */
  27. asm ("extrw,u,<> %1,23,8,%%r0\n\t"
  28. "ldi 2,%0\n\t"
  29. "extrw,u,<> %1,15,8,%%r0\n\t"
  30. "ldi 1,%0\n\t"
  31. "extrw,u,<> %1,7,8,%%r0\n\t"
  32. "ldi 0,%0"
  33. : "=r"(ret) : "r"(x), "0"(3));
  34. return ret;
  35. }
  36. /* Similarly, but perform the search for byte equality between X1 and X2. */
  37. static __always_inline unsigned int
  38. index_first_eq (op_t x1, op_t x2)
  39. {
  40. return index_first_zero (x1 ^ x2);
  41. }
  42. /* Similarly, but perform the search for zero within X1 or
  43. equality between X1 and X2. */
  44. static __always_inline unsigned int
  45. index_first_zero_eq (op_t x1, op_t x2)
  46. {
  47. unsigned int ret;
  48. /* Since we have no clz insn, direct tests of the bytes is faster
  49. than loading up the constants to do the masking. */
  50. asm ("extrw,u,= %1,23,8,%%r0\n\t"
  51. "extrw,u,<> %2,23,8,%%r0\n\t"
  52. "ldi 2,%0\n\t"
  53. "extrw,u,= %1,15,8,%%r0\n\t"
  54. "extrw,u,<> %2,15,8,%%r0\n\t"
  55. "ldi 1,%0\n\t"
  56. "extrw,u,= %1,7,8,%%r0\n\t"
  57. "extrw,u,<> %2,7,8,%%r0\n\t"
  58. "ldi 0,%0"
  59. : "=r"(ret) : "r"(x1), "r"(x1 ^ x2), "0"(3));
  60. return ret;
  61. }
  62. /* Similarly, but perform the search for zero within X1 or
  63. inequality between X1 and X2. */
  64. static __always_inline unsigned int
  65. index_first_zero_ne (op_t x1, op_t x2)
  66. {
  67. unsigned int ret;
  68. /* Since we have no clz insn, direct tests of the bytes is faster
  69. than loading up the constants to do the masking. */
  70. asm ("extrw,u,<> %2,23,8,%%r0\n\t"
  71. "extrw,u,<> %1,23,8,%%r0\n\t"
  72. "ldi 2,%0\n\t"
  73. "extrw,u,<> %2,15,8,%%r0\n\t"
  74. "extrw,u,<> %1,15,8,%%r0\n\t"
  75. "ldi 1,%0\n\t"
  76. "extrw,u,<> %2,7,8,%%r0\n\t"
  77. "extrw,u,<> %1,7,8,%%r0\n\t"
  78. "ldi 0,%0"
  79. : "=r"(ret) : "r"(x1), "r"(x1 ^ x2), "0"(3));
  80. return ret;
  81. }
  82. /* Similarly, but search for the last zero within X. */
  83. static __always_inline unsigned int
  84. index_last_zero (op_t x)
  85. {
  86. unsigned int ret;
  87. /* Since we have no ctz insn, direct tests of the bytes is faster
  88. than loading up the constants to do the masking. */
  89. asm ("extrw,u,<> %1,15,8,%%r0\n\t"
  90. "ldi 1,%0\n\t"
  91. "extrw,u,<> %1,23,8,%%r0\n\t"
  92. "ldi 2,%0\n\t"
  93. "extrw,u,<> %1,31,8,%%r0\n\t"
  94. "ldi 3,%0"
  95. : "=r"(ret) : "r"(x), "0"(0));
  96. return ret;
  97. }
  98. static __always_inline unsigned int
  99. index_last_eq (op_t x1, op_t x2)
  100. {
  101. return index_last_zero (x1 ^ x2);
  102. }
  103. #endif /* _STRING_FZC_H */