ProcessCommon.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef __PROCESSCOMMON_HPP__
  2. #define __PROCESSCOMMON_HPP__
  3. #include <assert.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. template<class T>
  7. static size_t GetLeastError( const T* err, size_t num )
  8. {
  9. size_t idx = 0;
  10. for( size_t i=1; i<num; i++ )
  11. {
  12. if( err[i] < err[idx] )
  13. {
  14. idx = i;
  15. }
  16. }
  17. return idx;
  18. }
  19. static uint64_t FixByteOrder( uint64_t d )
  20. {
  21. return ( ( d & 0x00000000FFFFFFFF ) ) |
  22. ( ( d & 0xFF00000000000000 ) >> 24 ) |
  23. ( ( d & 0x000000FF00000000 ) << 24 ) |
  24. ( ( d & 0x00FF000000000000 ) >> 8 ) |
  25. ( ( d & 0x0000FF0000000000 ) << 8 );
  26. }
  27. template<class T, class S>
  28. static uint64_t EncodeSelectors( uint64_t d, const T terr[2][8], const S tsel[16][8], const uint32_t* id )
  29. {
  30. size_t tidx[2];
  31. tidx[0] = GetLeastError( terr[0], 8 );
  32. tidx[1] = GetLeastError( terr[1], 8 );
  33. d |= tidx[0] << 26;
  34. d |= tidx[1] << 29;
  35. for( int i=0; i<16; i++ )
  36. {
  37. uint64_t t = tsel[i][tidx[id[i]%2]];
  38. d |= ( t & 0x1 ) << ( i + 32 );
  39. d |= ( t & 0x2 ) << ( i + 47 );
  40. }
  41. return d;
  42. }
  43. #endif