summaryrefslogtreecommitdiff
path: root/mpf/get_str.c
blob: a2c53a35d0b66c881173e44a41b2ea8623689f2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* mpf_get_str (digit_ptr, exp, base, n_digits, a) -- Convert the floating
  point number A to a base BASE number and store N_DIGITS raw digits at
  DIGIT_PTR, and the base BASE exponent in the word pointed to by EXP.  For
  example, the number 3.1416 would be returned as "31416" in DIGIT_PTR and
  1 in EXP.

Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000 Free Software Foundation,
Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include "gmp.h"
#include "gmp-impl.h"
#include "longlong.h"

/*
  The conversion routine works like this:

  1. If U >= 1, compute U' = U / base**n, where n is chosen such that U' is
     the largest number smaller than 1.
  2. Else, if U < 1, compute U' = U * base**n, where n is chosen such that U'
     is the largest number smaller than 1.
  3. Convert U' (by repeatedly multiplying it by base).  This process can
     easily be interrupted when the needed number of digits are generated.
*/

char *
#if __STDC__
mpf_get_str (char *digit_ptr, mp_exp_t *exp, int base, size_t n_digits, mpf_srcptr u)
#else
mpf_get_str (digit_ptr, exp, base, n_digits, u)
     char *digit_ptr;
     mp_exp_t *exp;
     int base;
     size_t n_digits;
     mpf_srcptr u;
