strnlen_user.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <linux/linkage.h>
  14. #include <asm/asmmacro.h>
  15. #include <asm/core.h>
  16. /*
  17. * size_t __strnlen_user(const char *s, size_t len)
  18. */
  19. #ifdef __XTENSA_EB__
  20. # define MASK0 0xff000000
  21. # define MASK1 0x00ff0000
  22. # define MASK2 0x0000ff00
  23. # define MASK3 0x000000ff
  24. #else
  25. # define MASK0 0x000000ff
  26. # define MASK1 0x0000ff00
  27. # define MASK2 0x00ff0000
  28. # define MASK3 0xff000000
  29. #endif
  30. # Register use:
  31. # a2/ src
  32. # a3/ len
  33. # a4/ tmp
  34. # a5/ mask0
  35. # a6/ mask1
  36. # a7/ mask2
  37. # a8/ mask3
  38. # a9/ tmp
  39. # a10/ tmp
  40. .text
  41. ENTRY(__strnlen_user)
  42. abi_entry_default
  43. # a2/ s, a3/ len
  44. addi a4, a2, -4 # because we overincrement at the end;
  45. # we compensate with load offsets of 4
  46. movi a5, MASK0 # mask for byte 0
  47. movi a6, MASK1 # mask for byte 1
  48. movi a7, MASK2 # mask for byte 2
  49. movi a8, MASK3 # mask for byte 3
  50. bbsi.l a2, 0, .L1mod2 # if only 8-bit aligned
  51. bbsi.l a2, 1, .L2mod4 # if only 16-bit aligned
  52. /*
  53. * String is word-aligned.
  54. */
  55. .Laligned:
  56. srli a10, a3, 2 # number of loop iterations with 4B per loop
  57. #if XCHAL_HAVE_LOOPS
  58. loopnez a10, .Ldone
  59. #else
  60. beqz a10, .Ldone
  61. slli a10, a10, 2
  62. add a10, a10, a4 # a10 = end of last 4B chunk
  63. #endif /* XCHAL_HAVE_LOOPS */
  64. .Loop:
  65. EX(10f) l32i a9, a4, 4 # get next word of string
  66. addi a4, a4, 4 # advance string pointer
  67. bnone a9, a5, .Lz0 # if byte 0 is zero
  68. bnone a9, a6, .Lz1 # if byte 1 is zero
  69. bnone a9, a7, .Lz2 # if byte 2 is zero
  70. bnone a9, a8, .Lz3 # if byte 3 is zero
  71. #if !XCHAL_HAVE_LOOPS
  72. blt a4, a10, .Loop
  73. #endif
  74. .Ldone:
  75. EX(10f) l32i a9, a4, 4 # load 4 bytes for remaining checks
  76. bbci.l a3, 1, .L100
  77. # check two more bytes (bytes 0, 1 of word)
  78. addi a4, a4, 2 # advance string pointer
  79. bnone a9, a5, .Lz0 # if byte 0 is zero
  80. bnone a9, a6, .Lz1 # if byte 1 is zero
  81. .L100:
  82. bbci.l a3, 0, .L101
  83. # check one more byte (byte 2 of word)
  84. # Actually, we don't need to check. Zero or nonzero, we'll add one.
  85. # Do not add an extra one for the NULL terminator since we have
  86. # exhausted the original len parameter.
  87. addi a4, a4, 1 # advance string pointer
  88. .L101:
  89. sub a2, a4, a2 # compute length
  90. abi_ret_default
  91. # NOTE that in several places below, we point to the byte just after
  92. # the zero byte in order to include the NULL terminator in the count.
  93. .Lz3: # byte 3 is zero
  94. addi a4, a4, 3 # point to zero byte
  95. .Lz0: # byte 0 is zero
  96. addi a4, a4, 1 # point just beyond zero byte
  97. sub a2, a4, a2 # subtract to get length
  98. abi_ret_default
  99. .Lz1: # byte 1 is zero
  100. addi a4, a4, 1+1 # point just beyond zero byte
  101. sub a2, a4, a2 # subtract to get length
  102. abi_ret_default
  103. .Lz2: # byte 2 is zero
  104. addi a4, a4, 2+1 # point just beyond zero byte
  105. sub a2, a4, a2 # subtract to get length
  106. abi_ret_default
  107. .L1mod2: # address is odd
  108. EX(10f) l8ui a9, a4, 4 # get byte 0
  109. addi a4, a4, 1 # advance string pointer
  110. beqz a9, .Lz3 # if byte 0 is zero
  111. bbci.l a4, 1, .Laligned # if string pointer is now word-aligned
  112. .L2mod4: # address is 2 mod 4
  113. addi a4, a4, 2 # advance ptr for aligned access
  114. EX(10f) l32i a9, a4, 0 # get word with first two bytes of string
  115. bnone a9, a7, .Lz2 # if byte 2 (of word, not string) is zero
  116. bany a9, a8, .Laligned # if byte 3 (of word, not string) is nonzero
  117. # byte 3 is zero
  118. addi a4, a4, 3+1 # point just beyond zero byte
  119. sub a2, a4, a2 # subtract to get length
  120. abi_ret_default
  121. ENDPROC(__strnlen_user)
  122. .section .fixup, "ax"
  123. .align 4
  124. 10:
  125. movi a2, 0
  126. abi_ret_default