memcmp.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* Copyright (C) 1991,1993,1995,1997,1998,2003,2004
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Torbjorn Granlund (tege@sics.se).
  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. #include <string.h>
  18. #include "memcopy.h"
  19. /* Experimentally off - libc_hidden_proto(memcmp) */
  20. #include <endian.h>
  21. #if __BYTE_ORDER == __BIG_ENDIAN
  22. # define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
  23. #else
  24. # define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
  25. #endif
  26. /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
  27. /* The strategy of this memcmp is:
  28. 1. Compare bytes until one of the block pointers is aligned.
  29. 2. Compare using memcmp_common_alignment or
  30. memcmp_not_common_alignment, regarding the alignment of the other
  31. block after the initial byte operations. The maximum number of
  32. full words (of type op_t) are compared in this way.
  33. 3. Compare the few remaining bytes. */
  34. #if __BYTE_ORDER != __BIG_ENDIAN
  35. /* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine.
  36. A and B are known to be different.
  37. This is needed only on little-endian machines. */
  38. static __inline__ int
  39. memcmp_bytes (op_t a, op_t b)
  40. {
  41. long int srcp1 = (long int) &a;
  42. long int srcp2 = (long int) &b;
  43. op_t a0, b0;
  44. do
  45. {
  46. a0 = ((byte *) srcp1)[0];
  47. b0 = ((byte *) srcp2)[0];
  48. srcp1 += 1;
  49. srcp2 += 1;
  50. }
  51. while (a0 == b0);
  52. return a0 - b0;
  53. }
  54. #endif
  55. /* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
  56. objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for
  57. memory operations on `op_t's. */
  58. static int
  59. memcmp_common_alignment (long int srcp1, long int srcp2, size_t len)
  60. {
  61. op_t a0, a1;
  62. op_t b0, b1;
  63. switch (len % 4)
  64. {
  65. default: /* Avoid warning about uninitialized local variables. */
  66. case 2:
  67. a0 = ((op_t *) srcp1)[0];
  68. b0 = ((op_t *) srcp2)[0];
  69. srcp1 -= 2 * OPSIZ;
  70. srcp2 -= 2 * OPSIZ;
  71. len += 2;
  72. goto do1;
  73. case 3:
  74. a1 = ((op_t *) srcp1)[0];
  75. b1 = ((op_t *) srcp2)[0];
  76. srcp1 -= OPSIZ;
  77. srcp2 -= OPSIZ;
  78. len += 1;
  79. goto do2;
  80. case 0:
  81. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  82. return 0;
  83. a0 = ((op_t *) srcp1)[0];
  84. b0 = ((op_t *) srcp2)[0];
  85. goto do3;
  86. case 1:
  87. a1 = ((op_t *) srcp1)[0];
  88. b1 = ((op_t *) srcp2)[0];
  89. srcp1 += OPSIZ;
  90. srcp2 += OPSIZ;
  91. len -= 1;
  92. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  93. goto do0;
  94. /* Fall through. */
  95. }
  96. do
  97. {
  98. a0 = ((op_t *) srcp1)[0];
  99. b0 = ((op_t *) srcp2)[0];
  100. if (a1 != b1)
  101. return CMP_LT_OR_GT (a1, b1);
  102. do3:
  103. a1 = ((op_t *) srcp1)[1];
  104. b1 = ((op_t *) srcp2)[1];
  105. if (a0 != b0)
  106. return CMP_LT_OR_GT (a0, b0);
  107. do2:
  108. a0 = ((op_t *) srcp1)[2];
  109. b0 = ((op_t *) srcp2)[2];
  110. if (a1 != b1)
  111. return CMP_LT_OR_GT (a1, b1);
  112. do1:
  113. a1 = ((op_t *) srcp1)[3];
  114. b1 = ((op_t *) srcp2)[3];
  115. if (a0 != b0)
  116. return CMP_LT_OR_GT (a0, b0);
  117. srcp1 += 4 * OPSIZ;
  118. srcp2 += 4 * OPSIZ;
  119. len -= 4;
  120. }
  121. while (len != 0);
  122. /* This is the right position for do0. Please don't move
  123. it into the loop. */
  124. do0:
  125. if (a1 != b1)
  126. return CMP_LT_OR_GT (a1, b1);
  127. return 0;
  128. }
  129. /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
  130. `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
  131. operations on `op_t', but SRCP1 *should be unaligned*. */
  132. static int
  133. memcmp_not_common_alignment (long int srcp1, long int srcp2, size_t len)
  134. {
  135. op_t a0, a1, a2, a3;
  136. op_t b0, b1, b2, b3;
  137. op_t x;
  138. int shl, shr;
  139. /* Calculate how to shift a word read at the memory operation
  140. aligned srcp1 to make it aligned for comparison. */
  141. shl = 8 * (srcp1 % OPSIZ);
  142. shr = 8 * OPSIZ - shl;
  143. /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
  144. it points in the middle of. */
  145. srcp1 &= -OPSIZ;
  146. switch (len % 4)
  147. {
  148. default: /* Avoid warning about uninitialized local variables. */
  149. case 2:
  150. a1 = ((op_t *) srcp1)[0];
  151. a2 = ((op_t *) srcp1)[1];
  152. b2 = ((op_t *) srcp2)[0];
  153. srcp1 -= 1 * OPSIZ;
  154. srcp2 -= 2 * OPSIZ;
  155. len += 2;
  156. goto do1;
  157. case 3:
  158. a0 = ((op_t *) srcp1)[0];
  159. a1 = ((op_t *) srcp1)[1];
  160. b1 = ((op_t *) srcp2)[0];
  161. srcp2 -= 1 * OPSIZ;
  162. len += 1;
  163. goto do2;
  164. case 0:
  165. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  166. return 0;
  167. a3 = ((op_t *) srcp1)[0];
  168. a0 = ((op_t *) srcp1)[1];
  169. b0 = ((op_t *) srcp2)[0];
  170. srcp1 += 1 * OPSIZ;
  171. goto do3;
  172. case 1:
  173. a2 = ((op_t *) srcp1)[0];
  174. a3 = ((op_t *) srcp1)[1];
  175. b3 = ((op_t *) srcp2)[0];
  176. srcp1 += 2 * OPSIZ;
  177. srcp2 += 1 * OPSIZ;
  178. len -= 1;
  179. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  180. goto do0;
  181. /* Fall through. */
  182. }
  183. do
  184. {
  185. a0 = ((op_t *) srcp1)[0];
  186. b0 = ((op_t *) srcp2)[0];
  187. x = MERGE(a2, shl, a3, shr);
  188. if (x != b3)
  189. return CMP_LT_OR_GT (x, b3);
  190. do3:
  191. a1 = ((op_t *) srcp1)[1];
  192. b1 = ((op_t *) srcp2)[1];
  193. x = MERGE(a3, shl, a0, shr);
  194. if (x != b0)
  195. return CMP_LT_OR_GT (x, b0);
  196. do2:
  197. a2 = ((op_t *) srcp1)[2];
  198. b2 = ((op_t *) srcp2)[2];
  199. x = MERGE(a0, shl, a1, shr);
  200. if (x != b1)
  201. return CMP_LT_OR_GT (x, b1);
  202. do1:
  203. a3 = ((op_t *) srcp1)[3];
  204. b3 = ((op_t *) srcp2)[3];
  205. x = MERGE(a1, shl, a2, shr);
  206. if (x != b2)
  207. return CMP_LT_OR_GT (x, b2);
  208. srcp1 += 4 * OPSIZ;
  209. srcp2 += 4 * OPSIZ;
  210. len -= 4;
  211. }
  212. while (len != 0);
  213. /* This is the right position for do0. Please don't move
  214. it into the loop. */
  215. do0:
  216. x = MERGE(a2, shl, a3, shr);
  217. if (x != b3)
  218. return CMP_LT_OR_GT (x, b3);
  219. return 0;
  220. }
  221. int
  222. memcmp (const __ptr_t s1, const __ptr_t s2, size_t len)
  223. {
  224. op_t a0;
  225. op_t b0;
  226. long int srcp1 = (long int) s1;
  227. long int srcp2 = (long int) s2;
  228. op_t res;
  229. if (len >= OP_T_THRES)
  230. {
  231. /* There are at least some bytes to compare. No need to test
  232. for LEN == 0 in this alignment loop. */
  233. while (srcp2 % OPSIZ != 0)
  234. {
  235. a0 = ((byte *) srcp1)[0];
  236. b0 = ((byte *) srcp2)[0];
  237. srcp1 += 1;
  238. srcp2 += 1;
  239. res = a0 - b0;
  240. if (res != 0)
  241. return res;
  242. len -= 1;
  243. }
  244. /* SRCP2 is now aligned for memory operations on `op_t'.
  245. SRCP1 alignment determines if we can do a simple,
  246. aligned compare or need to shuffle bits. */
  247. if (srcp1 % OPSIZ == 0)
  248. res = memcmp_common_alignment (srcp1, srcp2, len / OPSIZ);
  249. else
  250. res = memcmp_not_common_alignment (srcp1, srcp2, len / OPSIZ);
  251. if (res != 0)
  252. return res;
  253. /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
  254. srcp1 += len & -OPSIZ;
  255. srcp2 += len & -OPSIZ;
  256. len %= OPSIZ;
  257. }
  258. /* There are just a few bytes to compare. Use byte memory operations. */
  259. while (len != 0)
  260. {
  261. a0 = ((byte *) srcp1)[0];
  262. b0 = ((byte *) srcp2)[0];
  263. srcp1 += 1;
  264. srcp2 += 1;
  265. res = a0 - b0;
  266. if (res != 0)
  267. return res;
  268. len -= 1;
  269. }
  270. return 0;
  271. }
  272. libc_hidden_weak(memcmp)
  273. #ifdef __UCLIBC_SUSV3_LEGACY__
  274. strong_alias(memcmp,bcmp)
  275. #endif