mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-13 05:17:13 +02:00
implemented proper alignment in reorder function before casting to uint64_t ptr
This commit is contained in:
@ -573,18 +573,35 @@ void DataProcessor::Reorder(size_t &size, char *data) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto *source = (uint64_t *)(data + nAnalogDataBytes + ctbDbitOffset);
|
// make sure data is aligned to 8 bytes before casting to uint64_t
|
||||||
// TODO: leads to unaligned data
|
char *ptr = data + nAnalogDataBytes + ctbDbitOffset;
|
||||||
|
uint64_t *source = nullptr;
|
||||||
|
using AlignedBuffer =
|
||||||
|
std::aligned_storage<sizeof(uint64_t), alignof(uint64_t)>::type;
|
||||||
|
std::unique_ptr<AlignedBuffer[]> tempbuffer;
|
||||||
|
|
||||||
|
if (reinterpret_cast<uintptr_t>(ptr) % alignof(uint64_t) == 0) {
|
||||||
|
// If aligned, directly cast to uint64_t pointer
|
||||||
|
source = reinterpret_cast<uint64_t *>(ptr);
|
||||||
|
} else {
|
||||||
|
// Allocate a temporary buffer with proper alignment
|
||||||
|
tempbuffer = std::make_unique<AlignedBuffer[]>(ctbDigitalDataBytes /
|
||||||
|
sizeof(uint64_t));
|
||||||
|
|
||||||
|
std::memcpy(tempbuffer.get(), ptr, ctbDigitalDataBytes);
|
||||||
|
source = reinterpret_cast<uint64_t *>(tempbuffer.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
// auto *source = (uint64_t *)(data + nAnalogDataBytes + ctbDbitOffset);
|
||||||
|
// TODO: leads to unaligned data
|
||||||
|
|
||||||
const size_t numDigitalSamples = (ctbDigitalDataBytes / sizeof(uint64_t));
|
const size_t numDigitalSamples = (ctbDigitalDataBytes / sizeof(uint64_t));
|
||||||
|
|
||||||
size_t numBytesPerBit =
|
size_t numBytesPerBit =
|
||||||
0; // number of bytes per bit in digital data after reordering
|
(numDigitalSamples % 8 == 0)
|
||||||
|
? numDigitalSamples / 8
|
||||||
if ((numDigitalSamples % 8) == 0)
|
: numDigitalSamples / 8 +
|
||||||
numBytesPerBit = numDigitalSamples / 8;
|
1; // number of bytes per bit in digital data after reordering
|
||||||
else
|
|
||||||
numBytesPerBit = numDigitalSamples / 8 + 1;
|
|
||||||
|
|
||||||
size_t totalNumBytes =
|
size_t totalNumBytes =
|
||||||
numBytesPerBit *
|
numBytesPerBit *
|
||||||
@ -595,10 +612,13 @@ void DataProcessor::Reorder(size_t &size, char *data) {
|
|||||||
|
|
||||||
int bitoffset = 0;
|
int bitoffset = 0;
|
||||||
// reorder
|
// reorder
|
||||||
|
int count = 0;
|
||||||
|
int written = 0;
|
||||||
for (size_t bi = 0; bi < 64; ++bi) {
|
for (size_t bi = 0; bi < 64; ++bi) {
|
||||||
|
|
||||||
if (bitoffset != 0) {
|
if (bitoffset != 0) {
|
||||||
bitoffset = 0;
|
bitoffset = 0;
|
||||||
|
++count;
|
||||||
++dest;
|
++dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -606,11 +626,12 @@ void DataProcessor::Reorder(size_t &size, char *data) {
|
|||||||
uint8_t bit = (*ptr >> bi) & 1;
|
uint8_t bit = (*ptr >> bi) & 1;
|
||||||
*dest |= bit << bitoffset; // most significant bits will be padded
|
*dest |= bit << bitoffset; // most significant bits will be padded
|
||||||
++bitoffset;
|
++bitoffset;
|
||||||
|
|
||||||
if (bitoffset == 8) {
|
if (bitoffset == 8) {
|
||||||
bitoffset = 0;
|
bitoffset = 0;
|
||||||
|
++count;
|
||||||
++dest;
|
++dest;
|
||||||
}
|
}
|
||||||
|
++written;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,8 +39,8 @@ class GeneralDataTest : public GeneralData {
|
|||||||
// dummy DataProcessor class for testing
|
// dummy DataProcessor class for testing
|
||||||
class DataProcessorTest : public DataProcessor {
|
class DataProcessorTest : public DataProcessor {
|
||||||
public:
|
public:
|
||||||
DataProcessorTest() : DataProcessor(0){};
|
DataProcessorTest() : DataProcessor(0) {};
|
||||||
~DataProcessorTest(){};
|
~DataProcessorTest() {};
|
||||||
void ArrangeDbitData(size_t &size, char *data) {
|
void ArrangeDbitData(size_t &size, char *data) {
|
||||||
DataProcessor::ArrangeDbitData(size, data);
|
DataProcessor::ArrangeDbitData(size, data);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user