Geant4 Cross Reference |
1 // -*- C++ -*- << 1 // -*- C++ -*- 2 // ------------------------------------------- << 2 // $Id:$ 3 // HEP Random << 3 // ----------------------------------------------------------------------- 4 // --- DoubConv --- << 4 // HEP Random 5 // class implementation f << 5 // --- DoubConv --- 6 // ------------------------------------------- << 6 // class implementation file 7 << 7 // ----------------------------------------------------------------------- 8 #include "CLHEP/Random/DoubConv.h" << 8 9 << 9 #include "CLHEP/Random/DoubConv.h" 10 #include <sstream> << 10 11 #include <iomanip> << 11 #include <sstream> 12 #include <vector> << 12 #include <iomanip> 13 << 13 14 namespace CLHEP { << 14 namespace CLHEP { 15 << 15 16 CLHEP_THREAD_LOCAL bool DoubConv::byte_order_k << 16 CLHEP_THREAD_LOCAL bool DoubConv::byte_order_known = false; 17 CLHEP_THREAD_LOCAL int DoubConv::byte_order[8 << 17 CLHEP_THREAD_LOCAL int DoubConv::byte_order[8]; 18 << 18 19 void DoubConv::fill_byte_order () { << 19 void DoubConv::fill_byte_order () { 20 double x = 1.0; << 20 double x = 1.0; 21 int t30 = 1 << 30; << 21 int t30 = 1 << 30; 22 int t22 = 1 << 22; << 22 int t22 = 1 << 22; 23 x *= t30; << 23 x *= t30; 24 x *= t22; << 24 x *= t22; 25 double y = 1; << 25 double y = 1; 26 double z = 1; << 26 double z = 1; 27 x *= z; << 27 x *= z; 28 for (int k=0; k<6; k++) { << 28 for (int k=0; k<6; k++) { 29 x += y*z; << 29 x += y*z; 30 y += 1; << 30 y += 1; 31 z *= 256; << 31 z *= 256; 32 } << 32 } 33 // x, in IEEE format, would now be 0x4330060 << 33 // x, in IEEE format, would now be 0x4330060504030201 34 DB8 xb; << 34 union DB8 { 35 xb.d = x; << 35 unsigned char b[8]; 36 int n; << 36 double d; 37 static const int UNSET = -1; << 37 }; 38 for (n=0; n<8; n++) { << 38 DB8 xb; 39 byte_order[n] = UNSET; << 39 xb.d = x; 40 } << 40 int n; 41 int order; << 41 static const int UNSET = -1; 42 for (n=0; n<8; n++) { << 42 for (n=0; n<8; n++) { 43 switch ( xb.b[n] ) { << 43 byte_order[n] = UNSET; 44 case 0x43: << 44 } 45 order = 0; << 45 int order; 46 break; << 46 for (n=0; n<8; n++) { 47 case 0x30: << 47 switch ( xb.b[n] ) { 48 order = 1; << 48 case 0x43: 49 break; << 49 order = 0; 50 case 0x06: << 50 break; 51 order = 2; << 51 case 0x30: 52 break; << 52 order = 1; 53 case 0x05: << 53 break; 54 order = 3; << 54 case 0x06: 55 break; << 55 order = 2; 56 case 0x04: << 56 break; 57 order = 4; << 57 case 0x05: 58 break; << 58 order = 3; 59 case 0x03: << 59 break; 60 order = 5; << 60 case 0x04: 61 break; << 61 order = 4; 62 case 0x02: << 62 break; 63 order = 6; << 63 case 0x03: 64 break; << 64 order = 5; 65 case 0x01: << 65 break; 66 order = 7; << 66 case 0x02: 67 break; << 67 order = 6; 68 default: << 68 break; 69 throw DoubConvException( << 69 case 0x01: 70 "Cannot determine byte-ordering of doubles << 70 order = 7; 71 } << 71 break; 72 if (byte_order[n] != UNSET) { << 72 default: 73 throw DoubConvException( << 73 throw DoubConvException( 74 "Confusion in byte-ordering of doubles on << 74 "Cannot determine byte-ordering of doubles on this system"); 75 } << 75 } 76 byte_order[n] = order; << 76 if (byte_order[n] != UNSET) { 77 byte_order_known = true; << 77 throw DoubConvException( 78 } << 78 "Confusion in byte-ordering of doubles on this system"); 79 return; << 79 } 80 } << 80 byte_order[n] = order; 81 << 81 byte_order_known = true; 82 std::string DoubConv::d2x(double d) { << 82 } 83 if ( !byte_order_known ) fill_byte_order (); << 83 return; 84 DB8 db; << 84 } 85 db.d = d; << 85 86 std::ostringstream ss; << 86 std::string DoubConv::d2x(double d) { 87 for (int i=0; i<8; ++i) { << 87 if ( !byte_order_known ) fill_byte_order (); 88 int k = byte_order[i]; << 88 DB8 db; 89 ss << std::hex << std::setw(2) << std::set << 89 db.d = d; 90 } << 90 std::ostringstream ss; 91 return ss.str(); << 91 for (int i=0; i<8; ++i) { 92 } << 92 int k = byte_order[i]; 93 << 93 ss << std::hex << std::setw(2) << std::setfill('0') << (int)db.b[k]; 94 std::vector<unsigned long> DoubConv::dto2longs << 94 } 95 std::vector<unsigned long> v(2); << 95 return ss.str(); 96 if ( !byte_order_known ) fill_byte_order (); << 96 } 97 DB8 db; << 97 98 db.d = d; << 98 std::vector<unsigned long> DoubConv::dto2longs(double d) { 99 v[0] = ((static_cast<unsigned long>(db.b[b << 99 std::vector<unsigned long> v(2); 100 | ((static_cast<unsigned long>(db.b[b << 100 if ( !byte_order_known ) fill_byte_order (); 101 | ((static_cast<unsigned long>(db.b[b << 101 DB8 db; 102 | ((static_cast<unsigned long>(db.b[b << 102 db.d = d; 103 v[1] = ((static_cast<unsigned long>(db.b[b << 103 v[0] = ((static_cast<unsigned long>(db.b[byte_order[0]])) << 24) 104 | ((static_cast<unsigned long>(db.b[b << 104 | ((static_cast<unsigned long>(db.b[byte_order[1]])) << 16) 105 | ((static_cast<unsigned long>(db.b[b << 105 | ((static_cast<unsigned long>(db.b[byte_order[2]])) << 8) 106 | ((static_cast<unsigned long>(db.b[b << 106 | ((static_cast<unsigned long>(db.b[byte_order[3]])) ); 107 return v; << 107 v[1] = ((static_cast<unsigned long>(db.b[byte_order[4]])) << 24) 108 } << 108 | ((static_cast<unsigned long>(db.b[byte_order[5]])) << 16) 109 << 109 | ((static_cast<unsigned long>(db.b[byte_order[6]])) << 8) 110 double DoubConv::longs2double (const std::vect << 110 | ((static_cast<unsigned long>(db.b[byte_order[7]])) ); 111 DB8 db; << 111 return v; 112 unsigned char bytes[8]; << 112 } 113 if ( !byte_order_known ) fill_byte_order (); << 113 114 bytes[0] = static_cast<unsigned char>((v[0] << 114 double DoubConv::longs2double (const std::vector<unsigned long> & v) { 115 bytes[1] = static_cast<unsigned char>((v[0] << 115 DB8 db; 116 bytes[2] = static_cast<unsigned char>((v[0] << 116 unsigned char bytes[8]; 117 bytes[3] = static_cast<unsigned char>((v[0] << 117 if ( !byte_order_known ) fill_byte_order (); 118 bytes[4] = static_cast<unsigned char>((v[1] << 118 bytes[0] = static_cast<unsigned char>((v[0] >> 24) & 0xFF); 119 bytes[5] = static_cast<unsigned char>((v[1] << 119 bytes[1] = static_cast<unsigned char>((v[0] >> 16) & 0xFF); 120 bytes[6] = static_cast<unsigned char>((v[1] << 120 bytes[2] = static_cast<unsigned char>((v[0] >> 8) & 0xFF); 121 bytes[7] = static_cast<unsigned char>((v[1] << 121 bytes[3] = static_cast<unsigned char>((v[0] ) & 0xFF); 122 for (int i=0; i<8; ++i) { << 122 bytes[4] = static_cast<unsigned char>((v[1] >> 24) & 0xFF); 123 db.b[byte_order[i]] = bytes[i]; << 123 bytes[5] = static_cast<unsigned char>((v[1] >> 16) & 0xFF); 124 } << 124 bytes[6] = static_cast<unsigned char>((v[1] >> 8) & 0xFF); 125 return db.d; << 125 bytes[7] = static_cast<unsigned char>((v[1] ) & 0xFF); 126 } << 126 for (int i=0; i<8; ++i) { 127 << 127 db.b[byte_order[i]] = bytes[i]; 128 } // end namespace CLHEP << 128 } >> 129 return db.d; >> 130 } >> 131 >> 132 } // end namespace CLHEP 129 133