xref: /dflybsd-src/contrib/mpfr/src/hypot.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_hypot -- Euclidean distance
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino 
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino 
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino 
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino 
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino 
234a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
244a238c70SJohn Marino #include "mpfr-impl.h"
254a238c70SJohn Marino 
264a238c70SJohn Marino /* The computation of hypot of x and y is done by  *
274a238c70SJohn Marino  *    hypot(x,y)= sqrt(x^2+y^2) = z                */
284a238c70SJohn Marino 
294a238c70SJohn Marino int
mpfr_hypot(mpfr_ptr z,mpfr_srcptr x,mpfr_srcptr y,mpfr_rnd_t rnd_mode)304a238c70SJohn Marino mpfr_hypot (mpfr_ptr z, mpfr_srcptr x, mpfr_srcptr y, mpfr_rnd_t rnd_mode)
314a238c70SJohn Marino {
324a238c70SJohn Marino   int inexact, exact;
334a238c70SJohn Marino   mpfr_t t, te, ti; /* auxiliary variables */
344a238c70SJohn Marino   mpfr_prec_t N, Nz; /* size variables */
354a238c70SJohn Marino   mpfr_prec_t Nt;   /* precision of the intermediary variable */
364a238c70SJohn Marino   mpfr_prec_t threshold;
374a238c70SJohn Marino   mpfr_exp_t Ex, sh;
384a238c70SJohn Marino   mpfr_uexp_t diff_exp;
394a238c70SJohn Marino 
404a238c70SJohn Marino   MPFR_SAVE_EXPO_DECL (expo);
414a238c70SJohn Marino   MPFR_ZIV_DECL (loop);
424a238c70SJohn Marino   MPFR_BLOCK_DECL (flags);
434a238c70SJohn Marino 
444a238c70SJohn Marino   MPFR_LOG_FUNC
454a238c70SJohn Marino     (("x[%Pu]=%.*Rg y[%Pu]=%.*Rg rnd=%d",
464a238c70SJohn Marino       mpfr_get_prec (x), mpfr_log_prec, x,
474a238c70SJohn Marino       mpfr_get_prec (y), mpfr_log_prec, y, rnd_mode),
484a238c70SJohn Marino      ("z[%Pu]=%.*Rg inexact=%d",
494a238c70SJohn Marino       mpfr_get_prec (z), mpfr_log_prec, z, inexact));
504a238c70SJohn Marino 
514a238c70SJohn Marino   /* particular cases */
524a238c70SJohn Marino   if (MPFR_ARE_SINGULAR (x, y))
534a238c70SJohn Marino     {
544a238c70SJohn Marino       if (MPFR_IS_INF (x) || MPFR_IS_INF (y))
554a238c70SJohn Marino         {
564a238c70SJohn Marino           /* Return +inf, even when the other number is NaN. */
574a238c70SJohn Marino           MPFR_SET_INF (z);
584a238c70SJohn Marino           MPFR_SET_POS (z);
594a238c70SJohn Marino           MPFR_RET (0);
604a238c70SJohn Marino         }
614a238c70SJohn Marino       else if (MPFR_IS_NAN (x) || MPFR_IS_NAN (y))
624a238c70SJohn Marino         {
634a238c70SJohn Marino           MPFR_SET_NAN (z);
644a238c70SJohn Marino           MPFR_RET_NAN;
654a238c70SJohn Marino         }
664a238c70SJohn Marino       else if (MPFR_IS_ZERO (x))
674a238c70SJohn Marino         return mpfr_abs (z, y, rnd_mode);
684a238c70SJohn Marino       else /* y is necessarily 0 */
694a238c70SJohn Marino         return mpfr_abs (z, x, rnd_mode);
704a238c70SJohn Marino     }
714a238c70SJohn Marino 
724a238c70SJohn Marino   if (mpfr_cmpabs (x, y) < 0)
734a238c70SJohn Marino     {
744a238c70SJohn Marino       mpfr_srcptr u;
754a238c70SJohn Marino       u = x;
764a238c70SJohn Marino       x = y;
774a238c70SJohn Marino       y = u;
784a238c70SJohn Marino     }
794a238c70SJohn Marino 
804a238c70SJohn Marino   /* now |x| >= |y| */
814a238c70SJohn Marino 
824a238c70SJohn Marino   Ex = MPFR_GET_EXP (x);
834a238c70SJohn Marino   diff_exp = (mpfr_uexp_t) Ex - MPFR_GET_EXP (y);
844a238c70SJohn Marino 
854a238c70SJohn Marino   N = MPFR_PREC (x);   /* Precision of input variable */
864a238c70SJohn Marino   Nz = MPFR_PREC (z);   /* Precision of output variable */
874a238c70SJohn Marino   threshold = (MAX (N, Nz) + (rnd_mode == MPFR_RNDN ? 1 : 0)) << 1;
884a238c70SJohn Marino   if (rnd_mode == MPFR_RNDA)
894a238c70SJohn Marino     rnd_mode = MPFR_RNDU; /* since the result is positive, RNDA = RNDU */
904a238c70SJohn Marino 
914a238c70SJohn Marino   /* Is |x| a suitable approximation to the precision Nz ?
924a238c70SJohn Marino      (see algorithms.tex for explanations) */
934a238c70SJohn Marino   if (diff_exp > threshold)
944a238c70SJohn Marino     /* result is |x| or |x|+ulp(|x|,Nz) */
954a238c70SJohn Marino     {
964a238c70SJohn Marino       if (MPFR_UNLIKELY (rnd_mode == MPFR_RNDU))
974a238c70SJohn Marino         {
984a238c70SJohn Marino           /* If z > abs(x), then it was already rounded up; otherwise
994a238c70SJohn Marino              z = abs(x), and we need to add one ulp due to y. */
1004a238c70SJohn Marino           if (mpfr_abs (z, x, rnd_mode) == 0)
1014a238c70SJohn Marino             mpfr_nexttoinf (z);
1024a238c70SJohn Marino           MPFR_RET (1);
1034a238c70SJohn Marino         }
1044a238c70SJohn Marino       else /* MPFR_RNDZ, MPFR_RNDD, MPFR_RNDN */
1054a238c70SJohn Marino         {
1064a238c70SJohn Marino           if (MPFR_LIKELY (Nz >= N))
1074a238c70SJohn Marino             {
1084a238c70SJohn Marino               mpfr_abs (z, x, rnd_mode);  /* exact */
1094a238c70SJohn Marino               MPFR_RET (-1);
1104a238c70SJohn Marino             }
1114a238c70SJohn Marino           else
1124a238c70SJohn Marino             {
1134a238c70SJohn Marino               MPFR_SET_EXP (z, Ex);
1144a238c70SJohn Marino               MPFR_SET_SIGN (z, 1);
1154a238c70SJohn Marino               MPFR_RNDRAW_GEN (inexact, z, MPFR_MANT (x), N, rnd_mode, 1,
1164a238c70SJohn Marino                                goto addoneulp,
1174a238c70SJohn Marino                                if (MPFR_UNLIKELY (++ MPFR_EXP (z) >
1184a238c70SJohn Marino                                                   __gmpfr_emax))
1194a238c70SJohn Marino                                  return mpfr_overflow (z, rnd_mode, 1);
1204a238c70SJohn Marino                                );
1214a238c70SJohn Marino 
1224a238c70SJohn Marino               if (MPFR_UNLIKELY (inexact == 0))
1234a238c70SJohn Marino                 inexact = -1;
1244a238c70SJohn Marino               MPFR_RET (inexact);
1254a238c70SJohn Marino             }
1264a238c70SJohn Marino         }
1274a238c70SJohn Marino     }
1284a238c70SJohn Marino 
1294a238c70SJohn Marino   /* General case */
1304a238c70SJohn Marino 
1314a238c70SJohn Marino   N = MAX (MPFR_PREC (x), MPFR_PREC (y));
1324a238c70SJohn Marino 
1334a238c70SJohn Marino   /* working precision */
1344a238c70SJohn Marino   Nt = Nz + MPFR_INT_CEIL_LOG2 (Nz) + 4;
1354a238c70SJohn Marino 
1364a238c70SJohn Marino   mpfr_init2 (t, Nt);
1374a238c70SJohn Marino   mpfr_init2 (te, Nt);
1384a238c70SJohn Marino   mpfr_init2 (ti, Nt);
1394a238c70SJohn Marino 
1404a238c70SJohn Marino   MPFR_SAVE_EXPO_MARK (expo);
1414a238c70SJohn Marino 
1424a238c70SJohn Marino   /* Scale x and y to avoid overflow/underflow in x^2 and overflow in y^2
1434a238c70SJohn Marino      (as |x| >= |y|). The scaling of y can underflow only when the target
1444a238c70SJohn Marino      precision is huge, otherwise the case would already have been handled
1454a238c70SJohn Marino      by the diff_exp > threshold code. */
1464a238c70SJohn Marino   sh = mpfr_get_emax () / 2 - Ex - 1;
1474a238c70SJohn Marino 
1484a238c70SJohn Marino   MPFR_ZIV_INIT (loop, Nt);
1494a238c70SJohn Marino   for (;;)
1504a238c70SJohn Marino     {
1514a238c70SJohn Marino       mpfr_prec_t err;
1524a238c70SJohn Marino 
1534a238c70SJohn Marino       exact = mpfr_mul_2si (te, x, sh, MPFR_RNDZ);
1544a238c70SJohn Marino       exact |= mpfr_mul_2si (ti, y, sh, MPFR_RNDZ);
1554a238c70SJohn Marino       exact |= mpfr_sqr (te, te, MPFR_RNDZ);
1564a238c70SJohn Marino       /* Use fma in order to avoid underflow when diff_exp<=MPFR_EMAX_MAX-2 */
1574a238c70SJohn Marino       exact |= mpfr_fma (t, ti, ti, te, MPFR_RNDZ);
1584a238c70SJohn Marino       exact |= mpfr_sqrt (t, t, MPFR_RNDZ);
1594a238c70SJohn Marino 
1604a238c70SJohn Marino       err = Nt < N ? 4 : 2;
1614a238c70SJohn Marino       if (MPFR_LIKELY (exact == 0
1624a238c70SJohn Marino                        || MPFR_CAN_ROUND (t, Nt-err, Nz, rnd_mode)))
1634a238c70SJohn Marino         break;
1644a238c70SJohn Marino 
1654a238c70SJohn Marino       MPFR_ZIV_NEXT (loop, Nt);
1664a238c70SJohn Marino       mpfr_set_prec (t, Nt);
1674a238c70SJohn Marino       mpfr_set_prec (te, Nt);
1684a238c70SJohn Marino       mpfr_set_prec (ti, Nt);
1694a238c70SJohn Marino     }
1704a238c70SJohn Marino   MPFR_ZIV_FREE (loop);
1714a238c70SJohn Marino 
1724a238c70SJohn Marino   MPFR_BLOCK (flags, inexact = mpfr_div_2si (z, t, sh, rnd_mode));
1734a238c70SJohn Marino   MPFR_ASSERTD (exact == 0 || inexact != 0);
1744a238c70SJohn Marino 
1754a238c70SJohn Marino   mpfr_clear (t);
1764a238c70SJohn Marino   mpfr_clear (ti);
1774a238c70SJohn Marino   mpfr_clear (te);
1784a238c70SJohn Marino 
1794a238c70SJohn Marino   /*
1804a238c70SJohn Marino     exact  inexact
1814a238c70SJohn Marino     0         0         result is exact, ternary flag is 0
1824a238c70SJohn Marino     0       non zero    t is exact, ternary flag given by inexact
1834a238c70SJohn Marino     1         0         impossible (see above)
1844a238c70SJohn Marino     1       non zero    ternary flag given by inexact
1854a238c70SJohn Marino   */
1864a238c70SJohn Marino 
1874a238c70SJohn Marino   MPFR_SAVE_EXPO_FREE (expo);
1884a238c70SJohn Marino 
1894a238c70SJohn Marino   if (MPFR_OVERFLOW (flags))
1904a238c70SJohn Marino     mpfr_set_overflow ();
1914a238c70SJohn Marino   /* hypot(x,y) >= |x|, thus underflow is not possible. */
1924a238c70SJohn Marino 
1934a238c70SJohn Marino   return mpfr_check_range (z, inexact, rnd_mode);
1944a238c70SJohn Marino }
195