Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:42

0001 #include <cassert>
0002 #include "SPack.hh"
0003 
0004 struct C4 
0005 {
0006     unsigned char x, y, z, w ; 
0007 }; 
0008 
0009 struct u2
0010 {
0011     unsigned x, y  ; 
0012 }; 
0013 
0014 union ucccc_t 
0015 {
0016     unsigned int u ; 
0017     C4          c4 ; 
0018 };
0019 
0020 bool SPack::IsLittleEndian()
0021 {
0022     int n = 1; 
0023     return (*(char *)&n == 1) ;
0024 }
0025 
0026 
0027 unsigned SPack::Encode(unsigned x, unsigned y, unsigned z, unsigned w)  // static 
0028 {
0029     assert( x <= 0xff ); 
0030     assert( y <= 0xff ); 
0031     assert( z <= 0xff ); 
0032     assert( w <= 0xff ); 
0033 
0034     unsigned char xc = x ; 
0035     unsigned char yc = y ; 
0036     unsigned char zc = z ; 
0037     unsigned char wc = w ; 
0038 
0039     return SPack::Encode(xc, yc, zc, wc); 
0040 }
0041 
0042 
0043 unsigned SPack::Encode(unsigned char x, unsigned char y, unsigned char z, unsigned char w)  // static 
0044 {
0045     assert( sizeof(unsigned char) == 1); 
0046     assert( sizeof(unsigned int) == 4); 
0047 
0048     ucccc_t uc ; 
0049     uc.c4.x = x ; 
0050     uc.c4.y = y ; 
0051     uc.c4.z = z ; 
0052     uc.c4.w = w ; 
0053 
0054     unsigned int value = uc.u ;   
0055     return value  ; 
0056 }
0057 unsigned SPack::Encode(const unsigned char* ptr, const unsigned num) // static
0058 {
0059     assert( num == 4 ); 
0060     unsigned char x = *(ptr+0) ; 
0061     unsigned char y = *(ptr+1) ; 
0062     unsigned char z = *(ptr+2) ; 
0063     unsigned char w = *(ptr+3) ; 
0064     return SPack::Encode( x, y, z, w ); 
0065 }
0066 
0067 
0068 unsigned SPack::Encode13(unsigned char c, unsigned int ccc)  // static 
0069 {
0070     assert( (ccc & (0xff << 24)) == 0 ); 
0071     unsigned int value = ccc | ( c << 24 ) ; 
0072     return value  ; 
0073 }
0074 
0075 void SPack::Decode13( const unsigned int value, unsigned char& c, unsigned int& ccc ) // static
0076 {
0077     c = ( value >> 24 ) & 0xff ;  
0078     ccc = value & 0xffffff ; 
0079 }
0080 
0081 
0082 
0083 
0084 
0085 
0086 
0087 
0088 unsigned SPack::Encode22(unsigned a, unsigned b)  // static 
0089 {
0090     assert( sizeof(unsigned) == 4 ); 
0091     assert( (a & 0xffff0000) == 0 ); 
0092     assert( (b & 0xffff0000) == 0 ); 
0093     unsigned packed = ( a << 16 ) | ( b << 0 ) ; 
0094     return packed  ; 
0095 }
0096 void SPack::Decode22( const unsigned packed, unsigned& hi, unsigned& lo ) // static
0097 {
0098     assert( sizeof(unsigned) == 4 ); 
0099     hi = ( packed & 0xffff0000 ) >> 16 ;  
0100     lo = ( packed & 0x0000ffff ) >>  0 ;
0101 }
0102 
0103 unsigned SPack::Decode22a( const unsigned packed ) // static
0104 {
0105     return ( packed & 0xffff0000 ) >> 16  ; 
0106 }
0107 unsigned SPack::Decode22b( const unsigned packed ) // static
0108 {
0109     return ( packed & 0x0000ffff ) >>  0 ; 
0110 }
0111 
0112 /**
0113 SPack::Decode22hilo SPack::Decode22hi SPack::Decode22lo
0114 -----------------------------------------------------------
0115 
0116 Signed variants of SPack::Decode22 SPack::Decode22a and SPack::Decode22b
0117 
0118 **/
0119 
0120 unsigned SPack::Encode22hilo( int a, int b )
0121 {
0122     assert( a >= -0x8000 && a <= 0x7fff );   // 16 bit signed range     0x8000 - 0x10000 = -0x8000
0123     assert( b >= -0x8000 && b <= 0x7fff ); 
0124     unsigned packed = (( a & 0x0000ffff ) << 16 ) | (( b & 0x0000ffff ) << 0 ) ;  
0125     return packed ; 
0126 }
0127 
0128 void SPack::Decode22hilo( const unsigned packed, int& a, int& b ) // static
0129 {
0130     unsigned hi = ( packed & 0xffff0000 ) >> 16 ;
0131     unsigned lo = ( packed & 0x0000ffff ) >>  0 ;
0132 
0133     a = hi <= 0x7fff ? hi : hi - 0x10000 ;    // 16-bit twos complement
0134     b = lo <= 0x7fff ? lo : lo - 0x10000 ;    
0135 }
0136 
0137 int SPack::Decode22hi( const unsigned packed ) // static
0138 {
0139     unsigned hi = ( packed & 0xffff0000 ) >> 16 ;
0140     return hi <= 0x7fff ? hi : hi - 0x10000 ;    // 16-bit twos complement
0141 }
0142 int SPack::Decode22lo( const unsigned packed ) // static
0143 {
0144     unsigned lo = ( packed & 0x0000ffff ) >>  0 ;
0145     return lo <= 0x7fff ? lo : lo - 0x10000 ;    // 16-bit twos complement
0146 }
0147 
0148 
0149 
0150 
0151 void SPack::Decode( const unsigned int value,  unsigned& x, unsigned& y, unsigned& z, unsigned& w ) // static
0152 {
0153     unsigned char ucx ; 
0154     unsigned char ucy ; 
0155     unsigned char ucz ; 
0156     unsigned char ucw ; 
0157 
0158     Decode(value, ucx, ucy, ucz, ucw ); 
0159 
0160     x = ucx ; 
0161     y = ucy ; 
0162     z = ucz ; 
0163     w = ucw ; 
0164 }
0165 
0166 
0167 void SPack::Decode( const unsigned int value,  unsigned char& x, unsigned char& y, unsigned char& z, unsigned char& w ) // static
0168 {
0169     assert( sizeof(unsigned char) == 1); 
0170     assert( sizeof(unsigned int) == 4); 
0171 
0172     ucccc_t uc ; 
0173     uc.u = value ; 
0174     x = uc.c4.x ; 
0175     y = uc.c4.y ; 
0176     z = uc.c4.z ; 
0177     w = uc.c4.w ; 
0178 }
0179 
0180 void SPack::Decode( const unsigned int value,  unsigned char* ptr, const unsigned num ) // static
0181 {
0182     assert( num == 4); 
0183     assert( sizeof(unsigned char) == 1); 
0184     assert( sizeof(unsigned int) == 4); 
0185 
0186     ucccc_t uc ; 
0187     uc.u = value ; 
0188      
0189     *(ptr + 0) = uc.c4.x ; 
0190     *(ptr + 1) = uc.c4.y ; 
0191     *(ptr + 2) = uc.c4.z ; 
0192     *(ptr + 3) = uc.c4.w ; 
0193 }
0194 
0195 
0196 
0197 float SPack::int_as_float(const int i)
0198 {
0199     uif_t uif ; 
0200     uif.i = i ; 
0201     return uif.f ; 
0202 }
0203 int SPack::int_from_float(const float f)
0204 {
0205     uif_t uif ; 
0206     uif.f = f ; 
0207     return uif.i ; 
0208 }
0209 float SPack::uint_as_float(const unsigned i)
0210 {
0211     uif_t uif ; 
0212     uif.i = i ; 
0213     return uif.f ; 
0214 }
0215 unsigned SPack::uint_from_float(const float f)
0216 {
0217     uif_t uif ; 
0218     uif.f = f ; 
0219     return uif.u ; 
0220 }
0221 
0222 
0223 float SPack::unsigned_as_float( const unsigned u ) 
0224 {
0225     union { unsigned u; int i; float f; } uif ;   
0226     uif.u = u  ;   
0227     return uif.f ; 
0228 }
0229 
0230 double SPack::unsigned_as_double( const unsigned x, const unsigned y ) 
0231 {
0232     union { u2 uu ; double d ; } uud ;   
0233     uud.uu.x = x  ;   
0234     uud.uu.y = y  ;   
0235     return uud.d ; 
0236 }
0237 
0238 void SPack::double_as_unsigned(unsigned& x, unsigned& y, const double d ) 
0239 {
0240     union { u2 uu ; double d ; } uud ;   
0241     uud.d = d ; 
0242     x = uud.uu.x ;
0243     y = uud.uu.y ;   
0244 }
0245 
0246 
0247 
0248 
0249 unsigned SPack::float_as_unsigned( const float f ) 
0250 {
0251     union { unsigned u; int i; float f; } uif ;   
0252     uif.f = f  ;   
0253     return uif.u ; 
0254 }
0255 
0256 
0257 
0258 
0259 
0260 
0261 /**
0262 SPack::unsigned_as_int
0263 -----------------------
0264 
0265 The bits of unsigned integers can hold the bits of a signed int without problem 
0266 (within the signed range), thus can reinterpret those bits as a signed integer 
0267 using twos-complement.  Notice how the number of bits is relevant to the bit field 
0268 representation of negative integers in a way that is not the case for positive ones.
0269 
0270 **/
0271 
0272 template <int NUM_BITS>
0273 int SPack::unsigned_as_int(unsigned value)   // static
0274 {  
0275     unsigned twos_complement_sum = 0x1 << NUM_BITS ;   
0276     unsigned signed_max =  (0x1 << (NUM_BITS-1)) - 1  ; 
0277     int ivalue = value <= signed_max ? value : value - twos_complement_sum ; 
0278     return ivalue ; 
0279 }
0280 
0281 
0282 
0283 template int SPack::unsigned_as_int<8>(unsigned value) ;
0284 template int SPack::unsigned_as_int<16>(unsigned value) ;
0285 
0286 
0287 int SPack::unsigned_as_int_32(unsigned value)
0288 {
0289     uif_t uif ; 
0290     uif.u = value ; 
0291     return uif.i ; 
0292 }
0293 int SPack::unsigned_as_int_16(unsigned value)
0294 {
0295     ui16_t ui ; 
0296     ui.u = value ; 
0297     return ui.i ; 
0298 }
0299 
0300 
0301 
0302 
0303