From 9631233a2746582e98d754e6587fa110a832af55 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 5 Jul 2010 16:49:41 +0200 Subject: [PATCH] inline a few more functions for optimization --- src/simulator/high-precision-128.cc | 13 ---- src/simulator/high-precision-128.h | 44 ++++++++----- src/simulator/high-precision-cairo.cc | 25 -------- src/simulator/high-precision-cairo.h | 55 ++++++++++------- src/simulator/high-precision-double.cc | 85 -------------------------- src/simulator/high-precision-double.h | 83 +++++++++++++++++++++---- src/simulator/wscript | 7 +-- 7 files changed, 133 insertions(+), 179 deletions(-) delete mode 100644 src/simulator/high-precision-double.cc diff --git a/src/simulator/high-precision-128.cc b/src/simulator/high-precision-128.cc index b9b2796b4..a53c16f66 100644 --- a/src/simulator/high-precision-128.cc +++ b/src/simulator/high-precision-128.cc @@ -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 diff --git a/src/simulator/high-precision-128.h b/src/simulator/high-precision-128.h index 7c4670493..5fb8b9555 100644 --- a/src/simulator/high-precision-128.h +++ b/src/simulator/high-precision-128.h @@ -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 */ diff --git a/src/simulator/high-precision-cairo.cc b/src/simulator/high-precision-cairo.cc index 45f882b07..217d660f3 100644 --- a/src/simulator/high-precision-cairo.cc +++ b/src/simulator/high-precision-cairo.cc @@ -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 diff --git a/src/simulator/high-precision-cairo.h b/src/simulator/high-precision-cairo.h index 99a5217ff..7e79f7f0d 100644 --- a/src/simulator/high-precision-cairo.h +++ b/src/simulator/high-precision-cairo.h @@ -21,6 +21,7 @@ #define HIGH_PRECISION_CAIRO_H #include +#include #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 */ diff --git a/src/simulator/high-precision-double.cc b/src/simulator/high-precision-double.cc deleted file mode 100644 index 460792f9e..000000000 --- a/src/simulator/high-precision-double.cc +++ /dev/null @@ -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 - */ -#include "high-precision-double.h" - -#include -#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 diff --git a/src/simulator/high-precision-double.h b/src/simulator/high-precision-double.h index 82c5e1ccd..9ba7bc5bd 100644 --- a/src/simulator/high-precision-double.h +++ b/src/simulator/high-precision-double.h @@ -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 */ diff --git a/src/simulator/wscript b/src/simulator/wscript index 22b2ca59d..254cee93c 100644 --- a/src/simulator/wscript +++ b/src/simulator/wscript @@ -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'])