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

     1  /* mpz_ui_sub -- Subtract an unsigned one-word integer and an mpz_t.
     2  
     3  Copyright 2002, 2004 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library.
     6  
     7  The GNU MP Library is free software; you can redistribute it and/or modify
     8  it under the terms of either:
     9  
    10    * the GNU Lesser General Public License as published by the Free
    11      Software Foundation; either version 3 of the License, or (at your
    12      option) any later version.
    13  
    14  or
    15  
    16    * the GNU General Public License as published by the Free Software
    17      Foundation; either version 2 of the License, or (at your option) any
    18      later version.
    19  
    20  or both in parallel, as here.
    21  
    22  The GNU MP Library is distributed in the hope that it will be useful, but
    23  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    24  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    25  for more details.
    26  
    27  You should have received copies of the GNU General Public License and the
    28  GNU Lesser General Public License along with the GNU MP Library.  If not,
    29  see https://www.gnu.org/licenses/.  */
    30  
    31  #include "gmp.h"
    32  #include "gmp-impl.h"
    33  
    34  void
    35  mpz_ui_sub (mpz_ptr w, unsigned long int uval, mpz_srcptr v)
    36  {
    37    mp_ptr vp, wp;
    38    mp_size_t vn, wn;
    39    mp_limb_t cy;
    40  
    41  #if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
    42    if (uval > GMP_NUMB_MAX)
    43      {
    44        mpz_t u;
    45        mp_limb_t ul[2];
    46        PTR(u) = ul;
    47        ul[0] = uval & GMP_NUMB_MASK;
    48        ul[1] = uval >> GMP_NUMB_BITS;
    49        SIZ(u) = 2;
    50        mpz_sub (w, u, v);
    51        return;
    52      }
    53  #endif
    54  
    55    vp = PTR(v);
    56    vn = SIZ(v);
    57  
    58    wp = PTR(w);
    59  
    60    if (vn > 1)
    61      {
    62        wp = MPZ_REALLOC (w, vn);
    63        vp = PTR(v);
    64        mpn_sub_1 (wp, vp, vn, (mp_limb_t) uval);
    65        wn = -(vn - (wp[vn - 1] == 0));
    66      }
    67    else if (vn == 1)
    68      {
    69        if (uval >= vp[0])
    70  	{
    71  	  wp[0] = uval - vp[0];
    72  	  wn = wp[0] != 0;
    73  	}
    74        else
    75  	{
    76  	  wp[0] = vp[0] - uval;
    77  	  wn = -1;
    78  	}
    79      }
    80    else if (vn == 0)
    81      {
    82        wp[0] = uval;
    83        wn = uval != 0;
    84      }
    85    else /* (vn < 0) */
    86      {
    87        vn = -vn;
    88        wp = MPZ_REALLOC (w, vn + 1);
    89        vp = PTR(v);
    90        cy = mpn_add_1 (wp, vp, vn, (mp_limb_t) uval);
    91        wp[vn] = cy;
    92        wn = vn + (cy != 0);
    93      }
    94  
    95    SIZ(w) = wn;
    96  }