]>
Commit | Line | Data |
---|---|---|
bfff8b1b | 1 | /* Copyright (C) 1996-2017 Free Software Foundation, Inc. |
6d52618b | 2 | This file is part of the GNU C Library. |
5d08d218 | 3 | Written by Ulrich Drepper, <drepper@cygnus.com>. |
19bc17a9 | 4 | |
6d52618b | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
19bc17a9 | 9 | |
6d52618b UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
19bc17a9 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
19bc17a9 | 18 | |
8c0ab919 RM |
19 | #ifndef _WEIGHT_H_ |
20 | #define _WEIGHT_H_ 1 | |
21 | ||
9090848d | 22 | #include <libc-diag.h> |
bb5badf1 | 23 | |
450bf66e | 24 | /* Find index of weight. */ |
8c0ab919 RM |
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) | |
450bf66e UD |
30 | { |
31 | int_fast32_t i = table[*(*cpp)++]; | |
32 | const unsigned char *cp; | |
dbfdf944 | 33 | const unsigned char *usrc; |
19bc17a9 | 34 | |
450bf66e UD |
35 | if (i >= 0) |
36 | /* This is an index into the weight table. Cool. */ | |
37 | return i; | |
19bc17a9 | 38 | |
450bf66e UD |
39 | /* Oh well, more than one sequence starting with this byte. |
40 | Search for the correct one. */ | |
41 | cp = &extra[-i]; | |
dbfdf944 | 42 | usrc = *cpp; |
f3a6cc0a | 43 | --len; |
450bf66e | 44 | while (1) |
0393dfd6 | 45 | { |
450bf66e | 46 | size_t nhere; |
19bc17a9 | 47 | |
450bf66e | 48 | /* The first thing is the index. */ |
8b6e6767 | 49 | i = *((const int32_t *) cp); |
450bf66e | 50 | cp += sizeof (int32_t); |
19bc17a9 | 51 | |
450bf66e UD |
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++; | |
19bc17a9 | 56 | |
450bf66e | 57 | if (i >= 0) |
19bc17a9 | 58 | { |
450bf66e UD |
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; | |
19bc17a9 | 65 | |
93fe09cb CD |
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"); | |
f3a6cc0a | 73 | for (cnt = 0; cnt < nhere && cnt < len; ++cnt) |
450bf66e UD |
74 | if (cp[cnt] != usrc[cnt]) |
75 | break; | |
93fe09cb | 76 | DIAG_POP_NEEDS_COMMENT; |
19bc17a9 | 77 | |
450bf66e UD |
78 | if (cnt == nhere) |
79 | { | |
80 | /* Found it. */ | |
81 | *cpp += nhere; | |
82 | return i; | |
83 | } | |
19bc17a9 | 84 | |
450bf66e UD |
85 | /* Up to the next entry. */ |
86 | cp += nhere; | |
975569d0 JM |
87 | if (!LOCFILE_ALIGNED_P (1 + nhere)) |
88 | cp += LOCFILE_ALIGN - (1 + nhere) % LOCFILE_ALIGN; | |
19bc17a9 | 89 | } |
450bf66e UD |
90 | else |
91 | { | |
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; | |
19bc17a9 | 96 | |
f3a6cc0a | 97 | for (cnt = 0; cnt < nhere && cnt < len; ++cnt) |
450bf66e UD |
98 | if (cp[cnt] != usrc[cnt]) |
99 | break; | |
19bc17a9 | 100 | |
450bf66e UD |
101 | if (cnt != nhere) |
102 | { | |
f3a6cc0a | 103 | if (cnt == len || cp[cnt] > usrc[cnt]) |
450bf66e UD |
104 | { |
105 | /* Cannot be in this range. */ | |
106 | cp += 2 * nhere; | |
975569d0 JM |
107 | if (!LOCFILE_ALIGNED_P (1 + 2 * nhere)) |
108 | cp += (LOCFILE_ALIGN | |
109 | - (1 + 2 * nhere) % LOCFILE_ALIGN); | |
450bf66e UD |
110 | continue; |
111 | } | |
19bc17a9 | 112 | |
450bf66e UD |
113 | /* Test against the end of the range. */ |
114 | for (cnt = 0; cnt < nhere; ++cnt) | |
115 | if (cp[nhere + cnt] != usrc[cnt]) | |
116 | break; | |
19bc17a9 | 117 | |
450bf66e UD |
118 | if (cnt != nhere && cp[nhere + cnt] < usrc[cnt]) |
119 | { | |
120 | /* Cannot be in this range. */ | |
121 | cp += 2 * nhere; | |
975569d0 JM |
122 | if (!LOCFILE_ALIGNED_P (1 + 2 * nhere)) |
123 | cp += (LOCFILE_ALIGN | |
124 | - (1 + 2 * nhere) % LOCFILE_ALIGN); | |
450bf66e UD |
125 | continue; |
126 | } | |
19bc17a9 | 127 | |
450bf66e UD |
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); | |
131 | ||
132 | do | |
133 | { | |
134 | offset <<= 8; | |
135 | offset += usrc[cnt] - cp[cnt]; | |
136 | } | |
137 | while (++cnt < nhere); | |
19bc17a9 | 138 | } |
19bc17a9 | 139 | |
450bf66e | 140 | *cpp += nhere; |
dbfdf944 | 141 | return indirect[-i + offset]; |
450bf66e | 142 | } |
19bc17a9 | 143 | } |
19bc17a9 | 144 | |
450bf66e UD |
145 | /* NOTREACHED */ |
146 | return 0x43219876; | |
147 | } | |
8c0ab919 RM |
148 | |
149 | #endif /* weight.h */ |