Merge branch 'vendor/BINUTILS220' into bu220
[dragonfly.git] / contrib / mpfr / hypot.c
1 /* mpfr_hypot -- Euclidean distance
2
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
12
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LIB.  If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
25
26 /* The computation of hypot of x and y is done by  *
27  *    hypot(x,y)= sqrt(x^2+y^2) = z                */
28
29 int
30 mpfr_hypot (mpfr_ptr z, mpfr_srcptr x, mpfr_srcptr y, mp_rnd_t rnd_mode)
31 {
32   int inexact, exact;
33   mpfr_t t, te, ti; /* auxiliary variables */
34   mp_prec_t N, Nz; /* size variables */
35   mp_prec_t Nt;   /* precision of the intermediary variable */
36   mp_prec_t threshold;
37   mp_exp_t Ex, sh;
38   mp_exp_unsigned_t diff_exp;
39
40   MPFR_SAVE_EXPO_DECL (expo);
41   MPFR_ZIV_DECL (loop);
42   MPFR_BLOCK_DECL (flags);
43
44   /* particular cases */
45   if (MPFR_ARE_SINGULAR (x, y))
46     {
47       if (MPFR_IS_INF (x) || MPFR_IS_INF (y))
48         {
49           /* Return +inf, even when the other number is NaN. */
50           MPFR_SET_INF (z);
51           MPFR_SET_POS (z);
52           MPFR_RET (0);
53         }
54       else if (MPFR_IS_NAN (x) || MPFR_IS_NAN (y))
55         {
56           MPFR_SET_NAN (z);
57           MPFR_RET_NAN;
58         }
59       else if (MPFR_IS_ZERO (x))
60         return mpfr_abs (z, y, rnd_mode);
61       else /* y is necessarily 0 */
62         return mpfr_abs (z, x, rnd_mode);
63     }
64   MPFR_CLEAR_FLAGS(z);
65
66   if (mpfr_cmpabs (x, y) < 0)
67     {
68       mpfr_srcptr u;
69       u = x;
70       x = y;
71       y = u;
72     }
73
74   /* now |x| >= |y| */
75
76   Ex = MPFR_GET_EXP (x);
77   diff_exp = (mp_exp_unsigned_t) Ex - MPFR_GET_EXP (y);
78
79   N = MPFR_PREC (x);   /* Precision of input variable */
80   Nz = MPFR_PREC (z);   /* Precision of output variable */
81   threshold = (MAX (N, Nz) + (rnd_mode == GMP_RNDN ? 1 : 0)) << 1;
82
83   /* Is |x| a suitable approximation to the precision Nz ?
84      (see algorithms.tex for explanations) */
85   if (diff_exp > threshold)
86     /* result is |x| or |x|+ulp(|x|,Nz) */
87     {
88       if (MPFR_UNLIKELY (rnd_mode == GMP_RNDU))
89         {
90           /* If z > abs(x), then it was already rounded up; otherwise
91              z = abs(x), and we need to add one ulp due to y. */
92           if (mpfr_abs (z, x, rnd_mode) == 0)
93             mpfr_nexttoinf (z);
94           MPFR_RET (1);
95         }
96       else /* GMP_RNDZ, GMP_RNDD, GMP_RNDN */
97         {
98           if (MPFR_LIKELY (Nz >= N))
99             {
100               mpfr_abs (z, x, rnd_mode);  /* exact */
101               MPFR_RET (-1);
102             }
103           else
104             {
105               MPFR_SET_EXP (z, Ex);
106               MPFR_SET_SIGN (z, 1);
107               MPFR_RNDRAW_GEN (inexact, z, MPFR_MANT (x), N, rnd_mode, 1,
108                                goto addoneulp,
109                                if (MPFR_UNLIKELY (++ MPFR_EXP (z) >
110                                                   __gmpfr_emax))
111                                  return mpfr_overflow (z, rnd_mode, 1);
112                                );
113
114               if (MPFR_UNLIKELY (inexact == 0))
115                 inexact = -1;
116               MPFR_RET (inexact);
117             }
118         }
119     }
120
121   /* General case */
122
123   N = MAX (MPFR_PREC (x), MPFR_PREC (y));
124
125   /* working precision */
126   Nt = Nz + MPFR_INT_CEIL_LOG2 (Nz) + 4;
127
128   mpfr_init2 (t, Nt);
129   mpfr_init2 (te, Nt);
130   mpfr_init2 (ti, Nt);
131
132   MPFR_SAVE_EXPO_MARK (expo);
133
134   /* Scale x and y to avoid overflow/underflow in x^2 and overflow in y^2
135      (as |x| >= |y|). The scaling of y can underflow only when the target
136      precision is huge, otherwise the case would already have been handled
137      by the diff_exp > threshold code. */
138   sh = mpfr_get_emax () / 2 - Ex - 1;
139
140   MPFR_ZIV_INIT (loop, Nt);
141   for (;;)
142     {
143       mp_prec_t err;
144
145       exact = mpfr_mul_2si (te, x, sh, GMP_RNDZ);
146       exact |= mpfr_mul_2si (ti, y, sh, GMP_RNDZ);
147       exact |= mpfr_sqr (te, te, GMP_RNDZ);
148       /* Use fma in order to avoid underflow when diff_exp<=MPFR_EMAX_MAX-2 */
149       exact |= mpfr_fma (t, ti, ti, te, GMP_RNDZ);
150       exact |= mpfr_sqrt (t, t, GMP_RNDZ);
151
152       err = Nt < N ? 4 : 2;
153       if (MPFR_LIKELY (exact == 0
154                        || MPFR_CAN_ROUND (t, Nt-err, Nz, rnd_mode)))
155         break;
156
157       MPFR_ZIV_NEXT (loop, Nt);
158       mpfr_set_prec (t, Nt);
159       mpfr_set_prec (te, Nt);
160       mpfr_set_prec (ti, Nt);
161     }
162   MPFR_ZIV_FREE (loop);
163
164   MPFR_BLOCK (flags, inexact = mpfr_div_2si (z, t, sh, rnd_mode));
165   MPFR_ASSERTD (exact == 0 || inexact != 0);
166
167   mpfr_clear (t);
168   mpfr_clear (ti);
169   mpfr_clear (te);
170
171   /*
172     exact  inexact
173     0         0         result is exact, ternary flag is 0
174     0       non zero    t is exact, ternary flag given by inexact
175     1         0         impossible (see above)
176     1       non zero    ternary flag given by inexact
177   */
178
179   MPFR_SAVE_EXPO_FREE (expo);
180
181   if (MPFR_OVERFLOW (flags))
182     mpfr_set_overflow ();
183   /* hypot(x,y) >= |x|, thus underflow is not possible. */
184
185   return mpfr_check_range (z, inexact, rnd_mode);
186 }