1/*
2 * Copyright (c) 2015, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** \file
30 * \brief Functions for packing/unpacking arrays.
31 */
32
33#ifndef UTIL_PACK_BITS_H
34#define UTIL_PACK_BITS_H
35
36#include "ue2common.h"
37#include "unaligned.h"
38#include "partial_store.h"
39
40/**
41 * \brief Pack bits from an array of 32-bit words into \a out.
42 *
43 * \param out Output array. Must be large enough to store sum(bits).
44 * \param v Input array.
45 * \param bits Number of low bits in the corresponding element of \a v to pack.
46 * \param elements Size of the \a v and \a bits arrays.
47 */
48static really_inline
49void pack_bits_32(char *out, const u32 *v, const u32 *bits,
50 const unsigned int elements);
51
52/**
53 * \brief Pack bits from an array of 64-bit words into \a out.
54 *
55 * \param out Output array. Must be large enough to store sum(bits).
56 * \param v Input array.
57 * \param bits Number of low bits in the corresponding element of \a v to pack.
58 * \param elements Size of the \a v and \a bits arrays.
59 */
60static really_inline
61void pack_bits_64(char *out, const u64a *v, const u32 *bits,
62 const unsigned int elements);
63
64/**
65 * \brief Unpack bits into an array of 32-bit words according to the counts
66 * given.
67 *
68 * \param v Output array.
69 * \param in Packed input array.
70 * \param bits Number of bits to unpack into the corresponding element of \a v.
71 * \param elements Size of the \a v and \a bits arrays.
72 */
73static really_inline
74void unpack_bits_32(u32 *v, const u8 *in, const u32 *bits,
75 const unsigned int elements);
76
77/**
78 * \brief Unpack bits into an array of 64-bit words according to the counts
79 * given.
80 *
81 * \param v Output array.
82 * \param in Packed input array.
83 * \param bits Number of bits to unpack into the corresponding element of \a v.
84 * \param elements Size of the \a v and \a bits arrays.
85 */
86static really_inline
87void unpack_bits_64(u64a *v, const u8 *in, const u32 *bits,
88 const unsigned int elements);
89
90/*
91 * Inline implementations follow.
92 */
93
94static really_inline
95void pack_bits_32(char *out, const u32 *v, const u32 *bits,
96 const unsigned int elements) {
97 u32 write = 0; // accumulator
98 u32 idx = 0; // acc holds this many bits
99
100 for (unsigned int i = 0; i < elements; i++) {
101 assert(bits[i] <= 32);
102 write |= (v[i] << idx);
103 idx += bits[i];
104 if (idx >= 32) {
105 unaligned_store_u32(out, write);
106 out += 4;
107 idx -= 32;
108 u32 leftover = bits[i] - idx;
109 if (leftover == 32) {
110 write = 0;
111 } else {
112 assert(leftover < 32);
113 write = v[i] >> leftover;
114 }
115 }
116 }
117
118 // There might be a write left over.
119 partial_store_u32(out, write, (idx + 7) / 8);
120}
121
122static really_inline
123void pack_bits_64(char *out, const u64a *v, const u32 *bits,
124 const unsigned int elements) {
125 u64a write = 0; // accumulator
126 u32 idx = 0; // acc holds this many bits
127
128 for (unsigned int i = 0; i < elements; i++) {
129 assert(bits[i] <= 64);
130 write |= (v[i] << idx);
131 idx += bits[i];
132 if (idx >= 64) {
133 unaligned_store_u64a(out, write);
134 out += 8;
135 idx -= 64;
136 u32 leftover = bits[i] - idx;
137 if (leftover == 64) {
138 write = 0;
139 } else {
140 assert(leftover < 64);
141 write = v[i] >> leftover;
142 }
143 }
144 }
145
146 // There might be a write left over.
147 DEBUG_PRINTF("partial store of idx=%u\n", idx);
148 partial_store_u64a(out, write, (idx + 7) / 8);
149}
150
151static really_inline
152void unpack_bits_32(u32 *v, const u8 *in, const u32 *bits,
153 const unsigned int elements) {
154 u32 used = 0; // bits used from *in
155
156 for (unsigned int i = 0; i < elements; i++) {
157 assert(bits[i] <= 32);
158 u32 v_out = 0; // accumulator for v[i]
159 u32 b = bits[i]; // bits left to read for v[i]
160 u32 vidx = 0; // bits written to v[i]
161
162 while (b) {
163 u32 read = *in >> used;
164 u32 bits_read = 8 - used;
165
166 if (b <= bits_read) {
167 u32 mask = read & ((1U << b) - 1);
168 v_out |= mask << vidx;
169 vidx += b;
170 used += b;
171 b = 0;
172 if (used < 8) {
173 continue; // more from this *in
174 }
175 } else {
176 v_out |= read << vidx;
177 vidx += bits_read;
178 b -= bits_read;
179 }
180
181 used = 0;
182 in++;
183 }
184
185 v[i] = v_out;
186 }
187}
188
189static really_inline
190void unpack_bits_64(u64a *v, const u8 *in, const u32 *bits,
191 const unsigned int elements) {
192 u32 used = 0; // bits used from *in
193
194 for (unsigned int i = 0; i < elements; i++) {
195 assert(bits[i] <= 64);
196 u64a v_out = 0; // accumulator for v[i]
197 u32 b = bits[i]; // bits left to read for v[i]
198 u32 vidx = 0; // bits written to v[i]
199
200 while (b) {
201 u64a read = *in >> used;
202 u32 bits_read = 8 - used;
203
204 if (b <= bits_read) {
205 u64a mask = read & ((1U << b) - 1);
206 v_out |= mask << vidx;
207 vidx += b;
208 used += b;
209 b = 0;
210 if (used < 8) {
211 continue; // more from this *in
212 }
213 } else {
214 v_out |= read << vidx;
215 vidx += bits_read;
216 b -= bits_read;
217 }
218
219 used = 0;
220 in++;
221 }
222
223 v[i] = v_out;
224 }
225}
226
227#endif // UTIL_PACK_BITS_H
228