9#include <ecl/utility/ECLRawDataHadron.h>
11#ifdef ECL_RAW_DATA_HADRON_STANDALONE_BUILD
15#include <framework/logging/Logger.h>
34#ifdef ECL_RAW_DATA_HADRON_STANDALONE_BUILD
36 printf(
"%s:%d: Error! Amplitude can never be negative, you have to call this function as packAmplitude(peak_amp + 128).\n",
41 B2FATAL(
"Amplitude can never be negative, you have to call this function as packAmplitude(peak_amp + 128).");
44 unsigned long long exponent;
45 unsigned long long fraction;
46 unsigned long long amp_packed = 0;
51 if ((fraction & 0x20000) != 0)
53 else if ((fraction & 0x10000) != 0)
55 else if ((fraction & 0x08000) != 0)
57 else if ((fraction & 0x04000) != 0)
59 else if ((fraction & 0x02000) != 0)
61 else if ((fraction & 0x01000) != 0)
63 else if ((fraction & 0x00800) != 0)
70 fraction += 1 << (exponent - 2);
71 fraction = (fraction >> (exponent - 1)) - (1 << 11);
80 if ((fraction & 0x00800) != 0)
83 amp_packed = (exponent << 11) | fraction;
89 unsigned int exponent = (amp_packed >> 11) & 0b111;
90 unsigned int fraction = (amp_packed) & 0b11111111111;
92 return fraction - 128;
94 return (1 << (10 + exponent)) + fraction * (1 << (exponent - 1)) - 128;
108 unsigned int exponent = 0;
109 unsigned int sign = 0;
110 int fraction = peak_time;
111 if ((fraction & 0x800) != 0) {
113 fraction = -fraction;
116 if ((fraction & 0x400) != 0)
118 else if ((fraction & 0x200) != 0)
120 else if ((fraction & 0x100) != 0)
130 fraction += 1 << (exponent - 2);
132 fraction = (fraction >> (exponent - 1)) - (1 << 8);
146 if ((fraction & 0x100) != 0)
150 int peak_time_packed = (sign << 10) | (exponent << 8) | fraction;
151 return peak_time_packed;
156 unsigned int sign = (time_packed >> 10) & 0b1;
157 unsigned int exponent = (time_packed >> 8) & 0b11;
158 unsigned int fraction = (time_packed) & 0b11111111;
165 result = ((1 << (7 + exponent)) + fraction * (1 << (exponent - 1)));
223 if (dividend >= (1 << 18) || dividend < 0) {
224 fprintf(stderr,
"\n\033[31m");
225 fprintf(stderr,
"%s:%d: Error! Dividend outside of expected range: %d\n", __FILE__, __LINE__, dividend);
226 fprintf(stderr,
"\033[0m\n");
229 if (divisor >= (1 << 19)) {
230 fprintf(stderr,
"\n\033[31m");
231 fprintf(stderr,
"%s:%d: Error! Divisor outside of expected range: %d\n", __FILE__, __LINE__, divisor);
232 fprintf(stderr,
"\033[0m\n");
245 if ((divisor & (1 << i)) != 0) {
274 dividend = dividend << 5;
276 int n_S = dividend >> p;
277 int m_S = divisor >> p;
280 const int precalculated_constants[] = {
283 2016, 1956, 1900, 1846, 1796, 1748, 1702, 1659,
284 1618, 1579, 1542, 1507, 1473, 1440, 1409, 1380,
285 1351, 1324, 1298, 1273, 1248, 1225, 1202, 1181,
286 1160, 1140, 1120, 1101, 1083, 1066, 1049, 1032
295 return ((n_S + m_S / 2) * precalculated_constants[m_S - 32]) >> 16;
302 int dividend = 4 * A_hadron + A_total;
303 if (dividend < 0)
return 0;
305 return packed_fraction;
310 return fraction_packed / 32.0 * 0.75 - 0.25;
This is a set of function for packing/unpacking the data produced by the new ShaperDSP firmware that ...
int packHadronFraction(int A_hadron, int A_total)
Hadron fraction packing.
int integer_division_32(int dividend, int divisor)
Basically the fastest algorithm for approximate integer division (with the precision (1/32)).
unsigned long long packAmplitude(long long peak_amp)
Amplitude packing.
int packTime(int peak_time)
Time packing.
int unpackTime(int time_packed)
Time unpacking (from 11 bits float to 12 bits int)
unsigned long long unpackAmplitude(unsigned long long amp_packed)
Amplitude unpacking (from 14 bits float to 18 bit int)
double unpackHadronFraction(int fraction_packed)
Hadron fraction unpacking.