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

     1  /* mpz_add, mpz_sub -- add or subtract integers.
     2  
     3  Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2011, 2012 Free Software
     4  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  #ifdef OPERATION_add
    37  #define FUNCTION     mpz_add
    38  #define VARIATION
    39  #endif
    40  #ifdef OPERATION_sub
    41  #define FUNCTION     mpz_sub
    42  #define VARIATION    -
    43  #endif
    44  
    45  #ifndef FUNCTION
    46  Error, need OPERATION_add or OPERATION_sub
    47  #endif
    48  
    49  
    50  void
    51  FUNCTION (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
    52  {
    53    mp_srcptr up, vp;
    54    mp_ptr wp;
    55    mp_size_t usize, vsize, wsize;
    56    mp_size_t abs_usize;
    57    mp_size_t abs_vsize;
    58  
    59    usize = SIZ(u);
    60    vsize = VARIATION SIZ(v);
    61    abs_usize = ABS (usize);
    62    abs_vsize = ABS (vsize);
    63  
    64    if (abs_usize < abs_vsize)
    65      {
    66        /* Swap U and V. */
    67        MPZ_SRCPTR_SWAP (u, v);
    68        MP_SIZE_T_SWAP (usize, vsize);
    69        MP_SIZE_T_SWAP (abs_usize, abs_vsize);
    70      }
    71  
    72    /* True: ABS_USIZE >= ABS_VSIZE.  */
    73  
    74    /* If not space for w (and possible carry), increase space.  */
    75    wsize = abs_usize + 1;
    76    wp = MPZ_REALLOC (w, wsize);
    77  
    78    /* These must be after realloc (u or v may be the same as w).  */
    79    up = PTR(u);
    80    vp = PTR(v);
    81  
    82    if ((usize ^ vsize) < 0)
    83      {
    84        /* U and V have different sign.  Need to compare them to determine
    85  	 which operand to subtract from which.  */
    86  
    87        /* This test is right since ABS_USIZE >= ABS_VSIZE.  */
    88        if (abs_usize != abs_vsize)
    89  	{
    90  	  mpn_sub (wp, up, abs_usize, vp, abs_vsize);
    91  	  wsize = abs_usize;
    92  	  MPN_NORMALIZE (wp, wsize);
    93  	  if (usize < 0)
    94  	    wsize = -wsize;
    95  	}
    96        else if (mpn_cmp (up, vp, abs_usize) < 0)
    97  	{
    98  	  mpn_sub_n (wp, vp, up, abs_usize);
    99  	  wsize = abs_usize;
   100  	  MPN_NORMALIZE (wp, wsize);
   101  	  if (usize >= 0)
   102  	    wsize = -wsize;
   103  	}
   104        else
   105  	{
   106  	  mpn_sub_n (wp, up, vp, abs_usize);
   107  	  wsize = abs_usize;
   108  	  MPN_NORMALIZE (wp, wsize);
   109  	  if (usize < 0)
   110  	    wsize = -wsize;
   111  	}
   112      }
   113    else
   114      {
   115        /* U and V have same sign.  Add them.  */
   116        mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
   117        wp[abs_usize] = cy_limb;
   118        wsize = abs_usize + cy_limb;
   119        if (usize < 0)
   120  	wsize = -wsize;
   121      }
   122  
   123    SIZ(w) = wsize;
   124  }