split high precision implementations in different files
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 04 Dec 2006 08:39:11 +0100
changeset 168 037cd2b37c67
parent 167 48de2d5b16f3
child 169 bc06726969af
split high precision implementations in different files
BUILD
SConstruct
src/simulator/high-precision-128.cc
src/simulator/high-precision-128.h
src/simulator/high-precision-double.cc
src/simulator/high-precision-double.h
src/simulator/high-precision.cc
src/simulator/high-precision.h
--- a/BUILD	Mon Dec 04 08:11:44 2006 +0100
+++ b/BUILD	Mon Dec 04 08:39:11 2006 +0100
@@ -29,6 +29,11 @@
 Example: scons cxxflags="-O3 -ffast-math"
 - ldflags: flags for the linker:
 Example: scons ldflags="-L/foo -L/bar"
+- high-precision-as-double: set to 'y' to make sure that the
+  high-precision arithmetics performed by the Time class on
+  behalf of the user will use doubles. By default, the code
+  uses 128 integers.
+Example: scons high-precision-as-double=y
 
 2) Targets
 ----------
@@ -166,3 +171,4 @@
 # by default, a module is conceptually a library. If you
 # want to generate an executable from a module you need to:
 my_module.set_executable ()
+
--- a/SConstruct	Mon Dec 04 08:11:44 2006 +0100
+++ b/SConstruct	Mon Dec 04 08:39:11 2006 +0100
@@ -48,8 +48,8 @@
 ns3.add(simu)
 simu.add_dep('core')
 simu.add_sources([
+    'high-precision.cc',
     'cairo-wideint.c',
-    'high-precision.cc',
     'time.cc',
     'event-id.cc',
     'scheduler.cc',
@@ -75,8 +75,21 @@
     'scheduler.h',
     'scheduler-factory.h',
     ])
+high_precision_as_double = ARGUMENTS.get('high-precision-as-double', 'n')
+if high_precision_as_double == 1:
+    simu.add_inst_header ('high-precision-double.h')
+    simu.add_source ('high-precision-double.cc')
+else:
+    simu.add_inst_header ('high-precision-128.h')
+    simu.add_source ('high-precision-128.cc')
+
 def config_simulator (env, config):
     retval = []
+    high_precision_as_double = ARGUMENTS.get('high-precision-as-double', 'n')
+    if high_precision_as_double == 'y':
+        retval.append ('#define USE_HIGH_PRECISION_DOUBLE 1')
+    else:
+        retval.append ('#undef USE_HIGH_PRECISION_DOUBLE')
     if config.CheckCHeader ('stdint.h') == 1:
         retval.append ('#define HAVE_STDINT_H 1')
     elif config.CheckCHeader ('inttypes.h') == 1:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/simulator/high-precision-128.cc	Mon Dec 04 08:39:11 2006 +0100
