Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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_MathX_GenVectorX_BitReproducible
18#define ROOT_MathX_GenVectorX_BitReproducible 1
19
21
22#include <iostream>
23#include <string>
24#include <exception>
25
26#include <iomanip>
27
28#if !defined(ROOT_MATH_SYCL) && !defined(ROOT_MATH_CUDA)
29
30namespace ROOT {
31namespace ROOT_MATH_ARCH {
32namespace GenVector_detail {
33
34class BitReproducibleException : public std::exception {
35public:
36 BitReproducibleException(const std::string &w) noexcept : fMsg(w) {}
38 const char *what() const noexcept override { return fMsg.c_str(); }
39
40private:
41 std::string fMsg;
42};
43
45public:
46 // dto2longs(d, i1, i2) returns (in i1 and i2) two unsigned ints
47 // representation of its double input. This is byte-ordering
48 // independent, and depends for complete portability ONLY on adherence
49 // to the IEEE 754 standard for 64-bit floating point representation.
50 // The first unsigned int contains the high-order bits in IEEE; thus
51 // 1.0 will always be 0x3FF00000, 00000000
52 static void Dto2longs(double d, unsigned int &i1, unsigned int &i2);
53
54 // longs2double (i1,i2) returns a double containing the value represented by
55 // its input, which must be a 2 unsigned ints.
56 // The input is taken to be the representation according to
57 // the IEEE 754 standard for a 64-bit floating point number, whose value
58 // is returned as a double. The byte-ordering of the double result is,
59 // of course, tailored to the proper byte-ordering for the system.
60 static double Longs2double(unsigned int i1, unsigned int i2);
61
62 // dtox(d) returns a 16-character string containing the (zero-filled) hex
63 // representation of its double input. This is byte-ordering
64 // independent, and depends for complete portability ONLY on adherence
65 // to the IEEE 754 standard for 64-bit floating point representation.
66 static std::string D2x(double d);
67
68 static void Output(std::ostream &os, double d)
69 {
70 unsigned int i1, i2;
71 Dto2longs(d, i1, i2);
72 os << " " << i1 << " " << i2;
73 }
74
75 static void Input(std::istream &is, double &d)
76 {
77 unsigned int i1, i2;
78 is >> i1 >> i2;
79 d = Longs2double(i1, i2);
80 }
81
82 static void Output(std::ostream &os, float f)
83 {
84 unsigned int i1, i2;
85 Dto2longs(double(f), i1, i2);
86 os << " " << i1 << " " << i2;
87 }
88
89 static void Input(std::istream &is, float &f)
90 {
91 unsigned int i1, i2;
92 is >> i1 >> i2;
93 f = float(Longs2double(i1, i2));
94 }
95
96private:
97 union DB8 {
98 unsigned char fB[8];
99 double fD;
100 };
101 static void Fill_byte_order();
103 static int fgByte_order[8];
104 // Meaning of byte_order: The first (high-order in IEEE 754) byte to
105 // output (or the high-order byte of the first unsigned int)
106 // is of db.b[byte_order[0]]. Thus the index INTO byte_order
107 // is a position in the IEEE representation of the double, and the value
108 // of byte_order[k] is an offset in the memory representation of the
109 // double.
110
111}; // BitReproducible
112
113} // namespace GenVector_detail
114} // namespace ROOT_MATH_ARCH
115} // namespace ROOT
116
117// A note about floats and long doubles:
118//
119// BitReproducible can be used with floats by doing the equivalent of
120// float x = x0; BitReproducible::dto2longs (x, i, j);
121// float y = BitReproducible::longs2double (i, j);
122// The results are correct.
123// The only inefficiency is that two integers are used where one would suffice.
124//
125// The same artifice will compile for long double. However, any value of the
126// long double which is not precisely representable as a double will not
127// give exact results for the read-back.
128//
129// We intend in the near future to create a templated version of this class
130// which cures both the above flaws. (In the case of long double, this is
131// contingent upon finding some IEEE standard for the bits in a 128-bit double.)
132
133#endif
134
135#endif // DOUBCONV_HH
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
static void Output(std::ostream &os, float f)
static void Input(std::istream &is, float &f)
static double Longs2double(unsigned int i1, unsigned int i2)
static void Output(std::ostream &os, double d)
static void Dto2longs(double d, unsigned int &i1, unsigned int &i2)
static void Input(std::istream &is, double &d)