1 /* mpf_get_str (digit_ptr, exp, base, n_digits, a) -- Convert the floating
2 point number A to a base BASE number and store N_DIGITS raw digits at
3 DIGIT_PTR, and the base BASE exponent in the word pointed to by EXP. For
4 example, the number 3.1416 would be returned as "31416" in DIGIT_PTR and
7 Copyright 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2005, 2006 Free
8 Software Foundation, Inc.
10 This file is part of the GNU MP Library.
12 The GNU MP Library is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or (at your
15 option) any later version.
17 The GNU MP Library is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20 License for more details.
22 You should have received a copy of the GNU Lesser General Public License
23 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
25 #include <stdlib.h> /* for NULL */
28 #include "longlong.h" /* for count_leading_zeros */
30 /* Could use some more work.
32 1. Allocation is excessive. Try to combine areas. Perhaps use result
33 string area for temp limb space?
34 2. We generate up to two limbs of extra digits. This is because we don't
35 check the exact number of bits in the input operand, and from that
36 compute an accurate exponent (variable e in the code). It would be
37 cleaner and probably somewhat faster to change this.
40 /* Compute base^exp and return the most significant prec limbs in rp[].
41 Put the count of omitted low limbs in *ign.
42 Return the actual size (which might be less than prec).
43 Allocation of rp[] and the temporary tp[] should be 2*prec+2 limbs. */
45 mpn_pow_1_highpart (mp_ptr rp, mp_size_t *ignp,
46 mp_limb_t base, unsigned long exp,
47 mp_size_t prec, mp_ptr tp)
49 mp_size_t ign; /* counts number of ignored low limbs in r */
50 mp_size_t off; /* keeps track of offset where value starts */
51 mp_ptr passed_rp = rp;
67 count_leading_zeros (cnt, exp);
68 for (i = GMP_LIMB_BITS - cnt - 2; i >= 0; i--)
70 mpn_sqr (tp, rp + off, rn);
72 rn -= tp[rn - 1] == 0;
84 if (((exp >> i) & 1) != 0)
87 cy = mpn_mul_1 (rp, rp + off, rn, base);
96 ASSERT (rn == prec + 1);
103 /* With somewhat less than 50% probability, we can skip this copy. */
104 if (passed_rp != rp + off)
105 MPN_COPY_INCR (passed_rp, rp + off, rn);
111 mpf_get_str (char *dbuf, mp_exp_t *exp, int base, size_t n_digits, mpf_srcptr u)
114 mp_size_t n_limbs_needed;
117 mp_size_t un, pn, tn;
119 mp_exp_t exp_in_base;
120 size_t n_digits_computed;
122 const char *num_to_text;
123 size_t alloc_size = 0;
133 num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
138 num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
146 num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
149 MPF_SIGNIFICANT_DIGITS (max_digits, base, PREC(u));
150 if (n_digits == 0 || n_digits > max_digits)
151 n_digits = max_digits;
155 /* We didn't get a string from the user. Allocate one (and return
156 a pointer to it) with space for `-' and terminating null. */
157 alloc_size = n_digits + 2;
158 dbuf = (char *) (*__gmp_allocate_func) (n_digits + 2);
171 /* Allocate temporary digit space. We can't put digits directly in the user
172 area, since we generate more digits than requested. (We allocate
173 2 * GMP_LIMB_BITS extra bytes because of the digit block nature of the
175 tstr = (unsigned char *) TMP_ALLOC (n_digits + 2 * GMP_LIMB_BITS + 3);
177 n_limbs_needed = 2 + (mp_size_t)
178 (n_digits / (GMP_NUMB_BITS * mp_bases[base].chars_per_bit_exactly));
180 if (ue <= n_limbs_needed)
182 /* We need to multiply number by base^n to get an n_digits integer part. */
183 mp_size_t n_more_limbs_needed, ign, off;
186 n_more_limbs_needed = n_limbs_needed - ue;
187 e = (unsigned long) n_more_limbs_needed * (GMP_NUMB_BITS * mp_bases[base].chars_per_bit_exactly);
189 if (un > n_limbs_needed)
191 up += un - n_limbs_needed;
194 pp = TMP_ALLOC_LIMBS (2 * n_limbs_needed + 2);
195 tp = TMP_ALLOC_LIMBS (2 * n_limbs_needed + 2);
197 pn = mpn_pow_1_highpart (pp, &ign, (mp_limb_t) base, e, n_limbs_needed, tp);
199 mpn_mul (tp, up, un, pp, pn); /* FIXME: mpn_mul_highpart */
201 mpn_mul (tp, pp, pn, up, un); /* FIXME: mpn_mul_highpart */
203 tn -= tp[tn - 1] == 0;
207 MPN_COPY_DECR (tp - off, tp, tn);
212 n_digits_computed = mpn_get_str (tstr, base, tp + off, tn - off);
214 exp_in_base = n_digits_computed - e;
218 /* We need to divide number by base^n to get an n_digits integer part. */
219 mp_size_t n_less_limbs_needed, ign, off, xn;
223 n_less_limbs_needed = ue - n_limbs_needed;
224 e = (unsigned long) n_less_limbs_needed * (GMP_NUMB_BITS * mp_bases[base].chars_per_bit_exactly);
226 if (un > n_limbs_needed)
228 up += un - n_limbs_needed;
231 pp = TMP_ALLOC_LIMBS (2 * n_limbs_needed + 2);
232 tp = TMP_ALLOC_LIMBS (2 * n_limbs_needed + 2);
234 pn = mpn_pow_1_highpart (pp, &ign, (mp_limb_t) base, e, n_limbs_needed, tp);
236 xn = n_limbs_needed + (n_less_limbs_needed-ign);
237 xp = TMP_ALLOC_LIMBS (xn);
240 MPN_COPY (xp + off, up, un);
242 dummyp = TMP_ALLOC_LIMBS (pn);
243 mpn_tdiv_qr (tp, dummyp, (mp_size_t) 0, xp, xn, pp, pn);
245 tn -= tp[tn - 1] == 0;
246 n_digits_computed = mpn_get_str (tstr, base, tp, tn);
248 exp_in_base = n_digits_computed + e;
251 /* We should normally have computed too many digits. Round the result
252 at the point indicated by n_digits. */
253 if (n_digits_computed > n_digits)
256 /* Round the result. */
257 if (tstr[n_digits] * 2 >= base)
259 n_digits_computed = n_digits;
260 for (i = n_digits - 1;; i--)
269 /* We had something like `bbbbbbb...bd', where 2*d >= base
270 and `b' denotes digit with significance base - 1.
271 This rounds up to `1', increasing the exponent. */
273 n_digits_computed = 1;
281 /* We might have fewer digits than requested as a result of rounding above,
282 (i.e. 0.999999 => 1.0) or because we have a number that simply doesn't
283 need many digits in this base (e.g., 0.125 in base 10). */
284 if (n_digits > n_digits_computed)
285 n_digits = n_digits_computed;
287 /* Remove trailing 0. There can be many zeros. */
288 while (n_digits != 0 && tstr[n_digits - 1] == 0)
291 dp = dbuf + (SIZ(u) < 0);
293 /* Translate to ASCII and copy to result string. */
294 for (i = 0; i < n_digits; i++)
295 dp[i] = num_to_text[tstr[i]];
309 /* If the string was alloced then resize it down to the actual space
313 __GMP_REALLOCATE_FUNC_MAYBE_TYPE (dbuf, alloc_size, n_digits + 1, char);