1 /* Zero byte detection; indexes. Generic C version.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 #define _STRING_FZC_H 1
23 #include <string-fza.h>
24 #include <string-fzi.h>
27 /* Given a word X that is known to contain a zero byte, return the index of
28 the first such within the word in memory order. */
29 static __always_inline
unsigned int
30 index_first_zero (op_t x
)
32 if (__BYTE_ORDER
== __LITTLE_ENDIAN
)
33 x
= find_zero_low (x
);
35 x
= find_zero_all (x
);
36 return index_first (x
);
39 /* Similarly, but perform the search for byte equality between X1 and X2. */
40 static __always_inline
unsigned int
41 index_first_eq (op_t x1
, op_t x2
)
43 if (__BYTE_ORDER
== __LITTLE_ENDIAN
)
44 x1
= find_eq_low (x1
, x2
);
46 x1
= find_eq_all (x1
, x2
);
47 return index_first (x1
);
50 /* Similarly, but perform the search for zero within X1 or equality between
52 static __always_inline
unsigned int
53 index_first_zero_eq (op_t x1
, op_t x2
)
55 if (__BYTE_ORDER
== __LITTLE_ENDIAN
)
56 x1
= find_zero_eq_low (x1
, x2
);
58 x1
= find_zero_eq_all (x1
, x2
);
59 return index_first (x1
);
62 /* Similarly, but perform the search for zero within X1 or inequality between
64 static __always_inline
unsigned int
65 index_first_zero_ne (op_t x1
, op_t x2
)
67 return index_first (find_zero_ne_all (x1
, x2
));
70 /* Similarly, but search for the last zero within X. */
71 static __always_inline
unsigned int
72 index_last_zero (op_t x
)
74 if (__BYTE_ORDER
== __LITTLE_ENDIAN
)
75 x
= find_zero_all (x
);
77 x
= find_zero_low (x
);
78 return index_last (x
);
81 static __always_inline
unsigned int
82 index_last_eq (op_t x1
, op_t x2
)
84 return index_last_zero (x1
^ x2
);
87 #endif /* STRING_FZC_H */