Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // om-;TEST=SPackTest om-t 
0002 
0003 #include <vector>
0004 #include <cassert>
0005 #include <csignal>
0006 #include <iomanip>
0007 #include "SPack.hh"
0008 #include "OPTICKS_LOG.hh"
0009 
0010 void test_Encode_Decode_unsigned()
0011 {
0012     unsigned x = 0x00 ;   
0013     unsigned y = 0xaa ;   
0014     unsigned z = 0xbb ;   
0015     unsigned w = 0xff ;
0016 
0017     unsigned value_expect = 0xffbbaa00 ; 
0018     unsigned value = SPack::Encode(x,y,z,w); 
0019 
0020     LOG(info) 
0021         << " value " << std::hex << value 
0022         << " value_expect " << std::hex << value_expect
0023         ;
0024 
0025     assert( value == value_expect ); 
0026 
0027     unsigned x_ ; 
0028     unsigned y_ ; 
0029     unsigned z_ ; 
0030     unsigned w_ ; 
0031 
0032     SPack::Decode( value, x_, y_, z_, w_ ); 
0033 
0034     assert( x == x_ );   
0035     assert( y == y_ );   
0036     assert( z == z_ );   
0037     assert( w == w_ );   
0038 }
0039 
0040 
0041 void test_Encode()
0042 {
0043     unsigned char nu = 10 ; 
0044     unsigned char nv = 10 ; 
0045     unsigned char nw =  4 ; 
0046 
0047     unsigned char u = nu - 1 ; 
0048     unsigned char v = nv - 1  ; 
0049     unsigned char w = nw - 1 ; 
0050 
0051     unsigned int packed = SPack::Encode(u,v,w,0); 
0052     LOG(info) 
0053         << " u " << u 
0054         << " v " << v 
0055         << " w " << w 
0056         << " packed " << packed 
0057         ;
0058 
0059 }
0060 
0061 
0062 void test_Encode_Decode()
0063 {
0064     unsigned char x = 1 ; 
0065     unsigned char y = 128 ; 
0066     unsigned char z = 255 ; 
0067     unsigned char w = 128 ; 
0068 
0069     unsigned int value = SPack::Encode(x,y,z,w); 
0070     LOG(info) << " value " << value  ; 
0071 
0072     unsigned char x2, y2, z2, w2 ; 
0073     SPack::Decode( value, x2, y2, z2, w2 ); 
0074 
0075     assert( x == x2 ); 
0076     assert( y == y2 ); 
0077     assert( z == z2 ); 
0078     assert( w == w2 ); 
0079 }
0080 
0081 
0082 void test_Encode_Decode_ptr()
0083 {
0084     unsigned char a[4] ; 
0085     a[0] = 1 ; 
0086     a[1] = 128 ; 
0087     a[2] = 255 ; 
0088     a[3] = 128 ; 
0089 
0090     unsigned int value = SPack::Encode(a, 4); 
0091     LOG(info) << " value " << value  ; 
0092 
0093     unsigned char b[4] ; 
0094     SPack::Decode( value, b, 4 ); 
0095 
0096     assert( a[0] == b[0] ); 
0097     assert( a[1] == b[1] ); 
0098     assert( a[2] == b[2] ); 
0099     assert( a[3] == b[3] ); 
0100 }
0101 
0102 
0103 void test_Encode13_Decode13()
0104 {
0105     LOG(info); 
0106 
0107     unsigned char c  = 0xff ; 
0108     unsigned int ccc   = 0xffffff ; 
0109     unsigned expect  = 0xffffffff ; 
0110 
0111     unsigned value = SPack::Encode13( c, ccc );  
0112     bool value_expect = value == expect ; 
0113     assert( value_expect ); 
0114     if(!value_expect) std::raise(SIGINT); 
0115 
0116     unsigned char c2 ; 
0117     unsigned int  ccc2 ; 
0118     SPack::Decode13( value, c2, ccc2 ); 
0119     assert( c == c2 ); 
0120     assert( ccc == ccc2 ); 
0121 }
0122 
0123 
0124 void test_Encode22_Decode22()
0125 {
0126     LOG(info); 
0127 
0128     unsigned a0 = 0xdead ; 
0129     unsigned b0 = 0xbeef ; 
0130     unsigned expect  = 0xdeadbeef ; 
0131 
0132     unsigned value = SPack::Encode22( a0, b0 );  
0133     bool value_expect = value == expect ; 
0134     assert( value_expect ); 
0135     if(!value_expect) std::raise(SIGINT); 
0136 
0137 
0138     unsigned a1 ; 
0139     unsigned b1 ; 
0140     SPack::Decode22( value, a1, b1 ); 
0141 
0142     bool decode22_expect = a0 == a1 && b0 == b1 ;
0143     assert( decode22_expect ); 
0144     if(!decode22_expect) std::raise(SIGINT);
0145 
0146     unsigned a2 = SPack::Decode22a( value ); 
0147     unsigned b2 = SPack::Decode22b( value ); 
0148 
0149     bool decode22a_expect = a0 == a2 && b0 == b2 ;
0150     assert( decode22a_expect ); 
0151     if(!decode22a_expect) std::raise(SIGINT);
0152 
0153 
0154 
0155 }
0156 
0157 
0158 void test_Encode22hilo_Decode22hilo(int a0, int b0, bool dump)
0159 {
0160     unsigned packed = SPack::Encode22hilo( a0, b0 );  
0161 
0162     int a1 ; 
0163     int b1 ; 
0164     SPack::Decode22hilo(packed, a1, b1 ); 
0165 
0166     int a2 = SPack::Decode22hi(packed); 
0167     int b2 = SPack::Decode22lo(packed); 
0168 
0169     if(dump)
0170     {
0171         std::cout 
0172             << std::hex
0173             << " pk " << std::setw(8) << packed
0174             << "    "
0175             << " a0 " << std::setw(8) << a0
0176             << " a1 " << std::setw(8) << a1
0177             << " a2 " << std::setw(8) << a2
0178             << "    "
0179             << " b0 " << std::setw(8) << b0
0180             << " b1 " << std::setw(8) << b1
0181             << " b2 " << std::setw(8) << b2
0182             << std::endl
0183             ;
0184     }
0185 
0186     assert( a0 == a1 ); 
0187     assert( b0 == b1 ); 
0188     assert( a0 == a2 ); 
0189     assert( b0 == b2 ); 
0190 }
0191 
0192 void test_Encode22hilo_Decode22hilo()
0193 {
0194     LOG(info); 
0195     int i0 = -0x8000 ; 
0196     int i1 =  0x7fff ; 
0197     int s = 10 ; 
0198     bool dump = false ; 
0199 
0200     for(int i=i0 ; i <= i1 ; i+=s ) for(int j=i0 ; j <= i1 ; j+=s ) test_Encode22hilo_Decode22hilo(i,j,dump); 
0201 
0202     typedef std::vector<std::pair<int,int>> VII ; 
0203     VII hilo = { 
0204                   { 0x0000, -0x0000},
0205                   { 0x0001, -0x0001},
0206                   { 0x0002, -0x0002},
0207                   { 0x0003, -0x0003},
0208                   { 0x0004, -0x0004},
0209                   { 0x0005, -0x0005},
0210                   { 0x0006, -0x0006},
0211                   { 0x0007, -0x0007},
0212                   { 0x0008, -0x0008},
0213                }; 
0214 
0215     for(VII::const_iterator it=hilo.begin() ; it != hilo.end() ; it++)
0216         test_Encode22hilo_Decode22hilo(  it->first,  it->second, true); 
0217 
0218     for(VII::const_iterator it=hilo.begin() ; it != hilo.end() ; it++)
0219         test_Encode22hilo_Decode22hilo( -it->first, -it->second, true); 
0220 
0221 }
0222 
0223 
0224 
0225 void test_int_as_float()
0226 {
0227     int i0 = -420042 ;  
0228     float f0 = SPack::int_as_float( i0 ); 
0229     int i1 = SPack::int_from_float( f0 ); 
0230     assert( i0 == i1 ); 
0231     LOG(info) << " i0 " << i0 << " f0 " << f0 << " i1 " << i1 << " (NaN is expected) " ; 
0232 }
0233 
0234 void test_uint_as_float()
0235 {
0236     unsigned u0 = 420042 ;  
0237     float f0 = SPack::uint_as_float( u0 ); 
0238     unsigned u1 = SPack::uint_from_float( f0 ); 
0239     assert( u0 == u1 ); 
0240     LOG(info) << " u0 " << u0 << " f0 " << f0 << " u1 " << u1 ; 
0241 }
0242 
0243 
0244 void test_IsLittleEndian()
0245 {
0246     const char* LE = "LITTLE_ENDIAN : least significant byte at smaller memory address " ; 
0247     const char* BE = "BIG_ENDIAN    : most significant byte at smaller memory address " ; 
0248     LOG(info) << ( SPack::IsLittleEndian() ? LE : BE  ) ; 
0249 }
0250 
0251 
0252 void test_unsigned_as_int(int boundary, unsigned sensorIndex, bool dump)
0253 {
0254     // LOG(info) << " boundary " << boundary ; 
0255 
0256     //unsigned packed = ( boundary << 16 | sensorIndex << 0 ); 
0257     //     simple packing like this doesnt work with signed ints, 
0258     //     must control the masking first otherwise the bits from eg -1:0xffffffff leak 
0259 
0260     unsigned packed = ((boundary & 0xffff) << 16 ) | ((sensorIndex & 0xffff) << 0 ) ;  
0261     unsigned hi = ( packed & 0xffff0000 ) >> 16 ;
0262     unsigned lo = ( packed & 0x0000ffff ) >>  0 ;
0263 
0264     // int hi_s = hi <= 0x7fff ? hi : hi - 0x10000 ;    // twos complement
0265     int hi_s = SPack::unsigned_as_int<16>(hi); 
0266 
0267     bool expect = hi_s == boundary && lo == sensorIndex ; 
0268 
0269     if(!expect || dump)
0270     std::cout 
0271         << " boundary " << std::setw(10) << std::dec << boundary 
0272         << " sensorIndex(hex) " << std::hex << sensorIndex
0273         << " packed(hex) " << std::hex << packed 
0274         << " hi(hex) " << std::hex << hi 
0275         << " lo(hex) " << std::hex << lo 
0276         << " hi_s " << std::dec << hi_s 
0277         << std::endl 
0278         ;
0279 
0280     assert(expect) ; 
0281 }
0282 
0283 void test_unsigned_as_int()
0284 {
0285     //int boundary = -1 ; // 0xffff ;   // signed int that can easily fit into 16 bits 
0286     unsigned sensorIndex = 0xbeef ;   // unsigned int that can easily fit into 16 bits 
0287     unsigned signed_max_16 = (0x1 << (16 - 1)) - 1  ;   // 0x7fff  
0288 
0289     test_unsigned_as_int( -1                 , sensorIndex, true ); 
0290     test_unsigned_as_int( -(signed_max_16+1) , sensorIndex, true );
0291     test_unsigned_as_int(   signed_max_16    , sensorIndex, true );
0292 
0293     int boundary0 = -(signed_max_16+1) ; 
0294     int boundary1 = signed_max_16 ; 
0295 
0296     LOG(info) 
0297         << " boundary0 " << boundary0
0298         << " boundary1 " << boundary1
0299         ;
0300 
0301     for(int boundary=boundary0 ; boundary <= boundary1  ; boundary++)
0302         test_unsigned_as_int(boundary, sensorIndex, false); 
0303 
0304     //test_unsigned_as_int( -(signed_max_16+2), sensorIndex, false  );
0305     //test_unsigned_as_int(   (signed_max_16+1), sensorIndex, false  );
0306 
0307 }
0308 
0309 
0310 /**
0311 test_unsigned_as_int_16
0312 ------------------------
0313 
0314 This demonstrates that the union trick and twos-complement 
0315 reinterpretation give the same result : although note that 
0316 must use a union with elements of the appropriate number of bits.
0317 
0318 **/
0319 
0320 void test_unsigned_as_int_16(unsigned value)
0321 {
0322     int v16_0 = SPack::unsigned_as_int<16>(value); 
0323     int v16_1 = SPack::unsigned_as_int_16( value ); 
0324 
0325     bool expect = v16_0 == v16_1 ; 
0326     bool dump = false ; 
0327 
0328     if(!expect || dump)
0329     {
0330         std::cout 
0331             << " v16_0 " << v16_0 
0332             << " v16_1 " << v16_1 
0333             << ( expect ? " " : " NOT-EXPECT " )
0334             << std::endl 
0335             ; 
0336     } 
0337     assert( expect ); 
0338 }
0339 
0340 void test_unsigned_as_int_16()
0341 {
0342     unsigned value0 = 0 ; 
0343     unsigned value1 = ( 0x1 << 16 ) - 1 ; // 0xffff   
0344     for(unsigned value=value0 ; value <= value1  ; value++) test_unsigned_as_int_16(value); 
0345 }
0346 
0347 /**
0348 
0349 OptiX_700/SDK/optixWhitted/helpers.h 
0350 
0351 138 #define float3_as_args(u) \
0352 139     reinterpret_cast<uint32_t&>((u).x), \
0353 140     reinterpret_cast<uint32_t&>((u).y), \
0354 141     reinterpret_cast<uint32_t&>((u).z)
0355 
0356 **/
0357 
0358 
0359 union uif_t {
0360    unsigned u ; 
0361    int      i ; 
0362    float    f ; 
0363 };  
0364 
0365 
0366 void dummy_optixTrace(unsigned& p0 )
0367 {
0368     p0 += 100u ; 
0369 }
0370 
0371 
0372 
0373 #ifdef DEREFERENCING_TYPE_PUNNED_POINTER
0374 void test_reinterpret_cast()
0375 {
0376     unsigned u = 42 ; 
0377     uif_t uif ; 
0378     uif.u = u  ; 
0379 
0380     //unsigned u2 = reinterpret_cast<uint32_t&>(uif.f) ; 
0381     unsigned u2 = reinterpret_cast<unsigned&>(uif.f) ; 
0382     LOG(info) << " uif.f " << uif.f << " u2 " << u2 ; 
0383 
0384     bool u_expect = u2 == u  ;
0385     assert( u_expect ); 
0386     if(!u_expect) std::raise(SIGINT) ; 
0387 }
0388 
0389 void test_reinterpret_cast_arg()
0390 {
0391     uif_t uif ; 
0392     uif.u = 42u  ; 
0393     LOG(info) << " uif.f " << uif.f << " uif.u " << uif.u  ; 
0394     dummy_optixTrace(  reinterpret_cast<unsigned&>(uif.f) ) ; 
0395     LOG(info) << " uif.f " << uif.f << " uif.u " << uif.u  ; 
0396     assert( uif.u == 142u  );  
0397 }
0398 #endif
0399 
0400 void test_unsigned_as_double()
0401 {
0402     unsigned x = 42u ; 
0403     unsigned y = 420u ; 
0404 
0405     double d = SPack::unsigned_as_double(x, y ); 
0406 
0407     unsigned x2, y2 ; 
0408     SPack::double_as_unsigned(x2, y2, d ); 
0409     assert( x2 == x );
0410     assert( y2 == y );
0411 
0412     LOG(info) << " d " << d << " x2 " << x2 << " y2 " << y2 ; 
0413 }
0414 
0415 
0416 
0417 
0418 int main(int argc , char** argv )
0419 {
0420     OPTICKS_LOG(argc, argv);
0421 
0422     //test_Encode();  
0423 
0424     //test_Encode_Decode();  
0425     //test_Encode_Decode_ptr();  
0426     //test_Encode13_Decode13();  
0427     //test_Encode22_Decode22();  
0428 
0429     //test_int_as_float(); 
0430     //test_uint_as_float(); 
0431 
0432     //test_Encode_Decode_unsigned();  
0433     //test_IsLittleEndian();  
0434 
0435     //test_unsigned_as_int(); 
0436     //test_unsigned_as_int_16(); 
0437 
0438     //test_Encode22hilo_Decode22hilo(); 
0439 
0440     //test_reinterpret_cast(); 
0441     //test_reinterpret_cast_arg(); 
0442 
0443     test_unsigned_as_double(); 
0444 
0445     return 0  ; 
0446 }
0447 
0448 // om-;TEST=SPackTest om-t
0449