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<RColumnIndex, EColumnType::kIndex64>>();
115 case EColumnType::kIndex32: return std::make_unique<RColumnElement<RColumnIndex, 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>>();
132 case EColumnType::kSplitIndex64: return std::make_unique<RColumnElement<RColumnIndex, EColumnType::kSplitIndex64>>();
133 case EColumnType::kSplitIndex32: return std::make_unique<RColumnElement<RColumnIndex, EColumnType::kSplitIndex32>>();
134 case EColumnType::kSplitReal64: return std::make_unique<RColumnElement<double, EColumnType::kSplitReal64>>();
135 case EColumnType::kSplitReal32: return std::make_unique<RColumnElement<float, EColumnType::kSplitReal32>>();
136 case EColumnType::kSplitInt64: return std::make_unique<RColumnElement<std::int64_t, EColumnType::kSplitInt64>>();
137 case EColumnType::kSplitUInt64: return std::make_unique<RColumnElement<std::uint64_t, EColumnType::kSplitUInt64>>();
138 case EColumnType::kSplitInt32: return std::make_unique<RColumnElement<std::int32_t, EColumnType::kSplitInt32>>();
139 case EColumnType::kSplitUInt32: return std::make_unique<RColumnElement<std::uint32_t, EColumnType::kSplitUInt32>>();
140 case EColumnType::kSplitInt16: return std::make_unique<RColumnElement<std::int16_t, EColumnType::kSplitInt16>>();
141 case EColumnType::kSplitUInt16: return std::make_unique<RColumnElement<std::uint16_t, EColumnType::kSplitUInt16>>();
142 case EColumnType::kReal32Trunc: return std::make_unique<RColumnElement<float, EColumnType::kReal32Trunc>>();
143 case EColumnType::kReal32Quant: return std::make_unique<RColumnElement<float, EColumnType::kReal32Quant>>();
144 default:
145 if (onDiskType == kTestFutureType)
146 return std::make_unique<RColumnElement<Internal::RTestFutureColumn, kTestFutureType>>();
147 assert(false);
148 }
149 // never here
150 return nullptr;
151}
152
153std::unique_ptr<ROOT::Experimental::Internal::RColumnElementBase>
155{
156 if (inMemoryType == std::type_index(typeid(char))) {
157 return GenerateColumnElementInternal<char>(onDiskType);
158 } else if (inMemoryType == std::type_index(typeid(bool))) {
159 return GenerateColumnElementInternal<bool>(onDiskType);
160 } else if (inMemoryType == std::type_index(typeid(std::byte))) {
161 return GenerateColumnElementInternal<std::byte>(onDiskType);
162 } else if (inMemoryType == std::type_index(typeid(std::uint8_t))) {
163 return GenerateColumnElementInternal<std::uint8_t>(onDiskType);
164 } else if (inMemoryType == std::type_index(typeid(std::uint16_t))) {
165 return GenerateColumnElementInternal<std::uint16_t>(onDiskType);
166 } else if (inMemoryType == std::type_index(typeid(std::uint32_t))) {
167 return GenerateColumnElementInternal<std::uint32_t>(onDiskType);
168 } else if (inMemoryType == std::type_index(typeid(std::uint64_t))) {
169 return GenerateColumnElementInternal<std::uint64_t>(onDiskType);
170 } else if (inMemoryType == std::type_index(typeid(std::int8_t))) {
171 return GenerateColumnElementInternal<std::int8_t>(onDiskType);
172 } else if (inMemoryType == std::type_index(typeid(std::int16_t))) {
173 return GenerateColumnElementInternal<std::int16_t>(onDiskType);
174 } else if (inMemoryType == std::type_index(typeid(std::int32_t))) {
175 return GenerateColumnElementInternal<std::int32_t>(onDiskType);
176 } else if (inMemoryType == std::type_index(typeid(std::int64_t))) {
177 return GenerateColumnElementInternal<std::int64_t>(onDiskType);
178 } else if (inMemoryType == std::type_index(typeid(float))) {
179 return GenerateColumnElementInternal<float>(onDiskType);
180 } else if (inMemoryType == std::type_index(typeid(double))) {
181 return GenerateColumnElementInternal<double>(onDiskType);
182 } else if (inMemoryType == std::type_index(typeid(RColumnIndex))) {
183 return GenerateColumnElementInternal<RColumnIndex>(onDiskType);
184 } else if (inMemoryType == std::type_index(typeid(RColumnSwitch))) {
185 return GenerateColumnElementInternal<RColumnSwitch>(onDiskType);
186 } else if (inMemoryType == std::type_index(typeid(RTestFutureColumn))) {
187 return GenerateColumnElementInternal<RTestFutureColumn>(onDiskType);
188 } else {
189 R__ASSERT(!"Invalid memory type in GenerateColumnElement");
190 }
191 // never here
192 return nullptr;
193}
194
195std::unique_ptr<ROOT::Experimental::Internal::RColumnElementBase>
197{
198 return GenerateColumnElement(elementId.fInMemoryType, elementId.fOnDiskType);
199}
200
201void ROOT::Experimental::Internal::BitPacking::PackBits(void *dst, const void *src, std::size_t count,
202 std::size_t sizeofSrc, std::size_t nDstBits)
203{
204 assert(sizeofSrc <= sizeof(Word_t));
205 assert(0 < nDstBits && nDstBits <= sizeofSrc * 8);
206
207 const unsigned char *srcArray = reinterpret_cast<const unsigned char *>(src);
208 Word_t *dstArray = reinterpret_cast<Word_t *>(dst);
209 Word_t accum = 0;
210 std::size_t bitsUsed = 0;
211 std::size_t dstIdx = 0;
212 for (std::size_t i = 0; i < count; ++i) {
213 Word_t packedWord = 0;
214 memcpy(&packedWord, srcArray + i * sizeofSrc, sizeofSrc);
215 // truncate the LSB of the item
216 packedWord >>= sizeofSrc * 8 - nDstBits;
217
218 const std::size_t bitsRem = kBitsPerWord - bitsUsed;
219 if (bitsRem >= nDstBits) {
220 // append the entire item to the accumulator
221 accum |= (packedWord << bitsUsed);
222 bitsUsed += nDstBits;
223 } else {
224 // chop up the item into its `bitsRem` LSB bits + `nDstBits - bitsRem` MSB bits.
225 // The LSB bits will be saved in the current word and the MSB will be saved in the next one.
226 if (bitsRem > 0) {
227 Word_t packedWordLsb = packedWord;
228 packedWordLsb <<= (kBitsPerWord - bitsRem);
229 packedWordLsb >>= (kBitsPerWord - bitsRem);
230 accum |= (packedWordLsb << bitsUsed);
231 }
232
233 memcpy(&dstArray[dstIdx++], &accum, sizeof(accum));
234 accum = 0;
235 bitsUsed = 0;
236
237 if (bitsRem > 0) {
238 Word_t packedWordMsb = packedWord;
239 packedWordMsb >>= bitsRem;
240 accum |= packedWordMsb;
241 bitsUsed += nDstBits - bitsRem;
242 } else {
243 // we realigned to a word boundary: append the entire item
244 accum = packedWord;
245 bitsUsed += nDstBits;
246 }
247 }
248 }
249
250 if (bitsUsed)
251 memcpy(&dstArray[dstIdx++], &accum, (bitsUsed + 7) / 8);
252
253 [[maybe_unused]] auto expDstCount = (count * nDstBits + kBitsPerWord - 1) / kBitsPerWord;
254 assert(dstIdx == expDstCount);
255}
256
257void ROOT::Experimental::Internal::BitPacking::UnpackBits(void *dst, const void *src, std::size_t count,
258 std::size_t sizeofDst, std::size_t nSrcBits)
259{
260 assert(sizeofDst <= sizeof(Word_t));
261 assert(0 < nSrcBits && nSrcBits <= sizeofDst * 8);
262
263 unsigned char *dstArray = reinterpret_cast<unsigned char *>(dst);
264 const Word_t *srcArray = reinterpret_cast<const Word_t *>(src);
265 const auto nWordsToLoad = (count * nSrcBits + kBitsPerWord - 1) / kBitsPerWord;
266
267 // bit offset of the next packed item inside the currently loaded word
268 int offInWord = 0;
269 std::size_t dstIdx = 0;
270 Word_t prevWordLsb = 0;
271 std::size_t remBytesToLoad = (count * nSrcBits + 7) / 8;
272 for (std::size_t i = 0; i < nWordsToLoad; ++i) {
273 assert(dstIdx < count);
274
275 // load the next word, containing some packed items
276 Word_t packedBytes = 0;
277 std::size_t bytesLoaded = std::min(remBytesToLoad, sizeof(Word_t));
278 memcpy(&packedBytes, &srcArray[i], bytesLoaded);
279
280 assert(remBytesToLoad >= bytesLoaded);
281 remBytesToLoad -= bytesLoaded;
282
283 // If `offInWord` is negative, it means that the last item was split
284 // across 2 words and we need to recombine it.
285 if (offInWord < 0) {
286 std::size_t nMsb = nSrcBits + offInWord;
287 std::uint32_t msb = packedBytes << (8 * sizeofDst - nMsb);
288 Word_t packedWord = msb | prevWordLsb;
289 prevWordLsb = 0;
290 memcpy(dstArray + dstIdx * sizeofDst, &packedWord, sizeofDst);
291 ++dstIdx;
292 offInWord = nMsb;
293 }
294
295 // isolate each item in the loaded word
296 while (dstIdx < count) {
297 // Check if we need to load a split item or a full one
298 if (offInWord > static_cast<int>(kBitsPerWord - nSrcBits)) {
299 // save the LSB of the next item, next `for` loop will merge them with the MSB in the next word.
300 assert(offInWord <= static_cast<int>(kBitsPerWord));
301 std::size_t nLsbNext = kBitsPerWord - offInWord;
302 if (nLsbNext)
303 prevWordLsb = (packedBytes >> offInWord) << (8 * sizeofDst - nSrcBits);
304 offInWord -= kBitsPerWord;
305 break;
306 }
307
308 Word_t packedWord = packedBytes;
309 assert(nSrcBits + offInWord <= kBitsPerWord);
310 packedWord >>= offInWord;
311 packedWord <<= 8 * sizeofDst - nSrcBits;
312 memcpy(dstArray + dstIdx * sizeofDst, &packedWord, sizeofDst);
313 ++dstIdx;
314 offInWord += nSrcBits;
315 }
316 }
317
318 assert(prevWordLsb == 0);
319 assert(dstIdx == count);
320}
#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.
The in-memory representation of a 32bit or 64bit on-disk index column.
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)
constexpr EColumnType kTestFutureType
Every concrete RColumnElement type is identified by its on-disk type (column type) and the in-memory ...
static char accum[256]
Definition gifencode.c:210