sreal.c 11.5 KB
Newer Older
1
/* Simple data type for positive real numbers for the GNU compiler.
2
   Copyright (C) 2002, 2003, 2004, 2007, 2010 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 24 25

/* This library supports positive real numbers and 0;
   inf and nan are NOT supported.
   It is written to be simple and fast.

   Value of sreal is
	x = sig * 2 ^ exp
26
   where
27 28 29 30 31 32 33 34 35
	sig = significant
	  (for < 64-bit machines sig = sig_lo + sig_hi * 2 ^ SREAL_PART_BITS)
	exp = exponent

   One HOST_WIDE_INT is used for the significant on 64-bit (and more than
   64-bit) machines,
   otherwise two HOST_WIDE_INTs are used for the significant.
   Only a half of significant bits is used (in normalized sreals) so that we do
   not have problems with overflow, for example when c->sig = a->sig * b->sig.
36
   So the precision for 64-bit and 32-bit machines is 32-bit.
37

38 39 40 41 42
   Invariant: The numbers are normalized before and after each call of sreal_*.

   Normalized sreals:
   All numbers (except zero) meet following conditions:
	 SREAL_MIN_SIG <= sig && sig <= SREAL_MAX_SIG
43
	-SREAL_MAX_EXP <= exp && exp <= SREAL_MAX_EXP
44 45 46 47 48 49 50 51 52 53 54 55 56

   If the number would be too large, it is set to upper bounds of these
   conditions.

   If the number is zero or would be too small it meets following conditions:
	sig == 0 && exp == -SREAL_MAX_EXP
*/

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "sreal.h"

57 58 59
static inline void copy (sreal *, sreal *);
static inline void shift_right (sreal *, int);
static void normalize (sreal *);
60 61 62 63

/* Print the content of struct sreal.  */

void
64
dump_sreal (FILE *file, sreal *x)
65 66
{
#if SREAL_PART_BITS < 32
67 68 69
  fprintf (file, "((" HOST_WIDE_INT_PRINT_UNSIGNED " * 2^16 + "
	   HOST_WIDE_INT_PRINT_UNSIGNED ") * 2^%d)",
	   x->sig_hi, x->sig_lo, x->exp);
70
#else
71
  fprintf (file, "(" HOST_WIDE_INT_PRINT_UNSIGNED " * 2^%d)", x->sig, x->exp);
72 73 74 75 76 77
#endif
}

/* Copy the sreal number.  */

static inline void
78
copy (sreal *r, sreal *a)
79 80 81 82 83 84 85 86 87 88 89 90 91 92
{
#if SREAL_PART_BITS < 32
  r->sig_lo = a->sig_lo;
  r->sig_hi = a->sig_hi;
#else
  r->sig = a->sig;
#endif
  r->exp = a->exp;
}

/* Shift X right by S bits.  Needed: 0 < S <= SREAL_BITS.
   When the most significant bit shifted out is 1, add 1 to X (rounding).  */

static inline void
93
shift_right (sreal *x, int s)
94
{
95 96 97 98 99 100
  gcc_assert (s > 0);
  gcc_assert (s <= SREAL_BITS);
  /* Exponent should never be so large because shift_right is used only by
     sreal_add and sreal_sub ant thus the number cannot be shifted out from
     exponent range.  */
  gcc_assert (x->exp + s <= SREAL_MAX_EXP);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  x->exp += s;

#if SREAL_PART_BITS < 32
  if (s > SREAL_PART_BITS)
    {
      s -= SREAL_PART_BITS;
      x->sig_hi += (uhwi) 1 << (s - 1);
      x->sig_lo = x->sig_hi >> s;
      x->sig_hi = 0;
    }
  else
    {
      x->sig_lo += (uhwi) 1 << (s - 1);
      if (x->sig_lo & ((uhwi) 1 << SREAL_PART_BITS))
	{
	  x->sig_hi++;
	  x->sig_lo -= (uhwi) 1 << SREAL_PART_BITS;
	}
      x->sig_lo >>= s;
      x->sig_lo |= (x->sig_hi & (((uhwi) 1 << s) - 1)) << (SREAL_PART_BITS - s);
      x->sig_hi >>= s;
    }
#else
  x->sig += (uhwi) 1 << (s - 1);
  x->sig >>= s;
#endif
}

