1 /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 #include <string-fzb.h>
20 #include <string-fzc.h>
21 #include <string-fzi.h>
23 #include <sys/param.h>
29 #define STRNCMP strncmp
33 final_cmp (const op_t w1
, const op_t w2
, size_t n
)
35 unsigned int idx
= index_first_zero_ne (w1
, w2
);
38 return extractbyte (w1
, idx
) - extractbyte (w2
, idx
);
41 /* Aligned loop: if a difference is found, exit to compare the bytes. Else
42 if a zero is found we have equal strings. */
44 strncmp_aligned_loop (const op_t
*x1
, const op_t
*x2
, op_t w1
, size_t n
)
50 if (n
<= sizeof (op_t
))
60 return final_cmp (w1
, w2
, n
);
63 /* Unaligned loop: align the first partial of P2, with 0xff for the rest of
64 the bytes so that we can also apply the has_zero test to see if we have
65 already reached EOS. If we have, then we can simply fall through to the
68 strncmp_unaligned_loop (const op_t
*x1
, const op_t
*x2
, op_t w1
, uintptr_t ofs
,
72 uintptr_t sh_1
= ofs
* CHAR_BIT
;
73 uintptr_t sh_2
= sizeof(op_t
) * CHAR_BIT
- sh_1
;
75 op_t w2
= MERGE (w2a
, sh_1
, (op_t
)-1, sh_2
);
76 if (!has_zero (w2
) && n
> (sizeof (op_t
) - ofs
))
80 /* Unaligned loop. The invariant is that W2B, which is "ahead" of W1,
81 does not contain end-of-string. Therefore it is safe (and necessary)
82 to read another word from each while we do not have a difference. */
86 w2
= MERGE (w2a
, sh_1
, w2b
, sh_2
);
87 if (n
<= sizeof (op_t
) || w1
!= w2
)
88 return final_cmp (w1
, w2
, n
);
90 if (has_zero (w2b
) || n
<= (sizeof (op_t
) - ofs
))
96 /* Zero found in the second partial of P2. If we had EOS in the aligned
97 word, we have equality. */
101 /* Load the final word of P1 and align the final partial of P2. */
103 w2
= MERGE (w2b
, sh_1
, 0, sh_2
);
106 return final_cmp (w1
, w2
, n
);
109 /* Compare no more than N characters of S1 and S2,
110 returning less than, equal to or greater than zero
111 if S1 is lexicographically less than, equal to or
114 STRNCMP (const char *p1
, const char *p2
, size_t n
)
116 /* Handle the unaligned bytes of p1 first. */
117 uintptr_t a
= MIN (-(uintptr_t)p1
% sizeof(op_t
), n
);
119 for (int i
= 0; i
< a
; ++i
)
121 unsigned char c1
= *p1
++;
122 unsigned char c2
= *p2
++;
124 if (c1
== '\0' || diff
!= 0)
130 /* P1 is now aligned to op_t. P2 may or may not be. */
131 const op_t
*x1
= (const op_t
*) p1
;
133 uintptr_t ofs
= (uintptr_t) p2
% sizeof(op_t
);
135 ? strncmp_aligned_loop (x1
, (const op_t
*) p2
, w1
, n
- a
)
136 : strncmp_unaligned_loop (x1
, (const op_t
*) (p2
- ofs
), w1
, ofs
, n
- a
);
138 libc_hidden_builtin_def (STRNCMP
)