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>
26 # define strcmp STRCMP
30 final_cmp (const op_t w1
, const op_t w2
)
32 unsigned int idx
= index_first_zero_ne (w1
, w2
);
33 return extractbyte (w1
, idx
) - extractbyte (w2
, idx
);
36 /* Aligned loop: if a difference is found, exit to compare the bytes. Else
37 if a zero is found we have equal strings. */
39 strcmp_aligned_loop (const op_t
*x1
, const op_t
*x2
, op_t w1
)
51 return final_cmp (w1
, w2
);
54 /* Unaligned loop: align the first partial of P2, with 0xff for the rest of
55 the bytes so that we can also apply the has_zero test to see if we have
56 already reached EOS. If we have, then we can simply fall through to the
59 strcmp_unaligned_loop (const op_t
*x1
, const op_t
*x2
, op_t w1
, uintptr_t ofs
)
62 uintptr_t sh_1
= ofs
* CHAR_BIT
;
63 uintptr_t sh_2
= sizeof(op_t
) * CHAR_BIT
- sh_1
;
65 op_t w2
= MERGE (w2a
, sh_1
, (op_t
)-1, sh_2
);
70 /* Unaligned loop. The invariant is that W2B, which is "ahead" of W1,
71 does not contain end-of-string. Therefore it is safe (and necessary)
72 to read another word from each while we do not have a difference. */
76 w2
= MERGE (w2a
, sh_1
, w2b
, sh_2
);
78 return final_cmp (w1
, w2
);
85 /* Zero found in the second partial of P2. If we had EOS in the aligned
86 word, we have equality. */
90 /* Load the final word of P1 and align the final partial of P2. */
92 w2
= MERGE (w2b
, sh_1
, 0, sh_2
);
95 return final_cmp (w1
, w2
);
98 /* Compare S1 and S2, returning less than, equal to or
99 greater than zero if S1 is lexicographically less than,
100 equal to or greater than S2. */
102 strcmp (const char *p1
, const char *p2
)
104 /* Handle the unaligned bytes of p1 first. */
105 uintptr_t n
= -(uintptr_t)p1
% sizeof(op_t
);
106 for (int i
= 0; i
< n
; ++i
)
108 unsigned char c1
= *p1
++;
109 unsigned char c2
= *p2
++;
111 if (c1
== '\0' || diff
!= 0)
115 /* P1 is now aligned to op_t. P2 may or may not be. */
116 const op_t
*x1
= (const op_t
*) p1
;
118 uintptr_t ofs
= (uintptr_t) p2
% sizeof(op_t
);
120 ? strcmp_aligned_loop (x1
, (const op_t
*)p2
, w1
)
121 : strcmp_unaligned_loop (x1
, (const op_t
*)(p2
- ofs
), w1
, ofs
);
124 libc_hidden_builtin_def (strcmp
)