]>
Commit | Line | Data |
---|---|---|
3ea47630 | 1 | /************************************************************************** |
2 | * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. * | |
3 | * * | |
4 | * Author: The ALICE Off-line Project. * | |
5 | * Contributors are mentioned in the code where appropriate. * | |
6 | * * | |
7 | * Permission to use, copy, modify and distribute this software and its * | |
8 | * documentation strictly for non-commercial purposes is hereby granted * | |
9 | * without fee, provided that the above copyright notice appears in all * | |
10 | * copies and that both the copyright notice and this permission notice * | |
11 | * appear in the supporting documentation. The authors make no claims * | |
12 | * about the suitability of this software for any purpose. It is * | |
13 | * provided "as is" without express or implied warranty. * | |
14 | **************************************************************************/ | |
15 | ||
16 | /* $Id$ */ | |
17 | ||
18 | /////////////////////////////////////////////////////////////////////////////// | |
19 | /// | |
20 | /// This is a service class for packing and unpacking bits in a 32 bit word. | |
21 | /// | |
22 | /////////////////////////////////////////////////////////////////////////////// | |
23 | ||
24 | #include <TError.h> | |
25 | ||
26 | #include "AliBitPacking.h" | |
27 | ||
28 | ||
29 | ClassImp(AliBitPacking) | |
30 | ||
31 | ||
32 | //_____________________________________________________________________________ | |
33 | Bool_t AliBitPacking::PackWord(UInt_t data, UInt_t &word, | |
34 | Int_t startBit, Int_t stopBit) | |
35 | { | |
36 | // Packs data into the word buffer from startBit bit up to stopBit bit | |
37 | ||
38 | // check that startBit and stopBit are reasonable | |
39 | if (startBit > stopBit) { | |
40 | ::Error("AliBitPacking::PackWord", | |
41 | "startBit is larger than stopBit"); | |
42 | return kFALSE; | |
43 | } | |
44 | if (stopBit > 31) { | |
45 | ::Error("AliBitPacking::PackWord", | |
46 | "stopBit exceeds valid range of 32 bits"); | |
47 | return kFALSE; | |
48 | } | |
49 | ||
50 | // create a word with the bits 0 to (stopBit-startBit) set | |
51 | UInt_t bits = 0xFFFFFFFF; | |
52 | if (stopBit-startBit < 31) bits = (1 << (stopBit-startBit+1)) - 1; | |
53 | ||
54 | // check that the data fits into the given bit range | |
55 | if (data > bits){ | |
56 | ::Error("AliBitPacking::PackWord", | |
57 | "Word to be filled is not within desired length"); | |
58 | return kFALSE; | |
59 | } | |
60 | ||
61 | // clear the bits from startBit to stopBit | |
62 | word &= (0xFFFFFFFF ^ (bits << startBit)); | |
63 | ||
64 | // fill in the data bits | |
65 | word |= (data << startBit); | |
66 | ||
67 | return kTRUE; | |
68 | } | |
69 | ||
70 | //_____________________________________________________________________________ | |
71 | UInt_t AliBitPacking::UnpackWord(UInt_t word, Int_t startBit, Int_t stopBit) | |
72 | { | |
73 | // Unpacks data of stopBit-startBit+1 bits from word buffer starting from | |
74 | // the position indicated by startBit | |
75 | ||
76 | // check that startBit and stopBit are reasonable | |
77 | if (startBit > stopBit) { | |
78 | ::Error("AliBitPacking::UnpackWord", | |
79 | "startBit is larger than stopBit"); | |
80 | return 0xFFFFFFFF; | |
81 | } | |
82 | if (stopBit > 31) { | |
83 | ::Error("AliBitPacking::UnpackWord", | |
84 | "stopBit exceeds valid range of 32 bits"); | |
85 | return 0xFFFFFFFF; | |
86 | } | |
87 | ||
88 | // create a word with the bits 0 to (stopBit-startBit) set | |
89 | UInt_t bits = 0xFFFFFFFF; | |
90 | if (stopBit-startBit < 31) bits = (1 << (stopBit-startBit+1)) - 1; | |
91 | ||
92 | // pick out the requested bits | |
93 | return ((word >> startBit) & bits); | |
94 | } |