beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpf / add_ui.c
blobb1e57d04c1eb61f659a35de30d9643c1cca111ba
1 /* mpf_add_ui -- Add a float and an unsigned integer.
3 Copyright 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
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.
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.
20 or both in parallel, as here.
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.
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/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
34 void
35 mpf_add_ui (mpf_ptr sum, mpf_srcptr u, unsigned long int v)
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;
43 usize = u->_mp_size;
44 if (usize <= 0)
46 if (usize == 0)
48 mpf_set_ui (sum, v);
49 return;
51 else
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;
63 if (v == 0)
65 sum_is_u:
66 if (u != sum)
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;
73 return;
76 if (uexp > 0)
78 /* U >= 1. */
79 if (uexp > prec)
81 /* U >> V, V is not part of final result. */
82 goto sum_is_u;
84 else
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)
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;
105 else
107 /* uuuuuu.uuuu */
108 /* + v. */
109 mp_limb_t cy_limb;
110 if (usize > prec)
112 /* Ignore excess limbs in U. */
113 up += usize - prec;
114 usize -= usize - prec; /* Eq. usize = prec */
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;
126 else
128 /* U < 1, so V > U for sure. */
129 /* v. */
130 /* .0000uuuu */
131 if ((-uexp) >= prec)
133 sump[0] = v;
134 sum->_mp_size = 1;
135 sum->_mp_exp = 1;
137 else
139 if (usize + (-uexp) + 1 > prec)
141 /* Ignore excess limbs in U. */
142 up += usize + (-uexp) + 1 - prec;
143 usize -= usize + (-uexp) + 1 - prec;
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;