Blame mpf/sqrt_ui.c

Packit 5c3484
/* mpf_sqrt_ui -- Compute the square root of an unsigned integer.
Packit 5c3484
Packit 5c3484
Copyright 1993, 1994, 1996, 2000, 2001, 2004, 2005, 2015 Free Software
Packit 5c3484
Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include <stdio.h> /* for NULL */
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* As usual the aim is to produce PREC(r) limbs of result with the high limb
Packit 5c3484
   non-zero.  That high limb will end up floor(sqrt(u)), and limbs below are
Packit 5c3484
   produced by padding the input with zeros, two for each desired result
Packit 5c3484
   limb, being 2*(prec-1) for a total 2*prec-1 limbs passed to mpn_sqrtrem.
Packit 5c3484
   The way mpn_sqrtrem calculates floor(sqrt(x)) ensures the root is correct
Packit 5c3484
   to the intended accuracy, ie. truncated to prec limbs.
Packit 5c3484
Packit 5c3484
   With nails, u might be two limbs, in which case a total 2*prec limbs is
Packit 5c3484
   passed to mpn_sqrtrem (still giving a prec limb result).  If uhigh is
Packit 5c3484
   zero we adjust back to 2*prec-1, since mpn_sqrtrem requires the high
Packit 5c3484
   non-zero.  2*prec limbs are always allocated, even when uhigh is zero, so
Packit 5c3484
   the store of uhigh can be done without a conditional.
Packit 5c3484
Packit 5c3484
   u==0 is a special case so the rest of the code can assume the result is
Packit 5c3484
   non-zero (ie. will have a non-zero high limb on the result).
Packit 5c3484
Packit 5c3484
   Not done:
Packit 5c3484
Packit 5c3484
   No attempt is made to identify perfect squares.  It's considered this can
Packit 5c3484
   be left to an application if it might occur with any frequency.  As it
Packit 5c3484
   stands, mpn_sqrtrem does its normal amount of work on a perfect square
Packit 5c3484
   followed by zero limbs, though of course only an mpn_sqrtrem1 would be
Packit 5c3484
   actually needed.  We also end up leaving our mpf result with lots of low
Packit 5c3484
   trailing zeros, slowing down subsequent operations.
Packit 5c3484
Packit 5c3484
   We're not aware of any optimizations that can be made using the fact the
Packit 5c3484
   input has lots of trailing zeros (apart from the perfect square
Packit 5c3484
   case).  */
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* 1 if we (might) need two limbs for u */
Packit 5c3484
#define U2   (GMP_NUMB_BITS < BITS_PER_ULONG)
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpf_sqrt_ui (mpf_ptr r, unsigned long int u)
Packit 5c3484
{
Packit 5c3484
  mp_size_t rsize, zeros;
Packit 5c3484
  mp_ptr tp;
Packit 5c3484
  mp_size_t prec;
Packit 5c3484
  TMP_DECL;
Packit 5c3484
Packit 5c3484
  if (UNLIKELY (u <= 1))
Packit 5c3484
    {
Packit 5c3484
      SIZ (r) = EXP (r) = u;
Packit 5c3484
      *PTR (r) = u;
Packit 5c3484
      return;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  TMP_MARK;
Packit 5c3484
Packit 5c3484
  prec = PREC (r);
Packit 5c3484
  zeros = 2 * prec - 2;
Packit 5c3484
  rsize = zeros + 1 + U2;
Packit 5c3484
Packit 5c3484
  tp = TMP_ALLOC_LIMBS (rsize);
Packit 5c3484
Packit 5c3484
  MPN_ZERO (tp, zeros);
Packit 5c3484
  tp[zeros] = u & GMP_NUMB_MASK;
Packit 5c3484
Packit 5c3484
#if U2
Packit 5c3484
  {
Packit 5c3484
    mp_limb_t uhigh = u >> GMP_NUMB_BITS;
Packit 5c3484
    tp[zeros + 1] = uhigh;
Packit 5c3484
    rsize -= (uhigh == 0);
Packit 5c3484
  }
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
  mpn_sqrtrem (PTR (r), NULL, tp, rsize);
Packit 5c3484
Packit 5c3484
  SIZ (r) = prec;
Packit 5c3484
  EXP (r) = 1;
Packit 5c3484
  TMP_FREE;
Packit 5c3484
}