ATLAS Offline Software
Loading...
Searching...
No Matches
ITkPixQCoreEncodingLUT.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef ITKPIXV2QCOREENCODINGLUT_H
6#define ITKPIXV2QCOREENCODINGLUT_H
7#include <iostream>
8#include <cstdint>
9#include <array>
10#include <cstddef>
11#include <algorithm>
12#include <ranges>
13
15 constexpr std::size_t LookUpTableSize(1<<16);
16
17
18 template<std::size_t Length, typename Generator>
19 constexpr auto lut(Generator&& f){
20 using content_type = decltype(f(std::size_t{0}));
21 std::array<content_type, Length> arr;
22 using namespace std::ranges;
23 auto rangy = views::iota(std::size_t{0}, Length) // Generate a sequence
24 | views::transform(std::forward<Generator>(f)); // Transform using our generator
25 copy(rangy, arr.begin());
26 return arr;
27 }
28
29 constexpr uint32_t
30 one_bit(uint32_t v){
31 return v!=0;
32 }
33
34 // nb. If we use std::array here instead of a C array,
35 // then compilation fails with gcc13 in the dbg build
36 // because the compile-time execution of encode() then takes
37 // too many steps.
38 constexpr void prepByte(uint32_t word, uint32_t b[8]){
39 for(int i = 0 ;i<8;i++) {
40 b[i] = ((word >> (2*i)) & 0x1) << 1 | ((word >> (2*i+1)) & 0x1);
41 }
42 }
43
44 std::array<uint32_t, 8> prepByte(uint32_t word)
45 {
46 std::array<uint32_t, 8> b;
47 prepByte(word, b.data());
48 return b;
49 }
50
51
52 //QCore encoding function, taken from Matthias Wittgen and Carlos
53 uint32_t
54 constexpr encode(uint32_t decoded, uint32_t &encoded) {
55 uint32_t b[8];
56 prepByte(decoded, b);
57 //
58 uint32_t S1 = (one_bit(b[0] | b[1] | b[2] | b[3]) << 1) | one_bit(b[4] | b[5] | b[6] | b[7]);
59 uint32_t S2t = (one_bit(b[0] | b[1]) << 1) | one_bit(b[2] | b[3]);
60 uint32_t S2b = (one_bit(b[4] | b[5]) << 1) | one_bit(b[6] | b[7]);
61 uint32_t S3tl = (one_bit(b[0]) << 1) | one_bit(b[1]);
62 uint32_t S3tr = (one_bit(b[2]) << 1) | one_bit(b[3]);
63 uint32_t S3bl = (one_bit(b[4]) << 1) | one_bit(b[5]);
64 uint32_t S3br = (one_bit(b[6]) << 1) | one_bit(b[7]);
65
66 uint32_t pos = 0;
67 encoded = 0;
68
69 auto writeTwo = [&](uint32_t src) {
70 if (src == 0b01) {
71 encoded |= (0b0) << (28 - pos);
72 pos++;
73 } else {
74 encoded |= (src & 0x3) << (28 - pos);
75 pos += 2;
76 }
77 };
78 for(auto & val:{
79 S1, S2t, S3tl, S3tr, b[0], b[1],b[2], b[3],
80 S2b, S3bl, S3br,b[4], b[5], b[6], b[7]
81 }) if(val) writeTwo(val);
82 return pos;
83 }
84
85 //Create LUTs - if argument is true, return length LUT, if it's false return the encoded QCore LUT.
86 //It could be prettified / optimized to fill in both LUTs in one go, but happens once per job and takes O(ms)
87 //so no real gain there.
88 inline auto create_lut_encode(bool length = false) {
89 std::array<uint32_t, LookUpTableSize> lut;
90 lut[0] = 0;
91 for (uint32_t i = 1; i < 1 << 16; i++) {
92 uint32_t encoded;
93 uint64_t len = encode(i, encoded);
94 lut[i] = length ? len : encoded;
95 }
96 return lut;
97 }
98
99 //It could be prettified / optimized to fill in both LUTs in one go, but happens once per job and takes O(ms)
100 //so no real gain there.
101 template<std::size_t Length>
102 constexpr auto LutLen = lut<Length>([](uint32_t i){
103 uint32_t encoded;
104 return encode(i, encoded);
105 });
106
107 template<std::size_t Length>
108 constexpr auto LutBTree = lut<Length>([](uint32_t i){
109 uint32_t encoded;
110 uint64_t len = encode(i, encoded);
111 return encoded >> (30 - len);
112 });
113
114
115 constexpr std::array<uint32_t, LookUpTableSize > ITkPixV2QCoreEncodingLUT_Tree = LutBTree<LookUpTableSize>;
116 constexpr std::array<uint32_t, LookUpTableSize > ITkPixV2QCoreEncodingLUT_Length = LutLen<LookUpTableSize>;
117
118}
119#endif
double length(const pvec &v)
struct TBPatternUnitContext S1
constexpr auto lut(Generator &&f)
constexpr uint32_t one_bit(uint32_t v)
uint32_t constexpr encode(uint32_t decoded, uint32_t &encoded)
auto create_lut_encode(bool length=false)
constexpr std::array< uint32_t, LookUpTableSize > ITkPixV2QCoreEncodingLUT_Length
constexpr std::array< uint32_t, LookUpTableSize > ITkPixV2QCoreEncodingLUT_Tree
constexpr void prepByte(uint32_t word, uint32_t b[8])
constexpr std::size_t LookUpTableSize(1<< 16)