Logo ROOT   6.14/05
Reference Guide
BitReproducible.cxx
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: W. Brown, M. Fischler, L. Moneta 2005
3 
5 
6 #include <sstream>
7 #include <iomanip>
8 #include <exception>
9 
10 namespace ROOT {
11 namespace Math {
12 namespace GenVector_detail {
13 
16 
18  // Fill_byte_order
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  union DB8 {
34  unsigned char fB[8];
35  double fD;
36  };
37  DB8 xb;
38  xb.fD = x;
39  int n;
40  static const int kUNSET = -1;
41  for (n=0; n<8; n++) {
42  fgByte_order[n] = kUNSET;
43  }
44  int order;
45  for (n=0; n<8; n++) {
46  switch ( xb.fB[n] ) {
47  case 0x43:
48  order = 0;
49  break;
50  case 0x30:
51  order = 1;
52  break;
53  case 0x06:
54  order = 2;
55  break;
56  case 0x05:
57  order = 3;
58  break;
59  case 0x04:
60  order = 4;
61  break;
62  case 0x03:
63  order = 5;
64  break;
65  case 0x02:
66  order = 6;
67  break;
68  case 0x01:
69  order = 7;
70  break;
71  default:
73  "Cannot determine byte-ordering of doubles on this system");
74  }
75  if (fgByte_order[n] != kUNSET) {
77  "Confusion in byte-ordering of doubles on this system");
78  }
79  fgByte_order[n] = order;
80  fgByte_order_known = true;
81  }
82  return;
83 }
84 
85 std::string BitReproducible::D2x(double d) {
86  // hex conversion
88  DB8 db;
89  db.fD = d;
90  std::ostringstream ss;
91  for (int i=0; i<8; ++i) {
92  int k = fgByte_order[i];
93  ss << std::hex << std::setw(2) << std::setfill('0') << (int)db.fB[k];
94  }
95  return ss.str();
96 }
97 
98 void BitReproducible::Dto2longs(double d, unsigned int& i, unsigned int& j) {
99  // conversion to 2 longs
101  DB8 db;
102  db.fD = d;
103  i = ((static_cast<unsigned int>(db.fB[fgByte_order[0]])) << 24)
104  | ((static_cast<unsigned int>(db.fB[fgByte_order[1]])) << 16)
105  | ((static_cast<unsigned int>(db.fB[fgByte_order[2]])) << 8)
106  | ((static_cast<unsigned int>(db.fB[fgByte_order[3]])) );
107  j = ((static_cast<unsigned int>(db.fB[fgByte_order[4]])) << 24)
108  | ((static_cast<unsigned int>(db.fB[fgByte_order[5]])) << 16)
109  | ((static_cast<unsigned int>(db.fB[fgByte_order[6]])) << 8)
110  | ((static_cast<unsigned int>(db.fB[fgByte_order[7]])) );
111 }
112 
113 double BitReproducible::Longs2double (unsigned int i, unsigned int j) {
114  // conversion longs to double
115  DB8 db;
116  unsigned char bytes[8];
118  bytes[0] = static_cast<unsigned char>((i >> 24) & 0xFF);
119  bytes[1] = static_cast<unsigned char>((i >> 16) & 0xFF);
120  bytes[2] = static_cast<unsigned char>((i >> 8) & 0xFF);
121  bytes[3] = static_cast<unsigned char>((i ) & 0xFF);
122  bytes[4] = static_cast<unsigned char>((j >> 24) & 0xFF);
123  bytes[5] = static_cast<unsigned char>((j >> 16) & 0xFF);
124  bytes[6] = static_cast<unsigned char>((j >> 8) & 0xFF);
125  bytes[7] = static_cast<unsigned char>((j ) & 0xFF);
126  for (int k=0; k<8; ++k) {
127  db.fB[fgByte_order[k]] = bytes[k];
128  }
129  return db.fD;
130 }
131 
132 } // namespace _GenVector_detail
133 } // namespace Math
134 } // namespace ROOT
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
static double Longs2double(unsigned int i1, unsigned int i2)
Double_t x[n]
Definition: legend1.C:17
static void Dto2longs(double d, unsigned int &i1, unsigned int &i2)
#define d(i)
Definition: RSha256.hxx:102
Double_t y[n]
Definition: legend1.C:17
Namespace for new Math classes and functions.
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
const Int_t n
Definition: legend1.C:16