Commit eb94f46d authored by Lynn Garren's avatar Lynn Garren
Browse files

change doubleconversion arguments to avoid creating unnecessary vector

parent 77857f91
......@@ -2,7 +2,6 @@
#define DOUBLE_CONVERSION_HH
#include <string>
#include <vector>
#include <exception>
namespace HepMC {
......@@ -19,27 +18,27 @@ private:
class DoubleConversion {
public:
// dto2longs(d) returns (in a vector) two unsigned longs string containing the
// dto2longs(d,l1,l2) returns two unsigned longs containing the
// representation of its double input. This is byte-ordering
// independant, and depends for complete portability ONLY on adherance
// to the IEEE 754 standard for 64-bit floating point representation.
// The first unsigned long contains the high-order bits in IEEE; thus
// 1.0 will always be 0x3FF00000, 00000000
static std::vector<unsigned long> dto2longs(double d);
static void dto2longs(const double& d, unsigned long &, unsigned long & );
// longs2double (v) returns a double containing the value represented by its
// input, which must be a vector containing 2 unsigned longs.
// longs2double (l1,l2) returns a double containing the value represented
// by its input, which must be 2 unsigned longs.
// The input is taken to be the representation according to
// the IEEE 754 standard for a 64-bit floating point number, whose value
// is returned as a double. The byte-ordering of the double result is,
// of course, tailored to the proper byte-ordering for the system.
static double longs2double (const std::vector<unsigned long> & v);
static double longs2double ( const unsigned long&, const unsigned long& );
// dtox(d) returns a 16-character string containing the (zero-filled) hex
// representation of its double input. This is byte-ordering
// independant, and depends for complete portability ONLY on adherance
// to the IEEE 754 standard for 64-bit floating point representation.
static std::string d2x(double d);
static std::string d2x(const double& d);
private:
union DB8 {
......
......@@ -75,7 +75,8 @@ void DoubleConversion::fill_byte_order () {
return;
}
std::string DoubleConversion::d2x(double d) {
std::string DoubleConversion::d2x(const double& d)
{
if ( !byte_order_known ) fill_byte_order ();
DB8 db;
db.d = d;
......@@ -87,34 +88,35 @@ std::string DoubleConversion::d2x(double d) {
return ss.str();
}
std::vector<unsigned long> DoubleConversion::dto2longs(double d) {
std::vector<unsigned long> v(2);
void DoubleConversion::dto2longs(const double& d, unsigned long & i1, unsigned long & i2)
{
if ( !byte_order_known ) fill_byte_order ();
DB8 db;
db.d = d;
v[0] = ((static_cast<unsigned long>(db.b[byte_order[0]])) << 24)
i1 = ((static_cast<unsigned long>(db.b[byte_order[0]])) << 24)
| ((static_cast<unsigned long>(db.b[byte_order[1]])) << 16)
| ((static_cast<unsigned long>(db.b[byte_order[2]])) << 8)
| ((static_cast<unsigned long>(db.b[byte_order[3]])) );
v[1] = ((static_cast<unsigned long>(db.b[byte_order[4]])) << 24)
i2 = ((static_cast<unsigned long>(db.b[byte_order[4]])) << 24)
| ((static_cast<unsigned long>(db.b[byte_order[5]])) << 16)
| ((static_cast<unsigned long>(db.b[byte_order[6]])) << 8)
| ((static_cast<unsigned long>(db.b[byte_order[7]])) );
return v;
}
double DoubleConversion::longs2double (const std::vector<unsigned long> & v) {
double DoubleConversion::longs2double (const unsigned long& i1, const unsigned long& i2)
{
DB8 db;
unsigned char bytes[8];
if ( !byte_order_known ) fill_byte_order ();
bytes[0] = static_cast<unsigned char>((v[0] >> 24) & 0xFF);
bytes[1] = static_cast<unsigned char>((v[0] >> 16) & 0xFF);
bytes[2] = static_cast<unsigned char>((v[0] >> 8) & 0xFF);
bytes[3] = static_cast<unsigned char>((v[0] ) & 0xFF);
bytes[4] = static_cast<unsigned char>((v[1] >> 24) & 0xFF);
bytes[5] = static_cast<unsigned char>((v[1] >> 16) & 0xFF);
bytes[6] = static_cast<unsigned char>((v[1] >> 8) & 0xFF);
bytes[7] = static_cast<unsigned char>((v[1] ) & 0xFF);
bytes[0] = static_cast<unsigned char>((i1 >> 24) & 0xFF);
bytes[1] = static_cast<unsigned char>((i1 >> 16) & 0xFF);
bytes[2] = static_cast<unsigned char>((i1 >> 8) & 0xFF);
bytes[3] = static_cast<unsigned char>((i1 ) & 0xFF);
bytes[4] = static_cast<unsigned char>((i2 >> 24) & 0xFF);
bytes[5] = static_cast<unsigned char>((i2 >> 16) & 0xFF);
bytes[6] = static_cast<unsigned char>((i2 >> 8) & 0xFF);
bytes[7] = static_cast<unsigned char>((i2 ) & 0xFF);
for (int i=0; i<8; ++i) {
db.b[byte_order[i]] = bytes[i];
}
......
......@@ -354,16 +354,13 @@ namespace HepMC {
std::vector<double> GenEvent::random_states_double() const
{
std::vector<unsigned long> two(2);
std::vector<double> result;
double dd;
// take two longs and make a double
// we assume here that these were initially passed in as doubles
for( unsigned int i=0; i<(m_random_states.size()-1); ++i ) {
two[0]=m_random_states[i];
++i;
two[1]=m_random_states[i];
dd = DoubleConversion::longs2double(two);
for( unsigned int i=0; i<(m_random_states.size()-1); ++i) {
dd = DoubleConversion::longs2double(m_random_states[i],m_random_states[i+1]);
++i;
result.push_back(dd);
}
return result;
......@@ -372,11 +369,11 @@ namespace HepMC {
void GenEvent::set_random_states_double( const std::vector<double>& randomstates )
{
// convert doubles to longs and save
std::vector<unsigned long> two(2);
unsigned long l1, l2;
for( unsigned int i=0; i<randomstates.size(); ++i ) {
two = DoubleConversion::dto2longs(randomstates[i]);
m_random_states.push_back(two[0]);
m_random_states.push_back(two[1]);
DoubleConversion::dto2longs(randomstates[i], l1, l2);
m_random_states.push_back(l1);
m_random_states.push_back(l2);
}
}
......
......@@ -21,15 +21,12 @@ int main() {
}
unsigned long a1 = 0x40100000;
unsigned long a2 = 0;
std::vector<unsigned long> v;
v.push_back(a1);
v.push_back(a2);
double d4 = DoubleConversion::longs2double(v);
double d4 = DoubleConversion::longs2double(a1,a2);
#ifdef VERBOSE
std::cout << "d4 (should be 4.0) is " << d4 << "\n";
#endif
if (d4 != 4.0) {
std::cout << "longs2double(v): double represented by 0x4010000000000000 is "
std::cout << "longs2double(a1,a2): double represented by 0x4010000000000000 is "
<< d4 << "\n which should be 4.0\n";
return 2;
}
......@@ -56,20 +53,15 @@ int main() {
<< "\n which should be 42f0060504030201\n";
return 3;
}
v = DoubleConversion::dto2longs(x);
DoubleConversion::dto2longs(x,a1,a2);
#ifdef VERBOSE
std::cout << "Longs representing x are "
<< std::hex << v[0] << ", " << std::hex << v[1] << "\n";
<< std::hex << a1 << ", " << std::hex << a2 << "\n";
#endif
if (v.size() != 2) {
std::cout << "dto2longs(double): Longs representing x are vector of size "
<< v.size() << "\n";
return 4;
}
if ( (v[0] != 0x42f00605ul) || (v[1] != 0x04030201ul) ) {
std::cout << "dto2longs(double): Longs representing x are\n"
if ( (a1 != 0x42f00605ul) || (a2 != 0x04030201ul) ) {
std::cout << "dto2longs(double,long,long): Longs representing x are\n"
<< " "
<< std::hex << v[0] << ", " << std::hex << v[1] << "\n"
<< std::hex << a1 << ", " << std::hex << a2 << "\n"
<< " (should be 42f00605, 4030201\n";
return 5;
}
......
// $Id: StdRunInfoConvert.icc,v 1.1.1.1.2.1 2005/03/07 18:30:27 garren Exp $
// $Id: StdRunInfoConvert.icc,v 1.1.1.1.2.2 2005/03/14 17:36:31 garren Exp $
// ----------------------------------------------------------------------
//
// StdRunInfoConvert.icc
......@@ -27,8 +27,8 @@ StdRunInfo * getRunInfoFromSTDCM( int irun )
(double)(*cm)->stdcm1()->stdxsec );
// convert doubles to longs and save
std::vector<unsigned long> state(4);
state[0] = DoubleConversion::dto2longs((*cm)->stdcm1()->stdseed1);
state[2] = DoubleConversion::dto2longs((*cm)->stdcm1()->stdseed2);
DoubleConversion::dto2longs((*cm)->stdcm1()->stdseed1,*state[0],*state[1]);
DoubleConversion::dto2longs((*cm)->stdcm1()->stdseed2,*state[2],*state[3]);
run->saveSeeds( state );
// save names
std::string gname = (*cm)->stdcm2()->generatorname;
......@@ -50,7 +50,7 @@ bool RunInfoToSTDCM( StdRunInfo * run )
std::vector<unsigned long> state = run->seedArray();
std::vector<double> result;
for( unsigned int i=0; i<(itsSeedList.size()-1); ++i ) {
dd = DoubleConversion::longs2double(state[i]);
dd = DoubleConversion::longs2double(*state[i],*state[i+1]);
++i;
result.push_back(dd);
}
......
......@@ -95,11 +95,9 @@ void RandomState::appendSeed( unsigned long sd )
void RandomState::appendSeed( double sd )
{
std::ostringstream st(myRandomState);
std::vector<unsigned long> two;
two = HepMC::DoubleConversion::dto2longs(sd);
if (two.size() > 1 ) {
st << " " << two[0] << " " << two[1];
}
unsigned long i1, i2;
HepMC::DoubleConversion::dto2longs(sd,i1,i2);
st << " " << i1 << " " << i2;
myRandomState = st.str();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment