Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libgmp / mpf / add_ui.c
1 /* mpf_add_ui -- Add a float and an unsigned integer.
2
3 Copyright (C) 1993, 1994, 1996 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 Library General Public License as published by
9 the Free Software Foundation; either version 2 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 Library General Public
15 License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
21
22 #include "gmp.h"
23 #include "gmp-impl.h"
24
25 void
26 #if __STDC__
27 mpf_add_ui (mpf_ptr sum, mpf_srcptr u, unsigned long int v)
28 #else
29 mpf_add_ui (sum, u, v)
30      mpf_ptr sum;
31      mpf_srcptr u;
32      unsigned long int v;
33 #endif
34 {
35   mp_srcptr up = u->_mp_d;
36   mp_ptr sump = sum->_mp_d;
37   mp_size_t usize, sumsize;
38   mp_size_t prec = sum->_mp_prec;
39   mp_exp_t uexp = u->_mp_exp;
40
41   usize = u->_mp_size;
42   if (usize <= 0)
43     {
44       if (usize == 0)
45         {
46           mpf_set_ui (sum, v);
47           return;
48         }
49       else
50         {
51           __mpf_struct u_negated;
52           u_negated._mp_size = -usize;
53           u_negated._mp_exp = u->_mp_exp;
54           u_negated._mp_d = u->_mp_d;
55           mpf_sub_ui (sum, &u_negated, v);
56           sum->_mp_size = -(sum->_mp_size);
57           return;
58         }
59     }
60
61   if (v == 0)
62     {
63     sum_is_u:
64       if (u != sum)
65         {
66           sumsize = MIN (usize, prec + 1);
67           MPN_COPY (sum->_mp_d, up + usize - sumsize, sumsize);
68           sum->_mp_size = sumsize;
69           sum->_mp_exp = u->_mp_exp;
70         }
71       return;
72     }
73
74   if (uexp > 0)
75     {
76       /* U >= 1.  */
77       if (uexp > prec)
78         {
79           /* U >> V, V is not part of final result.  */
80           goto sum_is_u;
81         }
82       else
83         {
84           /* U's "limb point" is somewhere between the first limb
85              and the PREC:th limb.
86              Both U and V are part of the final result.  */
87           if (uexp > usize)
88             {
89               /*   uuuuuu0000. */
90               /* +          v. */
91               /* We begin with moving U to the top of SUM, to handle
92                  samevar(U,SUM).  */
93               MPN_COPY_DECR (sump + uexp - usize, up, usize);
94               sump[0] = v;
95               MPN_ZERO (sump + 1, uexp - usize - 1);
96 #if 0 /* What is this??? */
97               if (sum == u)
98                 MPN_COPY (sum->_mp_d, sump, uexp);
99 #endif
100               sum->_mp_size = uexp;
101               sum->_mp_exp = uexp;
102             }
103           else
104             {
105               /*   uuuuuu.uuuu */
106               /* +      v.     */
107               mp_limb_t cy_limb;
108               if (usize > prec)
109                 {
110                   /* Ignore excess limbs in U.  */
111                   up += usize - prec;
112                   usize -= usize - prec; /* Eq. usize = prec */
113                 }
114               if (sump != up)
115                 MPN_COPY (sump, up, usize - uexp);
116               cy_limb = mpn_add_1 (sump + usize - uexp, up + usize - uexp,
117                                    uexp, (mp_limb_t) v);
118               sump[usize] = cy_limb;
119               sum->_mp_size = usize + cy_limb;
120               sum->_mp_exp = uexp + cy_limb;
121             }
122         }
123     }
124   else
125     {
126       /* U < 1, so V > U for sure.  */
127       /* v.         */
128       /*  .0000uuuu */
129       if ((-uexp) >= prec)
130         {
131           sump[0] = v;
132           sum->_mp_size = 1;
133           sum->_mp_exp = 1;
134         }
135       else
136         {
137           if (usize + (-uexp) + 1 > prec)
138             {
139               /* Ignore excess limbs in U.  */
140               up += usize + (-uexp) + 1 - prec;
141               usize -= usize + (-uexp) + 1 - prec;
142             }
143           if (sump != up)
144             MPN_COPY (sump, up, usize);
145           MPN_ZERO (sump + usize, -uexp);
146           sump[usize + (-uexp)] = v;
147           sum->_mp_size = usize + (-uexp) + 1;
148           sum->_mp_exp = 1;
149         }
150     }
151 }