memcmp.S 5.1 KB

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