string-fza.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Zero byte detection; basics. RISCV 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 _RISCV_STRING_FZA_H
  16. #define _RISCV_STRING_FZA_H 1
  17. #if defined __riscv_zbb || defined __riscv_xtheadbb
  18. /* With bitmap extension we can use orc.b to find all zero bytes. */
  19. # include <string-misc.h>
  20. # include <string-optype.h>
  21. /* The functions return a byte mask. */
  22. typedef op_t find_t;
  23. /* This function returns 0xff for each byte that is zero in X. */
  24. static __always_inline find_t
  25. find_zero_all (op_t x)
  26. {
  27. find_t r;
  28. #ifdef __riscv_xtheadbb
  29. asm ("th.tstnbz %0, %1" : "=r" (r) : "r" (x));
  30. return r;
  31. #else
  32. asm ("orc.b %0, %1" : "=r" (r) : "r" (x));
  33. return ~r;
  34. #endif
  35. }
  36. /* This function returns 0xff for each byte that is equal between X1 and
  37. X2. */
  38. static __always_inline find_t
  39. find_eq_all (op_t x1, op_t x2)
  40. {
  41. return find_zero_all (x1 ^ x2);
  42. }
  43. /* Identify zero bytes in X1 or equality between X1 and X2. */
  44. static __always_inline find_t
  45. find_zero_eq_all (op_t x1, op_t x2)
  46. {
  47. return find_zero_all (x1) | find_eq_all (x1, x2);
  48. }
  49. /* Identify zero bytes in X1 or inequality between X1 and X2. */
  50. static __always_inline find_t
  51. find_zero_ne_all (op_t x1, op_t x2)
  52. {
  53. return find_zero_all (x1) | ~find_eq_all (x1, x2);
  54. }
  55. /* Define the "inexact" versions in terms of the exact versions. */
  56. # define find_zero_low find_zero_all
  57. # define find_eq_low find_eq_all
  58. # define find_zero_eq_low find_zero_eq_all
  59. #else
  60. #include <sysdeps/generic/string-fza.h>
  61. #endif
  62. #endif /* _RISCV_STRING_FZA_H */