Import Upstream version 0.8.2
[platform/upstream/mpc.git] / src / norm.c
1 /* mpc_norm -- Square of the norm of a complex number.
2
3 Copyright (C) 2002, 2005, 2008, 2009 Andreas Enge, Paul Zimmermann, Philippe Th\'eveny
4
5 This file is part of the MPC Library.
6
7 The MPC Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
11
12 The MPC Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the MPC Library; see the file COPYING.LIB.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
21
22 #include "mpc-impl.h"
23
24 /* the rounding mode is mpfr_rnd_t here since we return an mpfr number */
25 int
26 mpc_norm (mpfr_ptr a, mpc_srcptr b, mpfr_rnd_t rnd)
27 {
28   mpfr_t u, v;
29   mp_prec_t prec;
30   int inexact, overflow;
31
32   prec = MPFR_PREC(a);
33
34   /* handling of special values; consistent with abs in that
35      norm = abs^2; so norm (+-inf, nan) = norm (nan, +-inf) = +inf */
36   if (   (mpfr_nan_p (MPC_RE (b)) || mpfr_nan_p (MPC_IM (b)))
37       || (mpfr_inf_p (MPC_RE (b)) || mpfr_inf_p (MPC_IM (b))))
38       return mpc_abs (a, b, rnd);
39
40   mpfr_init (u);
41   mpfr_init (v);
42
43   if (!mpfr_zero_p(MPC_RE(b)) && !mpfr_zero_p(MPC_IM(b)) &&
44       2 * SAFE_ABS (mp_exp_t, MPFR_EXP (MPC_RE (b)) - MPFR_EXP (MPC_IM (b)))
45       > (mp_exp_t)prec)
46     /* If real and imaginary part have very different magnitudes, then the */
47     /* generic code increases the precision too much. Instead, compute the */
48     /* squarings _exactly_.                                                */
49   {
50      mpfr_set_prec (u, 2 * MPFR_PREC (MPC_RE (b)));
51      mpfr_set_prec (v, 2 * MPFR_PREC (MPC_IM (b)));
52      mpfr_sqr (u, MPC_RE (b), GMP_RNDN);
53      mpfr_sqr (v, MPC_IM (b), GMP_RNDN);
54      inexact = mpfr_add (a, u, v, rnd);
55   }
56   else
57   {
58     do
59       {
60         prec += mpc_ceil_log2 (prec) + 3;
61
62         mpfr_set_prec (u, prec);
63         mpfr_set_prec (v, prec);
64
65         inexact = mpfr_sqr (u, MPC_RE(b), GMP_RNDN);  /* err<=1/2ulp */
66         inexact |= mpfr_sqr (v, MPC_IM(b), GMP_RNDN); /* err<=1/2ulp*/
67         inexact |= mpfr_add (u, u, v, GMP_RNDN);      /* err <= 3/2 ulps */
68
69         overflow = mpfr_inf_p (u);
70       }
71     while (!overflow && inexact &&
72            mpfr_can_round (u, prec - 2, GMP_RNDN, rnd, MPFR_PREC(a)) == 0);
73
74     inexact |= mpfr_set (a, u, rnd);
75   }
76   mpfr_clear (u);
77   mpfr_clear (v);
78
79   return inexact;
80 }