inline a few more functions for optimization

This commit is contained in:
Mathieu Lacage committed 2010-07-05 16:49:41 +02:00
1 parent 64c5d1e329
commit 81bce7e39e
7 files changed
+133 -179

No files matched your search

-13
View File
@@ -4,19 +4,6 @@
namespace ns3 {
#define HP128_MAX_64 18446744073709551615.0
HighPrecision::HighPrecision (double value)
{
bool is_negative = value < 0;
value = is_negative?-value:value;
double hi = floor (value);
double lo = (value - hi) * HP128_MAX_64;
m_value = hi;
m_value <<= 64;
m_value += lo;
m_value = is_negative?-m_value:m_value;
}
#define MASK_LO ((((uint128_t)1)<<64)-1)
#define MASK_HI (~MASK_LO)
void
+28 -16
View File
@@ -64,22 +64,6 @@ HighPrecision::HighPrecision (int64_t value, bool dummy)
}
double
HighPrecision::GetDouble (void) const
{
#define HP128_MAX_64 18446744073709551615.0
bool is_negative = m_value < 0;
uint128_t value = is_negative ? -m_value:m_value;
uint64_t hi = value >> 64;
uint64_t lo = value;
double flo = lo;
flo /= HP128_MAX_64;
double retval = hi;
retval += flo;
retval = is_negative ? -retval : retval;
return retval;
#undef HP128_MAX_64
}
int64_t HighPrecision::GetInteger (void) const
{
@@ -110,6 +94,34 @@ HighPrecision::Zero (void)
return HighPrecision ();
}
#define HP128_MAX_64 18446744073709551615.0
double
HighPrecision::GetDouble (void) const
{
bool is_negative = m_value < 0;
uint128_t value = is_negative ? -m_value:m_value;
uint64_t hi = value >> 64;
uint64_t lo = value;
double flo = lo;
flo /= HP128_MAX_64;
double retval = hi;
retval += flo;
retval = is_negative ? -retval : retval;
return retval;
}
HighPrecision::HighPrecision (double value)
{
bool is_negative = value < 0;
value = is_negative?-value:value;
double hi = floor (value);
double lo = (value - hi) * HP128_MAX_64;
m_value = hi;
m_value <<= 64;
m_value += lo;
m_value = is_negative?-m_value:m_value;
}
#undef HP128_MAX_64
} // namespace ns3
#endif /* HIGH_PRECISION_128_H */
-25
View File
@@ -25,21 +25,6 @@
namespace ns3 {
const double HighPrecision::MAX_64 = 18446744073709551615.0;
double HighPrecision::GetDouble (void) const
{
bool is_negative = _cairo_int128_negative (m_value);
cairo_int128_t value = is_negative ? _cairo_int128_negate (m_value) : m_value;
cairo_int128_t hi = _cairo_int128_rsa (value, 64);
cairo_uint128_t lo = _cairo_int128_sub (value, _cairo_uint128_lsl (hi, 64));
double flo = _cairo_uint128_to_uint64 (lo);
flo /= MAX_64;
double retval = _cairo_uint128_to_uint64 (hi);
retval += flo;
retval = is_negative ? -retval: retval;
return retval;
}
void
HighPrecision::Mul (HighPrecision const &o)
{
@@ -48,16 +33,6 @@ HighPrecision::Mul (HighPrecision const &o)
}
HighPrecision::HighPrecision (double value)
{
int64_t hi = (int64_t) floor (value);
uint64_t lo = (uint64_t) ((value - floor (value)) * MAX_64);
m_value = _cairo_int64_to_int128 (hi);
m_value = _cairo_int128_lsl (m_value, 64);
cairo_int128_t clo = _cairo_uint128_to_int128 (_cairo_uint64_to_uint128 (lo));
m_value = _cairo_int128_add (m_value, clo);
}
/**
* this function multiplies two 128 bits fractions considering
* the high 64 bits as the integer part and the low 64 bits
+32 -23
View File
@@ -21,6 +21,7 @@
#define HIGH_PRECISION_CAIRO_H
#include <stdint.h>
#include <math.h>
#include "cairo-wideint-private.h"
/**
@@ -76,10 +77,10 @@ class HighPrecision
public:
inline HighPrecision ();
inline HighPrecision (int64_t value, bool dummy);
HighPrecision (double value);
inline HighPrecision (double value);
inline int64_t GetInteger (void) const;
double GetDouble (void) const;
inline double GetDouble (void) const;
inline void Add (HighPrecision const &o);
inline void Sub (HighPrecision const &o);
void Mul (HighPrecision const &o);
@@ -92,7 +93,6 @@ private:
cairo_int128_t Div128 (cairo_int128_t sa, cairo_int128_t sb) const;
inline bool IsNegative (void) const;
static const double MAX_64;
cairo_int128_t m_value;
};
@@ -147,26 +147,9 @@ HighPrecision::Sub (HighPrecision const &o)
int
HighPrecision::Compare (HighPrecision const &o) const
{
if (IsNegative () && !o.IsNegative ())
{
return -1;
}
else if (!IsNegative () && o.IsNegative ())
{
return 1;
}
else if (m_value.hi < o.m_value.hi)
{
return -1;
}
else if (m_value.hi > o.m_value.hi)
{
return 1;
}
else
{
return m_value.lo < o.m_value.lo;
}
HighPrecision tmp = *this;
tmp.Sub (o);
return tmp.IsNegative ()?-1:(tmp.m_value.hi == 0 && tmp.m_value.lo == 0)?0:1;
}
HighPrecision
HighPrecision::Zero (void)
@@ -174,6 +157,32 @@ HighPrecision::Zero (void)
return HighPrecision ();
}
#define HPCAIRO_MAX_64 18446744073709551615.0
double HighPrecision::GetDouble (void) const
{
bool is_negative = IsNegative ();
cairo_int128_t value = is_negative ? _cairo_int128_negate (m_value) : m_value;
double flo = value.lo;
flo /= HPCAIRO_MAX_64;
double retval = value.hi;
retval += flo;
retval = is_negative ? -retval: retval;
return retval;
}
#undef HPCAIRO_MAX_64
#define HPCAIRO_MAX_64 18446744073709551615.0
HighPrecision::HighPrecision (double value)
{
double fhi = floor (value);
int64_t hi = fhi;
uint64_t lo = (uint64_t) ((value - fhi) * HPCAIRO_MAX_64);
m_value.hi = hi;
m_value.lo = lo;
}
#undef HPCAIRO_MAX_64
} // namespace ns3
#endif /* HIGH_PRECISION_CAIRO_H */
-85
View File
@@ -1,85 +0,0 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2006 INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
#include "high-precision-double.h"
#include <cmath>
#include "ns3/assert.h"
namespace ns3 {
HighPrecision::HighPrecision ()
: m_value (0.0)
{
}
HighPrecision::HighPrecision (int64_t value, bool dummy)
: m_value ((double)value)
{
}
HighPrecision::HighPrecision (double value)
: m_value (value)
{
}
int64_t
HighPrecision::GetInteger (void) const
{
return (int64_t)floor (m_value);
}
double
HighPrecision::GetDouble (void) const
{
return m_value;
}
void
HighPrecision::Add (HighPrecision const &o)
{
m_value += o.m_value;
}
void
HighPrecision::Sub (HighPrecision const &o)
{
m_value -= o.m_value;
}
void
HighPrecision::Mul (HighPrecision const &o)
{
m_value *= o.m_value;
}
void
HighPrecision::Div (HighPrecision const &o)
{
m_value /= o.m_value;
}
int
HighPrecision::Compare (HighPrecision const &o) const
{
return 0;
}
HighPrecision
HighPrecision::Zero (void)
{
return HighPrecision (0,0);
}
} // namespace ns3
+72 -11
View File
@@ -34,19 +34,19 @@ namespace ns3 {
class HighPrecision
{
public:
HighPrecision ();
HighPrecision (int64_t value, bool dummy);
HighPrecision (double value);
inline HighPrecision ();
inline HighPrecision (int64_t value, bool dummy);
inline HighPrecision (double value);
int64_t GetInteger (void) const;
double GetDouble (void) const;
void Add (HighPrecision const &o);
void Sub (HighPrecision const &o);
void Mul (HighPrecision const &o);
void Div (HighPrecision const &o);
inline int64_t GetInteger (void) const;
inline double GetDouble (void) const;
inline void Add (HighPrecision const &o);
inline void Sub (HighPrecision const &o);
inline void Mul (HighPrecision const &o);
inline void Div (HighPrecision const &o);
int Compare (HighPrecision const &o) const;
static HighPrecision Zero (void);
inline int Compare (HighPrecision const &o) const;
inline static HighPrecision Zero (void);
private:
static const double MAX_64;
double m_value;
@@ -54,4 +54,65 @@ private:
} // namespace ns3
namespace ns3 {
HighPrecision::HighPrecision ()
: m_value (0.0)
{
}
HighPrecision::HighPrecision (int64_t value, bool dummy)
: m_value ((double)value)
{
}
HighPrecision::HighPrecision (double value)
: m_value (value)
{
}
int64_t
HighPrecision::GetInteger (void) const
{
return (int64_t)floor (m_value);
}
double
HighPrecision::GetDouble (void) const
{
return m_value;
}
void
HighPrecision::Add (HighPrecision const &o)
{
m_value += o.m_value;
}
void
HighPrecision::Sub (HighPrecision const &o)
{
m_value -= o.m_value;
}
void
HighPrecision::Mul (HighPrecision const &o)
{
m_value *= o.m_value;
}
void
HighPrecision::Div (HighPrecision const &o)
{
m_value /= o.m_value;
}
int
HighPrecision::Compare (HighPrecision const &o) const
{
return m_value < o.m_value;
}
HighPrecision
HighPrecision::Zero (void)
{
return HighPrecision (0,0);
}
} // namespace ns3
#endif /* HIGH_PRECISION_DOUBLE_H */
+1 -6
View File
@@ -98,12 +98,7 @@ def build(bld):
env = bld.env_of_name('default')
if env['USE_HIGH_PRECISION_DOUBLE']:
sim.source.extend([
'high-precision-double.cc',
])
headers.source.extend([
'high-precision-double.h',
])
headers.source.extend(['high-precision-double.h'])
elif env['USE_HIGH_PRECISION_128']:
headers.source.extend(['high-precision-128.h'])
sim.source.extend(['high-precision-128.cc'])