Skip to content

Commit dbf24c7

Browse files
committed
Compact 1st-entry / n-entries template class
1 parent a5af471 commit dbf24c7

4 files changed

Lines changed: 87 additions & 1 deletion

File tree

DataFormats/common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ O2_GENERATE_LIBRARY()
2323

2424
set(TEST_SRCS
2525
test/testTimeStamp.cxx
26+
test/testRangeRef.cxx
2627
)
2728

2829
O2_GENERATE_TESTS(

DataFormats/common/include/CommonDataFormat/RangeReference.h

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#ifndef ALICEO2_RANGEREFERENCE_H
1616
#define ALICEO2_RANGEREFERENCE_H
1717

18-
#include <Rtypes.h>
18+
#include "GPUCommonRtypes.h"
1919

2020
namespace o2
2121
{
@@ -41,13 +41,57 @@ class RangeReference
4141
void setFirstEntry(FirstEntry ent) { mFirstEntry = ent; }
4242
void setEntries(NElem n) { mEntries = n; }
4343
void changeEntriesBy(NElem inc) { mEntries += inc; }
44+
bool operator==(const RangeReference& other) const
45+
{
46+
return mFirstEntry == other.mFirstEntry && mEntries == other.mEntries;
47+
}
4448

4549
private:
4650
FirstEntry mFirstEntry; ///< 1st entry of the group
4751
NElem mEntries = 0; ///< number of entries
4852

4953
ClassDefNV(RangeReference, 1);
5054
};
55+
56+
// Compact (32bit long) range reference
57+
template <int NBitsN>
58+
class RangeRefComp
59+
{
60+
using Base = std::uint32_t;
61+
62+
private:
63+
static constexpr int NBitsTotal = sizeof(Base) * 8;
64+
static constexpr Base MaskN = ((0x1 << NBitsN) - 1);
65+
static constexpr Base MaskR = (~Base(0)) & (~MaskN);
66+
Base mData = 0; ///< packed 1st entry reference + N entries
67+
void sanityCheck()
68+
{
69+
static_assert(NBitsN < NBitsTotal, "NBitsN too large");
70+
}
71+
72+
public:
73+
RangeRefComp(int ent, int n) { set(ent, n); }
74+
RangeRefComp() = default;
75+
RangeRefComp(const RangeRefComp& src) = default;
76+
void set(int ent, int n)
77+
{
78+
mData = (Base(ent) << NBitsN) + (Base(n) & MaskN);
79+
}
80+
static constexpr Base getMaxFirstEntry() { return MaskR >> NBitsN; }
81+
static constexpr Base getMaxEntries() { return MaskN; }
82+
int getFirstEntry() const { return mData >> NBitsN; }
83+
int getEntries() const { return mData & ((0x1 << NBitsN) - 1); }
84+
void setFirstEntry(int ent) { mData = (Base(ent) << NBitsN) | (mData & MaskN); }
85+
void setEntries(int n) { mData = (mData & MaskR) | (Base(n) & MaskN); }
86+
void changeEntriesBy(int inc) { setEntries(getEntries() + inc); }
87+
bool operator==(const RangeRefComp& other) const
88+
{
89+
return mData == other.mData;
90+
}
91+
92+
ClassDefNV(RangeRefComp, 1);
93+
};
94+
5195
} // namespace dataformats
5296
} // namespace o2
5397

DataFormats/common/src/CommonDataFormatLinkDef.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#pragma link C++ class o2::dataformats::RangeReference < int, int > +;
3434
#pragma link C++ class o2::dataformats::RangeReference < o2::dataformats::EvIndex < int, int >, int > +;
3535

36+
#pragma link C++ class o2::dataformats::RangeRefComp < 4> + ; // reference to a set with 15 entries max (ITS clusters)
37+
3638
#pragma link C++ class o2::InteractionRecord + ;
3739
#pragma link C++ class o2::InteractionTimeRecord + ;
3840
#pragma link C++ class o2::BunchFilling + ;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
#define BOOST_TEST_MODULE Test TimeRangeRef class
12+
#define BOOST_TEST_MAIN
13+
#define BOOST_TEST_DYN_LINK
14+
#include <boost/test/unit_test.hpp>
15+
#include "CommonDataFormat/RangeReference.h"
16+
#include <FairLogger.h>
17+
18+
namespace o2
19+
{
20+
21+
// basic TimeStamp tests
22+
BOOST_AUTO_TEST_CASE(RangeRef)
23+
{
24+
int ent = 1000, nent = 5;
25+
o2::dataformats::RangeReference<int, int> rangeII(ent, nent);
26+
BOOST_CHECK_EQUAL(rangeII.getFirstEntry(), ent);
27+
rangeII.changeEntriesBy(nent);
28+
BOOST_CHECK_EQUAL(rangeII.getEntries(), 2 * nent);
29+
30+
o2::dataformats::RangeRefComp<4> range4(ent, nent);
31+
BOOST_CHECK_EQUAL(range4.getFirstEntry(), ent);
32+
range4.changeEntriesBy(nent);
33+
BOOST_CHECK_EQUAL(range4.getEntries(), 2 * nent);
34+
LOG(INFO) << "MaxEntryID : " << range4.getMaxFirstEntry() << " MaxEntries: " << range4.getMaxEntries();
35+
BOOST_CHECK_EQUAL(range4.getMaxFirstEntry(), (0x1 << (32 - 4)) - 1);
36+
BOOST_CHECK_EQUAL(range4.getMaxEntries(), (0x1 << 4) - 1);
37+
}
38+
39+
} // namespace o2

0 commit comments

Comments
 (0)