Logo ROOT  
Reference Guide
BitReproducible.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: W. Brown, M. Fischler, L. Moneta 2005
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2005 , LCG / FNAL ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class LorentzVector
12 //
13 // Created by: fischler at Mon Jun 25 2005
14 //
15 // Last update: $Id$
16 //
17 #ifndef ROOT_Math_GenVector_BitReproducible
18 #define ROOT_Math_GenVector_BitReproducible 1
19 
20 #include <iostream>
21 #include <string>
22 #include <exception>
23 
24 #include <iomanip>
25 
26 namespace ROOT {
27  namespace Math {
28  namespace GenVector_detail {
29 
30 class BitReproducibleException : public std::exception
31 {
32 public:
33  BitReproducibleException(const std::string & w) noexcept : fMsg(w) {}
34  ~BitReproducibleException() noexcept {}
35  const char *what() const noexcept override { return fMsg.c_str(); }
36  private:
37  std::string fMsg;
38 }; // DoubConvException
39 
40 class BitReproducible {
41 public:
42 
43  // dto2longs(d, i1, i2) returns (in i1 and i2) two unsigned ints
44  // representation of its double input. This is byte-ordering
45  // independent, and depends for complete portability ONLY on adherance
46  // to the IEEE 754 standard for 64-bit floating point representation.
47  // The first unsigned int contains the high-order bits in IEEE; thus
48  // 1.0 will always be 0x3FF00000, 00000000
49  static void Dto2longs(double d, unsigned int & i1, unsigned int & i2);
50 
51  // longs2double (i1,i2) returns a double containing the value represented by
52  // its input, which must be a 2 unsigned ints.
53  // The input is taken to be the representation according to
54  // the IEEE 754 standard for a 64-bit floating point number, whose value
55  // is returned as a double. The byte-ordering of the double result is,
56  // of course, tailored to the proper byte-ordering for the system.
57  static double Longs2double (unsigned int i1, unsigned int i2);
58 
59  // dtox(d) returns a 16-character string containing the (zero-filled) hex
60  // representation of its double input. This is byte-ordering
61  // independent, and depends for complete portability ONLY on adherance
62  // to the IEEE 754 standard for 64-bit floating point representation.
63  static std::string D2x(double d);
64 
65  static void Output ( std::ostream & os, double d ) {
66  unsigned int i1, i2;
67  Dto2longs(d, i1, i2);
68  os << " " << i1 << " " << i2;
69  }
70 
71  static void Input ( std::istream & is, double & d ) {
72  unsigned int i1, i2;
73  is >> i1 >> i2;
74  d = Longs2double(i1, i2);
75  }
76 
77  static void Output ( std::ostream & os, float f ) {
78  unsigned int i1, i2;
79  Dto2longs( double(f), i1, i2 );
80  os << " " << i1 << " " << i2;
81  }
82 
83  static void Input ( std::istream & is, float & f ) {
84  unsigned int i1, i2;
85  is >> i1 >> i2;
86  f = float( Longs2double(i1, i2) );
87  }
88 
89 
90 private:
91  union DB8 {
92  unsigned char fB[8];
93  double fD;
94  };
95  static void Fill_byte_order ();
96  static bool fgByte_order_known;
97  static int fgByte_order[8];
98  // Meaning of byte_order: The first (high-order in IEEE 754) byte to
99  // output (or the high-order byte of the first unsigned int)
100  // is of db.b[byte_order[0]]. Thus the index INTO byte_order
101  // is a position in the IEEE representation of the double, and the value
102  // of byte_order[k] is an offset in the memory representation of the
103  // double.
104 
105 }; // BitReproducible
106 
107 } // namespace _GenVector_detail
108 } // namespace Math
109 } // namespace ROOT
110 
111 // A note about floats and long doubles:
112 //
113 // BitReproducible can be used with floats by doing the equivalent of
114 // float x = x0; BitReproducible::dto2longs (x, i, j);
115 // float y = BitReproducible::longs2double (i, j);
116 // The results are correct.
117 // The only inefficiency is that two integers are used where one would suffice.
118 //
119 // The same artifice will compile for long double. However, any value of the
120 // long double which is not precisely representable as a double will not
121 // give exact results for the read-back.
122 //
123 // We intend in the near future to create a templated version of this class
124 // which cures both the above flaws. (In the case of long double, this is
125 // contingent upon finding some IEEE standard for the bits in a 128-bit double.)
126 
127 
128 #endif // DOUBCONV_HH
f
#define f(i)
Definition: RSha256.hxx:122
ROOT::Math::GenVector_detail::BitReproducible::Dto2longs
static void Dto2longs(double d, unsigned int &i1, unsigned int &i2)
Definition: BitReproducible.cxx:94
ROOT::Math::GenVector_detail::BitReproducible::Longs2double
static double Longs2double(unsigned int i1, unsigned int i2)
Definition: BitReproducible.cxx:109
ROOT::Math::GenVector_detail::BitReproducible::fgByte_order
static int fgByte_order[8]
Definition: BitReproducible.h:112
ROOT::Math::GenVector_detail::BitReproducibleException::~BitReproducibleException
~BitReproducibleException() noexcept
Definition: BitReproducible.h:54
ROOT::Math::GenVector_detail::BitReproducibleException::fMsg
std::string fMsg
Definition: BitReproducible.h:57
ROOT::Math::GenVector_detail::BitReproducible::DB8::fD
double fD
Definition: BitReproducible.h:108
ROOT::Math::GenVector_detail::BitReproducible::Fill_byte_order
static void Fill_byte_order()
Definition: BitReproducible.cxx:17
ROOT::Math::GenVector_detail::BitReproducible::Output
static void Output(std::ostream &os, double d)
Definition: BitReproducible.h:80
ROOT::Math::GenVector_detail::BitReproducibleException::what
const char * what() const noexcept override
Definition: BitReproducible.h:55
ROOT::Math::GenVector_detail::BitReproducibleException::BitReproducibleException
BitReproducibleException(const std::string &w) noexcept
Definition: BitReproducible.h:53
d
#define d(i)
Definition: RSha256.hxx:120
ROOT::Math::GenVector_detail::BitReproducible::fgByte_order_known
static bool fgByte_order_known
Definition: BitReproducible.h:111
ROOT::Math::GenVector_detail::BitReproducible::D2x
static std::string D2x(double d)
Definition: BitReproducible.cxx:81
ROOT::Math::GenVector_detail::BitReproducible::DB8::fB
unsigned char fB[8]
Definition: BitReproducible.h:107
ROOT
VSD Structures.
Definition: StringConv.hxx:21
Math
ROOT::Math::GenVector_detail::BitReproducible::Input
static void Input(std::istream &is, double &d)
Definition: BitReproducible.h:86