strcmp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <stdint.h>
  15. #include <string-fzb.h>
  16. #include <string-fzc.h>
  17. #include <string-fzi.h>
  18. #include <string.h>
  19. #include <memcopy.h>
  20. #ifdef STRCMP
  21. # define strcmp STRCMP
  22. #endif
  23. static inline int
  24. final_cmp (const op_t w1, const op_t w2)
  25. {
  26. unsigned int idx = index_first_zero_ne (w1, w2);
  27. return extractbyte (w1, idx) - extractbyte (w2, idx);
  28. }
  29. /* Aligned loop: if a difference is found, exit to compare the bytes. Else
  30. if a zero is found we have equal strings. */
  31. static inline int
  32. strcmp_aligned_loop (const op_t *x1, const op_t *x2, op_t w1)
  33. {
  34. op_t w2 = *x2++;
  35. while (w1 == w2)
  36. {
  37. if (has_zero (w1))
  38. return 0;
  39. w1 = *x1++;
  40. w2 = *x2++;
  41. }
  42. return final_cmp (w1, w2);
  43. }
  44. /* Unaligned loop: align the first partial of P2, with 0xff for the rest of
  45. the bytes so that we can also apply the has_zero test to see if we have
  46. already reached EOS. If we have, then we can simply fall through to the
  47. final comparison. */
  48. static inline int
  49. strcmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs)
  50. {
  51. op_t w2a = *x2++;
  52. uintptr_t sh_1 = ofs * CHAR_BIT;
  53. uintptr_t sh_2 = sizeof(op_t) * CHAR_BIT - sh_1;
  54. op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
  55. if (!has_zero (w2))
  56. {
  57. op_t w2b;
  58. /* Unaligned loop. The invariant is that W2B, which is "ahead" of W1,
  59. does not contain end-of-string. Therefore it is safe (and necessary)
  60. to read another word from each while we do not have a difference. */
  61. while (1)
  62. {
  63. w2b = *x2++;
  64. w2 = MERGE (w2a, sh_1, w2b, sh_2);
  65. if (w1 != w2)
  66. return final_cmp (w1, w2);
  67. if (has_zero (w2b))
  68. break;
  69. w1 = *x1++;
  70. w2a = w2b;
  71. }
  72. /* Zero found in the second partial of P2. If we had EOS in the aligned
  73. word, we have equality. */
  74. if (has_zero (w1))
  75. return 0;
  76. /* Load the final word of P1 and align the final partial of P2. */
  77. w1 = *x1++;
  78. w2 = MERGE (w2b, sh_1, 0, sh_2);
  79. }
  80. return final_cmp (w1, w2);
  81. }
  82. /* Compare S1 and S2, returning less than, equal to or
  83. greater than zero if S1 is lexicographically less than,
  84. equal to or greater than S2. */
  85. int
  86. strcmp (const char *p1, const char *p2)
  87. {
  88. /* Handle the unaligned bytes of p1 first. */
  89. uintptr_t n = -(uintptr_t)p1 % sizeof(op_t);
  90. for (int i = 0; i < n; ++i)
  91. {
  92. unsigned char c1 = *p1++;
  93. unsigned char c2 = *p2++;
  94. int diff = c1 - c2;
  95. if (c1 == '\0' || diff != 0)
  96. return diff;
  97. }
  98. /* P1 is now aligned to op_t. P2 may or may not be. */
  99. const op_t *x1 = (const op_t *) p1;
  100. op_t w1 = *x1++;
  101. uintptr_t ofs = (uintptr_t) p2 % sizeof(op_t);
  102. return ofs == 0
  103. ? strcmp_aligned_loop (x1, (const op_t *)p2, w1)
  104. : strcmp_unaligned_loop (x1, (const op_t *)(p2 - ofs), w1, ofs);
  105. }
  106. #ifndef STRCMP
  107. libc_hidden_builtin_def (strcmp)
  108. #endif