strlen.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright (C) 1998 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Code contributed by Matthew Wilcox <willy@odie.barnet.ac.uk>
  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. #include <features.h>
  16. #include <endian.h>
  17. #include <bits/arm_asm.h>
  18. #include <bits/arm_bx.h>
  19. /* size_t strlen(const char *S)
  20. * entry: r0 -> string
  21. * exit: r0 = len
  22. */
  23. .text
  24. .global strlen
  25. .type strlen,%function
  26. .align 4
  27. #if defined(THUMB1_ONLY)
  28. /* A simple implementation for when the ARM implementation can't be used. */
  29. .thumb_func
  30. strlen:
  31. mov r2, #0
  32. 1:
  33. ldrb r1, [r0, r2]
  34. add r2, r2, #1
  35. cmp r1, #0
  36. bne 1b
  37. sub r0, r2, #1
  38. bx lr
  39. #else
  40. strlen:
  41. bic r1, r0, $3 @ addr of word containing first byte
  42. ldr r2, [r1], $4 @ get the first word
  43. ands r3, r0, $3 @ how many bytes are duff?
  44. rsb r0, r3, $0 @ get - that number into counter.
  45. beq Laligned @ skip into main check routine if no
  46. @ more
  47. #if __BYTE_ORDER == __BIG_ENDIAN
  48. orr r2, r2, $0xff000000 @ set this byte to non-zero
  49. subs r3, r3, $1 @ any more to do?
  50. IT(t, gt)
  51. orrgt r2, r2, $0x00ff0000 @ if so, set this byte
  52. subs r3, r3, $1 @ more?
  53. IT(t, gt)
  54. orrgt r2, r2, $0x0000ff00 @ then set.
  55. #else
  56. orr r2, r2, $0x000000ff @ set this byte to non-zero
  57. subs r3, r3, $1 @ any more to do?
  58. IT(t, gt)
  59. orrgt r2, r2, $0x0000ff00 @ if so, set this byte
  60. subs r3, r3, $1 @ more?
  61. IT(t, gt)
  62. orrgt r2, r2, $0x00ff0000 @ then set.
  63. #endif
  64. Laligned: @ here, we have a word in r2. Does it
  65. tst r2, $0x000000ff @ contain any zeroes?
  66. IT(tttt, ne)
  67. tstne r2, $0x0000ff00 @
  68. tstne r2, $0x00ff0000 @
  69. tstne r2, $0xff000000 @
  70. addne r0, r0, $4 @ if not, the string is 4 bytes longer
  71. IT(t, ne)
  72. ldrne r2, [r1], $4 @ and we continue to the next word
  73. bne Laligned @
  74. Llastword: @ drop through to here once we find a
  75. #if __BYTE_ORDER == __BIG_ENDIAN
  76. tst r2, $0xff000000 @ word that has a zero byte in it
  77. IT(tttt, ne)
  78. addne r0, r0, $1 @
  79. tstne r2, $0x00ff0000 @ and add up to 3 bytes on to it
  80. addne r0, r0, $1 @
  81. tstne r2, $0x0000ff00 @ (if first three all non-zero, 4th
  82. IT(t, ne)
  83. addne r0, r0, $1 @ must be zero)
  84. #else
  85. tst r2, $0x000000ff @
  86. IT(tttt, ne)
  87. addne r0, r0, $1 @
  88. tstne r2, $0x0000ff00 @ and add up to 3 bytes on to it
  89. addne r0, r0, $1 @
  90. tstne r2, $0x00ff0000 @ (if first three all non-zero, 4th
  91. IT(t, ne)
  92. addne r0, r0, $1 @ must be zero)
  93. #endif
  94. BX(lr)
  95. #endif
  96. .size strlen,.-strlen
  97. libc_hidden_def(strlen)