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