sreal.h 5.79 KB
Newer Older
1
/* Definitions for simple data type for real numbers.
2
   Copyright (C) 2002-2014 Free Software Foundation, Inc.
3 4 5 6 7

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
8
Software Foundation; either version 3, or (at your option) any later
9 10 11 12 13 14 15 16
version.

GCC 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
17 18
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
19 20 21 22 23

#ifndef GCC_SREAL_H
#define GCC_SREAL_H

/* SREAL_PART_BITS has to be an even number.  */
Trevor Saunders committed
24
#define SREAL_PART_BITS 32
25

26 27
#define UINT64_BITS	64

28 29
#define SREAL_MIN_SIG ((int64_t) 1 << (SREAL_PART_BITS - 2))
#define SREAL_MAX_SIG (((int64_t) 1 << (SREAL_PART_BITS - 1)) - 1)
30 31 32 33 34
#define SREAL_MAX_EXP (INT_MAX / 4)

#define SREAL_BITS SREAL_PART_BITS

/* Structure for holding a simple real number.  */
Trevor Saunders committed
35
class sreal
36
{
Trevor Saunders committed
37 38
public:
  /* Construct an uninitialized sreal.  */
39
  sreal () : m_sig (-1), m_exp (-1) {}
Trevor Saunders committed
40 41

  /* Construct a sreal.  */
42
  sreal (int64_t sig, int exp = 0) : m_sig (sig), m_exp (exp)
43 44 45
  {
    normalize ();
  }
Trevor Saunders committed
46 47 48

  void dump (FILE *) const;
  int64_t to_int () const;
49
  double to_double () const;
Trevor Saunders committed
50 51 52 53 54 55 56
  sreal operator+ (const sreal &other) const;
  sreal operator- (const sreal &other) const;
  sreal operator* (const sreal &other) const;
  sreal operator/ (const sreal &other) const;

  bool operator< (const sreal &other) const
  {
57 58 59 60 61 62 63 64 65 66 67 68 69
    if (m_exp == other.m_exp)
      return m_sig < other.m_sig;
    else
    {
      bool negative = m_sig < 0;
      bool other_negative = other.m_sig < 0;

      if (negative != other_negative)
        return negative > other_negative;

      bool r = m_exp < other.m_exp;
      return negative ? !r : r;
    }
Trevor Saunders committed
70 71 72 73
  }

  bool operator== (const sreal &other) const
  {
74
    return m_exp == other.m_exp && m_sig == other.m_sig;
75 76 77 78 79
  }

  sreal operator- () const
  {
    sreal tmp = *this;
80
    tmp.m_sig *= -1;
81 82 83 84

    return tmp;
  }

85
  sreal shift (int s) const
86
  {
87 88 89
    /* Zero needs no shifting.  */
    if (!m_sig)
      return *this;
90 91 92
    gcc_checking_assert (s <= SREAL_BITS);
    gcc_checking_assert (s >= -SREAL_BITS);

93 94
    /* Overflows/drop to 0 could be handled gracefully, but hopefully we do not
       need to do so.  */
95 96 97
    gcc_checking_assert (m_exp + s <= SREAL_MAX_EXP);
    gcc_checking_assert (m_exp + s >= -SREAL_MAX_EXP);

98
    sreal tmp = *this;
99
    tmp.m_exp += s;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

    return tmp;
  }

  /* Global minimum sreal can hold.  */
  inline static sreal min ()
  {
    static sreal min = sreal (-SREAL_MAX_SIG, SREAL_MAX_EXP);
    return min;
  }

  /* Global minimum sreal can hold.  */
  inline static sreal max ()
  {
    static sreal max = sreal (SREAL_MAX_SIG, SREAL_MAX_EXP);
    return max;
Trevor Saunders committed
116 117 118
  }

private:
119 120 121
  inline void normalize ();
  inline void normalize_up ();
  inline void normalize_down ();
Trevor Saunders committed
122
  void shift_right (int amount);
123 124 125
  static sreal signedless_plus (const sreal &a, const sreal &b, bool negative);
  static sreal signedless_minus (const sreal &a, const sreal &b, bool negative);

126
  int64_t m_sig;			/* Significant.  */
Trevor Saunders committed
127
  signed int m_exp;			/* Exponent.  */
128
};
129

130 131
extern void debug (const sreal &ref);
extern void debug (const sreal *ptr);
Trevor Saunders committed
132 133 134 135 136 137 138 139

inline sreal &operator+= (sreal &a, const sreal &b)
{
  return a = a + b;
}

inline sreal &operator-= (sreal &a, const sreal &b)
{
140
  return a = a - b;
Trevor Saunders committed
141 142 143 144
}

inline sreal &operator/= (sreal &a, const sreal &b)
{
145
  return a = a / b;
Trevor Saunders committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
}

inline sreal &operator*= (sreal &a, const sreal &b)
{
  return a = a  * b;
}

inline bool operator!= (const sreal &a, const sreal &b)
{
  return !(a == b);
}

inline bool operator> (const sreal &a, const sreal &b)
{
  return !(a == b || a < b);
}

inline bool operator<= (const sreal &a, const sreal &b)
{
  return a < b || a == b;
}

inline bool operator>= (const sreal &a, const sreal &b)
{
  return a == b || a > b;
}
172

173 174 175 176 177 178 179 180 181 182
inline sreal operator<< (const sreal &a, int exp)
{
  return a.shift (exp);
}

inline sreal operator>> (const sreal &a, int exp)
{
  return a.shift (-exp);
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
/* Make significant to be >= SREAL_MIN_SIG.

   Make this separate method so inliner can handle hot path better.  */

inline void
sreal::normalize_up ()
{
  int64_t s = m_sig < 0 ? -1 : 1;
  unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
  int shift = SREAL_PART_BITS - 2 - floor_log2 (sig);

  gcc_checking_assert (shift > 0);
  sig <<= shift;
  m_exp -= shift;
  gcc_checking_assert (sig <= SREAL_MAX_SIG && sig >= SREAL_MIN_SIG);

  /* Check underflow.  */
  if (m_exp < -SREAL_MAX_EXP)
    {
      m_exp = -SREAL_MAX_EXP;
      sig = 0;
    }
  if (s == -1)
    m_sig = -sig;
  else
    m_sig = sig;
}

/* Make significant to be <= SREAL_MAX_SIG.

   Make this separate method so inliner can handle hot path better.  */

inline void
sreal::normalize_down ()
{
  int64_t s = m_sig < 0 ? -1 : 1;
  int last_bit;
  unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
  int shift = floor_log2 (sig) - SREAL_PART_BITS + 2;

  gcc_checking_assert (shift > 0);
  last_bit = (sig >> (shift-1)) & 1;
  sig >>= shift;
  m_exp += shift;
  gcc_checking_assert (sig <= SREAL_MAX_SIG && sig >= SREAL_MIN_SIG);

  /* Round the number.  */
  sig += last_bit;
  if (sig > SREAL_MAX_SIG)
    {
      sig >>= 1;
      m_exp++;
    }

  /* Check overflow.  */
  if (m_exp > SREAL_MAX_EXP)
    {
      m_exp = SREAL_MAX_EXP;
      sig = SREAL_MAX_SIG;
    }
  if (s == -1)
    m_sig = -sig;
  else
    m_sig = sig;
}

/* Normalize *this; the hot path.  */

inline void
sreal::normalize ()
{
  unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);

  if (sig == 0)
    m_exp = -SREAL_MAX_EXP;
  else if (sig > SREAL_MAX_SIG)
    normalize_down ();
  else if (sig < SREAL_MIN_SIG)
    normalize_up ();
}

264
#endif