14a238c70SJohn Marino /* mpfr_sqr -- Floating square
24a238c70SJohn Marino
3*ab6d115fSJohn Marino Copyright 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 #include "mpfr-impl.h"
244a238c70SJohn Marino
254a238c70SJohn Marino int
mpfr_sqr(mpfr_ptr a,mpfr_srcptr b,mpfr_rnd_t rnd_mode)264a238c70SJohn Marino mpfr_sqr (mpfr_ptr a, mpfr_srcptr b, mpfr_rnd_t rnd_mode)
274a238c70SJohn Marino {
284a238c70SJohn Marino int cc, inexact;
294a238c70SJohn Marino mpfr_exp_t ax;
304a238c70SJohn Marino mp_limb_t *tmp;
314a238c70SJohn Marino mp_limb_t b1;
324a238c70SJohn Marino mpfr_prec_t bq;
334a238c70SJohn Marino mp_size_t bn, tn;
344a238c70SJohn Marino MPFR_TMP_DECL(marker);
354a238c70SJohn Marino
364a238c70SJohn Marino MPFR_LOG_FUNC
374a238c70SJohn Marino (("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec (b), mpfr_log_prec, b, rnd_mode),
384a238c70SJohn Marino ("y[%Pu]=%.*Rg inexact=%d",
394a238c70SJohn Marino mpfr_get_prec (a), mpfr_log_prec, a, inexact));
404a238c70SJohn Marino
414a238c70SJohn Marino /* deal with special cases */
424a238c70SJohn Marino if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(b)))
434a238c70SJohn Marino {
444a238c70SJohn Marino if (MPFR_IS_NAN(b))
454a238c70SJohn Marino {
464a238c70SJohn Marino MPFR_SET_NAN(a);
474a238c70SJohn Marino MPFR_RET_NAN;
484a238c70SJohn Marino }
494a238c70SJohn Marino MPFR_SET_POS (a);
504a238c70SJohn Marino if (MPFR_IS_INF(b))
514a238c70SJohn Marino MPFR_SET_INF(a);
524a238c70SJohn Marino else
534a238c70SJohn Marino ( MPFR_ASSERTD(MPFR_IS_ZERO(b)), MPFR_SET_ZERO(a) );
544a238c70SJohn Marino MPFR_RET(0);
554a238c70SJohn Marino }
564a238c70SJohn Marino ax = 2 * MPFR_GET_EXP (b);
574a238c70SJohn Marino bq = MPFR_PREC(b);
584a238c70SJohn Marino
59*ab6d115fSJohn Marino MPFR_ASSERTN (2 * (mpfr_uprec_t) bq <= MPFR_PREC_MAX);
604a238c70SJohn Marino
614a238c70SJohn Marino bn = MPFR_LIMB_SIZE (b); /* number of limbs of b */
62*ab6d115fSJohn Marino tn = MPFR_PREC2LIMBS (2 * bq); /* number of limbs of square,
634a238c70SJohn Marino 2*bn or 2*bn-1 */
644a238c70SJohn Marino
654a238c70SJohn Marino if (MPFR_UNLIKELY(bn > MPFR_SQR_THRESHOLD))
664a238c70SJohn Marino return mpfr_mul (a, b, b, rnd_mode);
674a238c70SJohn Marino
684a238c70SJohn Marino MPFR_TMP_MARK(marker);
694a238c70SJohn Marino tmp = MPFR_TMP_LIMBS_ALLOC (2 * bn);
704a238c70SJohn Marino
714a238c70SJohn Marino /* Multiplies the mantissa in temporary allocated space */
724a238c70SJohn Marino mpn_sqr_n (tmp, MPFR_MANT(b), bn);
734a238c70SJohn Marino b1 = tmp[2 * bn - 1];
744a238c70SJohn Marino
754a238c70SJohn Marino /* now tmp[0]..tmp[2*bn-1] contains the product of both mantissa,
764a238c70SJohn Marino with tmp[2*bn-1]>=2^(GMP_NUMB_BITS-2) */
774a238c70SJohn Marino b1 >>= GMP_NUMB_BITS - 1; /* msb from the product */
784a238c70SJohn Marino
794a238c70SJohn Marino /* if the mantissas of b and c are uniformly distributed in ]1/2, 1],
804a238c70SJohn Marino then their product is in ]1/4, 1/2] with probability 2*ln(2)-1 ~ 0.386
814a238c70SJohn Marino and in [1/2, 1] with probability 2-2*ln(2) ~ 0.614 */
824a238c70SJohn Marino tmp += 2 * bn - tn; /* +0 or +1 */
834a238c70SJohn Marino if (MPFR_UNLIKELY(b1 == 0))
844a238c70SJohn Marino mpn_lshift (tmp, tmp, tn, 1); /* tn <= k, so no stack corruption */
854a238c70SJohn Marino
864a238c70SJohn Marino cc = mpfr_round_raw (MPFR_MANT (a), tmp, 2 * bq, 0,
874a238c70SJohn Marino MPFR_PREC (a), rnd_mode, &inexact);
884a238c70SJohn Marino /* cc = 1 ==> result is a power of two */
894a238c70SJohn Marino if (MPFR_UNLIKELY(cc))
904a238c70SJohn Marino MPFR_MANT(a)[MPFR_LIMB_SIZE(a)-1] = MPFR_LIMB_HIGHBIT;
914a238c70SJohn Marino
924a238c70SJohn Marino MPFR_TMP_FREE(marker);
934a238c70SJohn Marino {
944a238c70SJohn Marino mpfr_exp_t ax2 = ax + (mpfr_exp_t) (b1 - 1 + cc);
954a238c70SJohn Marino if (MPFR_UNLIKELY( ax2 > __gmpfr_emax))
964a238c70SJohn Marino return mpfr_overflow (a, rnd_mode, MPFR_SIGN_POS);
974a238c70SJohn Marino if (MPFR_UNLIKELY( ax2 < __gmpfr_emin))
984a238c70SJohn Marino {
994a238c70SJohn Marino /* In the rounding to the nearest mode, if the exponent of the exact
1004a238c70SJohn Marino result (i.e. before rounding, i.e. without taking cc into account)
1014a238c70SJohn Marino is < __gmpfr_emin - 1 or the exact result is a power of 2 (i.e. if
1024a238c70SJohn Marino both arguments are powers of 2), then round to zero. */
1034a238c70SJohn Marino if (rnd_mode == MPFR_RNDN &&
1044a238c70SJohn Marino (ax + (mpfr_exp_t) b1 < __gmpfr_emin || mpfr_powerof2_raw (b)))
1054a238c70SJohn Marino rnd_mode = MPFR_RNDZ;
1064a238c70SJohn Marino return mpfr_underflow (a, rnd_mode, MPFR_SIGN_POS);
1074a238c70SJohn Marino }
1084a238c70SJohn Marino MPFR_SET_EXP (a, ax2);
1094a238c70SJohn Marino MPFR_SET_POS (a);
1104a238c70SJohn Marino }
1114a238c70SJohn Marino MPFR_RET (inexact);
1124a238c70SJohn Marino }
113