10 #define UINT32 uint32_t 
   11 #define UINT16 uint16_t 
   17 #define code_ped4 TileRawChannel2Bytes5::code_ped4 
   18 #define code_ped5 TileRawChannel2Bytes5::code_ped5 
   19 #define code_amp5 TileRawChannel2Bytes5::code_amp5 
   20 #define code_amp6 TileRawChannel2Bytes5::code_amp6 
   21 #define code_raws TileRawChannel2Bytes5::code_raws 
   22 #define code_rawf TileRawChannel2Bytes5::code_rawf 
   23 #define code_full TileRawChannel2Bytes5::code_full 
   24 #define code_dump TileRawChannel2Bytes5::code_dump 
   25 #define code_null TileRawChannel2Bytes5::code_null 
   32 const int OF_E_FRACBITS[4] = { 4, 5, 5, -5 };
 
   36 const unsigned short ampcorr_lookup[64] = {
 
   37   0x505A, 0x4F62, 0x4E71, 0x4D87, 0x4CA4, 0x4BC8, 0x4AF4, 0x4A26,
 
   38   0x4960, 0x48A1, 0x47EA, 0x473A, 0x4692, 0x45F1, 0x4558, 0x44C7,
 
   39   0x443D, 0x43BC, 0x4342, 0x42D0, 0x4267, 0x4205, 0x41AC, 0x415B,
 
   40   0x4112, 0x40D2, 0x409A, 0x406B, 0x4044, 0x4026, 0x4011, 0x4004,
 
   41   0x4000, 0x4005, 0x4013, 0x402A, 0x404A, 0x4073, 0x40A6, 0x40E2,
 
   42   0x4127, 0x4175, 0x41CD, 0x422F, 0x429A, 0x430E, 0x438D, 0x4415,
 
   43   0x44A7, 0x4543, 0x45E9, 0x4699, 0x4753, 0x4817, 0x48E6, 0x49BF,
 
   44   0x4AA2, 0x4B8F, 0x4C87, 0x4D8A, 0x4E97, 0x4FAF, 0x50D2, 0x51FF
 
   50   a = 
a & 0x1F; 
b = 
b & 0x1F;
 
   54   a = 
a & 0x1F; 
b = 
b & 0x1F;
 
   58   return _ext(
d, ab >> 5, ab);
 
   74 unsigned char getbyte(
const unsigned char* 
ptr) {
 
   76   uintptr_t 
u = 
reinterpret_cast<uintptr_t
> (
ptr);
 
   77   u = 
u + 3 - 2*(
u % 4);
 
   78   ptr = 
reinterpret_cast<const unsigned char*
> (
u);
 
   88 uint8_t _mem1(
const char* 
ptr)     { 
return _mem1((
const unsigned char*) 
ptr); }
 
   97   return (b0 << 8) | b1;
 
   99 uint16_t _mem2(
const char* 
ptr)     { 
return _mem2((
const unsigned char*) 
ptr); }
 
  103   unsigned char b0, b1, b2, b3;
 
  108   return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
 
  112 uint32_t _mem4(
const char* 
ptr)     { 
return _mem4((
const unsigned char*) 
ptr); }
 
  123   while ((
u == (
d & 0x80000000)) && (
n < 31)) {
 
  132 int32_t _sadd(int32_t 
a, int32_t 
b) {
 
  134   if (
a > 0 && 
b > 0 && 
u < 0) 
u = 0x7FFFFFFF; 
else 
  135   if (
a < 0 && b < 0 && u > 0) 
u = 0x80000000;
 
  143     if (
a > 0) 
u = 0x7FFFFFFF; 
else u = 0x80000000;
 
  149   int16_t a16 = (
a & 0xFFFF), b16 = (
b & 0xFFFF);
 
  150   int32_t a32 = a16, b32 = b16; 
 
  157   return _mpy(
a, 
b) + _mpyh(
a, 
b);
 
  160   int64_t a64 = 
a, b64 = 
b;
 
  161   int64_t 
u = 0x100000000LL;
 
  162   if (a64 - b64 > 
u - 1) 
return (
u - 1) & 0xFFFFFFFF;
 
  163   if (a64 - b64 < -
u) 
return -
u & 0xFFFFFFFF;
 
  164   return (a64 - b64) & 0xFFFFFFFF;
 
  170   k = 1; 
k0 = 1; k1 = 1 << 16;
 
  172   for (
int i = 0; 
i < 16; ++
i) {
 
  199 #define sbits(u, n) (32 - 1 - (n) < _norm(u)) 
  200 #define mvbits(u, n, i) _extu(u, 32 - (n), 32 - (n + i)) 
  201 #define mvbits_ex(u, n, i, i0) mvbits(mvbits(u, n + i0, -(i0)), n, i) 
  202 #define nonsat_ene(ene) ((0 < ene) && (ene < 0x7FFF)) 
  205 #define _round(u, k) (_ext(u + _sshl(0x1, k - 1), 0, k)) 
  207 #define ssatbits(u, n) _ext(_sshl(u, 32 - (n)), 0, 32 - (n)) 
  210 #define saturate(u, n) _ext(_sshl(u + _abs(u), 30 - (n)), 0, 31 - (n)) 
  216 inline int _roundf(
float f) {
 
  217   if (
f > 0) 
return (
int)(
f + 0.5);
 
  218   else return (
int)(
f - 0.5);
 
  229   int FRACBITS_MINUS_11 = _sadd(OF_E_FRACBITS[
unit], 11);
 
  231   UINT16 calib_scale   = _extu(
calib, 16, 16) - FRACBITS_MINUS_11;
 
  232   UINT16 uncalib_coeff = (
int)((1.0/calib_coeff)*(1 << 14)*(1 << 14) + 0.5);
 
  233   UINT16 uncalib_scale = 17 - calib_scale;
 
  235   UINT32 uncalib = _pack2(uncalib_coeff, uncalib_scale);
 
  241   return _packhl2(_extr(hg, shift), hg);
 
  250   double ampcorr = ((0.0000006467*
time + 0.0002713687)*
time + 0.0000365732)*
time + 1;
 
  251   return _roundf(ampcorr*(0
x1 << 14));
 
  255 void print_ampcorr_lookup() {
 
  259   printf(
"// ampcorr lookup table, step = 0.5 ns\n");
 
  260   printf(
"unsigned short ampcorr_lookup[64] = {"); 
 
  262     UINT16 ampcorr_coeff = get_ampcorr_coeff(
time);
 
  263     if (
k % 8 == 0) printf(
"\n ");
 
  265     printf(
" 0x%04X,", ampcorr_coeff);
 
  272 #define unpack_diff(diff, nbits, d2, d3, d5, d6, d7)    \ 
  274   UINT32 u = _deal(_deal((diff >> nbits)));             \ 
  275   d5 = _ext(diff, 32 - nbits, 32 - nbits);              \ 
  276   d7 = _ext(u,  8 - nbits, 32 - nbits);                 \ 
  277   d6 = _ext(u, 16 - nbits, 32 - nbits);                 \ 
  278   d3 = _ext(u, 24 - nbits, 32 - nbits);                 \ 
  279   d2 = _ext(u, 32 - nbits, 32 - nbits);                 \ 
  282 #define fill_samp(s1, s2, s3, s4, s5, s6, s7, s)        \ 
  300   INT32  amp_reco = _extr(_mpyhl(uncalib, ene0), uncalib);
 
  305   int s1, 
int s2, 
int s3, 
int s5, 
int s6, 
int s7)
 
  310   UINT32 s1x, s32, s5x, s76;
 
  311   UINT32 a1x, a32, a54, a76;
 
  312   int amp, amp_reco, a4, 
s4;
 
  316   s32 = _pack2(
s3, 
s2);
 
  318   s76 = _pack2(s7, s6);
 
  330   amp = _mpyh (a1x, s1x)
 
  335   a4s4 = amp_reco - amp;
 
  336   a4 = _ext(a54, 16, 16);
 
  337   s4 = _roundf(a4s4/a4);
 
  342 inline UINT32 pop_buf(
const unsigned char** pptr_buff, 
int size)
 
  346   *pptr_buff += (
size) >> 3;
 
  351   int s1, 
int s2, 
int s3, 
int s5, 
int s6, 
int s7)
 
  353   return get_s4(ofw, 
unit, 
chan, 
gain, ene, 
s1, 
s2, 
s3, s5, s6, s7);
 
  357   int s1, 
int d2, 
int d3, 
int d5, 
int d6, 
int d7, 
int s[])
 
  359   int s2, 
s3, 
s4, s5, s6, s7;
 
  360   INT16 amp2, ampcorr, a_scale;
 
  361   int amptime, amp_reco;
 
  367   a_scale = _extu(
ofc[0], 16, 16); 
 
  369   amp2 = (
INT16) (amp_reco >> a_scale);
 
  371   ampcorr = ampcorr_lookup[
time + 32];
 
  372   amp2 = _mpy(amp2, ampcorr) >> 14;
 
  373   amptime  = _sshl(
_round(_mpy(amp2, 
time), 3), 16);
 
  374   wamptime = _packhl2(-amptime, amp2);
 
  378     UINT16 h_scale = _extu(
ofc[13], 0, 16) - 1;
 
  379         UINT16 shift   = h_scale - g_scale;
 
  390     int q2 = _extr(_dotp2(repack_hg(hg2, shift), wamptime), g_scale);
 
  391     int q3 = _extr(_dotp2(repack_hg(hg3, shift), wamptime), g_scale);
 
  392     int q5 = _extr(_dotp2(repack_hg(hg5, shift), wamptime), g_scale);
 
  393     int q6 = _extr(_dotp2(repack_hg(hg6, shift), wamptime), g_scale);
 
  394     int q7 = _extr(_dotp2(repack_hg(hg7, shift), wamptime), g_scale);
 
  402   s4 = get_s4(ofw, 
unit, 
chan, 
gain, ene, 
s1, 
s2, 
s3, s5, s6, s7);
 
  408   int s1, 
int s2, 
int s3, 
int s5, 
int s6, 
int s7, 
int s[])
 
  410   int s4 = get_s4(ofw, 
unit, 
chan, 
gain, ene, 
s1, 
s2, 
s3, s5, s6, s7);
 
  415 int unpack_null(
const UINT32* , 
int , 
const UINT32** pptr_reco,
 
  427   for (
i = 0; 
i < 7; 
i++)
 
  428     s[
i] = (
reco >> 1) & 0x3FF;
 
  434   const UINT32** pptr_reco, 
const unsigned char** pptr_buff,
 
  439   int s1, 
s2, 
s3, 
s4, s5, s6, s7, 
d2, d3, d5, d6, d7;
 
  454     data = pop_buf(pptr_buff, 8);
 
  461     data = pop_buf(pptr_buff, 16);
 
  473   s4 = calc_ped_s4(ofw, 
unit, 
chan, *
gain, *ene, 
s1, 
s2, 
s3, s5, s6, s7);
 
  479   const UINT32** pptr_reco, 
const unsigned char** pptr_buff,
 
  484   int s1 = 0, 
d2, d3, d5, d6, d7;
 
  493   *
time = _ext(
reco, 32 - 15 - 6,  32 - 6);
 
  498     diff = _extu(pop_buf(pptr_buff, 24), 1, 0) | 
diff;
 
  504     diff = pop_buf(pptr_buff, 32);
 
  505     s1 = _extu(
diff, 32 - 2, 32 - 2 - 5) | 
s1;
 
  512   calc_amp(ofw, 
unit, 
chan, *
gain, *ene, *
time, 
s1, 
d2, d3, d5, d6, d7, 
s);
 
  517   const UINT32** pptr_reco, 
const unsigned char** pptr_buff,
 
  520   int s1, 
s2, 
s3, s5, s6, s7;
 
  534     diff1 = pop_buf(pptr_buff, 32);
 
  535     diff2 = pop_buf(pptr_buff,  8);
 
  540     s6 = 
mvbits_ex(diff1, 9, 0, 10 + 10 + 3);
 
  545     diff1 = pop_buf(pptr_buff, 32);
 
  546     diff2 = pop_buf(pptr_buff, 16);
 
  556   calc_raw(ofw, 
unit, 
chan, *
gain, *ene, 
s1, 
s2, 
s3, s5, s6, s7, 
s);
 
  560 int unpack_full(
const UINT32* , 
int ,
 
  561   const UINT32** pptr_reco, 
const unsigned char** pptr_buff,
 
  571   diff1 = pop_buf(pptr_buff, 32);
 
  572   diff2 = pop_buf(pptr_buff, 16);
 
  576   *ene  = (overflow ? 0x7FFF : 0) - ((*
gain == 0) ? 512 : 2048);
 
  590 int unpack_dump(
const UINT32* , 
int ,
 
  591   const UINT32** pptr_reco, 
const unsigned char** pptr_buff,
 
  600   diff1 = pop_buf(pptr_buff, 32);
 
  601   diff2 = pop_buf(pptr_buff, 24);
 
  629   int amp, ene, ene0, ene_shift;
 
  633   int FRACBITS_MINUS_11 = _sadd(OF_E_FRACBITS[
unit], 11);
 
  635   int s1 = 
s[0], 
s2 = 
s[1], 
s3 = 
s[2], 
s4 = 
s[3], s5 = 
s[4], s6 = 
s[5], s7 = 
s[6];
 
  641   UINT32 s76 = _pack2(s7, s6);
 
  643   ene_shift = 512 + _mpy(
gain, 2048 - 512);
 
  654   amp = _mpyh (a1x, s1x)
 
  662   calib_coeff = _ext(
calib, 0, 16);
 
  663   calib_scale = _ssub(_extu(
calib, 16, 16) , FRACBITS_MINUS_11);
 
  665   ene0 = 
_round(ene0*calib_coeff, calib_scale);
 
  666   ene = ene0 + ene_shift;
 
  674   int unit    = _extu(ptr_frag[2], 0, 32 - 2); 
 
  675   int size_L2 = _extu(ptr_frag[2], 2, 32 - 3); 
 
  678   const UINT32* ptr_reco = ptr_frag + 3; 
 
  679   const unsigned char* ptr_buff = 
reinterpret_cast<const unsigned char*
> (ptr_frag + 3 + 48 + size_L2); 
 
  684   const unsigned char* ptr_ped4;
 
  685   const unsigned char* ptr_ped5;
 
  686   const unsigned char* ptr_amp5;
 
  687   const unsigned char* ptr_amp6;
 
  688   const unsigned char* ptr_raws;
 
  689   const unsigned char* ptr_rawf;
 
  690   const unsigned char* ptr_full;
 
  691   const unsigned char* ptr_dump;
 
  693   int cnt_ped4, cnt_ped5, cnt_amp5, cnt_amp6, 
 
  694       cnt_raws, cnt_rawf, cnt_full, cnt_null, cnt_dump;
 
  695   cnt_ped4 = cnt_ped5 = cnt_amp5 = cnt_amp6 = 
 
  696   cnt_raws = cnt_rawf = cnt_full = cnt_null = cnt_dump = 0;
 
  698   bad_bits[0] = pop_buf(&ptr_buff, 16);
 
  699   bad_bits[1] = pop_buf(&ptr_buff, 16);
 
  700   bad_bits[2] = pop_buf(&ptr_buff, 16);
 
  703     for (
i = 0; 
i < 7; ++
i) 
u[
i] = 0;
 
  717   ptr_ped5 = ptr_ped4 + cnt_ped4*1; 
 
  718   ptr_amp5 = ptr_ped5 + cnt_ped5*2; 
 
  719   ptr_amp6 = ptr_amp5 + cnt_amp5*3; 
 
  720   ptr_raws = ptr_amp6 + cnt_amp6*4; 
 
  721   ptr_rawf = ptr_raws + cnt_raws*5; 
 
  722   ptr_full = ptr_rawf + cnt_rawf*6; 
 
  723   ptr_dump = ptr_full + cnt_full*6; 
 
  726     if (
chan % 16 == 0) bad16 = ~bad_bits[
chan/16]; 
 
  727     bad = (bad16 & 0x1); bad16 >>= 1;
 
  740     if (
size == 0) printf(
"\nTileRawChannel2Bytes5::unpack ERROR size = 0\n");
 
  753     for (
i = 0; 
i < 7; ++
i) ChanData[
chan].
s[
i] = 
u[
i];
 
  761   int gain, ene, 
u[7], 
s[7];
 
  779       s[1-1] = _extu(s1x,  0, 16);
 
  780       s[2-1] = _extu(s32, 16, 16);
 
  781       s[3-1] = _extu(s32,  0, 16);
 
  782       s[4-1] = _extu(s54, 16, 16);
 
  783       s[5-1] = _extu(s54,  0, 16);
 
  784       s[6-1] = _extu(s76, 16, 16);
 
  785       s[7-1] = _extu(s76,  0, 16);
 
  788     for (
i = 0; 
i < 7; ++
i) {
 
  789       if (
s[
i] != 
u[
i]) chOK = 
false;
 
  791     if (!chOK) { printf(
"\n %d: Raw Data ERROR", 
chan); }
 
  793     ene += (
gain == 0 ? 512 : 2048);
 
  795     if (of_energy[
chan] != ene) {
 
  797       printf(
"\n %d : amp error ene: %d  e_t_amp: %d\n", 
chan, ene, of_energy[
chan]);
 
  802       printf(
"\n %2d ERROR", 
chan);
 
  805   printf(
"\nunpack_data");
 
  806   if (OK) printf(
" OK"); 
else printf(
" ERROR");
 
  823       printf(
"\nTileRawChannel2Bytes5::unpack_reco_bin ERROR\n");
 
  829     if (of_energy[
chan] != ene) {