memcmp.c 8.6 KB

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