@@ -0,0 +1,88 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2006 INRIA
+ * All rights reserved.
+ *
+ * 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-128.h"
+
+namespace ns3 {
+
+HighPrecision::HighPrecision ()
+  : m_high (0),
+    m_low (0)
+{}
+
+HighPrecision::HighPrecision (int64_t high, int64_t low)
+  : m_high (high),
+    m_low (low)
+{}
+
+HighPrecision::HighPrecision (double value)
+  : m_high (0), // XXX
+    m_low (0) // XXX
+{}
+
+int64_t
+HighPrecision::GetHigh (void) const
+{
+  return m_high;
+}
+int64_t
+HighPrecision::GetLow (void) const
+{
+  return m_low;
+}
+
+double 
+HighPrecision::GetDouble (void) const
+{
+  return 0.0;
+}
+bool 
+HighPrecision::Add (HighPrecision const &o)
+{
+  return false;
+}
+bool 
+HighPrecision::Sub (HighPrecision const &o)
+{
+  return false;
+}
+bool 
+HighPrecision::Mul (HighPrecision const &o)
+{
+  return false;
+}
+bool 
+HighPrecision::Div (HighPrecision const &o)
+{
+  return false;
+}
+int 
+HighPrecision::Compare (HighPrecision const &o) const
+{
+  return 0;
+}
+HighPrecision 
+HighPrecision::Zero (void)
+{
+  return HighPrecision (0,0);
+}
+
+
+}; // namespace ns3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/simulator/high-precision-128.h	Mon Dec 04 08:39:11 2006 +0100
@@ -0,0 +1,57 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2006 INRIA
+ * All rights reserved.
+ *
+ * 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>
+ */
+#ifndef HIGH_PRECISION_128_H
+#define HIGH_PRECISION_128_H
+
+#include <stdint.h>
+
+namespace ns3 {
+
+/**
+ * This should be a high-precision 128bit integer version of
+ * HighPrecision class. It should also be able to report 
+ * overflow and underflow.
+ */
+class HighPrecision 
+{
+public:
+  HighPrecision ();
+  HighPrecision (int64_t high, int64_t low);
+  HighPrecision (double value);
+  
+  int64_t GetHigh (void) const;
+  int64_t GetLow (void) const;
+  double GetDouble (void) const;
+  bool Add (HighPrecision const &o);
+  bool Sub (HighPrecision const &o);
+  bool Mul (HighPrecision const &o);
+  bool Div (HighPrecision const &o);
+
+  int Compare (HighPrecision const &o) const;
+  static HighPrecision Zero (void);
+private:
+  int64_t m_high;
+  int64_t m_low;
+};
+
+}; // namespace ns3
+
+#endif /* HIGH_PRECISION_128_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/simulator/high-precision-double.cc	Mon Dec 04 08:39:11 2006 +0100
@@ -0,0 +1,94 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2006 INRIA
+ * All rights reserved.
+ *
+ * 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 <cassert>
+
+
+namespace ns3 {
+
+const double HighPrecision::MAX_64 = 18446744073709551615.0;
+
+HighPrecision::HighPrecision ()
+  : m_value (0.0)
+{}
+
+HighPrecision::HighPrecision (int64_t high, int64_t low)
+  : m_value (((double)high) + (((double)low)/MAX_64))
+{}
+
+HighPrecision::HighPrecision (double value)
+  : m_value (value)
+{}
+
+int64_t
+HighPrecision::GetHigh (void) const
+{
+  return (int64_t)floor (m_value);
+}
+int64_t
+HighPrecision::GetLow (void) const
+{
+  return (int64_t)((m_value - floor (m_value)) * MAX_64);
+}
+
+double 
+HighPrecision::GetDouble (void) const
+{
+  return m_value;
+}
+bool 
+HighPrecision::Add (HighPrecision const &o)
+{
+  m_value += o.m_value;
+  return false;
+}
+bool 
+HighPrecision::Sub (HighPrecision const &o)
+{
+  m_value -= o.m_value;
+  return false;
+}
+bool 
+HighPrecision::Mul (HighPrecision const &o)
+{
+  m_value *= o.m_value;
+  return false;
+}
+bool 
+HighPrecision::Div (HighPrecision const &o)
+{
+  m_value /= o.m_value;
+  return false;
+}
+int 
+HighPrecision::Compare (HighPrecision const &o) const
+{
+  return 0;
+}
+HighPrecision 
+HighPrecision::Zero (void)
+{
+  return HighPrecision (0,0);
+}
+
+}; // namespace ns3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/simulator/high-precision-double.h	Mon Dec 04 08:39:11 2006 +0100
@@ -0,0 +1,59 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2006 INRIA
+ * All rights reserved.
+ *
+ * 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>
+ */
+#ifndef HIGH_PRECISION_DOUBLE_H
+#define HIGH_PRECISION_DOUBLE_H
+
+#include <stdint.h>
+
+namespace ns3 {
+
+/**
+ * Obviously, this implementation of the HighPrecision class does
+ * not provide the 128 bits accuracy since it uses a an IEEE754 double
+ * to store the value. It also does not report overflows.
+ * So, it is a nice shortcut but in no way a complete solution.
+ */
+
+class HighPrecision 
+{
+public:
+  HighPrecision ();
+  HighPrecision (int64_t high, int64_t low);
+  HighPrecision (double value);
+  
+  int64_t GetHigh (void) const;
+  int64_t GetLow (void) const;
+  double GetDouble (void) const;
+  bool Add (HighPrecision const &o);
+  bool Sub (HighPrecision const &o);
+  bool Mul (HighPrecision const &o);
+  bool Div (HighPrecision const &o);
+
+  int Compare (HighPrecision const &o) const;
+  static HighPrecision Zero (void);
+private:
+  static const double MAX_64;
+  double m_value;
+};
+
+}; // namespace ns3
+
+#endif /* HIGH_PRECISION_DOUBLE_H  */
--- a/src/simulator/high-precision.cc	Mon Dec 04 08:11:44 2006 +0100
+++ b/src/simulator/high-precision.cc	Mon Dec 04 08:39:11 2006 +0100
@@ -26,139 +26,6 @@
 
 namespace ns3 {
 
-#ifdef HIGH_PRECISION_I128
-
-HighPrecision::HighPrecision ()
-  : m_high (0),
-    m_low (0)
-{}
-
-HighPrecision::HighPrecision (int64_t high, int64_t low)
-  : m_high (high),
-    m_low (low)
-{}
-
-HighPrecision::HighPrecision (double value)
-  : m_high (0), // XXX
-    m_low (0) // XXX
-{}
-
-int64_t
-HighPrecision::GetHigh (void) const
-{
-  return m_high;
-}
-uint32_t
-HighPrecision::GetLow (void) const
-{
-  return m_low;
-}
-
-double 
-HighPrecision::GetDouble (void) const
-{
-  return 0.0;
-}
-bool 
-HighPrecision::Add (HighPrecision const &o)
-{
-  return false;
-}
-bool 
-HighPrecision::Sub (HighPrecision const &o)
-{
-  return false;
-}
-bool 
-HighPrecision::Mul (HighPrecision const &o)
-{
-  return false;
-}
-bool 
-HighPrecision::Div (HighPrecision const &o)
-{
-  return false;
-}
-int 
-HighPrecision::Compare (HighPrecision const &o) const
-{
-  return 0;
-}
-HighPrecision 
-HighPrecision::Zero (void)
-{
-  return HighPrecision (0,0);
-}
-
-#else /* HIGH_PRECISION_I128 */
-
-const double HighPrecision::MAX_64 = 18446744073709551615.0;
-
-HighPrecision::HighPrecision ()
-  : m_value (0.0)
-{}
-
-HighPrecision::HighPrecision (int64_t high, int64_t low)
-  : m_value (((double)high) + (((double)low)/MAX_64))
-{}
-
-HighPrecision::HighPrecision (double value)
-  : m_value (value)
-{}
-
-int64_t
-HighPrecision::GetHigh (void) const
-{
-  return (int64_t)floor (m_value);
-}
-int64_t
-HighPrecision::GetLow (void) const
-{
-  return (int64_t)((m_value - floor (m_value)) * MAX_64);
-}
-
-double 
-HighPrecision::GetDouble (void) const
-{
-  return m_value;
-}
-bool 
-HighPrecision::Add (HighPrecision const &o)
-{
-  m_value += o.m_value;
-  return false;
-}
-bool 
-HighPrecision::Sub (HighPrecision const &o)
-{
-  m_value -= o.m_value;
-  return false;
-}
-bool 
-HighPrecision::Mul (HighPrecision const &o)
-{
-  m_value *= o.m_value;
-  return false;
-}
-bool 
-HighPrecision::Div (HighPrecision const &o)
-{
-  m_value /= o.m_value;
-  return false;
-}
-int 
-HighPrecision::Compare (HighPrecision const &o) const
-{
-  return 0;
-}
-HighPrecision 
-HighPrecision::Zero (void)
-{
-  return HighPrecision (0,0);
-}
-
-#endif /* HIGH_PRECISION_I128 */
-
 HighPrecision Abs (HighPrecision const &value)
 {
   if (value.Compare (HighPrecision::Zero ()) <= 0)
--- a/src/simulator/high-precision.h	Mon Dec 04 08:11:44 2006 +0100
+++ b/src/simulator/high-precision.h	Mon Dec 04 08:39:11 2006 +0100
@@ -22,71 +22,16 @@
 #define HIGH_PRECISION_H
 
 #include <stdint.h>
+#include "ns3/simulator-config.h"
+
+#ifdef USE_HIGH_PRECISION_DOUBLE
+#include "high-precision-double.h"
+#else /* USE_HIGH_PRECISION_DOUBLE */
+#include "high-precision-128.h"
+#endif /* USE_HIGH_PRECISION_DOUBLE */
 
 namespace ns3 {
 
-#ifdef HIGH_PRECISION_I128
-
-/**
- * This should be a high-precision 128bit integer version of
- * HighPrecision class. It should also be able to report 
- * overflow and underflow.
- */
-class HighPrecision 
-{
-public:
-  HighPrecision ();
-  HighPrecision (int64_t high, int64_t low);
-  HighPrecision (double value);
-  
-  int64_t GetHigh (void) const;
-  int64_t GetLow (void) const;
-  double GetDouble (void) const;
-  bool Add (HighPrecision const &o);
-  bool Sub (HighPrecision const &o);
-  bool Mul (HighPrecision const &o);
-  bool Div (HighPrecision const &o);
-
-  int Compare (HighPrecision const &o) const;
-  static HighPrecision Zero (void);
-private:
-  int64_t m_high;
-  int64_t m_low;
-};
-
-#else /* HIGH_PRECISION_I128 */
-
-/**
- * Obviously, this implementation of the HighPrecision class does
- * not provide the 128 bits accuracy since it uses a an IEEE754 double
- * to store the value. It also does not report overflows.
- * So, it is a nice shortcut but in no way a complete solution.
- */
-
-class HighPrecision 
-{
-public:
-  HighPrecision ();
-  HighPrecision (int64_t high, int64_t low);
-  HighPrecision (double value);
-  
-  int64_t GetHigh (void) const;
-  int64_t GetLow (void) const;
-  double GetDouble (void) const;
-  bool Add (HighPrecision const &o);
-  bool Sub (HighPrecision const &o);
-  bool Mul (HighPrecision const &o);
-  bool Div (HighPrecision const &o);
-
-  int Compare (HighPrecision const &o) const;
-  static HighPrecision Zero (void);
-private:
-  static const double MAX_64;
-  double m_value;
-};
-
-#endif /* HIGH_PRECISION_I128 */
-
 HighPrecision Abs (HighPrecision const &value);
 HighPrecision Max (HighPrecision const &a, HighPrecision const &b);
 HighPrecision Min (HighPrecision const &a, HighPrecision const &b);