+/* Calculate X raised to the integer exponent N in mode MODE and store
+ the result in R. Return true if the result may be inexact due to
+ loss of precision. The algorithm is the classic "left-to-right binary
+ method" described in section 4.6.3 of Donald Knuth's "Seminumerical
+ Algorithms", "The Art of Computer Programming", Volume 2. */
+
+bool
+real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
+ const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
+{
+ unsigned HOST_WIDE_INT bit;
+ REAL_VALUE_TYPE t;
+ bool inexact = false;
+ bool init = false;
+ bool neg;
+ int i;
+
+ if (n == 0)
+ {
+ *r = dconst1;
+ return false;
+ }
+ else if (n < 0)
+ {
+ /* Don't worry about overflow, from now on n is unsigned. */
+ neg = true;
+ n = -n;
+ }
+ else
+ neg = false;
+
+ t = *x;
+ bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
+ for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
+ {
+ if (init)
+ {
+ inexact |= do_multiply (&t, &t, &t);
+ if (n & bit)
+ inexact |= do_multiply (&t, &t, x);
+ }
+ else if (n & bit)
+ init = true;
+ bit >>= 1;
+ }
+
+ if (neg)
+ inexact |= do_divide (&t, &dconst1, &t);
+
+ real_convert (r, mode, &t);
+ return inexact;
+}
+
+/* Round X to the nearest integer not larger in absolute value, i.e.
+ towards zero, placing the result in R in mode MODE. */
+
+void
+real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
+ const REAL_VALUE_TYPE *x)
+{
+ do_fix_trunc (r, x);
+ if (mode != VOIDmode)
+ real_convert (r, mode, r);
+}
+
+/* Round X to the largest integer not greater in value, i.e. round
+ down, placing the result in R in mode MODE. */
+
+void
+real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
+ const REAL_VALUE_TYPE *x)
+{
+ REAL_VALUE_TYPE t;
+
+ do_fix_trunc (&t, x);
+ if (! real_identical (&t, x) && x->sign)
+ do_add (&t, &t, &dconstm1, 0);
+ if (mode != VOIDmode)
+ real_convert (r, mode, &t);
+ else
+ *r = t;
+}
+
+/* Round X to the smallest integer not less then argument, i.e. round
+ up, placing the result in R in mode MODE. */
+
+void
+real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
+ const REAL_VALUE_TYPE *x)
+{
+ REAL_VALUE_TYPE t;
+
+ do_fix_trunc (&t, x);
+ if (! real_identical (&t, x) && ! x->sign)
+ do_add (&t, &t, &dconst1, 0);
+ if (mode != VOIDmode)
+ real_convert (r, mode, &t);
+ else
+ *r = t;
+}
+
+/* Round X to the nearest integer, but round halfway cases away from
+ zero. */
+
+void
+real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
+ const REAL_VALUE_TYPE *x)
+{
+ do_add (r, x, &dconsthalf, x->sign);
+ do_fix_trunc (r, r);
+ if (mode != VOIDmode)
+ real_convert (r, mode, r);
+}
+
+/* Set the sign of R to the sign of X. */
+
+void
+real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
+{
+ r->sign = x->sign;
+}
+
+/* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
+ for initializing and clearing the MPFR parameter. */
+
+void
+mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
+{
+ /* We use a string as an intermediate type. */
+ char buf[128];
+ int ret;
+
+ /* Take care of Infinity and NaN. */
+ if (r->cl == rvc_inf)
+ {
+ mpfr_set_inf (m, r->sign == 1 ? -1 : 1);
+ return;
+ }
+
+ if (r->cl == rvc_nan)
+ {
+ mpfr_set_nan (m);
+ return;
+ }
+
+ real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
+ /* mpfr_set_str() parses hexadecimal floats from strings in the same
+ format that GCC will output them. Nothing extra is needed. */
+ ret = mpfr_set_str (m, buf, 16, rndmode);
+ gcc_assert (ret == 0);
+}
+
+/* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
+ mode RNDMODE. TYPE is only relevant if M is a NaN. */
+
+void
+real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
+{
+ /* We use a string as an intermediate type. */
+ char buf[128], *rstr;
+ mp_exp_t exp;
+
+ /* Take care of Infinity and NaN. */
+ if (mpfr_inf_p (m))
+ {
+ real_inf (r);
+ if (mpfr_sgn (m) < 0)
+ *r = REAL_VALUE_NEGATE (*r);
+ return;
+ }
+
+ if (mpfr_nan_p (m))
+ {
+ real_nan (r, "", 1, TYPE_MODE (type));
+ return;
+ }
+
+ rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
+
+ /* The additional 12 chars add space for the sprintf below. This
+ leaves 6 digits for the exponent which is supposedly enough. */
+ gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
+
+ /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
+ mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
+ for that. */
+ exp *= 4;
+
+ if (rstr[0] == '-')
+ sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
+ else
+ sprintf (buf, "0x.%sp%d", rstr, (int) exp);
+
+ mpfr_free_str (rstr);
+
+ real_from_string (r, buf);
+}
+
+/* Check whether the real constant value given is an integer. */
+
+bool
+real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
+{
+ REAL_VALUE_TYPE cint;
+
+ real_trunc (&cint, mode, c);
+ return real_identical (c, &cint);
+}
+
+/* Write into BUF the maximum representable finite floating-point
+ number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
+ float string. LEN is the size of BUF, and the buffer must be large
+ enough to contain the resulting string. */
+
+void
+get_max_float (const struct real_format *fmt, char *buf, size_t len)
+{
+ int i, n;
+ char *p;
+
+ strcpy (buf, "0x0.");
+ n = fmt->p;
+ for (i = 0, p = buf + 4; i + 3 < n; i += 4)
+ *p++ = 'f';
+ if (i < n)
+ *p++ = "08ce"[n - i];
+ sprintf (p, "p%d", fmt->emax);
+ if (fmt->pnan < fmt->p)
+ {
+ /* This is an IBM extended double format made up of two IEEE
+ doubles. The value of the long double is the sum of the
+ values of the two parts. The most significant part is
+ required to be the value of the long double rounded to the
+ nearest double. Rounding means we need a slightly smaller
+ value for LDBL_MAX. */
+ buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
+ }
+
+ gcc_assert (strlen (buf) < len);
+}