/src/htslib/htscodecs/htscodecs/varint.h
Line | Count | Source (jump to first uncovered line) |
1 | | // FIXME: make get functions const uint8_t * |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2019-2021 Genome Research Ltd. |
5 | | * Author(s): James Bonfield |
6 | | * |
7 | | * Redistribution and use in source and binary forms, with or without |
8 | | * modification, are permitted provided that the following conditions are met: |
9 | | * |
10 | | * 1. Redistributions of source code must retain the above copyright notice, |
11 | | * this list of conditions and the following disclaimer. |
12 | | * |
13 | | * 2. Redistributions in binary form must reproduce the above |
14 | | * copyright notice, this list of conditions and the following |
15 | | * disclaimer in the documentation and/or other materials provided |
16 | | * with the distribution. |
17 | | * |
18 | | * 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger |
19 | | * Institute nor the names of its contributors may be used to endorse |
20 | | * or promote products derived from this software without specific |
21 | | * prior written permission. |
22 | | * |
23 | | * THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS |
24 | | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
25 | | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
26 | | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH |
27 | | * LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
28 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
29 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
30 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
31 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
32 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
33 | | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
34 | | */ |
35 | | |
36 | | #ifndef VARINT_H |
37 | | #define VARINT_H |
38 | | |
39 | | #include <stdint.h> |
40 | | |
41 | | #ifdef VARINT2 |
42 | | #include "varint2.h" |
43 | | #else |
44 | | |
45 | | // General API scheme is var_{get,put}_{s,u}{32,64} |
46 | | // s/u for signed/unsigned; 32/64 for integer size. |
47 | | |
48 | | // FIXME: consider returning the value and having nbytes passed in by |
49 | | // reference instead of vice-versa. |
50 | | // |
51 | | // ie uint64_t var_get_u64(uint8_t *cp, int *nbytes) |
52 | | // vs int var_get_u64(uint8_t *cp, uint64_t *val) |
53 | | // |
54 | | // The return value can then be assigned to 32-bit or 64-bit type |
55 | | // without need of a new function name. The cost is we can't then |
56 | | // do "cp += var_get_u32(cp, endp, &u_freq_sz);". Maybe we can't do |
57 | | // overflow detection with former? (Want 32-bit but got, say, 40 bit) |
58 | | |
59 | | |
60 | | // Big endian. |
61 | | // Harder for encoding, but a simpler and faster decoder. |
62 | | #define BIG_END |
63 | | #ifdef BIG_END |
64 | | |
65 | | static inline |
66 | 0 | int var_put_u64_safe(uint8_t *cp, const uint8_t *endp, uint64_t i) { |
67 | 0 | uint8_t *op = cp; |
68 | 0 | int s = 0; |
69 | 0 | uint64_t X = i; |
70 | | |
71 | | // safe method when we're near end of buffer |
72 | 0 | do { |
73 | 0 | s += 7; |
74 | 0 | X >>= 7; |
75 | 0 | } while (X); |
76 | |
|
77 | 0 | if (endp && (endp-cp)*7 < s) |
78 | 0 | return 0; |
79 | | |
80 | 0 | int n; |
81 | 0 | for (n = 0; n < 10; n++) { |
82 | 0 | s -= 7; |
83 | 0 | *cp++ = ((i>>s) & 0x7f) + (s?128:0); |
84 | 0 | if (!s) |
85 | 0 | break; |
86 | 0 | } |
87 | |
|
88 | 0 | return cp-op; |
89 | 0 | } Unexecuted instantiation: cram_io.c:var_put_u64_safe Unexecuted instantiation: arith_dynamic.c:var_put_u64_safe Unexecuted instantiation: fqzcomp_qual.c:var_put_u64_safe Unexecuted instantiation: rANS_static4x16pr.c:var_put_u64_safe Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_put_u64_safe Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_put_u64_safe Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_put_u64_safe Unexecuted instantiation: rANS_static32x16pr.c:var_put_u64_safe Unexecuted instantiation: rle.c:var_put_u64_safe Unexecuted instantiation: tokenise_name3.c:var_put_u64_safe Unexecuted instantiation: cram_codecs.c:var_put_u64_safe |
90 | | |
91 | | // This can be optimised further with __builtin_clzl(i) and goto various |
92 | | // bits of the if/else-if structure, but it's not a vast improvement and |
93 | | // we are dominated by small values. Simplicity wins for now |
94 | | static inline |
95 | 0 | int var_put_u64(uint8_t *cp, const uint8_t *endp, uint64_t i) { |
96 | 0 | if (endp && (endp-cp) < 10) |
97 | 0 | return var_put_u64_safe(cp, endp, i); |
98 | | |
99 | | // maximum of 10 bytes written |
100 | 0 | if (i < (1<<7)) { |
101 | 0 | *cp = i; |
102 | 0 | return 1; |
103 | 0 | } else if (i < (1<<14)) { |
104 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
105 | 0 | *cp++ = i & 0x7f; |
106 | 0 | return 2; |
107 | 0 | } else if (i < (1<<21)) { |
108 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
109 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
110 | 0 | *cp++ = i & 0x7f; |
111 | 0 | return 3; |
112 | 0 | } else if (i < (1<<28)) { |
113 | 0 | *cp++ = ((i>>21) & 0x7f) | 128; |
114 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
115 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
116 | 0 | *cp++ = i & 0x7f; |
117 | 0 | return 4; |
118 | 0 | } else if (i < (1LL<<35)) { |
119 | 0 | *cp++ = ((i>>28) & 0x7f) | 128; |
120 | 0 | *cp++ = ((i>>21) & 0x7f) | 128; |
121 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
122 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
123 | 0 | *cp++ = i & 0x7f; |
124 | 0 | return 5; |
125 | 0 | } else if (i < (1LL<<42)) { |
126 | 0 | *cp++ = ((i>>35) & 0x7f) | 128; |
127 | 0 | *cp++ = ((i>>28) & 0x7f) | 128; |
128 | 0 | *cp++ = ((i>>21) & 0x7f) | 128; |
129 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
130 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
131 | 0 | *cp++ = i & 0x7f; |
132 | 0 | return 6; |
133 | 0 | } else if (i < (1LL<<49)) { |
134 | 0 | *cp++ = ((i>>42) & 0x7f) | 128; |
135 | 0 | *cp++ = ((i>>35) & 0x7f) | 128; |
136 | 0 | *cp++ = ((i>>28) & 0x7f) | 128; |
137 | 0 | *cp++ = ((i>>21) & 0x7f) | 128; |
138 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
139 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
140 | 0 | *cp++ = i & 0x7f; |
141 | 0 | return 7; |
142 | 0 | } else if (i < (1LL<<56)) { |
143 | 0 | *cp++ = ((i>>49) & 0x7f) | 128; |
144 | 0 | *cp++ = ((i>>42) & 0x7f) | 128; |
145 | 0 | *cp++ = ((i>>35) & 0x7f) | 128; |
146 | 0 | *cp++ = ((i>>28) & 0x7f) | 128; |
147 | 0 | *cp++ = ((i>>21) & 0x7f) | 128; |
148 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
149 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
150 | 0 | *cp++ = i & 0x7f; |
151 | 0 | return 8; |
152 | 0 | } else if (i < (1LL<<63)) { |
153 | 0 | *cp++ = ((i>>56) & 0x7f) | 128; |
154 | 0 | *cp++ = ((i>>49) & 0x7f) | 128; |
155 | 0 | *cp++ = ((i>>42) & 0x7f) | 128; |
156 | 0 | *cp++ = ((i>>35) & 0x7f) | 128; |
157 | 0 | *cp++ = ((i>>28) & 0x7f) | 128; |
158 | 0 | *cp++ = ((i>>21) & 0x7f) | 128; |
159 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
160 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
161 | 0 | *cp++ = i & 0x7f; |
162 | 0 | return 9; |
163 | 0 | } else { |
164 | 0 | *cp++ = ((i>>63) & 0x7f) | 128; |
165 | 0 | *cp++ = ((i>>56) & 0x7f) | 128; |
166 | 0 | *cp++ = ((i>>49) & 0x7f) | 128; |
167 | 0 | *cp++ = ((i>>42) & 0x7f) | 128; |
168 | 0 | *cp++ = ((i>>35) & 0x7f) | 128; |
169 | 0 | *cp++ = ((i>>28) & 0x7f) | 128; |
170 | 0 | *cp++ = ((i>>21) & 0x7f) | 128; |
171 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
172 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
173 | 0 | *cp++ = i & 0x7f; |
174 | 0 | } |
175 | | |
176 | 0 | return 10; |
177 | 0 | } Unexecuted instantiation: cram_io.c:var_put_u64 Unexecuted instantiation: arith_dynamic.c:var_put_u64 Unexecuted instantiation: fqzcomp_qual.c:var_put_u64 Unexecuted instantiation: rANS_static4x16pr.c:var_put_u64 Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_put_u64 Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_put_u64 Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_put_u64 Unexecuted instantiation: rANS_static32x16pr.c:var_put_u64 Unexecuted instantiation: rle.c:var_put_u64 Unexecuted instantiation: tokenise_name3.c:var_put_u64 Unexecuted instantiation: cram_codecs.c:var_put_u64 |
178 | | |
179 | | static inline |
180 | 0 | int var_put_u32_safe(uint8_t *cp, const uint8_t *endp, uint32_t i) { |
181 | 0 | uint8_t *op = cp; |
182 | 0 | int s = 0; |
183 | 0 | uint32_t X = i; |
184 | | |
185 | | // safe method when we're near end of buffer |
186 | 0 | do { |
187 | 0 | s += 7; |
188 | 0 | X >>= 7; |
189 | 0 | } while (X); |
190 | |
|
191 | 0 | if (endp && (endp-cp)*7 < s) |
192 | 0 | return 0; |
193 | | |
194 | 0 | int n; |
195 | 0 | for (n = 0; n < 5; n++) { |
196 | 0 | s -= 7; |
197 | 0 | *cp++ = ((i>>s) & 0x7f) + (s?128:0); |
198 | 0 | if (!s) |
199 | 0 | break; |
200 | 0 | } |
201 | |
|
202 | 0 | return cp-op; |
203 | 0 | } Unexecuted instantiation: cram_io.c:var_put_u32_safe Unexecuted instantiation: arith_dynamic.c:var_put_u32_safe Unexecuted instantiation: fqzcomp_qual.c:var_put_u32_safe Unexecuted instantiation: rANS_static4x16pr.c:var_put_u32_safe Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_put_u32_safe Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_put_u32_safe Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_put_u32_safe Unexecuted instantiation: rANS_static32x16pr.c:var_put_u32_safe Unexecuted instantiation: rle.c:var_put_u32_safe Unexecuted instantiation: tokenise_name3.c:var_put_u32_safe Unexecuted instantiation: cram_codecs.c:var_put_u32_safe |
204 | | |
205 | | static inline |
206 | 0 | int var_put_u32(uint8_t *cp, const uint8_t *endp, uint32_t i) { |
207 | 0 | if (endp && (endp-cp) < 5) |
208 | 0 | return var_put_u32_safe(cp, endp, i); |
209 | | |
210 | 0 | if (i < (1<<7)) { |
211 | 0 | *cp = i; |
212 | 0 | return 1; |
213 | 0 | } else if (i < (1<<14)) { |
214 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
215 | 0 | *cp++ = i & 0x7f; |
216 | 0 | return 2; |
217 | 0 | } else if (i < (1<<21)) { |
218 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
219 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
220 | 0 | *cp++ = i & 0x7f; |
221 | 0 | return 3; |
222 | 0 | } else if (i < (1<<28)) { |
223 | 0 | *cp++ = ((i>>21) & 0x7f) | 128; |
224 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
225 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
226 | 0 | *cp++ = i & 0x7f; |
227 | 0 | return 4; |
228 | 0 | } else { |
229 | 0 | *cp++ = ((i>>28) & 0x7f) | 128; |
230 | 0 | *cp++ = ((i>>21) & 0x7f) | 128; |
231 | 0 | *cp++ = ((i>>14) & 0x7f) | 128; |
232 | 0 | *cp++ = ((i>> 7) & 0x7f) | 128; |
233 | 0 | *cp++ = i & 0x7f; |
234 | 0 | } |
235 | | |
236 | 0 | return 5; |
237 | 0 | } Unexecuted instantiation: cram_io.c:var_put_u32 Unexecuted instantiation: arith_dynamic.c:var_put_u32 Unexecuted instantiation: fqzcomp_qual.c:var_put_u32 Unexecuted instantiation: rANS_static4x16pr.c:var_put_u32 Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_put_u32 Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_put_u32 Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_put_u32 Unexecuted instantiation: rANS_static32x16pr.c:var_put_u32 Unexecuted instantiation: rle.c:var_put_u32 Unexecuted instantiation: tokenise_name3.c:var_put_u32 Unexecuted instantiation: cram_codecs.c:var_put_u32 |
238 | | |
239 | | static inline |
240 | 0 | int var_get_u64(uint8_t *cp, const uint8_t *endp, uint64_t *i) { |
241 | 0 | uint8_t *op = cp, c; |
242 | 0 | uint64_t j = 0; |
243 | |
|
244 | 0 | if (!endp || endp - cp >= 10) { |
245 | 0 | int n = 10; |
246 | 0 | do { |
247 | 0 | c = *cp++; |
248 | 0 | j = (j<<7) | (c & 0x7f); |
249 | 0 | } while ((c & 0x80) && n-- > 0); |
250 | 0 | } else { |
251 | 0 | if (cp >= endp) { |
252 | 0 | *i = 0; |
253 | 0 | return 0; |
254 | 0 | } |
255 | | |
256 | 0 | do { |
257 | 0 | c = *cp++; |
258 | 0 | j = (j<<7) | (c & 0x7f); |
259 | 0 | } while ((c & 0x80) && cp < endp); |
260 | 0 | } |
261 | | |
262 | 0 | *i = j; |
263 | 0 | return cp-op; |
264 | 0 | } Unexecuted instantiation: cram_io.c:var_get_u64 Unexecuted instantiation: arith_dynamic.c:var_get_u64 Unexecuted instantiation: fqzcomp_qual.c:var_get_u64 Unexecuted instantiation: rANS_static4x16pr.c:var_get_u64 Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_get_u64 Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_get_u64 Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_get_u64 Unexecuted instantiation: rANS_static32x16pr.c:var_get_u64 Unexecuted instantiation: rle.c:var_get_u64 Unexecuted instantiation: tokenise_name3.c:var_get_u64 Unexecuted instantiation: cram_codecs.c:var_get_u64 |
265 | | |
266 | | static inline |
267 | 63.6k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { |
268 | 63.6k | uint8_t *op = cp, c; |
269 | 63.6k | uint32_t j = 0; |
270 | | |
271 | 63.6k | if (!endp || endp - cp >= 6) { |
272 | | // Known maximum loop count helps optimiser. |
273 | | // NB: this helps considerably at -O3 level, but may harm -O2. |
274 | | // (However we optimise for those that want optimal code.) |
275 | 63.3k | int n = 5; |
276 | 80.3k | do { |
277 | 80.3k | c = *cp++; |
278 | 80.3k | j = (j<<7) | (c & 0x7f); |
279 | 80.3k | } while ((c & 0x80) && n-- > 0); |
280 | 63.3k | } else { |
281 | 302 | if (cp >= endp) { |
282 | 226 | *i = 0; |
283 | 226 | return 0; |
284 | 226 | } |
285 | | |
286 | 76 | if (*cp < 128) { |
287 | 70 | *i = *cp; |
288 | 70 | return 1; |
289 | 70 | } |
290 | | |
291 | 8 | do { |
292 | 8 | c = *cp++; |
293 | 8 | j = (j<<7) | (c & 0x7f); |
294 | 8 | } while ((c & 0x80) && cp < endp); |
295 | 6 | } |
296 | | |
297 | 63.3k | *i = j; |
298 | 63.3k | return cp-op; |
299 | 63.6k | } Unexecuted instantiation: cram_io.c:var_get_u32 arith_dynamic.c:var_get_u32 Line | Count | Source | 267 | 4.74k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 4.74k | uint8_t *op = cp, c; | 269 | 4.74k | uint32_t j = 0; | 270 | | | 271 | 4.74k | if (!endp || endp - cp >= 6) { | 272 | | // Known maximum loop count helps optimiser. | 273 | | // NB: this helps considerably at -O3 level, but may harm -O2. | 274 | | // (However we optimise for those that want optimal code.) | 275 | 4.71k | int n = 5; | 276 | 5.46k | do { | 277 | 5.46k | c = *cp++; | 278 | 5.46k | j = (j<<7) | (c & 0x7f); | 279 | 5.46k | } while ((c & 0x80) && n-- > 0); | 280 | 4.71k | } else { | 281 | 30 | if (cp >= endp) { | 282 | 21 | *i = 0; | 283 | 21 | return 0; | 284 | 21 | } | 285 | | | 286 | 9 | if (*cp < 128) { | 287 | 7 | *i = *cp; | 288 | 7 | return 1; | 289 | 7 | } | 290 | | | 291 | 2 | do { | 292 | 2 | c = *cp++; | 293 | 2 | j = (j<<7) | (c & 0x7f); | 294 | 2 | } while ((c & 0x80) && cp < endp); | 295 | 2 | } | 296 | | | 297 | 4.71k | *i = j; | 298 | 4.71k | return cp-op; | 299 | 4.74k | } |
fqzcomp_qual.c:var_get_u32 Line | Count | Source | 267 | 82 | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 82 | uint8_t *op = cp, c; | 269 | 82 | uint32_t j = 0; | 270 | | | 271 | 82 | if (!endp || endp - cp >= 6) { | 272 | | // Known maximum loop count helps optimiser. | 273 | | // NB: this helps considerably at -O3 level, but may harm -O2. | 274 | | // (However we optimise for those that want optimal code.) | 275 | 82 | int n = 5; | 276 | 230 | do { | 277 | 230 | c = *cp++; | 278 | 230 | j = (j<<7) | (c & 0x7f); | 279 | 230 | } while ((c & 0x80) && n-- > 0); | 280 | 82 | } else { | 281 | 0 | if (cp >= endp) { | 282 | 0 | *i = 0; | 283 | 0 | return 0; | 284 | 0 | } | 285 | | | 286 | 0 | if (*cp < 128) { | 287 | 0 | *i = *cp; | 288 | 0 | return 1; | 289 | 0 | } | 290 | | | 291 | 0 | do { | 292 | 0 | c = *cp++; | 293 | 0 | j = (j<<7) | (c & 0x7f); | 294 | 0 | } while ((c & 0x80) && cp < endp); | 295 | 0 | } | 296 | | | 297 | 82 | *i = j; | 298 | 82 | return cp-op; | 299 | 82 | } |
rANS_static4x16pr.c:var_get_u32 Line | Count | Source | 267 | 13.3k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 13.3k | uint8_t *op = cp, c; | 269 | 13.3k | uint32_t j = 0; | 270 | | | 271 | 13.3k | if (!endp || endp - cp >= 6) { | 272 | | // Known maximum loop count helps optimiser. | 273 | | // NB: this helps considerably at -O3 level, but may harm -O2. | 274 | | // (However we optimise for those that want optimal code.) | 275 | 13.1k | int n = 5; | 276 | 17.3k | do { | 277 | 17.3k | c = *cp++; | 278 | 17.3k | j = (j<<7) | (c & 0x7f); | 279 | 17.3k | } while ((c & 0x80) && n-- > 0); | 280 | 13.1k | } else { | 281 | 191 | if (cp >= endp) { | 282 | 188 | *i = 0; | 283 | 188 | return 0; | 284 | 188 | } | 285 | | | 286 | 3 | if (*cp < 128) { | 287 | 3 | *i = *cp; | 288 | 3 | return 1; | 289 | 3 | } | 290 | | | 291 | 0 | do { | 292 | 0 | c = *cp++; | 293 | 0 | j = (j<<7) | (c & 0x7f); | 294 | 0 | } while ((c & 0x80) && cp < endp); | 295 | 0 | } | 296 | | | 297 | 13.1k | *i = j; | 298 | 13.1k | return cp-op; | 299 | 13.3k | } |
rANS_static32x16pr_avx2.c:var_get_u32 Line | Count | Source | 267 | 10.0k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 10.0k | uint8_t *op = cp, c; | 269 | 10.0k | uint32_t j = 0; | 270 | | | 271 | 10.0k | if (!endp || endp - cp >= 6) { | 272 | | // Known maximum loop count helps optimiser. | 273 | | // NB: this helps considerably at -O3 level, but may harm -O2. | 274 | | // (However we optimise for those that want optimal code.) | 275 | 10.0k | int n = 5; | 276 | 11.6k | do { | 277 | 11.6k | c = *cp++; | 278 | 11.6k | j = (j<<7) | (c & 0x7f); | 279 | 11.6k | } while ((c & 0x80) && n-- > 0); | 280 | 10.0k | } else { | 281 | 45 | if (cp >= endp) { | 282 | 0 | *i = 0; | 283 | 0 | return 0; | 284 | 0 | } | 285 | | | 286 | 45 | if (*cp < 128) { | 287 | 45 | *i = *cp; | 288 | 45 | return 1; | 289 | 45 | } | 290 | | | 291 | 0 | do { | 292 | 0 | c = *cp++; | 293 | 0 | j = (j<<7) | (c & 0x7f); | 294 | 0 | } while ((c & 0x80) && cp < endp); | 295 | 0 | } | 296 | | | 297 | 10.0k | *i = j; | 298 | 10.0k | return cp-op; | 299 | 10.0k | } |
Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_get_u32 Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_get_u32 Unexecuted instantiation: rANS_static32x16pr.c:var_get_u32 Line | Count | Source | 267 | 36 | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 36 | uint8_t *op = cp, c; | 269 | 36 | uint32_t j = 0; | 270 | | | 271 | 36 | if (!endp || endp - cp >= 6) { | 272 | | // Known maximum loop count helps optimiser. | 273 | | // NB: this helps considerably at -O3 level, but may harm -O2. | 274 | | // (However we optimise for those that want optimal code.) | 275 | 18 | int n = 5; | 276 | 18 | do { | 277 | 18 | c = *cp++; | 278 | 18 | j = (j<<7) | (c & 0x7f); | 279 | 18 | } while ((c & 0x80) && n-- > 0); | 280 | 18 | } else { | 281 | 18 | if (cp >= endp) { | 282 | 16 | *i = 0; | 283 | 16 | return 0; | 284 | 16 | } | 285 | | | 286 | 2 | if (*cp < 128) { | 287 | 0 | *i = *cp; | 288 | 0 | return 1; | 289 | 0 | } | 290 | | | 291 | 4 | do { | 292 | 4 | c = *cp++; | 293 | 4 | j = (j<<7) | (c & 0x7f); | 294 | 4 | } while ((c & 0x80) && cp < endp); | 295 | 2 | } | 296 | | | 297 | 20 | *i = j; | 298 | 20 | return cp-op; | 299 | 36 | } |
tokenise_name3.c:var_get_u32 Line | Count | Source | 267 | 35.3k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 35.3k | uint8_t *op = cp, c; | 269 | 35.3k | uint32_t j = 0; | 270 | | | 271 | 35.3k | if (!endp || endp - cp >= 6) { | 272 | | // Known maximum loop count helps optimiser. | 273 | | // NB: this helps considerably at -O3 level, but may harm -O2. | 274 | | // (However we optimise for those that want optimal code.) | 275 | 35.3k | int n = 5; | 276 | 45.6k | do { | 277 | 45.6k | c = *cp++; | 278 | 45.6k | j = (j<<7) | (c & 0x7f); | 279 | 45.6k | } while ((c & 0x80) && n-- > 0); | 280 | 35.3k | } else { | 281 | 18 | if (cp >= endp) { | 282 | 1 | *i = 0; | 283 | 1 | return 0; | 284 | 1 | } | 285 | | | 286 | 17 | if (*cp < 128) { | 287 | 15 | *i = *cp; | 288 | 15 | return 1; | 289 | 15 | } | 290 | | | 291 | 2 | do { | 292 | 2 | c = *cp++; | 293 | 2 | j = (j<<7) | (c & 0x7f); | 294 | 2 | } while ((c & 0x80) && cp < endp); | 295 | 2 | } | 296 | | | 297 | 35.3k | *i = j; | 298 | 35.3k | return cp-op; | 299 | 35.3k | } |
Unexecuted instantiation: cram_codecs.c:var_get_u32 |
300 | | |
301 | | //----------------------------------------------------------------------------- |
302 | | #else // BIG_END |
303 | | |
304 | | // Little endian 7-bit variable sized integer encoding. |
305 | | // The unsigned value is equivalent to LEB128 encoding. |
306 | | // For signed, see below. |
307 | | // This is also the Google Protocol Buffer and WebAssembly format. |
308 | | static inline int var_put_u64(uint8_t *cp, const uint8_t *endp, uint64_t i) { |
309 | | uint8_t *op = cp; |
310 | | |
311 | | if (!endp || (endp-cp)*7 >= 10) { |
312 | | // Unsafe or big-enough anyway |
313 | | do { |
314 | | *cp++ = (i&0x7f) + ((i>=0x80)<<7); |
315 | | i >>= 7; |
316 | | } while (i); |
317 | | } else if (cp < endp) { |
318 | | // End checked variant |
319 | | do { |
320 | | *cp++ = (i&0x7f) + ((i>=0x80)<<7); |
321 | | i >>= 7; |
322 | | } while (i && cp < endp); |
323 | | } |
324 | | |
325 | | return cp-op; |
326 | | } |
327 | | |
328 | | static inline int var_put_u32(uint8_t *cp, const uint8_t *endp, uint32_t i) { |
329 | | uint8_t *op = cp; |
330 | | |
331 | | if (!endp || (endp-cp)*7 >= 5) { |
332 | | // Unsafe or big-enough anyway |
333 | | do { |
334 | | *cp++ = (i&0x7f) + ((i>=0x80)<<7); |
335 | | i >>= 7; |
336 | | } while (i); |
337 | | } else if (cp < endp) { |
338 | | // End checked variant |
339 | | do { |
340 | | *cp++ = (i&0x7f) + ((i>=0x80)<<7); |
341 | | i >>= 7; |
342 | | } while (i && cp < endp); |
343 | | } |
344 | | |
345 | | return cp-op; |
346 | | } |
347 | | |
348 | | static inline int var_get_u64(uint8_t *cp, const uint8_t *endp, uint64_t *i) { |
349 | | uint8_t *op = cp, c; |
350 | | uint64_t j = 0, s = 0; |
351 | | |
352 | | if (endp) { |
353 | | // Safe variant |
354 | | if (cp >= endp) { |
355 | | *i = 0; |
356 | | return 0; |
357 | | } |
358 | | |
359 | | do { |
360 | | c = *cp++; |
361 | | j |= (c & 0x7f) << s; |
362 | | s += 7; |
363 | | } while ((c & 0x80) && cp < endp); |
364 | | } else { |
365 | | // Unsafe variant |
366 | | do { |
367 | | c = *cp++; |
368 | | j |= (c & 0x7f) << s; |
369 | | s += 7; |
370 | | } while ((c & 0x80)); |
371 | | } |
372 | | |
373 | | *i = j; |
374 | | return cp-op; |
375 | | } |
376 | | |
377 | | static inline int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { |
378 | | uint8_t *op = cp, c; |
379 | | uint32_t j = 0, s = 0; |
380 | | |
381 | | if (endp) { |
382 | | // Safe variant |
383 | | if (cp >= endp) { |
384 | | *i = 0; |
385 | | return 0; |
386 | | } |
387 | | |
388 | | do { |
389 | | c = *cp++; |
390 | | j |= (c & 0x7f) << s; |
391 | | s += 7; |
392 | | } while ((c & 0x80) && cp < endp); |
393 | | } else { |
394 | | // Unsafe variant |
395 | | do { |
396 | | c = *cp++; |
397 | | j |= (c & 0x7f) << s; |
398 | | s += 7; |
399 | | } while ((c & 0x80)); |
400 | | } |
401 | | |
402 | | *i = j; |
403 | | return cp-op; |
404 | | } |
405 | | #endif // BIG_END |
406 | | |
407 | | //----------------------------------------------------------------------------- |
408 | | // Signed versions of the above using zig-zag integer encoding. |
409 | | // This folds the sign bit into the bottom bit so we iterate |
410 | | // 0, -1, +1, -2, +2, etc. |
411 | 0 | static inline int var_put_s32(uint8_t *cp, const uint8_t *endp, int32_t i) { |
412 | 0 | return var_put_u32(cp, endp, ((uint32_t)i << 1) ^ (i >> 31)); |
413 | 0 | } Unexecuted instantiation: cram_io.c:var_put_s32 Unexecuted instantiation: arith_dynamic.c:var_put_s32 Unexecuted instantiation: fqzcomp_qual.c:var_put_s32 Unexecuted instantiation: rANS_static4x16pr.c:var_put_s32 Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_put_s32 Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_put_s32 Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_put_s32 Unexecuted instantiation: rANS_static32x16pr.c:var_put_s32 Unexecuted instantiation: rle.c:var_put_s32 Unexecuted instantiation: tokenise_name3.c:var_put_s32 Unexecuted instantiation: cram_codecs.c:var_put_s32 |
414 | 0 | static inline int var_put_s64(uint8_t *cp, const uint8_t *endp, int64_t i) { |
415 | 0 | return var_put_u64(cp, endp, ((uint64_t)i << 1) ^ (i >> 63)); |
416 | 0 | } Unexecuted instantiation: cram_io.c:var_put_s64 Unexecuted instantiation: arith_dynamic.c:var_put_s64 Unexecuted instantiation: fqzcomp_qual.c:var_put_s64 Unexecuted instantiation: rANS_static4x16pr.c:var_put_s64 Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_put_s64 Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_put_s64 Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_put_s64 Unexecuted instantiation: rANS_static32x16pr.c:var_put_s64 Unexecuted instantiation: rle.c:var_put_s64 Unexecuted instantiation: tokenise_name3.c:var_put_s64 Unexecuted instantiation: cram_codecs.c:var_put_s64 |
417 | | |
418 | 0 | static inline int var_get_s32(uint8_t *cp, const uint8_t *endp, int32_t *i) { |
419 | 0 | int b = var_get_u32(cp, endp, (uint32_t *)i); |
420 | 0 | *i = ((uint32_t)*i >> 1) ^ -(int32_t)(*i & 1); |
421 | 0 | return b; |
422 | 0 | } Unexecuted instantiation: cram_io.c:var_get_s32 Unexecuted instantiation: arith_dynamic.c:var_get_s32 Unexecuted instantiation: fqzcomp_qual.c:var_get_s32 Unexecuted instantiation: rANS_static4x16pr.c:var_get_s32 Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_get_s32 Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_get_s32 Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_get_s32 Unexecuted instantiation: rANS_static32x16pr.c:var_get_s32 Unexecuted instantiation: rle.c:var_get_s32 Unexecuted instantiation: tokenise_name3.c:var_get_s32 Unexecuted instantiation: cram_codecs.c:var_get_s32 |
423 | 0 | static inline int var_get_s64(uint8_t *cp, const uint8_t *endp, int64_t *i) { |
424 | 0 | int b = var_get_u64(cp, endp, (uint64_t *)i); |
425 | 0 | *i = ((uint64_t)*i >> 1) ^ -(int64_t)(*i & 1); |
426 | 0 | return b; |
427 | 0 | } Unexecuted instantiation: cram_io.c:var_get_s64 Unexecuted instantiation: arith_dynamic.c:var_get_s64 Unexecuted instantiation: fqzcomp_qual.c:var_get_s64 Unexecuted instantiation: rANS_static4x16pr.c:var_get_s64 Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_get_s64 Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_get_s64 Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_get_s64 Unexecuted instantiation: rANS_static32x16pr.c:var_get_s64 Unexecuted instantiation: rle.c:var_get_s64 Unexecuted instantiation: tokenise_name3.c:var_get_s64 Unexecuted instantiation: cram_codecs.c:var_get_s64 |
428 | | |
429 | 0 | static inline int var_size_u64(uint64_t v) { |
430 | 0 | int i = 0; |
431 | 0 | do { |
432 | 0 | i++; |
433 | 0 | v >>= 7; |
434 | 0 | } while (v); |
435 | 0 | return i; |
436 | 0 | } Unexecuted instantiation: cram_io.c:var_size_u64 Unexecuted instantiation: arith_dynamic.c:var_size_u64 Unexecuted instantiation: fqzcomp_qual.c:var_size_u64 Unexecuted instantiation: rANS_static4x16pr.c:var_size_u64 Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_size_u64 Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_size_u64 Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_size_u64 Unexecuted instantiation: rANS_static32x16pr.c:var_size_u64 Unexecuted instantiation: rle.c:var_size_u64 Unexecuted instantiation: tokenise_name3.c:var_size_u64 Unexecuted instantiation: cram_codecs.c:var_size_u64 |
437 | | #define var_size_u32 var_size_u64 |
438 | | |
439 | 0 | static inline int var_size_s64(int64_t v) { |
440 | 0 | return var_size_u64(((uint64_t)v << 1) ^ (v >> 63)); |
441 | 0 | } Unexecuted instantiation: cram_io.c:var_size_s64 Unexecuted instantiation: arith_dynamic.c:var_size_s64 Unexecuted instantiation: fqzcomp_qual.c:var_size_s64 Unexecuted instantiation: rANS_static4x16pr.c:var_size_s64 Unexecuted instantiation: rANS_static32x16pr_avx2.c:var_size_s64 Unexecuted instantiation: rANS_static32x16pr_avx512.c:var_size_s64 Unexecuted instantiation: rANS_static32x16pr_sse4.c:var_size_s64 Unexecuted instantiation: rANS_static32x16pr.c:var_size_s64 Unexecuted instantiation: rle.c:var_size_s64 Unexecuted instantiation: tokenise_name3.c:var_size_s64 Unexecuted instantiation: cram_codecs.c:var_size_s64 |
442 | | #define var_size_s32 var_size_s64 |
443 | | |
444 | | #endif /* VARINT2 */ |
445 | | |
446 | | #endif /* VARINT_H */ |