Add MAP_SHARED_VALIDATE from Linux 4.15.
[glibc.git] / locale / weight.h
blobe2fdae17136498459f896c94129bbc65ed133008
1 /* Copyright (C) 1996-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Ulrich Drepper, <drepper@cygnus.com>.
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/>. */
19 #ifndef _WEIGHT_H_
20 #define _WEIGHT_H_ 1
22 #include <libc-diag.h>
24 /* Find index of weight. */
25 static inline int32_t __attribute__ ((always_inline))
26 findidx (const int32_t *table,
27 const int32_t *indirect,
28 const unsigned char *extra,
29 const unsigned char **cpp, size_t len)
31 int_fast32_t i = table[*(*cpp)++];
32 const unsigned char *cp;
33 const unsigned char *usrc;
35 if (i >= 0)
36 /* This is an index into the weight table. Cool. */
37 return i;
39 /* Oh well, more than one sequence starting with this byte.
40 Search for the correct one. */
41 cp = &extra[-i];
42 usrc = *cpp;
43 --len;
44 while (1)
46 size_t nhere;
48 /* The first thing is the index. */
49 i = *((const int32_t *) cp);
50 cp += sizeof (int32_t);
52 /* Next is the length of the byte sequence. These are always
53 short byte sequences so there is no reason to call any
54 function (even if they are inlined). */
55 nhere = *cp++;
57 if (i >= 0)
59 /* It is a single character. If it matches we found our
60 index. Note that at the end of each list there is an
61 entry of length zero which represents the single byte
62 sequence. The first (and here only) byte was tested
63 already. */
64 size_t cnt;
66 /* With GCC 5.3 when compiling with -Os the compiler warns
67 that seq2.back_us, which becomes usrc, might be used
68 uninitialized. This can't be true because we pass a length
69 of -1 for len at the same time which means that this loop
70 never executes. */
71 DIAG_PUSH_NEEDS_COMMENT;
72 DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
73 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
74 if (cp[cnt] != usrc[cnt])
75 break;
76 DIAG_POP_NEEDS_COMMENT;
78 if (cnt == nhere)
80 /* Found it. */
81 *cpp += nhere;
82 return i;
85 /* Up to the next entry. */
86 cp += nhere;
87 if (!LOCFILE_ALIGNED_P (1 + nhere))
88 cp += LOCFILE_ALIGN - (1 + nhere) % LOCFILE_ALIGN;
90 else
92 /* This is a range of characters. First decide whether the
93 current byte sequence lies in the range. */
94 size_t cnt;
95 size_t offset = 0;
97 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
98 if (cp[cnt] != usrc[cnt])
99 break;
101 if (cnt != nhere)
103 if (cnt == len || cp[cnt] > usrc[cnt])
105 /* Cannot be in this range. */
106 cp += 2 * nhere;
107 if (!LOCFILE_ALIGNED_P (1 + 2 * nhere))
108 cp += (LOCFILE_ALIGN
109 - (1 + 2 * nhere) % LOCFILE_ALIGN);
110 continue;
113 /* Test against the end of the range. */
114 for (cnt = 0; cnt < nhere; ++cnt)
115 if (cp[nhere + cnt] != usrc[cnt])
116 break;
118 if (cnt != nhere && cp[nhere + cnt] < usrc[cnt])
120 /* Cannot be in this range. */
121 cp += 2 * nhere;
122 if (!LOCFILE_ALIGNED_P (1 + 2 * nhere))
123 cp += (LOCFILE_ALIGN
124 - (1 + 2 * nhere) % LOCFILE_ALIGN);
125 continue;
128 /* This range matches the next characters. Now find
129 the offset in the indirect table. */
130 for (cnt = 0; cp[cnt] == usrc[cnt]; ++cnt);
134 offset <<= 8;
135 offset += usrc[cnt] - cp[cnt];
137 while (++cnt < nhere);
140 *cpp += nhere;
141 return indirect[-i + offset];
145 /* NOTREACHED */
146 return 0x43219876;
149 #endif /* weight.h */