strlen.S 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Optimized strlen for Xtensa.
  2. Copyright (C) 2001, 2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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 <sysdep.h>
  16. #include <bits/xtensa-config.h>
  17. #ifdef __XTENSA_EB__
  18. #define MASK0 0xff000000
  19. #define MASK1 0x00ff0000
  20. #define MASK2 0x0000ff00
  21. #define MASK3 0x000000ff
  22. #else
  23. #define MASK0 0x000000ff
  24. #define MASK1 0x0000ff00
  25. #define MASK2 0x00ff0000
  26. #define MASK3 0xff000000
  27. #endif
  28. .text
  29. ENTRY (strlen)
  30. /* a2 = s */
  31. addi a3, a2, -4 /* because we overincrement at the end */
  32. movi a4, MASK0
  33. movi a5, MASK1
  34. movi a6, MASK2
  35. movi a7, MASK3
  36. bbsi.l a2, 0, .L1mod2
  37. bbsi.l a2, 1, .L2mod4
  38. j .Laligned
  39. .L1mod2: /* address is odd */
  40. l8ui a8, a3, 4 /* get byte 0 */
  41. addi a3, a3, 1 /* advance string pointer */
  42. beqz a8, .Lz3 /* if byte 0 is zero */
  43. bbci.l a3, 1, .Laligned /* if string pointer is now word-aligned */
  44. .L2mod4: /* address is 2 mod 4 */
  45. addi a3, a3, 2 /* advance ptr for aligned access */
  46. l32i a8, a3, 0 /* get word with first two bytes of string */
  47. bnone a8, a6, .Lz2 /* if byte 2 (of word, not string) is zero */
  48. bany a8, a7, .Laligned /* if byte 3 (of word, not string) is nonzero */
  49. /* Byte 3 is zero. */
  50. addi a3, a3, 3 /* point to zero byte */
  51. sub a2, a3, a2 /* subtract to get length */
  52. abi_ret
  53. /* String is word-aligned. */
  54. .align 4
  55. /* (2 mod 4) alignment for loop instruction */
  56. .Laligned:
  57. #if XCHAL_HAVE_LOOPS
  58. _movi.n a8, 0 /* set up for the maximum loop count */
  59. loop a8, .Lz3 /* loop forever (almost anyway) */
  60. #endif
  61. 1: l32i a8, a3, 4 /* get next word of string */
  62. addi a3, a3, 4 /* advance string pointer */
  63. bnone a8, a4, .Lz0 /* if byte 0 is zero */
  64. bnone a8, a5, .Lz1 /* if byte 1 is zero */
  65. bnone a8, a6, .Lz2 /* if byte 2 is zero */
  66. #if XCHAL_HAVE_LOOPS
  67. bnone a8, a7, .Lz3 /* if byte 3 is zero */
  68. #else
  69. bany a8, a7, 1b /* repeat if byte 3 is non-zero */
  70. #endif
  71. .Lz3: /* Byte 3 is zero. */
  72. addi a3, a3, 3 /* point to zero byte */
  73. /* Fall through.... */
  74. .Lz0: /* Byte 0 is zero. */
  75. sub a2, a3, a2 /* subtract to get length */
  76. abi_ret
  77. .Lz1: /* Byte 1 is zero. */
  78. addi a3, a3, 1 /* point to zero byte */
  79. sub a2, a3, a2 /* subtract to get length */
  80. abi_ret
  81. .Lz2: /* Byte 2 is zero. */
  82. addi a3, a3, 2 /* point to zero byte */
  83. sub a2, a3, a2 /* subtract to get length */
  84. abi_ret
  85. libc_hidden_def (strlen)