1 /*
2  * Copyright 2016 CodiLime
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include "data/repack.h"
18 
19 #include <cstdlib>
20 
21 #include "util/math.h"
22 
23 using veles::util::math::gcd;
24 
25 namespace veles {
26 namespace data {
27 
repackUnit() const28 unsigned Repacker::repackUnit() const {
29   unsigned res = paddedWidth() / gcd(paddedWidth(), from_width) * from_width;
30   // Ensure no overflow.
31   assert(res % paddedWidth() == 0 && res % from_width == 0);
32   return res;
33 }
34 
repackSize(size_t num_elements) const35 size_t Repacker::repackSize(size_t num_elements) const {
36   size_t bits = num_elements * paddedWidth();
37   assert(bits / paddedWidth() == num_elements);
38   return (bits + from_width - 1) / from_width;
39 }
40 
repackableSize(size_t src_size) const41 size_t Repacker::repackableSize(size_t src_size) const {
42   size_t bits = from_width * src_size;
43   assert(bits / from_width == src_size);
44   return bits / paddedWidth();
45 }
46 
repack(const BinData & src,size_t start,size_t num_elements) const47 BinData Repacker::repack(const BinData& src, size_t start,
48                          size_t num_elements) const {
49   unsigned repack_unit = repackUnit();
50   unsigned src_per_unit = repack_unit / from_width;
51   unsigned dst_per_unit = repack_unit / paddedWidth();
52   assert(src.width() == from_width);
53   BinData workspace(repack_unit, 1);
54   assert(start <= src.size());
55   num_elements = std::min(num_elements, repackableSize(src.size() - start));
56   BinData res(to_width, num_elements);
57   size_t src_end = start + repackSize(num_elements);
58   assert(src_end <= src.size());
59   for (size_t dst_pos = 0, src_pos = start; dst_pos < num_elements;) {
60     for (unsigned i = 0; i < src_per_unit && src_pos < src_end;
61          i++, src_pos++) {
62       unsigned work_pos;
63       switch (endian) {
64         case Endian::LITTLE:
65           work_pos = i * from_width;
66           break;
67         case Endian::BIG:
68           work_pos = (src_per_unit - i - 1) * from_width;
69           break;
70         default:
71           abort();
72       }
73       BinData::copyBits(workspace.rawData(), work_pos, src.rawData(src_pos), 0,
74                         from_width);
75     }
76     for (unsigned i = 0; i < dst_per_unit && dst_pos < num_elements;
77          i++, dst_pos++) {
78       unsigned work_pos;
79       switch (endian) {
80         case Endian::LITTLE:
81           work_pos = i * paddedWidth() + low_pad;
82           break;
83         case Endian::BIG:
84           work_pos = (dst_per_unit - i - 1) * paddedWidth() + low_pad;
85           break;
86         default:
87           abort();
88       }
89       BinData::copyBits(res.rawData(dst_pos), 0, workspace.rawData(), work_pos,
90                         to_width);
91     }
92   }
93   return res;
94 }
95 
96 }  // namespace data
97 }  // namespace veles
98