/* real.c - software floating point emulation.
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002,
2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
Free Software Foundation, Inc.
Contributed by Stephen L. Moshier (moshier@world.std.com).
Re-written by Richard Henderson <rth@redhat.com>
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
Software Foundation; either version 3, or (at your option) any later
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
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "diagnostic-core.h"
#include "real.h"
#include "realmpfr.h"
#include "tm_p.h"
#include "dfp.h"
/* The floating point model used internally is not exactly IEEE 754
compliant, and close to the description in the ISO C99 standard,
section 5.2.4.2.2 Characteristics of floating types.
Specifically
x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
where
s = sign (+- 1)
b = base or radix, here always 2
e = exponent
p = precision (the number of base-b digits in the significand)
f_k = the digits of the significand.
We differ from typical IEEE 754 encodings in that the entire
significand is fractional. Normalized significands are in the
range [0.5, 1.0).
A requirement of the model is that P be larger than the largest
supported target floating-point type by at least 2 bits. This gives
us proper rounding when we truncate to the target type. In addition,
E must be large enough to hold the smallest supported denormal number
in a normalized form.
Both of these requirements are easily satisfied. The largest target
significand is 113 bits; we store at least 160. The smallest
denormal number fits in 17 exponent bits; we store 26.
Note that the decimal string conversion routines are sensitive to
rounding errors. Since the raw arithmetic routines do not themselves
have guard digits or rounding, the computation of 10**exp can
accumulate more than a few digits of error. The previous incarnation
of real.c successfully used a 144-bit fraction; given the current
layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits. */
/* Used to classify two numbers simultaneously. */
#define CLASS2(A, B) ((A) << 2 | (B))
#if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
#error "Some constant folding done by hand to avoid shift count warnings"
#endif
static void get_zero (REAL_VALUE_TYPE *, int);
static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
static void get_canonical_snan (REAL_VALUE_TYPE *, int);
static void get_inf (REAL_VALUE_TYPE *, int);
static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *, unsigned int);
static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
unsigned int);
static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
unsigned int);
static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *);
static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *, int);
static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
static int cmp_significand_0 (const REAL_VALUE_TYPE *);
static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *);
static void normalize (REAL_VALUE_TYPE *);
static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *, int);
static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *);
static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
const REAL_VALUE_TYPE *);
static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
static void decimal_from_integer (REAL_VALUE_TYPE *);
static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
size_t);
static const REAL_VALUE_TYPE * ten_to_ptwo (int);
static const REAL_VALUE_TYPE * ten_to_mptwo (int);
static const REAL_VALUE_TYPE * real_digit (int);
static void times_pten (REAL_VALUE_TYPE *, int);
static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
/* Initialize R with a positive zero. */
static inline void
get_zero (REAL_VALUE_TYPE *r, int sign)
{
memset (r, 0, sizeof (*r));
r->sign = sign;
}
/* Initialize R with the canonical quiet NaN. */
static inline void
get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
{
memset (r, 0, sizeof (*r));
r->cl = rvc_nan;
r->sign = sign;
r->canonical = 1;
}
static inline void
get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
{
memset (r, 0, sizeof (*r));
r->cl = rvc_nan;
r->sign = sign;
r->signalling = 1;
r->canonical = 1;
}
static inline void
get_inf (REAL_VALUE_TYPE *r, int sign)
{
memset (r, 0, sizeof (*r));
r->cl = rvc_inf;
r->sign = sign;
}
/* Right-shift the significand of A by N bits; put the result in the
significand of R. If any one bits are shifted out, return true. */
static bool
sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
unsigned int n)
{
unsigned long sticky = 0;
unsigned int i, ofs = 0;
if (n >= HOST_BITS_PER_LONG)
{
for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
sticky |= a->sig[i];
n &= HOST_BITS_PER_LONG - 1;
}
if (n != 0)
{
sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
for (i = 0; i < SIGSZ; ++i)
{
r->sig[i]
= (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
| ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
<< (HOST_BITS_PER_LONG - n)));
}
}
else
{
for (i = 0; ofs + i < SIGSZ; ++i)
r->sig[i] = a->sig[ofs + i];
for (; i < SIGSZ; ++i)
r->sig[i] = 0;
}
return sticky != 0;
}
/* Right-shift the significand of A by N bits; put the result in the
significand of R. */
static void
rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
unsigned int n)
{
unsigned int i, ofs = n / HOST_BITS_PER_LONG;
n &= HOST_BITS_PER_LONG - 1;
if (n != 0)
{
for (i = 0; i < SIGSZ; ++i)
{
r->sig[i]
= (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
| ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
<< (HOST_BITS_PER_LONG - n)));
}
}
else
{
for (i = 0; ofs + i < SIGSZ; ++i)
r->sig[i] = a->sig[ofs + i];
for (; i < SIGSZ; ++i)
r->sig[i] = 0;
}
}
/* Left-shift the significand of A by N bits; put the result in the
significand of R. */
static void
lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
unsigned int n)
{
unsigned int i, ofs = n / HOST_BITS_PER_LONG;
n &= HOST_BITS_PER_LONG - 1;
if (n == 0)
{
for (i = 0; ofs + i < SIGSZ; ++i)
r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
for (; i < SIGSZ; ++i)
r->sig[SIGSZ-1-i] = 0;
}
else
for (i = 0; i < SIGSZ; ++i)
{
r->sig[SIGSZ-1-i]
= (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
| ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
>> (HOST_BITS_PER_LONG - n)));
}
}
/* Likewise, but N is specialized to 1. */
static inline void
lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
{
unsigned int i;
for (i = SIGSZ - 1; i > 0; --i)
r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
r->sig[0] = a->sig[0] << 1;
}
/* Add the significands of A and B, placing the result in R. Return
true if there was carry out of the most significant word. */
static inline bool
add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
const REAL_VALUE_TYPE *b)
{
bool carry = false;
int i;
for (i = 0; i < SIGSZ; ++i)
{
unsigned long ai = a->sig[i];
unsigned long ri = ai + b->sig[i];
if (carry)
{
carry = ri < ai;
carry |= ++ri == 0;
}
else
carry = ri < ai;
r->sig[i] = ri;
}
return carry;
}
/* Subtract the significands of A and B, placing the result in R. CARRY is
true if there's a borrow incoming to the least significant word.
Return true if there was borrow out of the most significant word. */
static inline bool
sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
const REAL_VALUE_TYPE *b, int carry)
{
int i;
for (i = 0; i < SIGSZ; ++i)
{
unsigned long ai = a->sig[i];
unsigned long ri = ai - b->sig[i];
if (carry)
{
carry = ri > ai;
carry |= ~--ri == 0;
}
else
carry = ri > ai;
r->sig[i] = ri;
}
return carry;
}
/* Negate the significand A, placing the result in R. */
static inline void
neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
{
bool carry = true;
int i;
for (i = 0; i < SIGSZ; ++i)
{
unsigned long ri, ai = a->sig[i];
if (carry)
{
if (ai)
{
ri = -ai;
carry = false;
}
else
ri = ai;
}
else
ri = ~ai;
r->sig[i] = ri;
}
}
/* Compare significands. Return tri-state vs zero. */
static inline int
cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
{
int i;
for (i = SIGSZ - 1; i >= 0; --i)
{
unsigned long ai = a->sig[i];
unsigned long bi = b->sig[i];
if (ai > bi)
return 1;
if (ai < bi)
return -1;
}
return 0;
}
/* Return true if A is nonzero. */
static inline int
cmp_significand_0 (const REAL_VALUE_TYPE *a)
{
int i;
for (i = SIGSZ - 1; i >= 0; --i)
if (a->sig[i])
return 1;
return 0;
}
/* Set bit N of the significand of R. */
static inline void
set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
{
r->sig[n / HOST_BITS_PER_LONG]
|= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
}
/* Clear bit N of the significand of R. */
static inline void
clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
{
r->sig[n / HOST_BITS_PER_LONG]
&= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
}
/* Test bit N of the significand of R. */
static inline bool
test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
{
/* ??? Compiler bug here if we return this expression directly.
The conversion to bool strips the "&1" and we wind up testing
e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
return t;
}
/* Clear bits 0..N-1 of the significand of R. */
static void
clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
{
int i, w = n / HOST_BITS_PER_LONG;
for (i = 0; i < w; ++i)
r->sig[i] = 0;
r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
}
/* Divide the significands of A and B, placing the result in R. Return
true if the division was inexact. */
static inline bool
div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
const REAL_VALUE_TYPE *b)
{
REAL_VALUE_TYPE u;
int i, bit = SIGNIFICAND_BITS - 1;
unsigned long msb, inexact;
u = *a;
memset (r->sig, 0, sizeof (r->sig));
msb = 0;
goto start;
do
{
msb = u.sig[SIGSZ-1] & SIG_MSB;
lshift_significand_1 (&u, &u);
start:
if (msb || cmp_significands (&u, b) >= 0)
{
sub_significands (&u, &u, b, 0);
set_significand_bit (r, bit);
}
}
while (--bit >= 0);
for (i = 0, inexact = 0; i < SIGSZ; i++)
inexact |= u.sig[i];
return inexact != 0;
}
/* Adjust the exponent and significand of R such that the most
significant bit is set. We underflow to zero and overflow to
infinity here, without denormals. (The intermediate representation
exponent is large enough to handle target denormals normalized.) */
static void
normalize (REAL_VALUE_TYPE *r)
{
int shift = 0, exp;
int i, j;
if (r->decimal)
return;
/* Find the first word that is nonzero. */
for (i = SIGSZ - 1; i >= 0; i--)
if (r->sig[i] == 0)
shift += HOST_BITS_PER_LONG;
else
break;
/* Zero significand flushes to zero. */
if (i < 0)
{
r->cl = rvc_zero;
SET_REAL_EXP (r, 0);
return;
}
/* Find the first bit that is nonzero. */
for (j = 0; ; j++)
if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
break;
shift += j;
if (shift > 0)
{
exp = REAL_EXP (r) - shift;
if (exp > MAX_EXP)
get_inf (r, r->sign);
else if (exp < -MAX_EXP)
get_zero (r, r->sign);
else
{
SET_REAL_EXP (r, exp);
lshift_significand (r, r, shift);
}
}
}
/* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
result may be inexact due to a loss of precision. */
static bool
do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
const REAL_VALUE_TYPE *b, int subtract_p)
{
int dexp, sign, exp;
REAL_VALUE_TYPE t;
bool inexact = false;
/* Determine if we need to add or subtract. */
sign = a->sign;
subtract_p = (sign ^ b->sign) ^ subtract_p;
switch (CLASS2 (a->cl, b->cl))
{
case CLASS2 (rvc_zero, rvc_zero):
/* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
get_zero (r, sign & !subtract_p);
return false;
case CLASS2 (rvc_zero, rvc_normal):
case CLASS2 (rvc_zero, rvc_inf):
case CLASS2 (rvc_zero, rvc_nan):
/* 0 + ANY = ANY. */
case CLASS2 (rvc_normal, rvc_nan):
case CLASS2 (rvc_inf, rvc_nan):
case CLASS2 (rvc_nan, rvc_nan):
/* ANY + NaN = NaN. */
case CLASS2 (rvc_normal, rvc_inf):
/* R + Inf = Inf. */
*r = *b;
r->sign = sign ^ subtract_p;
return false;
case CLASS2 (rvc_normal, rvc_zero):
case CLASS2 (rvc_inf, rvc_zero):
case CLASS2 (rvc_nan, rvc_zero):
/* ANY + 0 = ANY. */
case CLASS2 (rvc_nan, rvc_normal):
case CLASS2 (rvc_nan, rvc_inf):
/* NaN + ANY = NaN. */
case CLASS2 (rvc_inf, rvc_normal):
/* Inf + R = Inf. */
*r = *a;
return false;
case CLASS2 (rvc_inf, rvc_inf):
if (subtract_p)
/* Inf - Inf = NaN. */
get_canonical_qnan (r, 0);
else
/* Inf + Inf = Inf. */
*r = *a;
return false;
case CLASS2 (rvc_normal, rvc_normal):
break;
default:
gcc_unreachable ();
}
/* Swap the arguments such that A has the larger exponent. */
dexp = REAL_EXP (a) - REAL_EXP (b);
if (dexp < 0)
{
const REAL_VALUE_TYPE *t;
t = a, a = b, b = t;
dexp = -dexp;
sign ^= subtract_p;
}
exp = REAL_EXP (a);
/* If the exponents are not identical, we need to shift the
significand of B down. */
if (dexp > 0)
{
/* If the exponents are too far apart, the significands
do not overlap, which makes the subtraction a noop. */
if (dexp >= SIGNIFICAND_BITS)
{
*r = *a;
r->sign = sign;
return true;
}
inexact |= sticky_rshift_significand (&t, b, dexp);
b = &t;
}
if (subtract_p)
{
if (sub_significands (r, a, b, inexact))
{
/* We got a borrow out of the subtraction. That means that
A and B had the same exponent, and B had the larger
significand. We need to swap the sign and negate the
significand. */
sign ^= 1;
neg_significand (r, r);
}
}
else
{
if (add_significands (r, a, b))
{
/* We got carry out of the addition. This means we need to
shift the significand back down one bit and increase the
exponent. */
inexact |= sticky_rshift_significand (r, r, 1);
r->sig[SIGSZ-1] |= SIG_MSB;
if (++exp > MAX_EXP)
{
get_inf (r, sign);
return true;
}
}
}
r->cl = rvc_normal;
r->sign = sign;
SET_REAL_EXP (r, exp);
/* Zero out the remaining fields. */
r->signalling = 0;
r->canonical = 0;
r->decimal = 0;
/* Re-normalize the result. */
normalize (r);
/* Special case: if the subtraction results in zero, the result
is positive. */
if (r->cl == rvc_zero)
r->sign = 0;
else
r->sig[0] |= inexact;
return inexact;
}
/* Calculate R = A * B. Return true if the result may be inexact. */
static bool
do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
const REAL_VALUE_TYPE *b)
{
REAL_VALUE_TYPE u, t, *rr;
unsigned int i, j, k;
int sign = a->sign ^ b->sign;
bool inexact = false;
switch (CLASS2 (a->cl, b->cl))
{
case CLASS2 (rvc_zero, rvc_zero):
case CLASS2 (rvc_zero, rvc_normal):
case CLASS2 (rvc_normal, rvc_zero):
/* +-0 * ANY = 0 with appropriate sign. */
get_zero (r, sign);
return false;
case CLASS2 (rvc_zero, rvc_nan):
case CLASS2 (rvc_normal, rvc_nan):
case CLASS2 (rvc_inf, rvc_nan):
case CLASS2 (rvc_nan, rvc_nan):
/* ANY * NaN = NaN. */
*r = *b;
r->sign = sign;
return false;
case CLASS2 (rvc_nan, rvc_zero):
case CLASS2 (rvc_nan, rvc_normal):
case CLASS2 (rvc_nan, rvc_inf):
/* NaN * ANY = NaN. */
*r = *a;
r->sign = sign;
return false;
case CLASS2 (rvc_zero, rvc_inf):
case CLASS2 (rvc_inf, rvc_zero):
/* 0 * Inf = NaN */
get_canonical_qnan (r, sign);
return false;
case CLASS2 (rvc_inf, rvc_inf):
case CLASS2 (rvc_normal, rvc_inf):
case CLASS2 (rvc_inf, rvc_normal):
/* Inf * Inf = Inf, R * Inf = Inf */
get_inf (r, sign);
return false;
case CLASS2 (rvc_normal, rvc_normal):
break;
default:
gcc_unreachable ();
}
if (r == a || r == b)
rr = &t;
else
rr = r;
get_zero (rr, 0);
/* Collect all the partial products. Since we don't have sure access
to a widening multiply, we split each long into two half-words.
Consider the long-hand form of a four half-word multiplication:
A B C D
* E F G H
--------------
DE DF DG DH
CE CF CG CH
BE BF BG BH
AE AF AG AH
We construct partial products of the widened half-word products
that are known to not overlap, e.g. DF+DH. Each such partial
product is given its proper exponent, which allows us to sum them
and obtain the finished product. */
for (i = 0; i < SIGSZ * 2; ++i)
{
unsigned long ai = a->sig[i / 2];
if (i & 1)
ai >>= HOST_BITS_PER_LONG / 2;
else
ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
if (ai == 0)
continue;
for (j = 0; j < 2; ++j)
{
int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
+ (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
if (exp > MAX_EXP)
{
get_inf (r, sign);
return true;
}
if (exp < -MAX_EXP)
{
/* Would underflow to zero, which we shouldn't bother adding. */
inexact = true;
continue;
}
memset (&u, 0, sizeof (u));
u.cl = rvc_normal;
SET_REAL_EXP (&u, exp);
for (k = j; k < SIGSZ * 2; k += 2)
{
unsigned long bi = b->sig[k / 2];
if (k & 1)
bi >>= HOST_BITS_PER_LONG / 2;
else
bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
u.sig[k / 2] = ai * bi;
}
normalize (&u);
inexact |= do_add (rr, rr, &u, 0);
}
}
rr->sign = sign;
if (rr != r)
*r = t;
return inexact;
}
/* Calculate R = A / B. Return true if the result may be inexact. */
static bool
do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
const REAL_VALUE_TYPE *b)
{
int exp, sign = a->sign ^ b->sign;
REAL_VALUE_TYPE t, *rr;
bool inexact;
switch (CLASS2 (a->cl, b->cl))
{
case CLASS2 (rvc_zero, rvc_zero):
/* 0 / 0 = NaN. */
case CLASS2 (rvc_inf, rvc_inf):
/* Inf / Inf = NaN. */
get_canonical_qnan (r, sign);
return false;
case CLASS2 (rvc_zero, rvc_normal):
case CLASS2 (rvc_zero, rvc_inf):
/* 0 / ANY = 0. */
case CLASS2 (rvc_normal, rvc_inf):
/* R / Inf = 0. */
get_zero (r, sign);
return false;
case CLASS2 (rvc_normal, rvc_zero):
/* R / 0 = Inf. */
case CLASS2 (rvc_inf, rvc_zero):
/* Inf / 0 = Inf. */
get_inf (r, sign);
return false;
case CLASS2 (rvc_zero, rvc_nan):
case CLASS2 (rvc_normal, rvc_nan):
case CLASS2 (rvc_inf, rvc_nan):
case CLASS2 (rvc_nan, rvc_nan):
/* ANY / NaN = NaN. */
*r = *b;
r->sign = sign;
return false;
case CLASS2 (rvc_nan, rvc_zero):
case CLASS2 (rvc_nan, rvc_normal):
case CLASS2 (rvc_nan, rvc_inf):
/* NaN / ANY = NaN. */
*r = *a;
r->sign = sign;
return false;
case CLASS2 (rvc_inf, rvc_normal):
/* Inf / R = Inf. */
get_inf (r, sign);
return false;
case CLASS2 (rvc_normal, rvc_normal):
break;
default:
gcc_unreachable ();
}
if (r == a || r == b)
rr = &t;
else
rr = r;
/* Make sure all fields in the result are initialized. */
get_zero (rr, 0);
rr->cl = rvc_normal;
rr->sign = sign;
exp = REAL_EXP (a) - REAL_EXP (b) + 1;
if (exp > MAX_EXP)
{
get_inf (r, sign);
return true;
}
if (exp < -MAX_EXP)
{
get_zero (r, sign);
return true;
}
SET_REAL_EXP (rr, exp);
inexact = div_significands (rr, a, b);
/* Re-normalize the result. */
normalize (rr);
rr->sig[0] |= inexact;
if (rr != r)
*r = t;
return inexact;
}
/* Return a tri-state comparison of A vs B. Return NAN_RESULT if
one of the two operands is a NaN. */
static int
do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
int nan_result)
{
int ret;
switch (CLASS2 (a->cl, b->cl))
{
case CLASS2 (rvc_zero, rvc_zero):
/* Sign of zero doesn't matter for compares. */
return 0;
case CLASS2 (rvc_normal, rvc_zero):
/* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
if (a->decimal)
return decimal_do_compare (a, b, nan_result);
/* Fall through. */
case CLASS2 (rvc_inf, rvc_zero):
case CLASS2 (rvc_inf, rvc_normal):
return (a->sign ? -1 : 1);
case CLASS2 (rvc_inf, rvc_inf):
return -a->sign - -b->sign;
case CLASS2 (rvc_zero, rvc_normal):
/* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
if (b->decimal)
return decimal_do_compare (a, b, nan_result);
/* Fall through. */
case CLASS2 (rvc_zero, rvc_inf):
case CLASS2 (rvc_normal, rvc_inf):
return (b->sign ? 1 : -1);
case CLASS2 (rvc_zero, rvc_nan):
case CLASS2 (rvc_normal, rvc_nan):
case CLASS2 (rvc_inf, rvc_nan):
case CLASS2 (rvc_nan, rvc_nan):
case CLASS2 (rvc_nan, rvc_zero):
case CLASS2 (rvc_nan, rvc_normal):
case CLASS2 (rvc_nan, rvc_inf):
return nan_result;
case CLASS2 (rvc_normal, rvc_normal):
break;
default:
gcc_unreachable ();
}
if (a->sign != b->sign)
return -a->sign - -b->sign;
if (a->decimal || b->decimal)
return decimal_do_compare (a, b, nan_result);
if (REAL_EXP (a) > REAL_EXP (b))
ret = 1;
else if (REAL_EXP (a) < REAL_EXP (b))
ret = -1;
else
ret = cmp_significands (a, b);
return (a->sign ? -ret : ret);
}
/* Return A truncated to an integral value toward zero. */
static void
do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
{
*r = *a;
switch (r->cl)
{
case rvc_zero:
case rvc_inf:
case rvc_nan:
break;
case rvc_normal:
if (r->decimal)
{
decimal_do_fix_trunc (r, a);
return;
}
if (REAL_EXP (r) <= 0)
get_zero (r, r->sign);
else if (REAL_EXP (r) < SIGNIFICAND_BITS)
clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
break;
default:
gcc_unreachable ();
}
}
/* Perform the binary or unary operation described by CODE.
For a unary operation, leave OP1 NULL. This function returns
true if the result may be inexact due to loss of precision. */
bool
real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
const REAL_VALUE_TYPE *op1)
{
enum tree_code code = (enum tree_code) icode;
if (op0->decimal || (op1 && op1->decimal))
return decimal_real_arithmetic (r, code, op0, op1);
switch (code)
{
case PLUS_EXPR:
/* Clear any padding areas in *r if it isn't equal to one of the
operands so that we can later do bitwise comparisons later on. */
if (r != op0 && r != op1)
memset (r, '\0', sizeof (*r));
return do_add (r, op0, op1, 0);
case MINUS_EXPR:
if (r != op0 && r != op1)
memset (r, '\0', sizeof (*r));
return do_add (r, op0, op1, 1);
case MULT_EXPR:
if (r != op0 && r != op1)
memset (r, '\0', sizeof (*r));
return do_multiply (r, op0, op1);
case RDIV_EXPR:
if (r != op0 && r != op1)
memset (r, '\0', sizeof (*r));
return do_divide (r, op0, op1);
case MIN_EXPR:
if (op1->cl == rvc_nan)
*r = *op1;
else if (do_compare (op0, op1, -1) < 0)
*r = *op0;
else
*r = *op1;
break;
case MAX_EXPR:
if (op1->cl == rvc_nan)
*r = *op1;
else if (do_compare (op0, op1, 1) < 0)
*r = *op1;
else
*r = *op0;
break;
case NEGATE_EXPR:
*r = *op0;
r->sign ^= 1;
break;
case ABS_EXPR:
*r = *op0;
r->sign = 0;
break;
case FIX_TRUNC_EXPR:
do_fix_trunc (r, op0);
break;
default:
gcc_unreachable ();
}
return false;
}
REAL_VALUE_TYPE
real_value_negate (const REAL_VALUE_TYPE *op0)
{
REAL_VALUE_TYPE r;
real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
return r;
}
REAL_VALUE_TYPE
real_value_abs (const REAL_VALUE_TYPE *op0)
{
REAL_VALUE_TYPE r;
real_arithmetic (&r, ABS_EXPR, op0, NULL);
return r;
}
bool
real_compare (int icode, const REAL_VALUE_TYPE *op0,
const REAL_VALUE_TYPE *op1)
{
enum tree_code code = (enum tree_code) icode;
switch (code)
{
case LT_EXPR:
return do_compare (op0, op1, 1) < 0;
case LE_EXPR:
return do_compare (op0, op1, 1) <= 0;
case GT_EXPR:
return do_compare (op0, op1, -1) > 0;
case GE_EXPR:
return do_compare (op0, op1, -1) >= 0;
case EQ_EXPR:
return do_compare (op0, op1, -1) == 0;
case NE_EXPR:
return do_compare (op0, op1, -1) != 0;
case UNORDERED_EXPR:
return op0->cl == rvc_nan || op1->cl == rvc_nan;
case ORDERED_EXPR:
return op0->cl != rvc_nan && op1->cl != rvc_nan;
case UNLT_EXPR:
return do_compare (op0, op1, -1) < 0;
case UNLE_EXPR:
return do_compare (op0, op1, -1) <= 0;
case UNGT_EXPR:
return do_compare (op0, op1, 1) > 0;
case UNGE_EXPR:
return do_compare (op0, op1, 1) >= 0;
case UNEQ_EXPR:
return do_compare (op0, op1, 0) == 0;
case LTGT_EXPR:
return do_compare (op0, op1, 0) != 0;
default:
gcc_unreachable ();
}
}
/* Return floor log2(R). */
int
real_exponent (const REAL_VALUE_TYPE *r)
{
switch (r->cl)
{
case rvc_zero:
return 0;
case rvc_inf:
case rvc_nan:
return (unsigned int)-1 >> 1;
case rvc_normal:
return REAL_EXP (r);
default:
gcc_unreachable ();
}
}
/* R = OP0 * 2**EXP. */
void
real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
{
*r = *op0;
switch (r->cl)
{
case rvc_zero:
case rvc_inf:
case rvc_nan:
break;
case rvc_normal:
exp += REAL_EXP (op0);
if (exp > MAX_EXP)
get_inf (r, r->sign);
else if (exp < -MAX_EXP)
get_zero (r, r->sign);
else
SET_REAL_EXP (r, exp);
break;
default:
gcc_unreachable ();
}
}
/* Determine whether a floating-point value X is infinite. */
bool
real_isinf (const REAL_VALUE_TYPE *r)
{
return (r->cl == rvc_inf);
}
/* Determine whether a floating-point value X is a NaN. */
bool
real_isnan (const REAL_VALUE_TYPE *r)
{
return (r->cl == rvc_nan);
}
/* Determine whether a floating-point value X is finite. */
bool
real_isfinite (const REAL_VALUE_TYPE *r)
{
return (r->cl != rvc_nan) && (r->cl != rvc_inf);
}
/* Determine whether a floating-point value X is negative. */
bool
real_isneg (const REAL_VALUE_TYPE *r)
{
return r->sign;
}
/* Determine whether a floating-point value X is minus zero. */
bool
real_isnegzero (const REAL_VALUE_TYPE *r)
{
return r->sign && r->cl == rvc_zero;
}
/* Compare two floating-point objects for bitwise identity. */
bool
real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
{
int i;
if (a->cl != b->cl)
return false;
if (a->sign != b->sign)
return false;
switch (a->cl)
{
case rvc_zero:
case rvc_inf:
return true;
case rvc_normal:
if (a->decimal != b->decimal)
return false;
if (REAL_EXP (a) != REAL_EXP (b))
return false;
break;
case rvc_nan:
if (a->signalling != b->signalling)
return false;
/* The significand is ignored for canonical NaNs. */
if (a->canonical || b->canonical)
return a->canonical == b->canonical;
break;
default:
gcc_unreachable ();
}
for (i = 0; i < SIGSZ; ++i)
if (a->sig[i] != b->sig[i])
return false;
return true;
}
/* Try to change R into its exact multiplicative inverse in machine
mode MODE. Return true if successful. */
bool
exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
{
const REAL_VALUE_TYPE *one = real_digit (1);
REAL_VALUE_TYPE u;
int i;
if (r->cl != rvc_normal)
return false;
/* Check for a power of two: all significand bits zero except the MSB. */
for (i = 0; i < SIGSZ-1; ++i)
if (r->sig[i] != 0)
return false;
|