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

     1  /* mpf_add_ui -- Add a float and an unsigned integer.
     2  
     3  Copyright 1993, 1994, 1996, 2000, 2001 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  mpf_add_ui (mpf_ptr sum, mpf_srcptr u, unsigned long int v)
    36  {
    37    mp_srcptr up = u->_mp_d;
    38    mp_ptr sump = sum->_mp_d;
    39    mp_size_t usize, sumsize;
    40    mp_size_t prec = sum->_mp_prec;
    41    mp_exp_t uexp = u->_mp_exp;
    42  
    43    usize = u->_mp_size;
    44    if (usize <= 0)
    45      {
    46        if (usize == 0)
    47  	{
    48  	  mpf_set_ui (sum, v);
    49  	  return;
    50  	}
    51        else
    52  	{
    53  	  __mpf_struct u_negated;
    54  	  u_negated._mp_size = -usize;
    55  	  u_negated._mp_exp = u->_mp_exp;
    56  	  u_negated._mp_d = u->_mp_d;
    57  	  mpf_sub_ui (sum, &u_negated, v);
    58  	  sum->_mp_size = -(sum->_mp_size);
    59  	  return;
    60  	}
    61      }
    62  
    63    if (v == 0)
    64      {
    65      sum_is_u:
    66        if (u != sum)
    67  	{
    68  	  sumsize = MIN (usize, prec + 1);
    69  	  MPN_COPY (sum->_mp_d, up + usize - sumsize, sumsize);
    70  	  sum->_mp_size = sumsize;
    71  	  sum->_mp_exp = u->_mp_exp;
    72  	}
    73        return;
    74      }
    75  
    76    if (uexp > 0)
    77      {
    78        /* U >= 1.  */
    79        if (uexp > prec)
    80  	{
    81  	  /* U >> V, V is not part of final result.  */
    82  	  goto sum_is_u;
    83  	}
    84        else
    85  	{
    86  	  /* U's "limb point" is somewhere between the first limb
    87  	     and the PREC:th limb.
    88  	     Both U and V are part of the final result.  */
    89  	  if (uexp > usize)
    90  	    {
    91  	      /*   uuuuuu0000. */
    92  	      /* +          v. */
    93  	      /* We begin with moving U to the top of SUM, to handle
    94  		 samevar(U,SUM).  */
    95  	      MPN_COPY_DECR (sump + uexp - usize, up, usize);
    96  	      sump[0] = v;
    97  	      MPN_ZERO (sump + 1, uexp - usize - 1);
    98  #if 0 /* What is this??? */
    99  	      if (sum == u)
   100  		MPN_COPY (sum->_mp_d, sump, uexp);
   101  #endif
   102  	      sum->_mp_size = uexp;
   103  	      sum->_mp_exp = uexp;
   104  	    }
   105  	  else
   106  	    {
   107  	      /*   uuuuuu.uuuu */
   108  	      /* +      v.     */
   109  	      mp_limb_t cy_limb;
   110  	      if (usize > prec)
   111  		{
   112  		  /* Ignore excess limbs in U.  */
   113  		  up += usize - prec;
   114  		  usize -= usize - prec; /* Eq. usize = prec */
   115  		}
   116  	      if (sump != up)
   117  		MPN_COPY_INCR (sump, up, usize - uexp);
   118  	      cy_limb = mpn_add_1 (sump + usize - uexp, up + usize - uexp,
   119  				   uexp, (mp_limb_t) v);
   120  	      sump[usize] = cy_limb;
   121  	      sum->_mp_size = usize + cy_limb;
   122  	      sum->_mp_exp = uexp + cy_limb;
   123  	    }
   124  	}
   125      }
   126    else
   127      {
   128        /* U < 1, so V > U for sure.  */
   129        /* v.         */
   130        /*  .0000uuuu */
   131        if ((-uexp) >= prec)
   132  	{
   133  	  sump[0] = v;
   134  	  sum->_mp_size = 1;
   135  	  sum->_mp_exp = 1;
   136  	}
   137        else
   138  	{
   139  	  if (usize + (-uexp) + 1 > prec)
   140  	    {
   141  	      /* Ignore excess limbs in U.  */
   142  	      up += usize + (-uexp) + 1 - prec;
   143  	      usize -= usize + (-uexp) + 1 - prec;
   144  	    }
   145  	  if (sump != up)
   146  	    MPN_COPY_INCR (sump, up, usize);
   147  	  MPN_ZERO (sump + usize, -uexp);
   148  	  sump[usize + (-uexp)] = v;
   149  	  sum->_mp_size = usize + (-uexp) + 1;
   150  	  sum->_mp_exp = 1;
   151  	}
   152      }
   153  }