/* Normalize *X.  */

static void
133
normalize (sreal *x)
134 135 136 137
{
#if SREAL_PART_BITS < 32
  int shift;
  HOST_WIDE_INT mask;
138

139 140 141 142 143 144 145 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 172 173 174 175 176 177 178 179 180 181 182 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 264 265 266 267 268
  if (x->sig_lo == 0 && x->sig_hi == 0)
    {
      x->exp = -SREAL_MAX_EXP;
    }
  else if (x->sig_hi < SREAL_MIN_SIG)
    {
      if (x->sig_hi == 0)
	{
	  /* Move lower part of significant to higher part.  */
	  x->sig_hi = x->sig_lo;
	  x->sig_lo = 0;
	  x->exp -= SREAL_PART_BITS;
	}
      shift = 0;
      while (x->sig_hi < SREAL_MIN_SIG)
	{
	  x->sig_hi <<= 1;
	  x->exp--;
	  shift++;
	}
      /* Check underflow.  */
      if (x->exp < -SREAL_MAX_EXP)
	{
	  x->exp = -SREAL_MAX_EXP;
	  x->sig_hi = 0;
	  x->sig_lo = 0;
	}
      else if (shift)
	{
	  mask = (1 << SREAL_PART_BITS) - (1 << (SREAL_PART_BITS - shift));
	  x->sig_hi |= (x->sig_lo & mask) >> (SREAL_PART_BITS - shift);
	  x->sig_lo = (x->sig_lo << shift) & (((uhwi) 1 << SREAL_PART_BITS) - 1);
	}
    }
  else if (x->sig_hi > SREAL_MAX_SIG)
    {
      unsigned HOST_WIDE_INT tmp = x->sig_hi;

      /* Find out how many bits will be shifted.  */
      shift = 0;
      do
	{
	  tmp >>= 1;
	  shift++;
	}
      while (tmp > SREAL_MAX_SIG);

      /* Round the number.  */
      x->sig_lo += (uhwi) 1 << (shift - 1);

      x->sig_lo >>= shift;
      x->sig_lo += ((x->sig_hi & (((uhwi) 1 << shift) - 1))
		    << (SREAL_PART_BITS - shift));
      x->sig_hi >>= shift;
      x->exp += shift;
      if (x->sig_lo & ((uhwi) 1 << SREAL_PART_BITS))
	{
	  x->sig_lo -= (uhwi) 1 << SREAL_PART_BITS;
	  x->sig_hi++;
	  if (x->sig_hi > SREAL_MAX_SIG)
	    {
	      /* x->sig_hi was SREAL_MAX_SIG before increment
		 so now last bit is zero.  */
	      x->sig_hi >>= 1;
	      x->sig_lo >>= 1;
	      x->exp++;
	    }
	}

      /* Check overflow.  */
      if (x->exp > SREAL_MAX_EXP)
	{
	  x->exp = SREAL_MAX_EXP;
	  x->sig_hi = SREAL_MAX_SIG;
	  x->sig_lo = SREAL_MAX_SIG;
	}
    }
#else
  if (x->sig == 0)
    {
      x->exp = -SREAL_MAX_EXP;
    }
  else if (x->sig < SREAL_MIN_SIG)
    {
      do
	{
	  x->sig <<= 1;
	  x->exp--;
	}
      while (x->sig < SREAL_MIN_SIG);

      /* Check underflow.  */
      if (x->exp < -SREAL_MAX_EXP)
	{
	  x->exp = -SREAL_MAX_EXP;
	  x->sig = 0;
	}
    }
  else if (x->sig > SREAL_MAX_SIG)
    {
      int last_bit;
      do
	{
	  last_bit = x->sig & 1;
	  x->sig >>= 1;
	  x->exp++;
	}
      while (x->sig > SREAL_MAX_SIG);

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

      /* Check overflow.  */
      if (x->exp > SREAL_MAX_EXP)
	{
	  x->exp = SREAL_MAX_EXP;
	  x->sig = SREAL_MAX_SIG;
	}
    }
#endif
}

/* Set *R to SIG * 2 ^ EXP.  Return R.  */

