Blame mpq/md_2exp.c

Packit 5c3484
/* mpq_mul_2exp, mpq_div_2exp - multiply or divide by 2^N */
Packit 5c3484
Packit 5c3484
/*
Packit 5c3484
Copyright 2000, 2002, 2012 Free Software 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 "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "longlong.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* The multiplier/divisor "n", representing 2^n, is applied by right shifting
Packit 5c3484
   "r" until it's odd (if it isn't already), and left shifting "l" for the
Packit 5c3484
   rest. */
Packit 5c3484
Packit 5c3484
static void
Packit 5c3484
mord_2exp (mpz_ptr ldst, mpz_ptr rdst, mpz_srcptr lsrc, mpz_srcptr rsrc,
Packit 5c3484
           mp_bitcnt_t n)
Packit 5c3484
{
Packit 5c3484
  mp_size_t  rsrc_size = SIZ(rsrc);
Packit 5c3484
  mp_size_t  len = ABS (rsrc_size);
Packit 5c3484
  mp_ptr     rsrc_ptr = PTR(rsrc);
Packit 5c3484
  mp_ptr     p, rdst_ptr;
Packit 5c3484
  mp_limb_t  plow;
Packit 5c3484
Packit 5c3484
  p = rsrc_ptr;
Packit 5c3484
  plow = *p;
Packit 5c3484
  while (n >= GMP_NUMB_BITS && plow == 0)
Packit 5c3484
    {
Packit 5c3484
      n -= GMP_NUMB_BITS;
Packit 5c3484
      p++;
Packit 5c3484
      plow = *p;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  /* no realloc here if rsrc==rdst, so p and rsrc_ptr remain valid */
Packit 5c3484
  len -= (p - rsrc_ptr);
Packit 5c3484
  rdst_ptr = MPZ_REALLOC (rdst, len);
Packit 5c3484
Packit 5c3484
  if ((plow & 1) || n == 0)
Packit 5c3484
    {
Packit 5c3484
      /* need INCR when src==dst */
Packit 5c3484
      if (p != rdst_ptr)
Packit 5c3484
        MPN_COPY_INCR (rdst_ptr, p, len);
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      unsigned long  shift;
Packit 5c3484
      if (plow == 0)
Packit 5c3484
        shift = n;
Packit 5c3484
      else
Packit 5c3484
        {
Packit 5c3484
          count_trailing_zeros (shift, plow);
Packit 5c3484
          shift = MIN (shift, n);
Packit 5c3484
        }
Packit 5c3484
      mpn_rshift (rdst_ptr, p, len, shift);
Packit 5c3484
      len -= (rdst_ptr[len-1] == 0);
Packit 5c3484
      n -= shift;
Packit 5c3484
    }
Packit 5c3484
  SIZ(rdst) = (rsrc_size >= 0) ? len : -len;
Packit 5c3484
Packit 5c3484
  if (n)
Packit 5c3484
    mpz_mul_2exp (ldst, lsrc, n);
Packit 5c3484
  else if (ldst != lsrc)
Packit 5c3484
    mpz_set (ldst, lsrc);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpq_mul_2exp (mpq_ptr dst, mpq_srcptr src, mp_bitcnt_t n)
Packit 5c3484
{
Packit 5c3484
  mord_2exp (NUM(dst), DEN(dst), NUM(src), DEN(src), n);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpq_div_2exp (mpq_ptr dst, mpq_srcptr src, mp_bitcnt_t n)
Packit 5c3484
{
Packit 5c3484
  if (SIZ(NUM(src)) == 0)
Packit 5c3484
    {
Packit 5c3484
      SIZ(NUM(dst)) = 0;
Packit 5c3484
      SIZ(DEN(dst)) = 1;
Packit 5c3484
      PTR(DEN(dst))[0] = 1;
Packit 5c3484
      return;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  mord_2exp (DEN(dst), NUM(dst), DEN(src), NUM(src), n);
Packit 5c3484
}