xref: /dragonfly/contrib/gmp/mpf/add_ui.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
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 the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19 
20 #include "gmp.h"
21 #include "gmp-impl.h"
22 
23 void
mpf_add_ui(mpf_ptr sum,mpf_srcptr u,unsigned long int v)24 mpf_add_ui (mpf_ptr sum, mpf_srcptr u, unsigned long int v)
25 {
26   mp_srcptr up = u->_mp_d;
27   mp_ptr sump = sum->_mp_d;
28   mp_size_t usize, sumsize;
29   mp_size_t prec = sum->_mp_prec;
30   mp_exp_t uexp = u->_mp_exp;
31 
32   usize = u->_mp_size;
33   if (usize <= 0)
34     {
35       if (usize == 0)
36           {
37             mpf_set_ui (sum, v);
38             return;
39           }
40       else
41           {
42             __mpf_struct u_negated;
43             u_negated._mp_size = -usize;
44             u_negated._mp_exp = u->_mp_exp;
45             u_negated._mp_d = u->_mp_d;
46             mpf_sub_ui (sum, &u_negated, v);
47             sum->_mp_size = -(sum->_mp_size);
48             return;
49           }
50     }
51 
52   if (v == 0)
53     {
54     sum_is_u:
55       if (u != sum)
56           {
57             sumsize = MIN (usize, prec + 1);
58             MPN_COPY (sum->_mp_d, up + usize - sumsize, sumsize);
59             sum->_mp_size = sumsize;
60             sum->_mp_exp = u->_mp_exp;
61           }
62       return;
63     }
64 
65   if (uexp > 0)
66     {
67       /* U >= 1.  */
68       if (uexp > prec)
69           {
70             /* U >> V, V is not part of final result.  */
71             goto sum_is_u;
72           }
73       else
74           {
75             /* U's "limb point" is somewhere between the first limb
76                and the PREC:th limb.
77                Both U and V are part of the final result.  */
78             if (uexp > usize)
79               {
80                 /*   uuuuuu0000. */
81                 /* +          v. */
82                 /* We begin with moving U to the top of SUM, to handle
83                      samevar(U,SUM).  */
84                 MPN_COPY_DECR (sump + uexp - usize, up, usize);
85                 sump[0] = v;
86                 MPN_ZERO (sump + 1, uexp - usize - 1);
87 #if 0 /* What is this??? */
88                 if (sum == u)
89                     MPN_COPY (sum->_mp_d, sump, uexp);
90 #endif
91                 sum->_mp_size = uexp;
92                 sum->_mp_exp = uexp;
93               }
94             else
95               {
96                 /*   uuuuuu.uuuu */
97                 /* +      v.     */
98                 mp_limb_t cy_limb;
99                 if (usize > prec)
100                     {
101                       /* Ignore excess limbs in U.  */
102                       up += usize - prec;
103                       usize -= usize - prec; /* Eq. usize = prec */
104                     }
105                 if (sump != up)
106                     MPN_COPY_INCR (sump, up, usize - uexp);
107                 cy_limb = mpn_add_1 (sump + usize - uexp, up + usize - uexp,
108                                            uexp, (mp_limb_t) v);
109                 sump[usize] = cy_limb;
110                 sum->_mp_size = usize + cy_limb;
111                 sum->_mp_exp = uexp + cy_limb;
112               }
113           }
114     }
115   else
116     {
117       /* U < 1, so V > U for sure.  */
118       /* v.         */
119       /*  .0000uuuu */
120       if ((-uexp) >= prec)
121           {
122             sump[0] = v;
123             sum->_mp_size = 1;
124             sum->_mp_exp = 1;
125           }
126       else
127           {
128             if (usize + (-uexp) + 1 > prec)
129               {
130                 /* Ignore excess limbs in U.  */
131                 up += usize + (-uexp) + 1 - prec;
132                 usize -= usize + (-uexp) + 1 - prec;
133               }
134             if (sump != up)
135               MPN_COPY_INCR (sump, up, usize);
136             MPN_ZERO (sump + usize, -uexp);
137             sump[usize + (-uexp)] = v;
138             sum->_mp_size = usize + (-uexp) + 1;
139             sum->_mp_exp = 1;
140           }
141     }
142 }
143