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