1 /* mpfr_sqr -- Floating square
3 Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
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 3 of the License, or (at your
11 option) any later version.
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.
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.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "mpfr-impl.h"
26 mpfr_sqr (mpfr_ptr a
, mpfr_srcptr b
, mpfr_rnd_t rnd_mode
)
34 MPFR_TMP_DECL(marker
);
37 (("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec (b
), mpfr_log_prec
, b
, rnd_mode
),
38 ("y[%Pu]=%.*Rg inexact=%d",
39 mpfr_get_prec (a
), mpfr_log_prec
, a
, inexact
));
41 /* deal with special cases */
42 if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(b
)))
53 ( MPFR_ASSERTD(MPFR_IS_ZERO(b
)), MPFR_SET_ZERO(a
) );
56 ax
= 2 * MPFR_GET_EXP (b
);
59 MPFR_ASSERTN (2 * (mpfr_uprec_t
) bq
<= MPFR_PREC_MAX
);
61 bn
= MPFR_LIMB_SIZE (b
); /* number of limbs of b */
62 tn
= MPFR_PREC2LIMBS (2 * bq
); /* number of limbs of square,
65 if (MPFR_UNLIKELY(bn
> MPFR_SQR_THRESHOLD
))
66 return mpfr_mul (a
, b
, b
, rnd_mode
);
68 MPFR_TMP_MARK(marker
);
69 tmp
= MPFR_TMP_LIMBS_ALLOC (2 * bn
);
71 /* Multiplies the mantissa in temporary allocated space */
72 mpn_sqr_n (tmp
, MPFR_MANT(b
), bn
);
75 /* now tmp[0]..tmp[2*bn-1] contains the product of both mantissa,
76 with tmp[2*bn-1]>=2^(GMP_NUMB_BITS-2) */
77 b1
>>= GMP_NUMB_BITS
- 1; /* msb from the product */
79 /* if the mantissas of b and c are uniformly distributed in ]1/2, 1],
80 then their product is in ]1/4, 1/2] with probability 2*ln(2)-1 ~ 0.386
81 and in [1/2, 1] with probability 2-2*ln(2) ~ 0.614 */
82 tmp
+= 2 * bn
- tn
; /* +0 or +1 */
83 if (MPFR_UNLIKELY(b1
== 0))
84 mpn_lshift (tmp
, tmp
, tn
, 1); /* tn <= k, so no stack corruption */
86 cc
= mpfr_round_raw (MPFR_MANT (a
), tmp
, 2 * bq
, 0,
87 MPFR_PREC (a
), rnd_mode
, &inexact
);
88 /* cc = 1 ==> result is a power of two */
89 if (MPFR_UNLIKELY(cc
))
90 MPFR_MANT(a
)[MPFR_LIMB_SIZE(a
)-1] = MPFR_LIMB_HIGHBIT
;
92 MPFR_TMP_FREE(marker
);
94 mpfr_exp_t ax2
= ax
+ (mpfr_exp_t
) (b1
- 1 + cc
);
95 if (MPFR_UNLIKELY( ax2
> __gmpfr_emax
))
96 return mpfr_overflow (a
, rnd_mode
, MPFR_SIGN_POS
);
97 if (MPFR_UNLIKELY( ax2
< __gmpfr_emin
))
99 /* In the rounding to the nearest mode, if the exponent of the exact
100 result (i.e. before rounding, i.e. without taking cc into account)
101 is < __gmpfr_emin - 1 or the exact result is a power of 2 (i.e. if
102 both arguments are powers of 2), then round to zero. */
103 if (rnd_mode
== MPFR_RNDN
&&
104 (ax
+ (mpfr_exp_t
) b1
< __gmpfr_emin
|| mpfr_powerof2_raw (b
)))
105 rnd_mode
= MPFR_RNDZ
;
106 return mpfr_underflow (a
, rnd_mode
, MPFR_SIGN_POS
);
108 MPFR_SET_EXP (a
, ax2
);