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