<string.h>: Make strchrnul, strcasestr, memmem available by default
[platform/upstream/glibc.git] / string / strncmp.c
1 /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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
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.
13
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/>.  */
17
18 #include <stdint.h>
19 #include <string-fzb.h>
20 #include <string-fzc.h>
21 #include <string-fzi.h>
22 #include <string.h>
23 #include <sys/param.h>
24 #include <memcopy.h>
25
26 #undef strncmp
27
28 #ifndef STRNCMP
29 #define STRNCMP strncmp
30 #endif
31
32 static inline int
33 final_cmp (const op_t w1, const op_t w2, size_t n)
34 {
35   unsigned int idx = index_first_zero_ne (w1, w2);
36   if (n <= idx)
37     return 0;
38   return extractbyte (w1, idx) - extractbyte (w2, idx);
39 }
40
41 /* Aligned loop: if a difference is found, exit to compare the bytes.  Else
42    if a zero is found we have equal strings.  */
43 static inline int
44 strncmp_aligned_loop (const op_t *x1, const op_t *x2, op_t w1, size_t n)
45 {
46   op_t w2 = *x2++;
47
48   while (w1 == w2)
49     {
50       if (n <= sizeof (op_t))
51         break;
52       n -= sizeof (op_t);
53
54       if (has_zero (w1))
55         return 0;
56       w1 = *x1++;
57       w2 = *x2++;
58     }
59
60   return final_cmp (w1, w2, n);
61 }
62
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
66    final comparison.  */
67 static inline int
68 strncmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs,
69                         size_t n)
70 {
71   op_t w2a = *x2++;
72   uintptr_t sh_1 = ofs * CHAR_BIT;
73   uintptr_t sh_2 = sizeof(op_t) * CHAR_BIT - sh_1;
74
75   op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
76   if (!has_zero (w2) && n > (sizeof (op_t) - ofs))
77     {
78       op_t w2b;
79
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.  */
83       while (1)
84         {
85           w2b = *x2++;
86           w2 = MERGE (w2a, sh_1, w2b, sh_2);
87           if (n <= sizeof (op_t) || w1 != w2)
88             return final_cmp (w1, w2, n);
89           n -= sizeof(op_t);
90           if (has_zero (w2b) || n <= (sizeof (op_t) - ofs))
91             break;
92           w1 = *x1++;
93           w2a = w2b;
94         }
95
96       /* Zero found in the second partial of P2.  If we had EOS in the aligned
97          word, we have equality.  */
98       if (has_zero (w1))
99         return 0;
100
101       /* Load the final word of P1 and align the final partial of P2.  */
102       w1 = *x1++;
103       w2 = MERGE (w2b, sh_1, 0, sh_2);
104     }
105
106   return final_cmp (w1, w2, n);
107 }
108
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
112    greater than S2.  */
113 int
114 STRNCMP (const char *p1, const char *p2, size_t n)
115 {
116   /* Handle the unaligned bytes of p1 first.  */
117   uintptr_t a = MIN (-(uintptr_t)p1 % sizeof(op_t), n);
118   int diff = 0;
119   for (int i = 0; i < a; ++i)
120     {
121       unsigned char c1 = *p1++;
122       unsigned char c2 = *p2++;
123       diff = c1 - c2;
124       if (c1 == '\0' || diff != 0)
125         return diff;
126     }
127   if (a == n)
128     return 0;
129
130   /* P1 is now aligned to op_t.  P2 may or may not be.  */
131   const op_t *x1 = (const op_t *) p1;
132   op_t w1 = *x1++;
133   uintptr_t ofs = (uintptr_t) p2 % sizeof(op_t);
134   return ofs == 0
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);
137 }
138 libc_hidden_builtin_def (STRNCMP)