strlen.S 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Copyright (C) 1996-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <https://www.gnu.org/licenses/>. */
  14. /* Finds length of a 0-terminated string. Optimized for the Alpha
  15. architecture:
  16. - memory accessed as aligned quadwords only
  17. - uses cmpbge to compare 8 bytes in parallel
  18. - does binary search to find 0 byte in last quadword (HAKMEM
  19. needed 12 instructions to do this instead of the 8 instructions
  20. that the binary search needs).
  21. */
  22. #include <sysdep.h>
  23. .set noreorder
  24. .set noat
  25. ENTRY(strlen)
  26. #ifdef PROF
  27. ldgp gp, 0(pv)
  28. lda AT, _mcount
  29. jsr AT, (AT), _mcount
  30. .prologue 1
  31. #else
  32. .prologue 0
  33. #endif
  34. ldq_u t0, 0(a0) # load first quadword (a0 may be misaligned)
  35. lda t1, -1(zero)
  36. insqh t1, a0, t1
  37. andnot a0, 7, v0
  38. or t1, t0, t0
  39. nop # dual issue the next two on ev5
  40. cmpbge zero, t0, t1 # t1 <- bitmask: bit i == 1 <==> i-th byte == 0
  41. bne t1, $found
  42. $loop: ldq t0, 8(v0)
  43. addq v0, 8, v0 # addr += 8
  44. cmpbge zero, t0, t1
  45. beq t1, $loop
  46. $found: negq t1, t2 # clear all but least set bit
  47. and t1, t2, t1
  48. and t1, 0xf0, t2 # binary search for that set bit
  49. and t1, 0xcc, t3
  50. and t1, 0xaa, t4
  51. cmovne t2, 4, t2
  52. cmovne t3, 2, t3
  53. cmovne t4, 1, t4
  54. addq t2, t3, t2
  55. addq v0, t4, v0
  56. addq v0, t2, v0
  57. nop # dual issue next two on ev4 and ev5
  58. subq v0, a0, v0
  59. ret
  60. END(strlen)
  61. libc_hidden_builtin_def (strlen)