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

     1  /* mpz_com(mpz_ptr dst, mpz_ptr src) -- Assign the bit-complemented value of
     2     SRC to DST.
     3  
     4  Copyright 1991, 1993, 1994, 1996, 2001, 2003, 2012 Free Software Foundation,
     5  Inc.
     6  
     7  This file is part of the GNU MP Library.
     8  
     9  The GNU MP Library is free software; you can redistribute it and/or modify
    10  it under the terms of either:
    11  
    12    * the GNU Lesser General Public License as published by the Free
    13      Software Foundation; either version 3 of the License, or (at your
    14      option) any later version.
    15  
    16  or
    17  
    18    * the GNU General Public License as published by the Free Software
    19      Foundation; either version 2 of the License, or (at your option) any
    20      later version.
    21  
    22  or both in parallel, as here.
    23  
    24  The GNU MP Library is distributed in the hope that it will be useful, but
    25  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    26  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    27  for more details.
    28  
    29  You should have received copies of the GNU General Public License and the
    30  GNU Lesser General Public License along with the GNU MP Library.  If not,
    31  see https://www.gnu.org/licenses/.  */
    32  
    33  #include "gmp.h"
    34  #include "gmp-impl.h"
    35  
    36  void
    37  mpz_com (mpz_ptr dst, mpz_srcptr src)
    38  {
    39    mp_size_t size = SIZ (src);
    40    mp_srcptr src_ptr;
    41    mp_ptr dst_ptr;
    42  
    43    if (size >= 0)
    44      {
    45        /* As with infinite precision: one's complement, two's complement.
    46  	 But this can be simplified using the identity -x = ~x + 1.
    47  	 So we're going to compute (~~x) + 1 = x + 1!  */
    48  
    49        if (UNLIKELY (size == 0))
    50  	{
    51  	  /* special case, as mpn_add_1 wants size!=0 */
    52  	  PTR (dst)[0] = 1;
    53  	  SIZ (dst) = -1;
    54  	}
    55        else
    56  	{
    57  	  mp_limb_t cy;
    58  
    59  	  dst_ptr = MPZ_REALLOC (dst, size + 1);
    60  
    61  	  src_ptr = PTR (src);
    62  
    63  	  cy = mpn_add_1 (dst_ptr, src_ptr, size, (mp_limb_t) 1);
    64  	  dst_ptr[size] = cy;
    65  	  size += cy;
    66  
    67  	  /* Store a negative size, to indicate ones-extension.  */
    68  	  SIZ (dst) = -size;
    69        }
    70      }
    71    else
    72      {
    73        /* As with infinite precision: two's complement, then one's complement.
    74  	 But that can be simplified using the identity -x = ~(x - 1).
    75  	 So we're going to compute ~~(x - 1) = x - 1!  */
    76        size = -size;
    77  
    78        dst_ptr = MPZ_REALLOC (dst, size);
    79  
    80        src_ptr = PTR (src);
    81  
    82        mpn_sub_1 (dst_ptr, src_ptr, size, (mp_limb_t) 1);
    83        size -= dst_ptr[size - 1] == 0;
    84  
    85        /* Store a positive size, to indicate zero-extension.  */
    86        SIZ (dst) = size;
    87      }
    88  }