strlen.S 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Optimized version of the standard strlen() function.
  2. This file is part of the GNU C Library.
  3. Copyright (C) 2000, 2001, 2003, 2005 Free Software Foundation, Inc.
  4. Contributed by Dan Pop <Dan.Pop@cern.ch>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Return: the length of the input string
  17. Input:
  18. in0: str
  19. Look for the null character byte by byte, until we reach a word aligned
  20. address, then search word by word, using the czx instruction. We're
  21. also doing one word of read ahead, which could cause problems if the
  22. null character is on the last word of a page and the next page is not
  23. mapped in the process address space. Hence the use of the speculative
  24. load.
  25. This implementation assumes little endian mode. For big endian mode,
  26. the instruction czx1.r should be replaced by czx1.l. */
  27. #include <sysdep.h>
  28. #undef ret
  29. #define saved_lc r18
  30. #define str r19
  31. #define pos0 r20
  32. #define val1 r21
  33. #define val2 r22
  34. #define origadd r23
  35. #define tmp r24
  36. #define loopcnt r30
  37. #define len ret0
  38. ENTRY(strlen)
  39. .prologue
  40. alloc r2 = ar.pfs, 1, 0, 0, 0
  41. .save ar.lc, saved_lc
  42. mov saved_lc = ar.lc /* save the loop counter */
  43. .body
  44. mov str = in0
  45. mov len = r0 /* len = 0 */
  46. and tmp = 7, in0 /* tmp = str % 8 */
  47. ;;
  48. sub loopcnt = 8, tmp /* loopcnt = 8 - tmp */
  49. cmp.eq p6, p0 = tmp, r0
  50. (p6) br.cond.sptk .str_aligned;;
  51. adds loopcnt = -1, loopcnt;;
  52. mov ar.lc = loopcnt
  53. .l1:
  54. ld1 val2 = [str], 1
  55. ;;
  56. cmp.eq p6, p0 = val2, r0
  57. (p6) br.cond.spnt .restore_and_exit
  58. adds len = 1, len
  59. br.cloop.dptk .l1
  60. .str_aligned:
  61. mov origadd = str /* origadd = orig */
  62. ld8 val1 = [str], 8;;
  63. nop.b 0
  64. nop.b 0
  65. .l2: ld8.s val2 = [str], 8 /* don't bomb out here */
  66. czx1.r pos0 = val1
  67. ;;
  68. cmp.ne p6, p0 = 8, pos0
  69. (p6) br.cond.spnt .foundit
  70. chk.s val2, .recovery
  71. .back:
  72. mov val1 = val2
  73. br.cond.dptk .l2
  74. .foundit:
  75. sub tmp = str, origadd /* tmp = crt address - orig */
  76. add len = len, pos0;;
  77. add len = len, tmp;;
  78. adds len = -16, len
  79. .restore_and_exit:
  80. mov ar.lc = saved_lc /* restore the loop counter */
  81. br.ret.sptk.many b0
  82. .recovery:
  83. adds str = -8, str;;
  84. ld8 val2 = [str], 8 /* bomb out here */
  85. br.cond.sptk .back
  86. END(strlen)
  87. libc_hidden_def (strlen)