src/simulator/high-precision.h
changeset 153 22c3d53dbae3
child 154 9ef3700452c7
equal deleted inserted replaced
152:f3e570a78662 153:22c3d53dbae3
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2006 INRIA
       
     4  * All rights reserved.
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License version 2 as
       
     8  * published by the Free Software Foundation;
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  *
       
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    20  */
       
    21 #ifndef HIGH_PRECISION_H
       
    22 #define HIGH_PRECISION_H
       
    23 
       
    24 #include <stdint.h>
       
    25 
       
    26 #ifdef HIGH_PRECISION_I128
       
    27 
       
    28 /**
       
    29  * This should be a high-precision 128bit integer version of
       
    30  * HighPrecision class. It should also be able to report 
       
    31  * overflow and underflow.
       
    32  */
       
    33 class HighPrecision 
       
    34 {
       
    35 public:
       
    36   HighPrecision ();
       
    37   HighPrecision (int64_t high, int64_t low);
       
    38   HighPrecision (double value);
       
    39   
       
    40   int64_t GetHigh (void) const;
       
    41   int64_t GetLow (void) const;
       
    42   double GetDouble (void) const;
       
    43   bool Add (HighPrecision const &o);
       
    44   bool Sub (HighPrecision const &o);
       
    45   bool Mul (HighPrecision const &o);
       
    46   bool Div (HighPrecision const &o);
       
    47 
       
    48   int Compare (HighPrecision const &o) const;
       
    49   static HighPrecision Zero (void);
       
    50 private:
       
    51   int64_t m_high;
       
    52   int64_t m_low;
       
    53 };
       
    54 
       
    55 
       
    56 #else /* HIGH_PRECISION_I128 */
       
    57 
       
    58 /**
       
    59  * Obviously, this implementation of the HighPrecision class does
       
    60  * not provide the 128 bits accuracy since it uses a an IEEE754 double
       
    61  * to store the value. It also does not report overflows.
       
    62  * So, it is a nice shortcut but in no way a complete solution.
       
    63  */
       
    64 
       
    65 class HighPrecision 
       
    66 {
       
    67 public:
       
    68   HighPrecision ();
       
    69   HighPrecision (int64_t high, int64_t low);
       
    70   HighPrecision (double value);
       
    71   
       
    72   int64_t GetHigh (void) const;
       
    73   int64_t GetLow (void) const;
       
    74   double GetDouble (void) const;
       
    75   bool Add (HighPrecision const &o);
       
    76   bool Sub (HighPrecision const &o);
       
    77   bool Mul (HighPrecision const &o);
       
    78   bool Div (HighPrecision const &o);
       
    79 
       
    80   int Compare (HighPrecision const &o) const;
       
    81   static HighPrecision Zero (void);
       
    82 private:
       
    83   static const double MAX_64;
       
    84   double m_value;
       
    85 };
       
    86 
       
    87 #endif /* HIGH_PRECISION_I128 */
       
    88 
       
    89 
       
    90 
       
    91 #endif /* HIGH_PRECISION_H */