summaryrefslogtreecommitdiff
path: root/mpf/add_ui.c
blob: 1e2a94b1122c7e0eda1da7fce17f42de02216e08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* mpf_add_ui -- Add a float and an unsigned integer.

Copyright 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */

#include "gmp-impl.h"

void
mpf_add_ui (mpf_ptr sum, mpf_srcptr u, unsigned long int v)
{
  mp_srcptr up = u->_mp_d;
  mp_ptr sump = sum->_mp_d;
  mp_size_t usize, sumsize;
  mp_size_t prec = sum->_mp_prec;
  mp_exp_t uexp = u->_mp_exp;

  usize = u->_mp_size;
  if (usize <= 0)
    {
      if (usize == 0)
	{
	  mpf_set_ui (sum, v);
	  return;
	}
      else
	{
	  __mpf_struct u_negated;
	  u_negated._mp_size = -usize;
	  u_negated._mp_exp = u->_mp_exp;
	  u_negated._mp_d = u->_mp_d;
	  mpf_sub_ui (sum, &u_negated, v);
	  sum->_mp_size = -(sum->_mp_size);
	  return;
	}
    }

  if (v == 0)
    {
    sum_is_u:
      if (u != sum)
	{
	  sumsize = MIN (usize, prec + 1);
	  MPN_COPY (sum->_mp_d, up + usize - sumsize, sumsize);
	  sum->_mp_size = sumsize;
	  sum->_mp_exp = u->_mp_exp;
	}
      return;
    }

  if (uexp > 0)
    {
      /* U >= 1.  */
      if (uexp > prec)
	{
	  /* U >> V, V is not part of final result.  */
	  goto sum_is_u;
	}
      else
	{
	  /* U's "limb point" is somewhere between the first limb
	     and the PREC:th limb.
	     Both U and V are part of the final result.  */
	  if (uexp > usize)
	    {
	      /*   uuuuuu0000. */
	      /* +          v. */
	      /* We begin with moving U to the top of SUM, to handle
		 samevar(U,SUM).  */
	      MPN_COPY_DECR (sump + uexp - usize, up, usize);
	      sump[0] = v;
	      MPN_ZERO (sump + 1, uexp - usize - 1);
#if 0 /* What is this??? */
	      if (sum == u)
		MPN_COPY (sum->_mp_d, sump, uexp);
#endif
	      sum->_mp_size = uexp;
	      sum->_mp_exp = uexp;
	    }
	  else
	    {
	      /*   uuuuuu.uuuu */
	      /* +      v.     */
	      mp_limb_t cy_limb;
	      if (usize > prec)
		{
		  /* Ignore excess limbs in U.  */
		  up += usize - prec;
		  usize -= usize - prec; /* Eq. usize = prec */
		}
	      if (sump != up)
		MPN_COPY_INCR (sump, up, usize - uexp);
	      cy_limb = mpn_add_1 (sump + usize - uexp, up + usize - uexp,
				   uexp, (mp_limb_t) v);
	      sump[usize] = cy_limb;
	      sum->_mp_size = usize + cy_limb;
	      sum->_mp_exp = uexp + cy_limb;
	    }
	}
    }
  else
    {
      /* U < 1, so V > U for sure.  */
      /* v.         */
      /*  .0000uuuu */
      if ((-uexp) >= prec)
	{
	  sump[0] = v;
	  sum->_mp_size = 1;
	  sum->_mp_exp = 1;
	}
      else
	{
	  if (usize + (-uexp) + 1 > prec)
	    {
	      /* Ignore excess limbs in U.  */
	      up += usize + (-uexp) + 1 - prec;
	      usize -= usize + (-uexp) + 1 - prec;
	    }
	  if (sump != up)
	    MPN_COPY_INCR (sump, up, usize);
	  MPN_ZERO (sump + usize, -uexp);
	  sump[usize + (-uexp)] = v;
	  sum->_mp_size = usize + (-uexp) + 1;
	  sum->_mp_exp = 1;
	}
    }
}