summaryrefslogtreecommitdiff
path: root/mpf/set_str.c
blob: 306dd27db5a5a13ce5891c70d816c7cb93d44a5f (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
/* mpf_set_str (dest, string, base) -- Convert the string STRING
   in base BASE to a float in dest.  If BASE is zero, the leading characters
   of STRING is used to figure out the base.

Copyright 1993, 1994, 1995, 1996, 1997, 2000, 2001 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 "config.h"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#if HAVE_LOCALE_H
#include <locale.h>    /* for localeconv */
#endif

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

static int
digit_value_in_base (int c, int base)
{
  int digit;

  if (isdigit (c))
    digit = c - '0';
  else if (islower (c))
    digit = c - 'a' + 10;
  else if (isupper (c))
    digit = c - 'A' + 10;
  else
    return -1;

  if (digit < base)
    return digit;
  return -1;
}

#if HAVE_LOCALECONV
/* Avoid memcmp for the usual case that point is one character.  Don't
   bother with str+1,point+1,pointlen-1 since those offsets would add to the
   code size.  */
#define POINTCMP_FAST(c, str)                                           \
  ((c) == point0                                                        \
   && (pointlen == 1 || memcmp (str, point, pointlen) == 0))
#define POINTCMP_SMALL(c, str)   (memcmp (str, point, pointlen) == 0)
#define POINTLEN                 (pointlen)
#else
#define POINTCMP_FAST(c, str)    ((c) == '.')
#define POINTCMP_SMALL(c, str)   ((c) == '.')
#define POINTLEN                 1
#endif

int
mpf_set_str (mpf_ptr x, const char *str, int base)
{
  size_t str_size;
  char *s, *begs;
  size_t i;
  mp_size_t xsize;
  int c;
  int negative;
  char *dotpos = 0;
  int expflag;
  int decimal_exponent_flag;
#if HAVE_LOCALECONV
  const char  *point = localeconv()->decimal_point;
  size_t      pointlen = strlen (point);
  int         point0 = point[0];
#endif
  TMP_DECL (marker);

  TMP_MARK (marker);

  c = *str;

  /* Skip whitespace.  */
  while (isspace (c))
    c = *++str;

  negative = 0;
  if (c == '-')
    {
      negative = 1;
      c = *++str;
    }

  decimal_exponent_flag = base < 0;
  base = ABS (base);

  /* require at least one digit, possibly after an initial decimal point */
  if (digit_value_in_base (c, base == 0 ? 10 : base) < 0)
    {
      if (! POINTCMP_SMALL (c, str))
        return -1;
      if (digit_value_in_base (str[POINTLEN], base == 0 ? 10 : base) < 0)
        return -1;
    }

  /* If BASE is 0, try to find out the base by looking at the initial
     characters.  */
  if (base == 0)
    {
      base = 10;
#if 0
      if (c == '0')
	{
	  base = 8;
	  if (str[1] == 'x' || str[1] == 'X')
	    {
	      base = 16;
	      str += 2;
	      c = *str;
	    }
	}
#endif
    }

  expflag = 0;
  str_size = strlen (str);
  for (i = 0; i < str_size; i++)
    {
      c = str[i];
      if (c == '@' || (base <= 10 && (c == 'e' || c == 'E')))
	{
	  expflag = 1;
	  str_size = i;
	  break;
	}

    }

  s = begs = (char *) TMP_ALLOC (str_size + 1);

  for (i = 0; i < str_size; i++)
    {
      c = *str;
      if (!isspace (c))
	{
	  int dig;

	  if (POINTCMP_FAST (c, str))
            {
	      if (dotpos != 0)
		{
		  TMP_FREE (marker);
		  return -1;
		}
	      dotpos = s;
              str += POINTLEN - 1;
              i += POINTLEN - 1;
	    }
	  else
	    {
	      dig = digit_value_in_base (c, base);
	      if (dig < 0)
		{
		  TMP_FREE (marker);
		  return -1;
		}
	      *s++ = dig;
	    }
	}
      c = *++str;
    }

  str_size = s - begs;

  xsize = str_size / __mp_bases[base].chars_per_limb + 2;
  {
    long exp_in_base;
    mp_size_t ralloc, rsize, msize;
    int cnt, i;
    mp_ptr mp, tp, rp;
    mp_exp_t exp_in_limbs;
    mp_size_t prec = x->_mp_prec + 1;
    int divflag;
    mp_size_t madj, radj;

    mp = (mp_ptr) TMP_ALLOC (xsize * BYTES_PER_MP_LIMB);
    msize = mpn_set_str (mp, (unsigned char *) begs, str_size, base);

    if (msize == 0)
      {
	x->_mp_size = 0;
	x->_mp_exp = 0;
	TMP_FREE (marker);
	return 0;
      }

    madj = 0;
    /* Ignore excess limbs in MP,MSIZE.  */
    if (msize > prec)
      {
	madj = msize - prec;
	mp += msize - prec;
	msize = prec;
      }

    if (expflag != 0)
      exp_in_base = strtol (str + 1, (char **) 0,
			    decimal_exponent_flag ? 10 : base);
    else
      exp_in_base = 0;
    if (dotpos != 0)
      exp_in_base -= s - dotpos;
    divflag = exp_in_base < 0;
    exp_in_base = ABS (exp_in_base);

    if (exp_in_base == 0)
      {
	MPN_COPY (x->_mp_d, mp, msize);
	x->_mp_size = negative ? -msize : msize;
	x->_mp_exp = msize + madj;
	TMP_FREE (marker);
	return 0;
      }

    radj = 0;			/* counts number of ignored low limbs in r */

    ralloc = 2 * (prec + 1);
    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 = GMP_LIMB_BITS - cnt - 2; i >= 0; i--)
      {
	mpn_sqr_n (tp, rp, rsize);
	rsize = 2 * rsize;
	rsize -= tp[rsize - 1] == 0;
	radj <<= 1;

	if (rsize > prec)
	  {
	    radj += rsize - prec;
	    MPN_COPY_INCR (rp, tp + rsize - prec, prec);
	    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;
	  }
      }

    if (rsize > prec)
      {
	radj += rsize - prec;
	rp += rsize - prec;
	rsize = prec;
      }

    if (divflag)
      {
	mp_ptr qp;
	mp_limb_t qlimb;
	if (msize < rsize)
	  {
	    /* Pad out MP,MSIZE for current divrem semantics.  */
	    mp_ptr tmp = (mp_ptr) TMP_ALLOC ((rsize + 1) * BYTES_PER_MP_LIMB);
	    MPN_ZERO (tmp, rsize - msize);
	    MPN_COPY (tmp + rsize - msize, mp, msize);
	    mp = tmp;
	    madj -= rsize - msize;
	    msize = rsize;
	  }
	if ((rp[rsize - 1] & GMP_NUMB_HIGHBIT) == 0)
	  {
	    mp_limb_t cy;
            count_leading_zeros (cnt, rp[rsize - 1]);
	    cnt -= GMP_NAIL_BITS;
	    mpn_lshift (rp, rp, rsize, cnt);
	    cy = mpn_lshift (mp, mp, msize, cnt);
	    if (cy)
	      mp[msize++] = cy;
	  }

	qp = (mp_ptr) TMP_ALLOC ((prec + 1) * BYTES_PER_MP_LIMB);
	qlimb = mpn_divrem (qp, prec - (msize - rsize), mp, msize, rp, rsize);
	tp = qp;
	exp_in_limbs = qlimb + (msize - rsize) + (madj - radj);
	rsize = prec;
	if (qlimb != 0)
	  {
	    tp[prec] = qlimb;
	    /* Skip the least significant limb not to overrun the destination
	       variable.  */
	    tp++;
	  }
      }
    else
      {
	tp = (mp_ptr) TMP_ALLOC ((rsize + msize) * BYTES_PER_MP_LIMB);
	if (rsize > msize)
	  mpn_mul (tp, rp, rsize, mp, msize);
	else
	  mpn_mul (tp, mp, msize, rp, rsize);
	rsize += msize;
	rsize -= tp[rsize - 1] == 0;
	exp_in_limbs = rsize + madj + radj;

	if (rsize > prec)
	  {
	    tp += rsize - prec;
	    rsize = prec;
	    exp_in_limbs += 0;
	  }
      }

    MPN_COPY (x->_mp_d, tp, rsize);
    x->_mp_size = negative ? -rsize : rsize;
    x->_mp_exp = exp_in_limbs;
    TMP_FREE (marker);
    return 0;
  }
}