github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpz/cfdiv_q_2exp.c (about)

     1  /* mpz_cdiv_q_2exp, mpz_fdiv_q_2exp -- quotient from mpz divided by 2^n.
     2  
     3  Copyright 1991, 1993, 1994, 1996, 1998, 1999, 2001, 2002, 2004, 2012,
     4  2015 Free Software Foundation, Inc.
     5  
     6  This file is part of the GNU MP Library.
     7  
     8  The GNU MP Library is free software; you can redistribute it and/or modify
     9  it under the terms of either:
    10  
    11    * the GNU Lesser General Public License as published by the Free
    12      Software Foundation; either version 3 of the License, or (at your
    13      option) any later version.
    14  
    15  or
    16  
    17    * the GNU General Public License as published by the Free Software
    18      Foundation; either version 2 of the License, or (at your option) any
    19      later version.
    20  
    21  or both in parallel, as here.
    22  
    23  The GNU MP Library is distributed in the hope that it will be useful, but
    24  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    25  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    26  for more details.
    27  
    28  You should have received copies of the GNU General Public License and the
    29  GNU Lesser General Public License along with the GNU MP Library.  If not,
    30  see https://www.gnu.org/licenses/.  */
    31  
    32  #include "gmp.h"
    33  #include "gmp-impl.h"
    34  
    35  
    36  /* dir==1 for ceil, dir==-1 for floor */
    37  
    38  static void __gmpz_cfdiv_q_2exp (REGPARM_3_1 (mpz_ptr, mpz_srcptr, mp_bitcnt_t, int)) REGPARM_ATTR (1);
    39  #define cfdiv_q_2exp(w,u,cnt,dir)  __gmpz_cfdiv_q_2exp (REGPARM_3_1 (w,u,cnt,dir))
    40  
    41  REGPARM_ATTR (1) static void
    42  cfdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt, int dir)
    43  {
    44    mp_size_t  wsize, usize, abs_usize, limb_cnt, i;
    45    mp_srcptr  up;
    46    mp_ptr     wp;
    47    mp_limb_t  round, rmask;
    48  
    49    usize = SIZ (u);
    50    abs_usize = ABS (usize);
    51    limb_cnt = cnt / GMP_NUMB_BITS;
    52    wsize = abs_usize - limb_cnt;
    53    if (wsize <= 0)
    54      {
    55        /* u < 2**cnt, so result 1, 0 or -1 according to rounding */
    56        PTR(w)[0] = 1;
    57        SIZ(w) = (usize == 0 || (usize ^ dir) < 0 ? 0 : dir);
    58        return;
    59      }
    60  
    61    /* +1 limb to allow for mpn_add_1 below */
    62    wp = MPZ_REALLOC (w, wsize+1);
    63  
    64    /* Check for rounding if direction matches u sign.
    65       Set round if we're skipping non-zero limbs.  */
    66    up = PTR(u);
    67    round = 0;
    68    rmask = ((usize ^ dir) >= 0 ? MP_LIMB_T_MAX : 0);
    69    if (rmask != 0)
    70      for (i = 0; i < limb_cnt && round == 0; i++)
    71        round = up[i];
    72  
    73    cnt %= GMP_NUMB_BITS;
    74    if (cnt != 0)
    75      {
    76        round |= rmask & mpn_rshift (wp, up + limb_cnt, wsize, cnt);
    77        wsize -= (wp[wsize - 1] == 0);
    78      }
    79    else
    80      MPN_COPY_INCR (wp, up + limb_cnt, wsize);
    81  
    82    if (round != 0)
    83      {
    84        if (wsize != 0)
    85  	{
    86  	  mp_limb_t cy;
    87  	  cy = mpn_add_1 (wp, wp, wsize, CNST_LIMB(1));
    88  	  wp[wsize] = cy;
    89  	  wsize += cy;
    90  	}
    91        else
    92  	{
    93  	  /* We shifted something to zero.  */
    94  	  wp[0] = 1;
    95  	  wsize = 1;
    96  	}
    97      }
    98    SIZ(w) = (usize >= 0 ? wsize : -wsize);
    99  }
   100  
   101  
   102  void
   103  mpz_cdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
   104  {
   105    cfdiv_q_2exp (w, u, cnt, 1);
   106  }
   107  
   108  void
   109  mpz_fdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
   110  {
   111    cfdiv_q_2exp (w, u, cnt, -1);
   112  }