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