sreal *
269
sreal_init (sreal *r, unsigned HOST_WIDE_INT sig, signed int exp)
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
{
#if SREAL_PART_BITS < 32
  r->sig_lo = 0;
  r->sig_hi = sig;
  r->exp = exp - 16;
#else
  r->sig = sig;
  r->exp = exp;
#endif
  normalize (r);
  return r;
}

/* Return integer value of *R.  */

HOST_WIDE_INT
286
sreal_to_int (sreal *r)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
{
#if SREAL_PART_BITS < 32
  if (r->exp <= -SREAL_BITS)
    return 0;
  if (r->exp >= 0)
    return MAX_HOST_WIDE_INT;
  return ((r->sig_hi << SREAL_PART_BITS) + r->sig_lo) >> -r->exp;
#else
  if (r->exp <= -SREAL_BITS)
    return 0;
  if (r->exp >= SREAL_PART_BITS)
    return MAX_HOST_WIDE_INT;
  if (r->exp > 0)
    return r->sig << r->exp;
  if (r->exp < 0)
    return r->sig >> -r->exp;
  return r->sig;
#endif
}

/* Compare *A and *B. Return -1 if *A < *B, 1 if *A > *B and 0 if *A == *B.  */

int
310
sreal_compare (sreal *a, sreal *b)
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
{
  if (a->exp > b->exp)
    return 1;
  if (a->exp < b->exp)
    return -1;
#if SREAL_PART_BITS < 32
  if (a->sig_hi > b->sig_hi)
    return 1;
  if (a->sig_hi < b->sig_hi)
    return -1;
  if (a->sig_lo > b->sig_lo)
    return 1;
  if (a->sig_lo < b->sig_lo)
    return -1;
#else
  if (a->sig > b->sig)
    return 1;
  if (a->sig < b->sig)
    return -1;
#endif
  return 0;
}

/* *R = *A + *B.  Return R.  */

sreal *
337
sreal_add (sreal *r, sreal *a, sreal *b)
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
{
  int dexp;
  sreal tmp;
  sreal *bb;

  if (sreal_compare (a, b) < 0)
    {
      sreal *swap;
      swap = a;
      a = b;
      b = swap;
    }

  dexp = a->exp - b->exp;
  r->exp = a->exp;
  if (dexp > SREAL_BITS)
    {
#if SREAL_PART_BITS < 32
      r->sig_hi = a->sig_hi;
      r->sig_lo = a->sig_lo;
#else
      r->sig = a->sig;
#endif
      return r;
    }

  if (dexp == 0)
    bb = b;
  else
    {
      copy (&tmp, b);
      shift_right (&tmp, dexp);
      bb = &tmp;
    }

#if SREAL_PART_BITS < 32
  r->sig_hi = a->sig_hi + bb->sig_hi;
  r->sig_lo = a->sig_lo + bb->sig_lo;
  if (r->sig_lo & ((uhwi) 1 << SREAL_PART_BITS))
    {
      r->sig_hi++;
      r->sig_lo -= (uhwi) 1 << SREAL_PART_BITS;
    }
#else
  r->sig = a->sig + bb->sig;
#endif
  normalize (r);
  return r;
}

/* *R = *A - *B.  Return R.  */

sreal *
391
sreal_sub (sreal *r, sreal *a, sreal *b)
392 393 394 395 396
{
  int dexp;
  sreal tmp;
  sreal *bb;

397
  gcc_assert (sreal_compare (a, b) >= 0);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

  dexp = a->exp - b->exp;
  r->exp = a->exp;
  if (dexp > SREAL_BITS)
    {
#if SREAL_PART_BITS < 32
      r->sig_hi = a->sig_hi;
      r->sig_lo = a->sig_lo;
#else
      r->sig = a->sig;
#endif
      return r;
    }
  if (dexp == 0)
    bb = b;
  else
    {
      copy (&tmp, b);
      shift_right (&tmp, dexp);
      bb = &tmp;
    }

#if SREAL_PART_BITS < 32
  if (a->sig_lo < bb->sig_lo)
    {
      r->sig_hi = a->sig_hi - bb->sig_hi - 1;
      r->sig_lo = a->sig_lo + ((uhwi) 1 << SREAL_PART_BITS) - bb->sig_lo;
    }
  else
    {
      r->sig_hi = a->sig_hi - bb->sig_hi;
      r->sig_lo = a->sig_lo - bb->sig_lo;
    }
#else
  r->sig = a->sig - bb->sig;
#endif
  normalize (r);
  return r;
}

