ATLAS Offline Software
Loading...
Searching...
No Matches
Compressor.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5/*
6 * Compressor.cpp
7 * Compressor
8 *
9 * Created by Ilija Vukotic on 05/07/07.
10 * <ivukotic@cern.ch>
11 *
12 */
13
15using namespace std;
16
17// ---------- STANDARD
18void Compressor::reduceToUS(const std::vector<float> &vf, std::vector<unsigned short> &vc ){
19 constexpr unsigned int max_short_flt = 0x7f7f7fff;
20 union {unsigned int u;float f;} m;
21 vc.reserve(vf.size());
22 for (const auto& value : vf){
23 m.f = value;
24 if ( (m.u & 0x7fffffff) > max_short_flt) vc.push_back (m.u>>16);
25 else vc.push_back((m.u+0x8000)>>16);
26 }
27}
28
29void Compressor::expandFromUStoFloat(const std::vector<unsigned short> &vc, std::vector<float> &vf){
30 union {unsigned int u;float f;} m;
31 vf.reserve(vc.size());
32 for (const auto& value : vc){
33 unsigned int ui(value << 16);
34 m.u = ui;
35 vf.push_back(m.f);
36 }
37}
38
39// ------------- WITH BITS AND SIGN
40void Compressor::reduce(const std::vector<float> &vf, std::vector<unsigned int> &vi){
41
42 vi.reserve(vf.size()*m_bits/16+17);//this is roughly. can be made precisse. depends on sign stripping too.
43 // composing format word
44 unsigned int format=0;
45 unsigned int vfs=vf.size();
46 format |= vfs; // writes down 25 bits of vector size
47 format=format<<6;
48 format |= m_bits & 63;// six bits
49 format =format<<1;
50 format |= m_sign;
51 vi.push_back(format);
52
53 // initializing variables
54 int bshift=32-m_bits;
55 int rounding=0x1<<(bshift-1);
56 unsigned int vmax= 0x7f7<<20;
57 vmax |= 0x000fffff xor (rounding);
58 unsigned int rems=0xffffffff;
59 if(m_sign)
60 rems>>=(bshift+1);
61 else
62 rems>>=bshift;
63
64 union {unsigned int u;float f;} m;
65
66
67 if (m_bitStrip){
68 int F=32;
69 int L;
70 if (m_sign) L=m_bits-1; else L=m_bits;
71 unsigned int CUR=0; int IN=0;
72 for (const auto& value : vf){
73 m.f = value;
74 if ( (m.u & 0x7fffffff) > vmax) IN=m.u>>bshift;
75 else IN=(m.u+rounding)>>bshift;
76
77 IN&=rems;
78
79 if (F>L){
80 F-=L;
81 CUR|=(IN<<F);
82 }
83 else if(F==L){
84 F-=L;
85 CUR|=(IN<<F);
86 vi.push_back(CUR);
87 CUR=0;F=32;
88 }
89 else{
90 int D=L-F;
91 CUR|=(IN>>D);
92 vi.push_back(CUR);
93 CUR=0; F=32-D;
94 // Avoid int overflow in shift.
95 IN &= (1U<<D)-1;
96 CUR|=(IN<<F);
97 }
98 }
99 vi.push_back(CUR);
100
101 }
102 else
103 for (const auto& value : vf){
104 m.f = value;
105 vi.push_back(m.u);
106 }
107
108 return;
109}
110
111
112void Compressor::expandToFloat(const std::vector<unsigned int> & vi, std::vector<float> & vf){
113 vf.clear();
114 if (vi.empty())
115 return;
116
117 std::vector<unsigned int>::const_iterator i=vi.begin();
118
119 int format=(*i); ++i;
120 m_sign= 1 & format;
121// if (m_sign) cout<<"Sign was neglected"<<endl;
122 format=format>>1;
123 m_bits= 63 & format;
124// cout<<"Uncompressing "<<m_bits<<" bits!"<<endl;
125 unsigned int vecs=(format>>6)&0xfffffff;
126// cout<<"vec size: "<<vecs<<endl;
127 int bshift=32-m_bits;
128
129 vf.reserve(vi.size()*m_bits/16+17); //this is roughly. can be made precisse. depends on sign stripping too.
130
131 int L;
132 if (m_sign) L=m_bits-1; else L=m_bits;
133 int CP=0; int FP=0; int REM=0;
134 unsigned int V=0xffffffff>>(32-L); unsigned int R=0;
135 unsigned int ui(*i);
136
137 union {unsigned int u;float f;} m;
138
139 while (vecs){
140 FP = CP + L; // Future point = Current point + lenght
141 if (FP<=32){ // all of it is inside this integer
142 R = ( ui >> (32-FP) ) & V;
143 R <<= bshift;
144 m.u=R;
145 vf.push_back(m.f);
146 if (FP < 32)
147 CP=FP;
148 else {
149 CP = 0;
150 ++i;
151 ui = (*i); // take next integer
152 }
153 }
154 else{ // part of the float is in the next integer
155 REM = FP - 32; // Remainder = Future point - 32
156 R = ( ui & (0xffffffff >> CP) ) << REM; // find first part
157 ++i;
158 ui = (*i); // take next integer
159 R |= ui >> (32-REM) ;
160 R <<= bshift;
161 if (m_sign) R &= 0x7fffffff;
162 m.u=R;
163 vf.push_back(m.f);
164 CP = REM; // move Current point
165 }
166 --vecs;
167 }
168 return;
169}
#define F(x, y, z)
Definition MD5.cxx:112
bool m_sign
Definition Compressor.h:67
void expandFromUStoFloat(const std::vector< unsigned short > &vi, std::vector< float > &vf)
void reduceToUS(const std::vector< float > &vf, std::vector< unsigned short > &vi)
bool m_bitStrip
Definition Compressor.h:68
void expandToFloat(const std::vector< unsigned int > &vc, std::vector< float > &vf)
void reduce(const std::vector< float > &vf, std::vector< unsigned int > &vi)
Select isolated Photons, Electrons and Muons.
STL namespace.