Add HighPrecision class used for external Time bookkeeping
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 03 Nov 2006 09:05:58 +0100
changeset 153 22c3d53dbae3
parent 152 f3e570a78662
child 154 9ef3700452c7
Add HighPrecision class used for external Time bookkeeping
SConstruct
src/simulator/high-precision.cc
src/simulator/high-precision.h
--- a/SConstruct	Fri Nov 03 09:04:12 2006 +0100
+++ b/SConstruct	Fri Nov 03 09:05:58 2006 +0100
@@ -48,6 +48,7 @@
 ns3.add(simu)
 simu.add_dep('core')
 simu.add_sources([
+    'high-precision.cc',
     'time.cc',
     'event-id.cc',
     'scheduler.cc',
@@ -64,6 +65,7 @@
     'scheduler-list.h'
     ])
 simu.add_inst_headers([
+    'high-precision.h',
     'nstime.h',
     'event-id.h',
     'event-impl.h',
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/simulator/high-precision.cc	Fri Nov 03 09:05:58 2006 +0100
@@ -0,0 +1,158 @@
+/* -*- 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.h"
+
+#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 */
+
+#include <cmath>
+#include <cassert>
+
+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 */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/simulator/high-precision.h	Fri Nov 03 09:05:58 2006 +0100
@@ -0,0 +1,91 @@
+/* -*- 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_H
+#define HIGH_PRECISION_H
+
+#include <stdint.h>
+
+#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 */
+
+
+
+#endif /* HIGH_PRECISION_H */