strncmp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <sys/param.h>
  20. #include <memcopy.h>
  21. #undef strncmp
  22. #ifndef STRNCMP
  23. #define STRNCMP strncmp
  24. #endif
  25. static inline int
  26. final_cmp (const op_t w1, const op_t w2, size_t n)
  27. {
  28. unsigned int idx = index_first_zero_ne (w1, w2);
  29. if (n <= idx)
  30. return 0;
  31. return extractbyte (w1, idx) - extractbyte (w2, idx);
  32. }
  33. /* Aligned loop: if a difference is found, exit to compare the bytes. Else
  34. if a zero is found we have equal strings. */
  35. static inline int
  36. strncmp_aligned_loop (const op_t *x1, const op_t *x2, op_t w1, size_t n)
  37. {
  38. op_t w2 = *x2++;
  39. while (w1 == w2)
  40. {
  41. if (n <= sizeof (op_t))
  42. break;
  43. n -= sizeof (op_t);
  44. if (has_zero (w1))
  45. return 0;
  46. w1 = *x1++;
  47. w2 = *x2++;
  48. }
  49. return final_cmp (w1, w2, n);
  50. }
  51. /* Unaligned loop: align the first partial of P2, with 0xff for the rest of
  52. the bytes so that we can also apply the has_zero test to see if we have
  53. already reached EOS. If we have, then we can simply fall through to the
  54. final comparison. */
  55. static inline int
  56. strncmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs,
  57. size_t n)
  58. {
  59. op_t w2a = *x2++;
  60. uintptr_t sh_1 = ofs * CHAR_BIT;
  61. uintptr_t sh_2 = sizeof(op_t) * CHAR_BIT - sh_1;
  62. op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
  63. if (!has_zero (w2) && n > (sizeof (op_t) - ofs))
  64. {
  65. op_t w2b;
  66. /* Unaligned loop. The invariant is that W2B, which is "ahead" of W1,
  67. does not contain end-of-string. Therefore it is safe (and necessary)
  68. to read another word from each while we do not have a difference. */
  69. while (1)
  70. {
  71. w2b = *x2++;
  72. w2 = MERGE (w2a, sh_1, w2b, sh_2);
  73. if (n <= sizeof (op_t) || w1 != w2)
  74. return final_cmp (w1, w2, n);
  75. n -= sizeof(op_t);
  76. if (has_zero (w2b) || n <= (sizeof (op_t) - ofs))
  77. break;
  78. w1 = *x1++;
  79. w2a = w2b;
  80. }
  81. /* Zero found in the second partial of P2. If we had EOS in the aligned
  82. word, we have equality. */
  83. if (has_zero (w1))
  84. return 0;
  85. /* Load the final word of P1 and align the final partial of P2. */
  86. w1 = *x1++;
  87. w2 = MERGE (w2b, sh_1, 0, sh_2);
  88. }
  89. return final_cmp (w1, w2, n);
  90. }
  91. /* Compare no more than N characters of S1 and S2,
  92. returning less than, equal to or greater than zero
  93. if S1 is lexicographically less than, equal to or
  94. greater than S2. */
  95. int
  96. STRNCMP (const char *p1, const char *p2, size_t n)
  97. {
  98. /* Handle the unaligned bytes of p1 first. */
  99. uintptr_t a = MIN (-(uintptr_t)p1 % sizeof(op_t), n);
  100. int diff = 0;
  101. for (int i = 0; i < a; ++i)
  102. {
  103. unsigned char c1 = *p1++;
  104. unsigned char c2 = *p2++;
  105. diff = c1 - c2;
  106. if (c1 == '\0' || diff != 0)
  107. return diff;
  108. }
  109. if (a == n)
  110. return 0;
  111. /* P1 is now aligned to op_t. P2 may or may not be. */
  112. const op_t *x1 = (const op_t *) p1;
  113. op_t w1 = *x1++;
  114. uintptr_t ofs = (uintptr_t) p2 % sizeof(op_t);
  115. return ofs == 0
  116. ? strncmp_aligned_loop (x1, (const op_t *) p2, w1, n - a)
  117. : strncmp_unaligned_loop (x1, (const op_t *) (p2 - ofs), w1, ofs, n - a);
  118. }
  119. libc_hidden_builtin_def (STRNCMP)