strchr.S 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Optimized version of the standard strchr() function.
  2. This file is part of the GNU C Library.
  3. Copyright (C) 2000, 2001, 2003 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, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Return: the address of the first occurence of chr in str or NULL
  17. Inputs:
  18. in0: str
  19. in1: chr
  20. A modified version of memchr.S, the search ends when the character is
  21. found or the terminating null character is encountered.
  22. This implementation assumes little endian mode. For big endian mode,
  23. the instruction czx1.r should be replaced by czx1.l. */
  24. #include <sysdep.h>
  25. #undef ret
  26. #define saved_lc r18
  27. #define poschr r19
  28. #define pos0 r20
  29. #define val1 r21
  30. #define val2 r22
  31. #define tmp r24
  32. #define chrx8 r25
  33. #define loopcnt r30
  34. #define str in0
  35. #define chr in1
  36. ENTRY(strchr)
  37. .prologue
  38. alloc r2 = ar.pfs, 2, 0, 0, 0
  39. .save ar.lc, saved_lc
  40. mov saved_lc = ar.lc /* save the loop counter */
  41. .body
  42. mov ret0 = str
  43. and tmp = 7, str /* tmp = str % 8 */
  44. mux1 chrx8 = chr, @brcst
  45. extr.u chr = chr, 0, 8 /* retain only the last byte */
  46. cmp.ne p8, p0 = r0, r0 /* clear p8 */
  47. ;;
  48. sub loopcnt = 8, tmp /* loopcnt = 8 - tmp */
  49. cmp.eq p6, p0 = tmp, r0
  50. (p6) br.cond.sptk .str_aligned;;
  51. adds loopcnt = -1, loopcnt;;
  52. mov ar.lc = loopcnt
  53. .l1:
  54. ld1 val2 = [ret0], 1
  55. ;;
  56. cmp.eq p6, p0 = val2, chr
  57. cmp.eq p7, p0 = val2, r0
  58. (p6) br.cond.spnt .restore_and_exit
  59. (p7) br.cond.spnt .notfound
  60. br.cloop.sptk .l1
  61. .str_aligned:
  62. ld8 val1 = [ret0], 8;;
  63. nop.b 0
  64. nop.b 0
  65. .l2:
  66. ld8.s val2 = [ret0], 8 /* don't bomb out here */
  67. czx1.r pos0 = val1
  68. xor tmp = val1, chrx8 /* if val1 contains chr, tmp will */
  69. ;; /* contain a zero in its position */
  70. czx1.r poschr = tmp
  71. cmp.ne p6, p0 = 8, pos0
  72. ;;
  73. cmp.ne p7, p0 = 8, poschr
  74. (p7) br.cond.spnt .foundit
  75. (p6) br.cond.spnt .notfound
  76. chk.s val2, .recovery
  77. .back:
  78. mov val1 = val2
  79. br.cond.dptk .l2
  80. .foundit:
  81. (p6) cmp.lt p8, p0 = pos0, poschr /* we found chr and null in the word */
  82. (p8) br.cond.spnt .notfound /* null was found before chr */
  83. add ret0 = ret0, poschr ;;
  84. adds ret0 = -15, ret0 ;; /* should be -16, but we decrement */
  85. .restore_and_exit: /* ret0 in the next instruction */
  86. adds ret0 = -1, ret0 /* ret0 was pointing 1 char too far */
  87. mov ar.lc = saved_lc /* restore the loop counter */
  88. br.ret.sptk.many b0
  89. .notfound:
  90. mov ret0 = r0 /* return NULL if null was found */
  91. mov ar.lc = saved_lc
  92. br.ret.sptk.many b0
  93. .recovery:
  94. adds ret0 = -8, ret0;;
  95. ld8 val2 = [ret0], 8 /* bomb out here */
  96. br.cond.sptk .back
  97. END(strchr)
  98. libc_hidden_def (strchr)
  99. #ifdef __UCLIBC_SUSV3_LEGACY__
  100. strong_alias (strchr, index)
  101. #endif