strlen.S 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. /* Return: the length of the input string
  18. Input:
  19. in0: str
  20. Look for the null character byte by byte, until we reach a word aligned
  21. address, then search word by word, using the czx instruction. We're
  22. also doing one word of read ahead, which could cause problems if the
  23. null character is on the last word of a page and the next page is not
  24. mapped in the process address space. Hence the use of the speculative
  25. load.
  26. This implementation assumes little endian mode. For big endian mode,
  27. the instruction czx1.r should be replaced by czx1.l. */
  28. #include "sysdep.h"
  29. #undef ret
  30. #define saved_lc r18
  31. #define str r19
  32. #define pos0 r20
  33. #define val1 r21
  34. #define val2 r22
  35. #define origadd r23
  36. #define tmp r24
  37. #define loopcnt r30
  38. #define len ret0
  39. ENTRY(strlen)
  40. .prologue
  41. alloc r2 = ar.pfs, 1, 0, 0, 0
  42. .save ar.lc, saved_lc
  43. mov saved_lc = ar.lc // save the loop counter
  44. .body
  45. mov str = in0
  46. mov len = r0 // len = 0
  47. and tmp = 7, in0 // tmp = str % 8
  48. ;;
  49. sub loopcnt = 8, tmp // loopcnt = 8 - tmp
  50. cmp.eq p6, p0 = tmp, r0
  51. (p6) br.cond.sptk .str_aligned;;
  52. adds loopcnt = -1, loopcnt;;
  53. mov ar.lc = loopcnt
  54. .l1:
  55. ld1 val2 = [str], 1
  56. ;;
  57. cmp.eq p6, p0 = val2, r0
  58. (p6) br.cond.spnt .restore_and_exit
  59. adds len = 1, len
  60. br.cloop.dptk .l1
  61. .str_aligned:
  62. mov origadd = str // origadd = orig
  63. ld8 val1 = [str], 8;;
  64. nop.b 0
  65. nop.b 0
  66. .l2: ld8.s val2 = [str], 8 // don't bomb out here
  67. czx1.r pos0 = val1
  68. ;;
  69. cmp.ne p6, p0 = 8, pos0
  70. (p6) br.cond.spnt .foundit
  71. chk.s val2, .recovery
  72. .back:
  73. mov val1 = val2
  74. br.cond.dptk .l2
  75. .foundit:
  76. sub tmp = str, origadd // tmp = crt address - orig
  77. add len = len, pos0;;
  78. add len = len, tmp;;
  79. adds len = -16, len
  80. .restore_and_exit:
  81. mov ar.lc = saved_lc // restore the loop counter
  82. br.ret.sptk.many b0
  83. .recovery:
  84. adds str = -8, str;;
  85. ld8 val2 = [str], 8 // bomb out here
  86. br.cond.sptk .back
  87. END(strlen)
  88. libc_hidden_def (strlen)