strnlen_user.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * arch/xtensa/lib/strnlen_user.S
  3. *
  4. * This file is subject to the terms and conditions of the GNU General
  5. * Public License. See the file "COPYING" in the main directory of
  6. * this archive for more details.
  7. *
  8. * Returns strnlen, including trailing zero terminator.
  9. * Zero indicates error.
  10. *
  11. * Copyright (C) 2002 Tensilica Inc.
  12. */
  13. #include <variant/core.h>
  14. /* Load or store instructions that may cause exceptions use the EX macro. */
  15. #define EX(insn,reg1,reg2,offset,handler) \
  16. 9: insn reg1, reg2, offset; \
  17. .section __ex_table, "a"; \
  18. .word 9b, handler; \
  19. .previous
  20. /*
  21. * size_t __strnlen_user(const char *s, size_t len)
  22. */
  23. #ifdef __XTENSA_EB__
  24. # define MASK0 0xff000000
  25. # define MASK1 0x00ff0000
  26. # define MASK2 0x0000ff00
  27. # define MASK3 0x000000ff
  28. #else
  29. # define MASK0 0x000000ff
  30. # define MASK1 0x0000ff00
  31. # define MASK2 0x00ff0000
  32. # define MASK3 0xff000000
  33. #endif
  34. # Register use:
  35. # a2/ src
  36. # a3/ len
  37. # a4/ tmp
  38. # a5/ mask0
  39. # a6/ mask1
  40. # a7/ mask2
  41. # a8/ mask3
  42. # a9/ tmp
  43. # a10/ tmp
  44. .text
  45. .align 4
  46. .global __strnlen_user
  47. .type __strnlen_user,@function
  48. __strnlen_user:
  49. entry sp, 16 # minimal stack frame
  50. # a2/ s, a3/ len
  51. addi a4, a2, -4 # because we overincrement at the end;
  52. # we compensate with load offsets of 4
  53. movi a5, MASK0 # mask for byte 0
  54. movi a6, MASK1 # mask for byte 1
  55. movi a7, MASK2 # mask for byte 2
  56. movi a8, MASK3 # mask for byte 3
  57. bbsi.l a2, 0, .L1mod2 # if only 8-bit aligned
  58. bbsi.l a2, 1, .L2mod4 # if only 16-bit aligned
  59. /*
  60. * String is word-aligned.
  61. */
  62. .Laligned:
  63. srli a10, a3, 2 # number of loop iterations with 4B per loop
  64. #if XCHAL_HAVE_LOOPS
  65. loopnez a10, .Ldone
  66. #else
  67. beqz a10, .Ldone
  68. slli a10, a10, 2
  69. add a10, a10, a4 # a10 = end of last 4B chunk
  70. #endif /* XCHAL_HAVE_LOOPS */
  71. .Loop:
  72. EX(l32i, a9, a4, 4, lenfixup) # get next word of string
  73. addi a4, a4, 4 # advance string pointer
  74. bnone a9, a5, .Lz0 # if byte 0 is zero
  75. bnone a9, a6, .Lz1 # if byte 1 is zero
  76. bnone a9, a7, .Lz2 # if byte 2 is zero
  77. bnone a9, a8, .Lz3 # if byte 3 is zero
  78. #if !XCHAL_HAVE_LOOPS
  79. blt a4, a10, .Loop
  80. #endif
  81. .Ldone:
  82. EX(l32i, a9, a4, 4, lenfixup) # load 4 bytes for remaining checks
  83. bbci.l a3, 1, .L100
  84. # check two more bytes (bytes 0, 1 of word)
  85. addi a4, a4, 2 # advance string pointer
  86. bnone a9, a5, .Lz0 # if byte 0 is zero
  87. bnone a9, a6, .Lz1 # if byte 1 is zero
  88. .L100:
  89. bbci.l a3, 0, .L101
  90. # check one more byte (byte 2 of word)
  91. # Actually, we don't need to check. Zero or nonzero, we'll add one.
  92. # Do not add an extra one for the NULL terminator since we have
  93. # exhausted the original len parameter.
  94. addi a4, a4, 1 # advance string pointer
  95. .L101:
  96. sub a2, a4, a2 # compute length
  97. retw
  98. # NOTE that in several places below, we point to the byte just after
  99. # the zero byte in order to include the NULL terminator in the count.
  100. .Lz3: # byte 3 is zero
  101. addi a4, a4, 3 # point to zero byte
  102. .Lz0: # byte 0 is zero
  103. addi a4, a4, 1 # point just beyond zero byte
  104. sub a2, a4, a2 # subtract to get length
  105. retw
  106. .Lz1: # byte 1 is zero
  107. addi a4, a4, 1+1 # point just beyond zero byte
  108. sub a2, a4, a2 # subtract to get length
  109. retw
  110. .Lz2: # byte 2 is zero
  111. addi a4, a4, 2+1 # point just beyond zero byte
  112. sub a2, a4, a2 # subtract to get length
  113. retw
  114. .L1mod2: # address is odd
  115. EX(l8ui, a9, a4, 4, lenfixup) # get byte 0
  116. addi a4, a4, 1 # advance string pointer
  117. beqz a9, .Lz3 # if byte 0 is zero
  118. bbci.l a4, 1, .Laligned # if string pointer is now word-aligned
  119. .L2mod4: # address is 2 mod 4
  120. addi a4, a4, 2 # advance ptr for aligned access
  121. EX(l32i, a9, a4, 0, lenfixup) # get word with first two bytes of string
  122. bnone a9, a7, .Lz2 # if byte 2 (of word, not string) is zero
  123. bany a9, a8, .Laligned # if byte 3 (of word, not string) is nonzero
  124. # byte 3 is zero
  125. addi a4, a4, 3+1 # point just beyond zero byte
  126. sub a2, a4, a2 # subtract to get length
  127. retw
  128. .section .fixup, "ax"
  129. .align 4
  130. lenfixup:
  131. movi a2, 0
  132. retw