Blame mpz/cfdiv_r_2exp.c

Packit 5c3484
/* mpz_cdiv_r_2exp, mpz_fdiv_r_2exp -- remainder from mpz divided by 2^n.
Packit 5c3484
Packit 5c3484
Copyright 2001, 2002, 2004, 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
Packit 5c3484
Packit 5c3484
/* Bit mask of "n" least significant bits of a limb. */
Packit 5c3484
#define LOW_MASK(n)   ((CNST_LIMB(1) << (n)) - 1)
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* dir==1 for ceil, dir==-1 for floor */
Packit 5c3484
Packit 5c3484
static void __gmpz_cfdiv_r_2exp (REGPARM_3_1 (mpz_ptr, mpz_srcptr, mp_bitcnt_t, int)) REGPARM_ATTR (1);
Packit 5c3484
#define cfdiv_r_2exp(w,u,cnt,dir)  __gmpz_cfdiv_r_2exp (REGPARM_3_1 (w, u, cnt, dir))
Packit 5c3484
Packit 5c3484
REGPARM_ATTR (1) static void
Packit 5c3484
cfdiv_r_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt, int dir)
Packit 5c3484
{
Packit 5c3484
  mp_size_t  usize, abs_usize, limb_cnt, i;
Packit 5c3484
  mp_srcptr  up;
Packit 5c3484
  mp_ptr     wp;
Packit 5c3484
  mp_limb_t  high;
Packit 5c3484
Packit 5c3484
  usize = SIZ(u);
Packit 5c3484
  if (usize == 0)
Packit 5c3484
    {
Packit 5c3484
      SIZ(w) = 0;
Packit 5c3484
      return;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  limb_cnt = cnt / GMP_NUMB_BITS;
Packit 5c3484
  cnt %= GMP_NUMB_BITS;
Packit 5c3484
  abs_usize = ABS (usize);
Packit 5c3484
Packit 5c3484
  /* MPZ_REALLOC(w) below is only when w!=u, so we can fetch PTR(u) here
Packit 5c3484
     nice and early */
Packit 5c3484
  up = PTR(u);
Packit 5c3484
Packit 5c3484
  if ((usize ^ dir) < 0)
Packit 5c3484
    {
Packit 5c3484
      /* Round towards zero, means just truncate */
Packit 5c3484
Packit 5c3484
      if (w == u)
Packit 5c3484
	{
Packit 5c3484
	  /* if already smaller than limb_cnt then do nothing */
Packit 5c3484
	  if (abs_usize <= limb_cnt)
Packit 5c3484
	    return;
Packit 5c3484
	  wp = (mp_ptr) up;
Packit 5c3484
	}
Packit 5c3484
      else
Packit 5c3484
	{
Packit 5c3484
	  i = MIN (abs_usize, limb_cnt+1);
Packit 5c3484
	  wp = MPZ_NEWALLOC (w, i);
Packit 5c3484
	  MPN_COPY (wp, up, i);
Packit 5c3484
Packit 5c3484
	  /* if smaller than limb_cnt then only the copy is needed */
Packit 5c3484
	  if (abs_usize <= limb_cnt)
Packit 5c3484
	    {
Packit 5c3484
	      SIZ(w) = usize;
Packit 5c3484
	      return;
Packit 5c3484
	    }
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      /* Round away from zero, means twos complement if non-zero */
Packit 5c3484
Packit 5c3484
      /* if u!=0 and smaller than divisor, then must negate */
Packit 5c3484
      if (abs_usize <= limb_cnt)
Packit 5c3484
	goto negate;
Packit 5c3484
Packit 5c3484
      /* if non-zero low limb, then must negate */
Packit 5c3484
      for (i = 0; i < limb_cnt; i++)
Packit 5c3484
	if (up[i] != 0)
Packit 5c3484
	  goto negate;
Packit 5c3484
Packit 5c3484
      /* if non-zero partial limb, then must negate */
Packit 5c3484
      if ((up[limb_cnt] & LOW_MASK (cnt)) != 0)
Packit 5c3484
	goto negate;
Packit 5c3484
Packit 5c3484
      /* otherwise low bits of u are zero, so that's the result */
Packit 5c3484
      SIZ(w) = 0;
Packit 5c3484
      return;
Packit 5c3484
Packit 5c3484
    negate:
Packit 5c3484
      /* twos complement negation to get 2**cnt-u */
Packit 5c3484
Packit 5c3484
      wp = MPZ_REALLOC (w, limb_cnt+1);
Packit 5c3484
      up = PTR(u);
Packit 5c3484
Packit 5c3484
      /* Ones complement */
Packit 5c3484
      i = MIN (abs_usize, limb_cnt+1);
Packit 5c3484
      ASSERT_CARRY (mpn_neg (wp, up, i));
Packit 5c3484
      for ( ; i <= limb_cnt; i++)
Packit 5c3484
	wp[i] = GMP_NUMB_MAX;
Packit 5c3484
Packit 5c3484
      usize = -usize;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  /* Mask the high limb */
Packit 5c3484
  high = wp[limb_cnt];
Packit 5c3484
  high &= LOW_MASK (cnt);
Packit 5c3484
  wp[limb_cnt] = high;
Packit 5c3484
Packit 5c3484
  /* Strip any consequent high zeros */
Packit 5c3484
  while (high == 0)
Packit 5c3484
    {
Packit 5c3484
      limb_cnt--;
Packit 5c3484
      if (limb_cnt < 0)
Packit 5c3484
	{
Packit 5c3484
	  SIZ(w) = 0;
Packit 5c3484
	  return;
Packit 5c3484
	}
Packit 5c3484
      high = wp[limb_cnt];
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  limb_cnt++;
Packit 5c3484
  SIZ(w) = (usize >= 0 ? limb_cnt : -limb_cnt);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpz_cdiv_r_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
Packit 5c3484
{
Packit 5c3484
  cfdiv_r_2exp (w, u, cnt, 1);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpz_fdiv_r_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
Packit 5c3484
{
Packit 5c3484
  cfdiv_r_2exp (w, u, cnt, -1);
Packit 5c3484
}