/* *R = *A * *B.  Return R.  */

sreal *
441
sreal_mul (sreal *r, sreal *a, sreal *b)
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
{
#if SREAL_PART_BITS < 32
  if (a->sig_hi < SREAL_MIN_SIG || b->sig_hi < SREAL_MIN_SIG)
    {
      r->sig_lo = 0;
      r->sig_hi = 0;
      r->exp = -SREAL_MAX_EXP;
    }
  else
    {
      unsigned HOST_WIDE_INT tmp1, tmp2, tmp3;
      if (sreal_compare (a, b) < 0)
	{
	  sreal *swap;
	  swap = a;
	  a = b;
	  b = swap;
	}

      r->exp = a->exp + b->exp + SREAL_PART_BITS;

      tmp1 = a->sig_lo * b->sig_lo;
      tmp2 = a->sig_lo * b->sig_hi;
      tmp3 = a->sig_hi * b->sig_lo + (tmp1 >> SREAL_PART_BITS);

      r->sig_hi = a->sig_hi * b->sig_hi;
      r->sig_hi += (tmp2 >> SREAL_PART_BITS) + (tmp3 >> SREAL_PART_BITS);
      tmp2 &= ((uhwi) 1 << SREAL_PART_BITS) - 1;
      tmp3 &= ((uhwi) 1 << SREAL_PART_BITS) - 1;
      tmp1 = tmp2 + tmp3;

      r->sig_lo = tmp1 & (((uhwi) 1 << SREAL_PART_BITS) - 1);
      r->sig_hi += tmp1 >> SREAL_PART_BITS;

      normalize (r);
    }
#else
  if (a->sig < SREAL_MIN_SIG || b->sig < SREAL_MIN_SIG)
    {
      r->sig = 0;
      r->exp = -SREAL_MAX_EXP;
    }
  else
    {
      r->sig = a->sig * b->sig;
      r->exp = a->exp + b->exp;
      normalize (r);
    }
#endif
  return r;
}

/* *R = *A / *B.  Return R.  */

sreal *
497
sreal_div (sreal *r, sreal *a, sreal *b)
498 499 500 501
{
#if SREAL_PART_BITS < 32
  unsigned HOST_WIDE_INT tmp, tmp1, tmp2;

502 503
  gcc_assert (b->sig_hi >= SREAL_MIN_SIG);
  if (a->sig_hi < SREAL_MIN_SIG)
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
    {
      r->sig_hi = 0;
      r->sig_lo = 0;
      r->exp = -SREAL_MAX_EXP;
    }
  else
    {
      /* Since division by the whole number is pretty ugly to write
	 we are dividing by first 3/4 of bits of number.  */

      tmp1 = (a->sig_hi << SREAL_PART_BITS) + a->sig_lo;
      tmp2 = ((b->sig_hi << (SREAL_PART_BITS / 2))
	      + (b->sig_lo >> (SREAL_PART_BITS / 2)));
      if (b->sig_lo & ((uhwi) 1 << ((SREAL_PART_BITS / 2) - 1)))
	tmp2++;

      r->sig_lo = 0;
      tmp = tmp1 / tmp2;
      tmp1 = (tmp1 % tmp2) << (SREAL_PART_BITS / 2);
      r->sig_hi = tmp << SREAL_PART_BITS;

      tmp = tmp1 / tmp2;
      tmp1 = (tmp1 % tmp2) << (SREAL_PART_BITS / 2);
      r->sig_hi += tmp << (SREAL_PART_BITS / 2);

      tmp = tmp1 / tmp2;
      r->sig_hi += tmp;

      r->exp = a->exp - b->exp - SREAL_BITS - SREAL_PART_BITS / 2;
      normalize (r);
    }
#else
536 537 538 539
  gcc_assert (b->sig != 0);
  r->sig = (a->sig << SREAL_PART_BITS) / b->sig;
  r->exp = a->exp - b->exp - SREAL_PART_BITS;
  normalize (r);
540 541 542
#endif
  return r;
}