Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RColumnElement.cxx
Go to the documentation of this file.
1/// \file RColumnElement.cxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2019-08-11
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#include "ROOT/RColumn.hxx"
18
19#include "RColumnElement.hxx"
20
21#include <algorithm>
22#include <bitset>
23#include <cassert>
24#include <cstdint>
25#include <memory>
26#include <utility>
27
28std::pair<std::uint16_t, std::uint16_t>
30{
31 switch (type) {
32 case EColumnType::kIndex64: return std::make_pair(64, 64);
33 case EColumnType::kIndex32: return std::make_pair(32, 32);
34 case EColumnType::kSwitch: return std::make_pair(96, 96);
35 case EColumnType::kByte: return std::make_pair(8, 8);
36 case EColumnType::kChar: return std::make_pair(8, 8);
37 case EColumnType::kBit: return std::make_pair(1, 1);
38 case EColumnType::kReal64: return std::make_pair(64, 64);
39 case EColumnType::kReal32: return std::make_pair(32, 32);
40 case EColumnType::kReal16: return std::make_pair(16, 16);
41 case EColumnType::kInt64: return std::make_pair(64, 64);
42 case EColumnType::kUInt64: return std::make_pair(64, 64);
43 case EColumnType::kInt32: return std::make_pair(32, 32);
44 case EColumnType::kUInt32: return std::make_pair(32, 32);
45 case EColumnType::kInt16: return std::make_pair(16, 16);
46 case EColumnType::kUInt16: return std::make_pair(16, 16);
47 case EColumnType::kInt8: return std::make_pair(8, 8);
48 case EColumnType::kUInt8: return std::make_pair(8, 8);
49 case EColumnType::kSplitIndex64: return std::make_pair(64, 64);
50 case EColumnType::kSplitIndex32: return std::make_pair(32, 32);
51 case EColumnType::kSplitReal64: return std::make_pair(64, 64);
52 case EColumnType::kSplitReal32: return std::make_pair(32, 32);
53 case EColumnType::kSplitInt64: return std::make_pair(64, 64);
54 case EColumnType::kSplitUInt64: return std::make_pair(64, 64);
55 case EColumnType::kSplitInt32: return std::make_pair(32, 32);
56 case EColumnType::kSplitUInt32: return std::make_pair(32, 32);
57 case EColumnType::kSplitInt16: return std::make_pair(16, 16);
58 case EColumnType::kSplitUInt16: return std::make_pair(16, 16);
59 case EColumnType::kReal32Trunc: return std::make_pair(10, 31);
60 case EColumnType::kReal32Quant: return std::make_pair(1, 32);
61 default:
62 if (type == kTestFutureType)
63 return std::make_pair(32, 32);
64 assert(false);
65 }
66 // never here
67 return std::make_pair(0, 0);
68}
69
71{
72 switch (type) {
73 case EColumnType::kIndex64: return "Index64";
74 case EColumnType::kIndex32: return "Index32";
75 case EColumnType::kSwitch: return "Switch";
76 case EColumnType::kByte: return "Byte";
77 case EColumnType::kChar: return "Char";
78 case EColumnType::kBit: return "Bit";
79 case EColumnType::kReal64: return "Real64";
80 case EColumnType::kReal32: return "Real32";
81 case EColumnType::kReal16: return "Real16";
82 case EColumnType::kInt64: return "Int64";
83 case EColumnType::kUInt64: return "UInt64";
84 case EColumnType::kInt32: return "Int32";
85 case EColumnType::kUInt32: return "UInt32";
86 case EColumnType::kInt16: return "Int16";
87 case EColumnType::kUInt16: return "UInt16";
88 case EColumnType::kInt8: return "Int8";
89 case EColumnType::kUInt8: return "UInt8";
90 case EColumnType::kSplitIndex64: return "SplitIndex64";
91 case EColumnType::kSplitIndex32: return "SplitIndex32";
92 case EColumnType::kSplitReal64: return "SplitReal64";
93 case EColumnType::kSplitReal32: return "SplitReal32";
94 case EColumnType::kSplitInt64: return "SplitInt64";
95 case EColumnType::kSplitUInt64: return "SplitUInt64";
96 case EColumnType::kSplitInt32: return "SplitInt32";
97 case EColumnType::kSplitUInt32: return "SplitUInt32";
98 case EColumnType::kSplitInt16: return "SplitInt16";
99 case EColumnType::kSplitUInt16: return "SplitUInt16";
100 case EColumnType::kReal32Trunc: return "Real32Trunc";
101 case EColumnType::kReal32Quant: return "Real32Quant";
102 default:
103 if (type == kTestFutureType)
104 return "TestFutureType";
105 return "UNKNOWN";
106 }
107}
108
109template <>
110std::unique_ptr<ROOT::Experimental::Internal::RColumnElementBase>
111ROOT::Experimental::Internal::RColumnElementBase::Generate<void>(EColumnType onDiskType)
112{
113 switch (onDiskType) {
114 case EColumnType::kIndex64: return std::make_unique<RColumnElement<ClusterSize_t, EColumnType::kIndex64>>();
115 case EColumnType::kIndex32: return std::make_unique<RColumnElement<ClusterSize_t, EColumnType::kIndex32>>();
116 case EColumnType::kSwitch: return std::make_unique<RColumnElement<RColumnSwitch, EColumnType::kSwitch>>();
117 case EColumnType::kByte: return std::make_unique<RColumnElement<std::byte, EColumnType::kByte>>();
118 case EColumnType::kChar: return std::make_unique<RColumnElement<char, EColumnType::kChar>>();
119 case EColumnType::kBit: return std::make_unique<RColumnElement<bool, EColumnType::kBit>>();
120 case EColumnType::kReal64: return std::make_unique<RColumnElement<double, EColumnType::kReal64>>();
121 case EColumnType::kReal32: return std::make_unique<RColumnElement<float, EColumnType::kReal32>>();
122 // TODO: Change to std::float16_t in-memory type once available (from C++23).
123 case EColumnType::kReal16: return std::make_unique<RColumnElement<float, EColumnType::kReal16>>();
124 case EColumnType::kInt64: return std::make_unique<RColumnElement<std::int64_t, EColumnType::kInt64>>();
125 case EColumnType::kUInt64: return std::make_unique<RColumnElement<std::uint64_t, EColumnType::kUInt64>>();
126 case EColumnType::kInt32: return std::make_unique<RColumnElement<std::int32_t, EColumnType::kInt32>>();
127 case EColumnType::kUInt32: return std::make_unique<RColumnElement<std::uint32_t, EColumnType::kUInt32>>();
128 case EColumnType::kInt16: return std::make_unique<RColumnElement<std::int16_t, EColumnType::kInt16>>();
129 case EColumnType::kUInt16: return std::make_unique<RColumnElement<std::uint16_t, EColumnType::kUInt16>>();
130 case EColumnType::kInt8: return std::make_unique<RColumnElement<std::int8_t, EColumnType::kInt8>>();
131 case EColumnType::kUInt8: return std::make_unique<RColumnElement<std::uint8_t, EColumnType::kUInt8>>();
133 return std::make_unique<RColumnElement<ClusterSize_t, EColumnType::kSplitIndex64>>();
135 return std::make_unique<RColumnElement<ClusterSize_t, EColumnType::kSplitIndex32>>();
136 case EColumnType::kSplitReal64: return std::make_unique<RColumnElement<double, EColumnType::kSplitReal64>>();
137 case EColumnType::kSplitReal32: return std::make_unique<RColumnElement<float, EColumnType::kSplitReal32>>();
138 case EColumnType::kSplitInt64: return std::make_unique<RColumnElement<std::int64_t, EColumnType::kSplitInt64>>();
139 case EColumnType::kSplitUInt64: return std::make_unique<RColumnElement<std::uint64_t, EColumnType::kSplitUInt64>>();
140 case EColumnType::kSplitInt32: return std::make_unique<RColumnElement<std::int32_t, EColumnType::kSplitInt32>>();
141 case EColumnType::kSplitUInt32: return std::make_unique<RColumnElement<std::uint32_t, EColumnType::kSplitUInt32>>();
142 case EColumnType::kSplitInt16: return std::make_unique<RColumnElement<std::int16_t, EColumnType::kSplitInt16>>();
143 case EColumnType::kSplitUInt16: return std::make_unique<RColumnElement<std::uint16_t, EColumnType::kSplitUInt16>>();
144 case EColumnType::kReal32Trunc: return std::make_unique<RColumnElement<float, EColumnType::kReal32Trunc>>();
145 case EColumnType::kReal32Quant: return std::make_unique<RColumnElement<float, EColumnType::kReal32Quant>>();
146 default:
147 if (onDiskType == kTestFutureType)
148 return std::make_unique<RColumnElement<Internal::RTestFutureColumn, kTestFutureType>>();
149 assert(false);
150 }
151 // never here
152 return nullptr;
153}
154
155std::unique_ptr<ROOT::Experimental::Internal::RColumnElementBase>
157{
158 if (inMemoryType == std::type_index(typeid(char))) {
159 return GenerateColumnElementInternal<char>(onDiskType);
160 } else if (inMemoryType == std::type_index(typeid(bool))) {
161 return GenerateColumnElementInternal<bool>(onDiskType);
162 } else if (inMemoryType == std::type_index(typeid(std::byte))) {
163 return GenerateColumnElementInternal<std::byte>(onDiskType);
164 } else if (inMemoryType == std::type_index(typeid(std::uint8_t))) {
165 return GenerateColumnElementInternal<std::uint8_t>(onDiskType);
166 } else if (inMemoryType == std::type_index(typeid(std::uint16_t))) {
167 return GenerateColumnElementInternal<std::uint16_t>(onDiskType);
168 } else if (inMemoryType == std::type_index(typeid(std::uint32_t))) {
169 return GenerateColumnElementInternal<std::uint32_t>(onDiskType);
170 } else if (inMemoryType == std::type_index(typeid(std::uint64_t))) {
171 return GenerateColumnElementInternal<std::uint64_t>(onDiskType);
172 } else if (inMemoryType == std::type_index(typeid(std::int8_t))) {
173 return GenerateColumnElementInternal<std::int8_t>(onDiskType);
174 } else if (inMemoryType == std::type_index(typeid(std::int16_t))) {
175 return GenerateColumnElementInternal<std::int16_t>(onDiskType);
176 } else if (inMemoryType == std::type_index(typeid(std::int32_t))) {
177 return GenerateColumnElementInternal<std::int32_t>(onDiskType);
178 } else if (inMemoryType == std::type_index(typeid(std::int64_t))) {
179 return GenerateColumnElementInternal<std::int64_t>(onDiskType);
180 } else if (inMemoryType == std::type_index(typeid(float))) {
181 return GenerateColumnElementInternal<float>(onDiskType);
182 } else if (inMemoryType == std::type_index(typeid(double))) {
183 return GenerateColumnElementInternal<double>(onDiskType);
184 } else if (inMemoryType == std::type_index(typeid(ClusterSize_t))) {
185 return GenerateColumnElementInternal<ClusterSize_t>(onDiskType);
186 } else if (inMemoryType == std::type_index(typeid(RColumnSwitch))) {
187 return GenerateColumnElementInternal<RColumnSwitch>(onDiskType);
188 } else if (inMemoryType == std::type_index(typeid(RTestFutureColumn))) {
189 return GenerateColumnElementInternal<RTestFutureColumn>(onDiskType);
190 } else {
191 R__ASSERT(!"Invalid memory type in GenerateColumnElement");
192 }
193 // never here
194 return nullptr;
195}
196
197std::unique_ptr<ROOT::Experimental::Internal::RColumnElementBase>
199{
200 return GenerateColumnElement(elementId.fInMemoryType, elementId.fOnDiskType);
201}
202
203void ROOT::Experimental::Internal::BitPacking::PackBits(void *dst, const void *src, std::size_t count,
204 std::size_t sizeofSrc, std::size_t nDstBits)
205{
206 assert(sizeofSrc <= sizeof(Word_t));
207 assert(0 < nDstBits && nDstBits <= sizeofSrc * 8);
208
209 const unsigned char *srcArray = reinterpret_cast<const unsigned char *>(src);
210 Word_t *dstArray = reinterpret_cast<Word_t *>(dst);
211 Word_t accum = 0;
212 std::size_t bitsUsed = 0;
213 std::size_t dstIdx = 0;
214 for (std::size_t i = 0; i < count; ++i) {
215 Word_t packedWord = 0;
216 memcpy(&packedWord, srcArray + i * sizeofSrc, sizeofSrc);
217 // truncate the LSB of the item
218 packedWord >>= sizeofSrc * 8 - nDstBits;
219
220 const std::size_t bitsRem = kBitsPerWord - bitsUsed;
221 if (bitsRem >= nDstBits) {
222 // append the entire item to the accumulator
223 accum |= (packedWord << bitsUsed);
224 bitsUsed += nDstBits;
225 } else {
226 // chop up the item into its `bitsRem` LSB bits + `nDstBits - bitsRem` MSB bits.
227 // The LSB bits will be saved in the current word and the MSB will be saved in the next one.
228 if (bitsRem > 0) {
229 Word_t packedWordLsb = packedWord;
230 packedWordLsb <<= (kBitsPerWord - bitsRem);
231 packedWordLsb >>= (kBitsPerWord - bitsRem);
232 accum |= (packedWordLsb << bitsUsed);
233 }
234
235 memcpy(&dstArray[dstIdx++], &accum, sizeof(accum));
236 accum = 0;
237 bitsUsed = 0;
238
239 if (bitsRem > 0) {
240 Word_t packedWordMsb = packedWord;
241 packedWordMsb >>= bitsRem;
242 accum |= packedWordMsb;
243 bitsUsed += nDstBits - bitsRem;
244 } else {
245 // we realigned to a word boundary: append the entire item
246 accum = packedWord;
247 bitsUsed += nDstBits;
248 }
249 }
250 }
251
252 if (bitsUsed)
253 memcpy(&dstArray[dstIdx++], &accum, (bitsUsed + 7) / 8);
254
255 [[maybe_unused]] auto expDstCount = (count * nDstBits + kBitsPerWord - 1) / kBitsPerWord;
256 assert(dstIdx == expDstCount);
257}
258
259void ROOT::Experimental::Internal::BitPacking::UnpackBits(void *dst, const void *src, std::size_t count,
260 std::size_t sizeofDst, std::size_t nSrcBits)
261{
262 assert(sizeofDst <= sizeof(Word_t));
263 assert(0 < nSrcBits && nSrcBits <= sizeofDst * 8);
264
265 unsigned char *dstArray = reinterpret_cast<unsigned char *>(dst);
266 const Word_t *srcArray = reinterpret_cast<const Word_t *>(src);
267 const auto nWordsToLoad = (count * nSrcBits + kBitsPerWord - 1) / kBitsPerWord;
268
269 // bit offset of the next packed item inside the currently loaded word
270 int offInWord = 0;
271 std::size_t dstIdx = 0;
272 Word_t prevWordLsb = 0;
273 std::size_t remBytesToLoad = (count * nSrcBits + 7) / 8;
274 for (std::size_t i = 0; i < nWordsToLoad; ++i) {
275 assert(dstIdx < count);
276
277 // load the next word, containing some packed items
278 Word_t packedBytes = 0;
279 std::size_t bytesLoaded = std::min(remBytesToLoad, sizeof(Word_t));
280 memcpy(&packedBytes, &srcArray[i], bytesLoaded);
281
282 assert(remBytesToLoad >= bytesLoaded);
283 remBytesToLoad -= bytesLoaded;
284
285 // If `offInWord` is negative, it means that the last item was split
286 // across 2 words and we need to recombine it.
287 if (offInWord < 0) {
288 std::size_t nMsb = nSrcBits + offInWord;
289 std::uint32_t msb = packedBytes << (8 * sizeofDst - nMsb);
290 Word_t packedWord = msb | prevWordLsb;
291 prevWordLsb = 0;
292 memcpy(dstArray + dstIdx * sizeofDst, &packedWord, sizeofDst);
293 ++dstIdx;
294 offInWord = nMsb;
295 }
296
297 // isolate each item in the loaded word
298 while (dstIdx < count) {
299 // Check if we need to load a split item or a full one
300 if (offInWord > static_cast<int>(kBitsPerWord - nSrcBits)) {
301 // save the LSB of the next item, next `for` loop will merge them with the MSB in the next word.
302 assert(offInWord <= static_cast<int>(kBitsPerWord));
303 std::size_t nLsbNext = kBitsPerWord - offInWord;
304 if (nLsbNext)
305 prevWordLsb = (packedBytes >> offInWord) << (8 * sizeofDst - nSrcBits);
306 offInWord -= kBitsPerWord;
307 break;
308 }
309
310 Word_t packedWord = packedBytes;
311 assert(nSrcBits + offInWord <= kBitsPerWord);
312 packedWord >>= offInWord;
313 packedWord <<= 8 * sizeofDst - nSrcBits;
314 memcpy(dstArray + dstIdx * sizeofDst, &packedWord, sizeofDst);
315 ++dstIdx;
316 offInWord += nSrcBits;
317 }
318 }
319
320 assert(prevWordLsb == 0);
321 assert(dstIdx == count);
322}
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
static const char * GetColumnTypeName(EColumnType type)
static std::pair< std::uint16_t, std::uint16_t > GetValidBitRange(EColumnType type)
Most types have a fixed on-disk bit width.
Holds the index and the tag of a kSwitch column.
void PackBits(void *dst, const void *src, std::size_t count, std::size_t sizeofSrc, std::size_t nDstBits)
Tightly packs count items of size sizeofSrc contained in src into dst using nDstBits per item.
void UnpackBits(void *dst, const void *src, std::size_t count, std::size_t sizeofDst, std::size_t nSrcBits)
Undoes the effect of PackBits.
std::unique_ptr< RColumnElementBase > GenerateColumnElement(std::type_index inMemoryType, EColumnType onDiskType)
Every concrete RColumnElement type is identified by its on-disk type (column type) and the in-memory ...
Wrap the integer in a struct in order to avoid template specialization clash with std::uint64_t.
static char accum[256]
Definition gifencode.c:210