memcmp.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Optimized version of the standard memcmp() function.
  2. This file is part of the GNU C Library.
  3. Copyright (C) 2000, 2001, 2004 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 result of the comparison
  17. Inputs:
  18. in0: dest (aka s1)
  19. in1: src (aka s2)
  20. in2: byte count
  21. In this form, it assumes little endian mode. For big endian mode, the
  22. the two shifts in .l2 must be inverted:
  23. shl tmp1[0] = r[1 + MEMLAT], sh1 // tmp1 = w0 << sh1
  24. shr.u tmp2[0] = r[0 + MEMLAT], sh2 // tmp2 = w1 >> sh2
  25. and all the mux1 instructions should be replaced by plain mov's. */
  26. #include <sysdep.h>
  27. #undef ret
  28. #define OP_T_THRES 16
  29. #define OPSIZ 8
  30. #define MEMLAT 2
  31. #define start r15
  32. #define saved_pr r17
  33. #define saved_lc r18
  34. #define dest r19
  35. #define src r20
  36. #define len r21
  37. #define asrc r22
  38. #define tmp r23
  39. #define value1 r24
  40. #define value2 r25
  41. #define sh2 r28
  42. #define sh1 r29
  43. #define loopcnt r30
  44. ENTRY(memcmp)
  45. .prologue
  46. alloc r2 = ar.pfs, 3, 37, 0, 40
  47. .rotr r[MEMLAT + 2], q[MEMLAT + 5], tmp1[4], tmp2[4], val[2]
  48. .rotp p[MEMLAT + 4 + 1]
  49. mov ret0 = r0 /* by default return value = 0 */
  50. .save pr, saved_pr
  51. mov saved_pr = pr /* save the predicate registers */
  52. .save ar.lc, saved_lc
  53. mov saved_lc = ar.lc /* save the loop counter */
  54. .body
  55. mov dest = in0 /* dest */
  56. mov src = in1 /* src */
  57. mov len = in2 /* len */
  58. sub tmp = r0, in0 /* tmp = -dest */
  59. ;;
  60. and loopcnt = 7, tmp /* loopcnt = -dest % 8 */
  61. cmp.ge p6, p0 = OP_T_THRES, len /* is len <= OP_T_THRES */
  62. (p6) br.cond.spnt .cmpfew /* compare byte by byte */
  63. ;;
  64. cmp.eq p6, p0 = loopcnt, r0
  65. (p6) br.cond.sptk .dest_aligned
  66. sub len = len, loopcnt /* len -= -dest % 8 */
  67. adds loopcnt = -1, loopcnt /* --loopcnt */
  68. ;;
  69. mov ar.lc = loopcnt
  70. .l1: /* copy -dest % 8 bytes */
  71. ld1 value1 = [src], 1 /* value = *src++ */
  72. ld1 value2 = [dest], 1
  73. ;;
  74. cmp.ne p6, p0 = value1, value2
  75. (p6) br.cond.spnt .done
  76. br.cloop.dptk .l1
  77. .dest_aligned:
  78. and sh1 = 7, src /* sh1 = src % 8 */
  79. and tmp = -8, len /* tmp = len & -OPSIZ */
  80. and asrc = -8, src /* asrc = src & -OPSIZ -- align src */
  81. shr.u loopcnt = len, 3 /* loopcnt = len / 8 */
  82. and len = 7, len ;; /* len = len % 8 */
  83. shl sh1 = sh1, 3 /* sh1 = 8 * (src % 8) */
  84. adds loopcnt = -1, loopcnt /* --loopcnt */
  85. mov pr.rot = 1 << 16 ;; /* set rotating predicates */
  86. sub sh2 = 64, sh1 /* sh2 = 64 - sh1 */
  87. mov ar.lc = loopcnt /* set LC */
  88. cmp.eq p6, p0 = sh1, r0 /* is the src aligned? */
  89. (p6) br.cond.sptk .src_aligned
  90. add src = src, tmp /* src += len & -OPSIZ */
  91. mov ar.ec = MEMLAT + 4 + 1 /* four more passes needed */
  92. ld8 r[1] = [asrc], 8 ;; /* r[1] = w0 */
  93. .align 32
  94. /* We enter this loop with p6 cleared by the above comparison */
  95. .l2:
  96. (p[0]) ld8 r[0] = [asrc], 8 /* r[0] = w1 */
  97. (p[0]) ld8 q[0] = [dest], 8
  98. (p[MEMLAT]) shr.u tmp1[0] = r[1 + MEMLAT], sh1 /* tmp1 = w0 >> sh1 */
  99. (p[MEMLAT]) shl tmp2[0] = r[0 + MEMLAT], sh2 /* tmp2 = w1 << sh2 */
  100. (p[MEMLAT+4]) cmp.ne p6, p0 = q[MEMLAT + 4], val[1]
  101. (p[MEMLAT+3]) or val[0] = tmp1[3], tmp2[3] /* val = tmp1 | tmp2 */
  102. (p6) br.cond.spnt .l2exit
  103. br.ctop.sptk .l2
  104. br.cond.sptk .cmpfew
  105. .l3exit:
  106. mux1 value1 = r[MEMLAT], @rev
  107. mux1 value2 = q[MEMLAT], @rev
  108. cmp.ne p6, p0 = r0, r0 ;; /* clear p6 */
  109. .l2exit:
  110. (p6) mux1 value1 = val[1], @rev
  111. (p6) mux1 value2 = q[MEMLAT + 4], @rev ;;
  112. cmp.ltu p6, p7 = value2, value1 ;;
  113. (p6) mov ret0 = -1
  114. (p7) mov ret0 = 1
  115. mov pr = saved_pr, -1 /* restore the predicate registers */
  116. mov ar.lc = saved_lc /* restore the loop counter */
  117. br.ret.sptk.many b0
  118. .src_aligned:
  119. cmp.ne p6, p0 = r0, r0 /* clear p6 */
  120. mov ar.ec = MEMLAT + 1 ;; /* set EC */
  121. .l3:
  122. (p[0]) ld8 r[0] = [src], 8
  123. (p[0]) ld8 q[0] = [dest], 8
  124. (p[MEMLAT]) cmp.ne p6, p0 = r[MEMLAT], q[MEMLAT]
  125. (p6) br.cond.spnt .l3exit
  126. br.ctop.dptk .l3 ;;
  127. .cmpfew:
  128. cmp.eq p6, p0 = len, r0 /* is len == 0 ? */
  129. adds len = -1, len /* --len; */
  130. (p6) br.cond.spnt .restore_and_exit ;;
  131. mov ar.lc = len
  132. .l4:
  133. ld1 value1 = [src], 1
  134. ld1 value2 = [dest], 1
  135. ;;
  136. cmp.ne p6, p0 = value1, value2
  137. (p6) br.cond.spnt .done
  138. br.cloop.dptk .l4 ;;
  139. .done:
  140. (p6) sub ret0 = value2, value1 /* don't execute it if falling thru */
  141. .restore_and_exit:
  142. mov pr = saved_pr, -1 /* restore the predicate registers */
  143. mov ar.lc = saved_lc /* restore the loop counter */
  144. br.ret.sptk.many b0
  145. END(memcmp)
  146. libc_hidden_def (memcmp)
  147. #ifdef __UCLIBC_SUSV3_LEGACY__
  148. strong_alias (memcmp, bcmp)
  149. #endif