1 /* Copyright (C) 1996-2022 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 #ifndef _WEIGHTWC_H_
19 #define _WEIGHTWC_H_	1
20 
21 #include <libc-diag.h>
22 
23 /* Find index of weight.  */
24 static inline int32_t __attribute__ ((always_inline))
findidx(const int32_t * table,const int32_t * indirect,const wint_t * extra,const wint_t ** cpp,size_t len)25 findidx (const int32_t *table,
26 	 const int32_t *indirect,
27 	 const wint_t *extra,
28 	 const wint_t **cpp, size_t len)
29 {
30   /* With GCC 7 when compiling with -Os the compiler warns that
31      seq1.back_us and seq2.back_us, which become *cpp, might be used
32      uninitialized.  This is impossible as this function cannot be
33      called except in cases where those fields have been
34      initialized.  */
35   DIAG_PUSH_NEEDS_COMMENT;
36   DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
37   wint_t ch = *(*cpp)++;
38   DIAG_POP_NEEDS_COMMENT;
39   int32_t i = __collidx_table_lookup ((const char *) table, ch);
40 
41   if (i >= 0)
42     /* This is an index into the weight table.  Cool.  */
43     return i;
44 
45   /* Oh well, more than one sequence starting with this byte.
46      Search for the correct one.  */
47   const int32_t *cp = (const int32_t *) &extra[-i];
48   --len;
49   while (1)
50     {
51       size_t nhere;
52       const int32_t *usrc = (const int32_t *) *cpp;
53 
54       /* The first thing is the index.  */
55       i = *cp++;
56 
57       /* Next is the length of the byte sequence.  These are always
58 	 short byte sequences so there is no reason to call any
59 	 function (even if they are inlined).  */
60       nhere = *cp++;
61 
62       if (i >= 0)
63 	{
64 	  /* It is a single character.  If it matches we found our
65 	     index.  Note that at the end of each list there is an
66 	     entry of length zero which represents the single byte
67 	     sequence.  The first (and here only) byte was tested
68 	     already.  */
69 	  size_t cnt;
70 
71 	  /* With GCC 5.3 when compiling with -Os the compiler warns
72 	     that seq2.back_us, which becomes usrc, might be used
73 	     uninitialized.  This can't be true because we pass a length
74 	     of -1 for len at the same time which means that this loop
75 	     never executes.  */
76 	  DIAG_PUSH_NEEDS_COMMENT;
77 	  DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
78 	  for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
79 	    if (cp[cnt] != usrc[cnt])
80 	      break;
81 	  DIAG_POP_NEEDS_COMMENT;
82 
83 	  if (cnt == nhere)
84 	    {
85 	      /* Found it.  */
86 	      *cpp += nhere;
87 	      return i;
88 	    }
89 
90 	  /* Up to the next entry.  */
91 	  cp += nhere;
92 	}
93       else
94 	{
95 	  /* This is a range of characters.  First decide whether the
96 	     current byte sequence lies in the range.  */
97 	  size_t cnt;
98 	  size_t offset;
99 
100 	  /* With GCC 7 when compiling with -Os the compiler warns
101 	     that seq1.back_us and seq2.back_us, which become usrc,
102 	     might be used uninitialized.  This is impossible for the
103 	     same reason as described above.  */
104 	  DIAG_PUSH_NEEDS_COMMENT;
105 	  DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
106 	  for (cnt = 0; cnt < nhere - 1 && cnt < len; ++cnt)
107 	    if (cp[cnt] != usrc[cnt])
108 	      break;
109 	  DIAG_POP_NEEDS_COMMENT;
110 
111 	  if (cnt < nhere - 1 || cnt == len)
112 	    {
113 	      cp += 2 * nhere;
114 	      continue;
115 	    }
116 
117 	  /* With GCC 7 when compiling with -Os the compiler warns
118 	     that seq1.back_us and seq2.back_us, which become usrc,
119 	     might be used uninitialized.  This is impossible for the
120 	     same reason as described above.  */
121 	  DIAG_PUSH_NEEDS_COMMENT;
122 	  DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
123 	  if (cp[nhere - 1] > usrc[nhere - 1])
124 	    {
125 	      cp += 2 * nhere;
126 	      continue;
127 	    }
128 	  DIAG_POP_NEEDS_COMMENT;
129 
130 	  if (cp[2 * nhere - 1] < usrc[nhere - 1])
131 	    {
132 	      cp += 2 * nhere;
133 	      continue;
134 	    }
135 
136 	  /* This range matches the next characters.  Now find
137 	     the offset in the indirect table.  */
138 	  offset = usrc[nhere - 1] - cp[nhere - 1];
139 	  *cpp += nhere;
140 
141 	  return indirect[-i + offset];
142 	}
143     }
144 
145   /* NOTREACHED */
146   return 0x43219876;
147 }
148 
149 #endif	/* weightwc.h */
150