#endif
{
  mp_ptr up;
  mp_size_t usize;
  mp_exp_t uexp;
  mp_size_t prec;
  unsigned char *str;
  char *num_to_text;
  mp_ptr rp;
  mp_size_t rsize;
  mp_limb_t big_base;
  size_t digits_computed_so_far;
  int dig_per_u;
  unsigned char *tstr;
  mp_exp_t exp_in_base;
  int cnt;
  TMP_DECL (marker);

  TMP_MARK (marker);
  usize = u->_mp_size;
  uexp = u->_mp_exp;
  prec = u->_mp_prec + 1;

  if (base >= 0)
    {
      if (base == 0)
	base = 10;
      num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
    }
  else
    {
      base = -base;
      num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    }

  /* Don't compute more digits than U can accurately represent.
     Also, if 0 digits were requested, give *exactly* as many digits
     as can be accurately represented.  */
  {
    size_t max_digits = 2 + (size_t) (((prec - 2) * BITS_PER_MP_LIMB)
				      * __mp_bases[base].chars_per_bit_exactly);
    if (n_digits == 0 || n_digits > max_digits)
      n_digits = max_digits;
#if 0
/* This seems to work, but check it better before enabling it.  */
    else
      /* Limit the computation precision if only a limited digits are
	 desired.  We could probably decrease both this, and avoid the +1
	 for setting prec above.  */
      prec = 2 + (mp_size_t)
	(n_digits / (BITS_PER_MP_LIMB * __mp_bases[base].chars_per_bit_exactly));
#endif
  }

  if (digit_ptr == 0)
    {
      /* We didn't get a string from the user.  Allocate one (and return
	 a pointer to it) with space for `-' and terminating null.  */
      digit_ptr = (char *) (*_mp_allocate_func) (n_digits + 2);
    }

  if (usize == 0)
    {
      *exp = 0;
      *digit_ptr = 0;
      return digit_ptr;
    }

  str = (unsigned char *) digit_ptr;

  if (usize < 0)
    {
      *digit_ptr = '-';
      str++;
      usize = -usize;
    }

  up = PTR (u);

  if (uexp > 0)
    {
      /* U >= 1.  Compute U' = U / base**n, where n is chosen such that U' < 1.  */
      mp_size_t ralloc;
      mp_ptr tp;
      int i;

      /* Limit the number of digits to develop for small integers.  */
#if 0
      if (exp_in_base < n_digits)
	n_digits = exp_in_base;
#endif

      count_leading_zeros (cnt, up[usize - 1]);
      exp_in_base = (((double) uexp * BITS_PER_MP_LIMB - cnt)
		     * __mp_bases[base].chars_per_bit_exactly);
      exp_in_base += 1;

      ralloc = (prec + 1) * 2;
      rp = (mp_ptr) TMP_ALLOC (ralloc * BYTES_PER_MP_LIMB);
      tp = (mp_ptr) TMP_ALLOC (ralloc * BYTES_PER_MP_LIMB);

      rp[0] = base;
      rsize = 1;
      count_leading_zeros (cnt, exp_in_base);
      for (i = BITS_PER_MP_LIMB - cnt - 2; i >= 0; i--)
	{
	  mpn_mul_n (tp, rp, rp, rsize);
	  rsize = 2 * rsize;
	  rsize -= tp[rsize - 1] == 0;

	  if (rsize > prec)
	    {
	      MPN_COPY (rp, tp + rsize - prec, prec + 1);
	      rsize = prec;
	    }
	  else
	    MP_PTR_SWAP (rp, tp);

	  if (((exp_in_base >> i) & 1) != 0)
	    {
	      mp_limb_t cy;
	      cy = mpn_mul_1 (rp, rp, rsize, (mp_limb_t) base);
	      rp[rsize] = cy;
	      rsize += cy != 0;
	    }
	}

      count_leading_zeros (cnt, rp[rsize - 1]);
      if (cnt != 0)
	{
	  mpn_lshift (rp, rp, rsize, cnt);

	  if (usize < rsize)
	    {
	      /* Pad out U to the size of R while shifting it.
		 (Reuse temporary space at tp.)  */
	      mp_limb_t cy;

	      MPN_ZERO (tp, rsize - usize);
	      cy = mpn_lshift (tp + rsize - usize, up, usize, cnt);
	      up = tp;
	      usize = rsize;
	      if (cy)
		up[usize++] = cy;
	      ASSERT_ALWAYS (usize <= ralloc);	/* sufficient space? */
	    }
	  else
	    {
	      /* Copy U to temporary space.  */
	      /* FIXME: Allocate more space for tp above, and reuse it here.  */
	      mp_limb_t cy;
	      mp_ptr tup = (mp_ptr) TMP_ALLOC ((usize + 1) * BYTES_PER_MP_LIMB);

	      cy = mpn_lshift (tup, up, usize, cnt);
	      up = tup;
	      if (cy)
		up[usize++] = cy;
	    }
	}
      else
	{
	  if (usize < rsize)
	    {
	      /* Pad out U to the size of R.  (Reuse temporary space at tp.)  */
	      MPN_ZERO (tp, rsize - usize);
	      MPN_COPY (tp + rsize - usize, up, usize);
	      up = tp;
	      usize = rsize;
	    }
	  else
	    {
	      /* Copy U to temporary space.  */
	      mp_ptr tmp = (mp_ptr) TMP_ALLOC (usize * BYTES_PER_MP_LIMB);
	      MPN_COPY (tmp, up, usize);
	      up = tmp;
	    }
	}

      {
	mp_ptr qp;
	qp = (mp_ptr) TMP_ALLOC (prec * BYTES_PER_MP_LIMB);
	mpn_divrem (qp, prec - (usize - rsize), up, usize, rp, rsize);
	rsize = prec;
	rp = qp;
      }
    }
  else
    {
      /* U < 1.  Compute U' = U * base**n, where n is chosen such that U' is
	 the greatest number that still satisfies U' < 1.  */
      mp_size_t ralloc;
      mp_ptr tp;
      int i;

      uexp = -uexp;
      count_leading_zeros (cnt, up[usize - 1]);
      exp_in_base = (((double) uexp * BITS_PER_MP_LIMB + cnt - 1)
		     * __mp_bases[base].chars_per_bit_exactly);
      if (exp_in_base < 0)
	exp_in_base = 0;

      if (exp_in_base != 0)
	{
	  ralloc = (prec + 1) * 2;
	  rp = (mp_ptr) TMP_ALLOC (ralloc * BYTES_PER_MP_LIMB);
	  tp = (mp_ptr) TMP_ALLOC (ralloc * BYTES_PER_MP_LIMB);

	  rp[0] = base;
	  rsize = 1;
	  count_leading_zeros (cnt, exp_in_base);
	  for (i = BITS_PER_MP_LIMB - cnt - 2; i >= 0; i--)
	    {
	      mpn_mul_n (tp, rp, rp, rsize);
	      rsize = 2 * rsize;
	      rsize -= tp[rsize - 1] == 0;
	      if (rsize > prec)
		{
		  MPN_COPY (rp, tp + rsize - prec, prec + 1);
		  rsize = prec;
		}
	      else
		MP_PTR_SWAP (rp, tp);

	      if (((exp_in_base >> i) & 1) != 0)
		{
		  mp_limb_t cy;
		  cy = mpn_mul_1 (rp, rp, rsize, (mp_limb_t) base);
		  rp[rsize] = cy;
		  rsize += cy != 0;
		}
	    }

	  {
	    mp_limb_t cy;
	    tp = (mp_ptr) TMP_ALLOC ((rsize + usize) * BYTES_PER_MP_LIMB);
	    if (rsize > usize)
	      cy = mpn_mul (tp, rp, rsize, up, usize);
	    else
	      cy = mpn_mul (tp, up, usize, rp, rsize);
	    rsize += usize;
	    rsize -= cy == 0;
	    rp = tp;
	  }
	  exp_in_base = -exp_in_base;
	}
      else
	{
	  rp = (mp_ptr) TMP_ALLOC (usize * BYTES_PER_MP_LIMB);
	  MPN_COPY (rp, up, usize);
	  rsize = usize;
	}
    }

  big_base = __mp_bases[base].big_base;
  dig_per_u = __mp_bases[base].chars_per_limb;

  /* Hack for correctly (although not optimally) converting to bases that are
     powers of 2.  If we deem it important, we could handle powers of 2 by
     shifting and masking (just like mpn_get_str).  */
  if (big_base < 10)		/* logarithm of base when power of two */
    {
      int logbase = big_base;
      if (dig_per_u * logbase == BITS_PER_MP_LIMB)
	dig_per_u--;
      big_base = (mp_limb_t) 1 << (dig_per_u * logbase);
      /* fall out to general code... */
    }

  /* Allocate temporary digit space.  We can't put digits directly in the user
     area, since we generate more digits than requested.  (We allocate
     BITS_PER_MP_LIMB + 1 extra bytes because of the digit block nature of the
     conversion.)  */
  tstr = (unsigned char *) TMP_ALLOC (n_digits + BITS_PER_MP_LIMB + 1);

  for (digits_computed_so_far = 0; digits_computed_so_far <= n_digits;
       digits_computed_so_far += dig_per_u)
    {
      mp_limb_t cy;
      /* For speed: skip trailing zeroes.  */
      if (rp[0] == 0)
	{
	  rp++;
	  rsize--;
	  if (rsize == 0)
	    {
	      n_digits = digits_computed_so_far;
	      break;
	    }
	}

      cy = mpn_mul_1 (rp, rp, rsize, big_base);

      ASSERT_ALWAYS (! (digits_computed_so_far == 0 && cy == 0));

      /* Convert N1 from BIG_BASE to a string of digits in BASE
	 using single precision operations.  */
      {
	int i;
	unsigned char *s = tstr + digits_computed_so_far + dig_per_u;
	for (i = dig_per_u - 1; i >= 0; i--)
	  {
	    *--s = cy % base;
	    cy /= base;
	  }
      }
    }

  /* We can have at most two leading 0.  Remove them.  */
  if (tstr[0] == 0)
    {
      tstr++;
      digits_computed_so_far--;
      exp_in_base--;

      if (tstr[0] == 0)
	{
	  tstr++;
	  digits_computed_so_far--;
	  exp_in_base--;

	  if (tstr[0] == 0)
	    abort ();
	}
    }

  {
    size_t i;

    /* We should normally have computed too many digits.  Round the result
       at the point indicated by n_digits.  */
    if (digits_computed_so_far > n_digits)
      {
	/* Round the result.  */
	if (tstr[n_digits] * 2 >= base)
	  {
	    digits_computed_so_far = n_digits;
	    for (i = n_digits - 1;; i--)
	      {
		unsigned int x;
		x = ++(tstr[i]);
		if (x != base)
		  break;
		digits_computed_so_far--;
		if (i == 0)
		  {
		    /* We had something like `9999999...9d', where 2*d >= base.
		       This rounds up to `1', increasing the exponent.  */
		    tstr[0] = 1;
		    digits_computed_so_far = 1;
		    exp_in_base++;
		    break;
		  }
	      }
	  }
      }

    /* We might have fewer digits than requested as a result of rounding above,
       (i.e. 0.999999 => 1.0) or because we have a number that simply doesn't
       need many digits in this base (i.e., 0.125 in base 10).  */
    if (n_digits > digits_computed_so_far)
      n_digits = digits_computed_so_far;

    /* Remove trailing 0.  There can be many zeros.  */
    while (n_digits != 0 && tstr[n_digits - 1] == 0)
      n_digits--;

    /* Translate to ascii and null-terminate.  */
    for (i = 0; i < n_digits; i++)
      *str++ = num_to_text[tstr[i]];
  }
  *str = 0;
  *exp = exp_in_base;
  TMP_FREE (marker);
  return digit_ptr;
}