/src/htslib/htscodecs/htscodecs/varint.h
Line | Count | Source |
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 | 0 |
|
71 | 0 | // 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 | 0 |
|
77 | 0 | if (endp && (endp-cp)*7 < s) |
78 | 0 | return 0; |
79 | 0 |
|
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 | 0 |
|
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 | 0 |
|
99 | 0 | // 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 < (1ULL<<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 < (1ULL<<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 < (1ULL<<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 < (1ULL<<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 < (1ULL<<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 | 0 |
|
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 | 46.4M | int var_put_u32(uint8_t *cp, const uint8_t *endp, uint32_t i) { |
207 | 46.4M | if (endp && (endp-cp) < 5) |
208 | 0 | return var_put_u32_safe(cp, endp, i); |
209 | | |
210 | 46.4M | if (i < (1<<7)) { |
211 | 45.4M | *cp = i; |
212 | 45.4M | return 1; |
213 | 45.4M | } else if (i < (1<<14)) { |
214 | 911k | *cp++ = ((i>> 7) & 0x7f) | 128; |
215 | 911k | *cp++ = i & 0x7f; |
216 | 911k | return 2; |
217 | 911k | } else if (i < (1<<21)) { |
218 | 8.70k | *cp++ = ((i>>14) & 0x7f) | 128; |
219 | 8.70k | *cp++ = ((i>> 7) & 0x7f) | 128; |
220 | 8.70k | *cp++ = i & 0x7f; |
221 | 8.70k | return 3; |
222 | 8.70k | } else if (i < (1<<28)) { |
223 | 3.86k | *cp++ = ((i>>21) & 0x7f) | 128; |
224 | 3.86k | *cp++ = ((i>>14) & 0x7f) | 128; |
225 | 3.86k | *cp++ = ((i>> 7) & 0x7f) | 128; |
226 | 3.86k | *cp++ = i & 0x7f; |
227 | 3.86k | return 4; |
228 | 3.86k | } 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 | 46.4M | } Unexecuted instantiation: cram_io.c:var_put_u32 Unexecuted instantiation: arith_dynamic.c:var_put_u32 Unexecuted instantiation: fqzcomp_qual.c:var_put_u32 rANS_static4x16pr.c:var_put_u32 Line | Count | Source | 206 | 5.36M | int var_put_u32(uint8_t *cp, const uint8_t *endp, uint32_t i) { | 207 | 5.36M | if (endp && (endp-cp) < 5) | 208 | 0 | return var_put_u32_safe(cp, endp, i); | 209 | | | 210 | 5.36M | if (i < (1<<7)) { | 211 | 5.23M | *cp = i; | 212 | 5.23M | return 1; | 213 | 5.23M | } else if (i < (1<<14)) { | 214 | 122k | *cp++ = ((i>> 7) & 0x7f) | 128; | 215 | 122k | *cp++ = i & 0x7f; | 216 | 122k | return 2; | 217 | 122k | } else if (i < (1<<21)) { | 218 | 7.39k | *cp++ = ((i>>14) & 0x7f) | 128; | 219 | 7.39k | *cp++ = ((i>> 7) & 0x7f) | 128; | 220 | 7.39k | *cp++ = i & 0x7f; | 221 | 7.39k | return 3; | 222 | 7.39k | } else if (i < (1<<28)) { | 223 | 3.41k | *cp++ = ((i>>21) & 0x7f) | 128; | 224 | 3.41k | *cp++ = ((i>>14) & 0x7f) | 128; | 225 | 3.41k | *cp++ = ((i>> 7) & 0x7f) | 128; | 226 | 3.41k | *cp++ = i & 0x7f; | 227 | 3.41k | return 4; | 228 | 3.41k | } 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 | 5.36M | } |
rANS_static32x16pr_avx2.c:var_put_u32 Line | Count | Source | 206 | 220k | int var_put_u32(uint8_t *cp, const uint8_t *endp, uint32_t i) { | 207 | 220k | if (endp && (endp-cp) < 5) | 208 | 0 | return var_put_u32_safe(cp, endp, i); | 209 | | | 210 | 220k | if (i < (1<<7)) { | 211 | 199k | *cp = i; | 212 | 199k | return 1; | 213 | 199k | } else if (i < (1<<14)) { | 214 | 20.7k | *cp++ = ((i>> 7) & 0x7f) | 128; | 215 | 20.7k | *cp++ = i & 0x7f; | 216 | 20.7k | return 2; | 217 | 20.7k | } else if (i < (1<<21)) { | 218 | 3 | *cp++ = ((i>>14) & 0x7f) | 128; | 219 | 3 | *cp++ = ((i>> 7) & 0x7f) | 128; | 220 | 3 | *cp++ = i & 0x7f; | 221 | 3 | return 3; | 222 | 3 | } 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 | 220k | } |
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 Line | Count | Source | 206 | 40.6M | int var_put_u32(uint8_t *cp, const uint8_t *endp, uint32_t i) { | 207 | 40.6M | if (endp && (endp-cp) < 5) | 208 | 0 | return var_put_u32_safe(cp, endp, i); | 209 | | | 210 | 40.6M | if (i < (1<<7)) { | 211 | 39.9M | *cp = i; | 212 | 39.9M | return 1; | 213 | 39.9M | } else if (i < (1<<14)) { | 214 | 767k | *cp++ = ((i>> 7) & 0x7f) | 128; | 215 | 767k | *cp++ = i & 0x7f; | 216 | 767k | return 2; | 217 | 767k | } else if (i < (1<<21)) { | 218 | 1.30k | *cp++ = ((i>>14) & 0x7f) | 128; | 219 | 1.30k | *cp++ = ((i>> 7) & 0x7f) | 128; | 220 | 1.30k | *cp++ = i & 0x7f; | 221 | 1.30k | return 3; | 222 | 1.30k | } else if (i < (1<<28)) { | 223 | 451 | *cp++ = ((i>>21) & 0x7f) | 128; | 224 | 451 | *cp++ = ((i>>14) & 0x7f) | 128; | 225 | 451 | *cp++ = ((i>> 7) & 0x7f) | 128; | 226 | 451 | *cp++ = i & 0x7f; | 227 | 451 | return 4; | 228 | 451 | } 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 | 40.6M | } |
tokenise_name3.c:var_put_u32 Line | Count | Source | 206 | 131k | int var_put_u32(uint8_t *cp, const uint8_t *endp, uint32_t i) { | 207 | 131k | if (endp && (endp-cp) < 5) | 208 | 0 | return var_put_u32_safe(cp, endp, i); | 209 | | | 210 | 131k | if (i < (1<<7)) { | 211 | 130k | *cp = i; | 212 | 130k | return 1; | 213 | 130k | } else if (i < (1<<14)) { | 214 | 429 | *cp++ = ((i>> 7) & 0x7f) | 128; | 215 | 429 | *cp++ = i & 0x7f; | 216 | 429 | return 2; | 217 | 429 | } 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 | 131k | } |
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 | 0 |
|
244 | 0 | if (!endp || endp - cp >= 11) { |
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 | 0 |
|
256 | 0 | do { |
257 | 0 | c = *cp++; |
258 | 0 | j = (j<<7) | (c & 0x7f); |
259 | 0 | } while ((c & 0x80) && cp < endp); |
260 | 0 | } |
261 | 0 |
|
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 | 289k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { |
268 | 289k | uint8_t *op = cp, c; |
269 | 289k | uint32_t j = 0; |
270 | | |
271 | 289k | 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 | 243k | int n = 5; |
276 | 393k | do { |
277 | 393k | c = *cp++; |
278 | 393k | j = (j<<7) | (c & 0x7f); |
279 | 393k | } while ((c & 0x80) && n-- > 0); |
280 | 243k | } else { |
281 | 45.9k | if (cp >= endp) { |
282 | 41.6k | *i = 0; |
283 | 41.6k | return 0; |
284 | 41.6k | } |
285 | | |
286 | 4.27k | if (*cp < 128) { |
287 | 2.87k | *i = *cp; |
288 | 2.87k | return 1; |
289 | 2.87k | } |
290 | | |
291 | 3.43k | do { |
292 | 3.43k | c = *cp++; |
293 | 3.43k | j = (j<<7) | (c & 0x7f); |
294 | 3.43k | } while ((c & 0x80) && cp < endp); |
295 | 1.40k | } |
296 | | |
297 | 245k | *i = j; |
298 | 245k | return cp-op; |
299 | 289k | } Unexecuted instantiation: cram_io.c:var_get_u32 arith_dynamic.c:var_get_u32 Line | Count | Source | 267 | 24.6k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 24.6k | uint8_t *op = cp, c; | 269 | 24.6k | uint32_t j = 0; | 270 | | | 271 | 24.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 | 23.5k | int n = 5; | 276 | 36.4k | do { | 277 | 36.4k | c = *cp++; | 278 | 36.4k | j = (j<<7) | (c & 0x7f); | 279 | 36.4k | } while ((c & 0x80) && n-- > 0); | 280 | 23.5k | } else { | 281 | 1.09k | if (cp >= endp) { | 282 | 312 | *i = 0; | 283 | 312 | return 0; | 284 | 312 | } | 285 | | | 286 | 778 | if (*cp < 128) { | 287 | 423 | *i = *cp; | 288 | 423 | return 1; | 289 | 423 | } | 290 | | | 291 | 959 | do { | 292 | 959 | c = *cp++; | 293 | 959 | j = (j<<7) | (c & 0x7f); | 294 | 959 | } while ((c & 0x80) && cp < endp); | 295 | 355 | } | 296 | | | 297 | 23.9k | *i = j; | 298 | 23.9k | return cp-op; | 299 | 24.6k | } |
fqzcomp_qual.c:var_get_u32 Line | Count | Source | 267 | 781 | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 781 | uint8_t *op = cp, c; | 269 | 781 | uint32_t j = 0; | 270 | | | 271 | 781 | 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 | 760 | int n = 5; | 276 | 1.72k | do { | 277 | 1.72k | c = *cp++; | 278 | 1.72k | j = (j<<7) | (c & 0x7f); | 279 | 1.72k | } while ((c & 0x80) && n-- > 0); | 280 | 760 | } else { | 281 | 21 | if (cp >= endp) { | 282 | 5 | *i = 0; | 283 | 5 | return 0; | 284 | 5 | } | 285 | | | 286 | 16 | if (*cp < 128) { | 287 | 5 | *i = *cp; | 288 | 5 | return 1; | 289 | 5 | } | 290 | | | 291 | 41 | do { | 292 | 41 | c = *cp++; | 293 | 41 | j = (j<<7) | (c & 0x7f); | 294 | 41 | } while ((c & 0x80) && cp < endp); | 295 | 11 | } | 296 | | | 297 | 771 | *i = j; | 298 | 771 | return cp-op; | 299 | 781 | } |
rANS_static4x16pr.c:var_get_u32 Line | Count | Source | 267 | 41.8k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 41.8k | uint8_t *op = cp, c; | 269 | 41.8k | uint32_t j = 0; | 270 | | | 271 | 41.8k | 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 | 31.6k | int n = 5; | 276 | 78.6k | do { | 277 | 78.6k | c = *cp++; | 278 | 78.6k | j = (j<<7) | (c & 0x7f); | 279 | 78.6k | } while ((c & 0x80) && n-- > 0); | 280 | 31.6k | } else { | 281 | 10.2k | if (cp >= endp) { | 282 | 9.42k | *i = 0; | 283 | 9.42k | return 0; | 284 | 9.42k | } | 285 | | | 286 | 796 | if (*cp < 128) { | 287 | 469 | *i = *cp; | 288 | 469 | return 1; | 289 | 469 | } | 290 | | | 291 | 852 | do { | 292 | 852 | c = *cp++; | 293 | 852 | j = (j<<7) | (c & 0x7f); | 294 | 852 | } while ((c & 0x80) && cp < endp); | 295 | 327 | } | 296 | | | 297 | 31.9k | *i = j; | 298 | 31.9k | return cp-op; | 299 | 41.8k | } |
rANS_static32x16pr_avx2.c:var_get_u32 Line | Count | Source | 267 | 94.1k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 94.1k | uint8_t *op = cp, c; | 269 | 94.1k | uint32_t j = 0; | 270 | | | 271 | 94.1k | 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 | 85.0k | int n = 5; | 276 | 113k | do { | 277 | 113k | c = *cp++; | 278 | 113k | j = (j<<7) | (c & 0x7f); | 279 | 113k | } while ((c & 0x80) && n-- > 0); | 280 | 85.0k | } else { | 281 | 9.12k | if (cp >= endp) { | 282 | 7.38k | *i = 0; | 283 | 7.38k | return 0; | 284 | 7.38k | } | 285 | | | 286 | 1.74k | if (*cp < 128) { | 287 | 1.21k | *i = *cp; | 288 | 1.21k | return 1; | 289 | 1.21k | } | 290 | | | 291 | 1.05k | do { | 292 | 1.05k | c = *cp++; | 293 | 1.05k | j = (j<<7) | (c & 0x7f); | 294 | 1.05k | } while ((c & 0x80) && cp < endp); | 295 | 532 | } | 296 | | | 297 | 85.5k | *i = j; | 298 | 85.5k | return cp-op; | 299 | 94.1k | } |
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 | 29.6k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 29.6k | uint8_t *op = cp, c; | 269 | 29.6k | uint32_t j = 0; | 270 | | | 271 | 29.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 | 5.10k | int n = 5; | 276 | 7.10k | do { | 277 | 7.10k | c = *cp++; | 278 | 7.10k | j = (j<<7) | (c & 0x7f); | 279 | 7.10k | } while ((c & 0x80) && n-- > 0); | 280 | 24.5k | } else { | 281 | 24.5k | if (cp >= endp) { | 282 | 24.4k | *i = 0; | 283 | 24.4k | return 0; | 284 | 24.4k | } | 285 | | | 286 | 106 | if (*cp < 128) { | 287 | 70 | *i = *cp; | 288 | 70 | return 1; | 289 | 70 | } | 290 | | | 291 | 100 | do { | 292 | 100 | c = *cp++; | 293 | 100 | j = (j<<7) | (c & 0x7f); | 294 | 100 | } while ((c & 0x80) && cp < endp); | 295 | 36 | } | 296 | | | 297 | 5.13k | *i = j; | 298 | 5.13k | return cp-op; | 299 | 29.6k | } |
tokenise_name3.c:var_get_u32 Line | Count | Source | 267 | 98.6k | int var_get_u32(uint8_t *cp, const uint8_t *endp, uint32_t *i) { | 268 | 98.6k | uint8_t *op = cp, c; | 269 | 98.6k | uint32_t j = 0; | 270 | | | 271 | 98.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 | 97.6k | int n = 5; | 276 | 155k | do { | 277 | 155k | c = *cp++; | 278 | 155k | j = (j<<7) | (c & 0x7f); | 279 | 155k | } while ((c & 0x80) && n-- > 0); | 280 | 97.6k | } else { | 281 | 958 | if (cp >= endp) { | 282 | 121 | *i = 0; | 283 | 121 | return 0; | 284 | 121 | } | 285 | | | 286 | 837 | if (*cp < 128) { | 287 | 690 | *i = *cp; | 288 | 690 | return 1; | 289 | 690 | } | 290 | | | 291 | 428 | do { | 292 | 428 | c = *cp++; | 293 | 428 | j = (j<<7) | (c & 0x7f); | 294 | 428 | } while ((c & 0x80) && cp < endp); | 295 | 147 | } | 296 | | | 297 | 97.8k | *i = j; | 298 | 97.8k | return cp-op; | 299 | 98.6k | } |
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 */ |