]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTDataDeflater.cxx
Overlaps corrected
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataDeflater.cxx
CommitLineData
80fb7693 1// $Id$
2
3//**************************************************************************
4//* This file is property of and copyright by the ALICE HLT Project *
5//* ALICE Experiment at CERN, All rights reserved. *
6//* *
7//* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8//* for The ALICE HLT Project. *
9//* *
10//* Permission to use, copy, modify and distribute this software and its *
11//* documentation strictly for non-commercial purposes is hereby granted *
12//* without fee, provided that the above copyright notice appears in all *
13//* copies and that both the copyright notice and this permission notice *
14//* appear in the supporting documentation. The authors make no claims *
15//* about the suitability of this software for any purpose. It is *
16//* provided "as is" without express or implied warranty. *
17//**************************************************************************
18
19/// @file AliHLTDataDeflater.cxx
20/// @author Matthias Richter, Timm Steinbeck
21/// @date 2011-08-10
22/// @brief Data deflater class storing only necessary bits
23/// @note Code original from AliHLTTPCCompModelDeflater
24
25#include "AliHLTDataDeflater.h"
26#include "AliHLTErrorGuard.h"
27#include <memory>
28#include <algorithm>
29#include <iostream>
30
31/** ROOT macro for the implementation of ROOT specific class methods */
32ClassImp(AliHLTDataDeflater)
33
34AliHLTDataDeflater::AliHLTDataDeflater()
35 : AliHLTLogging()
36 , fBitDataCurrentWord(0)
37 , fBitDataCurrentPosInWord(0)
38 , fBitDataCurrentOutput(NULL)
39 , fBitDataCurrentOutputStart(NULL)
40 , fBitDataCurrentOutputEnd(NULL)
41{
42 // see header file for class documentation
43 // or
44 // refer to README to build package
45 // or
46 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
47}
48
49AliHLTDataDeflater::~AliHLTDataDeflater()
50{
51 // destructor
52 Clear();
53}
54
55int AliHLTDataDeflater::InitBitDataOutput( AliHLTUInt8_t* output, UInt_t outputSize)
56{
57 // init the target buffer
58 fBitDataCurrentWord = 0;
59 fBitDataCurrentPosInWord = 7;
60 fBitDataCurrentOutput = fBitDataCurrentOutputStart = output;
61 fBitDataCurrentOutputEnd = output+outputSize;
62
63 return 0;
64}
65
66AliHLTUInt8_t AliHLTDataDeflater::GetCurrentOutputByte( Int_t offset ) const
67{
68 // get the current byte
69 if ( !offset )
70 return fBitDataCurrentWord;
71 else
72 return *(fBitDataCurrentOutput+offset);
73}
74
75bool AliHLTDataDeflater::OutputBit( AliHLTUInt32_t const & value )
76{
77 // write one bit to the current byte and position
78 if ( fBitDataCurrentOutput>=fBitDataCurrentOutputEnd )
79 return false;
80 fBitDataCurrentWord |= (value & 1) << fBitDataCurrentPosInWord;
81 if ( fBitDataCurrentPosInWord )
82 fBitDataCurrentPosInWord--;
83 else {
84 *fBitDataCurrentOutput = fBitDataCurrentWord;
85 fBitDataCurrentPosInWord = 7;
86 fBitDataCurrentOutput++;
87 fBitDataCurrentWord = 0;
88 }
89 return true;
90}
91
92bool AliHLTDataDeflater::OutputBits( AliHLTUInt64_t const & value, UInt_t const & bitCount )
93{
94 // write bit pattern to the current byte and position
95 if ( bitCount>64 ) {
96 HLTFatal( "Internal error: Attempt to write more than 64 bits (%u)", (unsigned)bitCount );
97 return false;
98 }
99 UInt_t bitsToWrite=bitCount;
100 UInt_t curBitCount;
101 while ( bitsToWrite>0 ) {
102 if ( fBitDataCurrentOutput>=fBitDataCurrentOutputEnd )
103 return false;
104#if 1
105 if ( bitsToWrite >= fBitDataCurrentPosInWord+1 )
106 curBitCount = fBitDataCurrentPosInWord+1;
107 else
108 curBitCount = bitsToWrite;
109 fBitDataCurrentWord |= ( (value >> (bitsToWrite-curBitCount)) & ((1<<curBitCount)-1) ) << (fBitDataCurrentPosInWord+1-curBitCount);
110 if ( fBitDataCurrentPosInWord < curBitCount )
111 {
112 *fBitDataCurrentOutput = fBitDataCurrentWord;
113 fBitDataCurrentPosInWord = 7;
114 fBitDataCurrentOutput++;
115 fBitDataCurrentWord = 0;
116 }
117 else
118 fBitDataCurrentPosInWord -= curBitCount;
119 bitsToWrite -= curBitCount;
120
121#else
122 AliHLTUInt8_t curValue;
123 if ( bitsToWrite>=8 )
124 {
125 curBitCount=8;
126 curValue = (value >> bitsToWrite-8) & 0xFF;
127 bitsToWrite -= 8;
128 }
129 else
130 {
131 curBitCount=bitsToWrite;
132 curValue = value & ( (1<<bitsToWrite)-1 );
133 bitsToWrite = 0;
134 }
135 if ( fBitDataCurrentPosInWord+1>curBitCount )
136 {
137 fBitDataCurrentWord |= curValue << (fBitDataCurrentPosInWord-curBitCount+1);
138 fBitDataCurrentPosInWord -= curBitCount;
139 }
140 else if ( fBitDataCurrentPosInWord+1==curBitCount )
141 {
142 fBitDataCurrentWord |= curValue;
143 *fBitDataCurrentOutput = fBitDataCurrentWord;
144 fBitDataCurrentPosInWord = 7;
145 fBitDataCurrentOutput++;
146 fBitDataCurrentWord = 0;
147 }
148 else
149 {
150 const UInt_t first = fBitDataCurrentPosInWord+1; // Number of bits for first block
151 const UInt_t second = curBitCount-first; // Number of bits for second block
152 fBitDataCurrentWord |= ( curValue >> second ) & ((1<<first)-1);
153 *fBitDataCurrentOutput = fBitDataCurrentWord;
154 fBitDataCurrentOutput++;
155 if ( fBitDataCurrentOutput>=fBitDataCurrentOutputEnd )
156 return false;
157 fBitDataCurrentWord = curValue & ((1<<second)-1) << (8-second);
158 fBitDataCurrentPosInWord = 7-second;
159 }
160#endif
161 }
162 return true;
163}
164
165void AliHLTDataDeflater::Pad8Bits()
166{
167 // finish the current word
168 if ( fBitDataCurrentPosInWord==7 )
169 return;
170 *fBitDataCurrentOutput = fBitDataCurrentWord;
171 fBitDataCurrentPosInWord = 7;
172 fBitDataCurrentOutput++;
173 fBitDataCurrentWord = 0;
174}
175
176bool AliHLTDataDeflater::OutputBytes( AliHLTUInt8_t const * data, UInt_t const & byteCount )
177{
178 // write sequence of bytes
179 Pad8Bits();
180 if ( fBitDataCurrentOutput+byteCount>fBitDataCurrentOutputEnd )
181 return false;
182 memcpy( fBitDataCurrentOutput, data, byteCount );
183 fBitDataCurrentOutput += byteCount;
184 return true;
185}
186
187bool AliHLTDataDeflater::OutputParameterBits( int /*(parameterId*/, AliHLTUInt64_t const & /*value*/ )
188{
189 // write bit pattern of a member to the current byte and position
190 ALIHLTERRORGUARD(1,"method needs to be implemented in child class");
191 return false;
192}
193
194void AliHLTDataDeflater::Clear(Option_t * /*option*/)
195{
196 // internal cleanup
197}
198
199void AliHLTDataDeflater::Print(Option_t *option) const
200{
201 // print info
202 Print(cout, option);
203}
204
205void AliHLTDataDeflater::Print(ostream& out, Option_t */*option*/) const
206{
207 // print to stream
208 out << "AliHLTDataDeflater: " << endl;
209}
210
211ostream& operator<<(ostream &out, const AliHLTDataDeflater& me)
212{
213 me.Print(out);
214 return out;
215}