strchr.S 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* strchr - find a character in a string
  2. Copyright (C) 2014-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. <https://www.gnu.org/licenses/>. */
  15. #include <sysdep.h>
  16. /* Assumptions:
  17. *
  18. * ARMv8-a, AArch64, Advanced SIMD.
  19. * MTE compatible.
  20. */
  21. #define srcin x0
  22. #define chrin w1
  23. #define result x0
  24. #define src x2
  25. #define tmp1 x1
  26. #define tmp2 x3
  27. #define vrepchr v0
  28. #define vdata v1
  29. #define qdata q1
  30. #define vhas_nul v2
  31. #define vhas_chr v3
  32. #define vrepmask v4
  33. #define vend v5
  34. #define dend d5
  35. /* Core algorithm.
  36. For each 16-byte chunk we calculate a 64-bit syndrome value with four bits
  37. per byte. Bits 0-1 are set if the relevant byte matched the requested
  38. character, bits 2-3 are set if the byte is NUL or matched. Count trailing
  39. zeroes gives the position of the matching byte if it is a multiple of 4.
  40. If it is not a multiple of 4, there was no match. */
  41. ENTRY (strchr)
  42. bic src, srcin, 15
  43. dup vrepchr.16b, chrin
  44. ld1 {vdata.16b}, [src]
  45. movi vrepmask.16b, 0x33
  46. cmeq vhas_nul.16b, vdata.16b, 0
  47. cmeq vhas_chr.16b, vdata.16b, vrepchr.16b
  48. bit vhas_nul.16b, vhas_chr.16b, vrepmask.16b
  49. lsl tmp2, srcin, 2
  50. shrn vend.8b, vhas_nul.8h, 4 /* 128->64 */
  51. fmov tmp1, dend
  52. lsr tmp1, tmp1, tmp2
  53. cbz tmp1, L(loop)
  54. rbit tmp1, tmp1
  55. clz tmp1, tmp1
  56. /* Tmp1 is an even multiple of 2 if the target character was
  57. found first. Otherwise we've found the end of string. */
  58. tst tmp1, 2
  59. add result, srcin, tmp1, lsr 2
  60. csel result, result, xzr, eq
  61. ret
  62. .p2align 4
  63. L(loop):
  64. ldr qdata, [src, 16]
  65. cmeq vhas_chr.16b, vdata.16b, vrepchr.16b
  66. cmhs vhas_nul.16b, vhas_chr.16b, vdata.16b
  67. umaxp vend.16b, vhas_nul.16b, vhas_nul.16b
  68. fmov tmp1, dend
  69. cbnz tmp1, L(end)
  70. ldr qdata, [src, 32]!
  71. cmeq vhas_chr.16b, vdata.16b, vrepchr.16b
  72. cmhs vhas_nul.16b, vhas_chr.16b, vdata.16b
  73. umaxp vend.16b, vhas_nul.16b, vhas_nul.16b
  74. fmov tmp1, dend
  75. cbz tmp1, L(loop)
  76. sub src, src, 16
  77. L(end):
  78. #ifdef __AARCH64EB__
  79. bif vhas_nul.16b, vhas_chr.16b, vrepmask.16b
  80. shrn vend.8b, vhas_nul.8h, 4 /* 128->64 */
  81. fmov tmp1, dend
  82. #else
  83. bit vhas_nul.16b, vhas_chr.16b, vrepmask.16b
  84. shrn vend.8b, vhas_nul.8h, 4 /* 128->64 */
  85. fmov tmp1, dend
  86. rbit tmp1, tmp1
  87. #endif
  88. add src, src, 16
  89. clz tmp1, tmp1
  90. /* Tmp1 is a multiple of 4 if the target character was found. */
  91. tst tmp1, 2
  92. add result, src, tmp1, lsr 2
  93. csel result, result, xzr, eq
  94. ret
  95. END (strchr)
  96. libc_hidden_builtin_def (strchr)
  97. weak_alias (strchr, index)