Annotation of OpenXM_contrib/gmp/mpfr/set_str_raw.c, Revision 1.1.1.2
1.1 maekawa 1: /* mpfr_set_str_raw -- set a floating-point number from a binary string
2:
1.1.1.2 ! ohara 3: Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
1.1 maekawa 4:
5: This file is part of the MPFR Library.
6:
7: The MPFR Library is free software; you can redistribute it and/or modify
1.1.1.2 ! ohara 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
1.1 maekawa 10: option) any later version.
11:
12: The MPFR Library is distributed in the hope that it will be useful, but
13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2 ! ohara 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1.1 maekawa 15: License for more details.
16:
1.1.1.2 ! ohara 17: You should have received a copy of the GNU Lesser General Public License
1.1 maekawa 18: along with the MPFR 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 <stdio.h>
23: #include <stdlib.h>
24: #include <string.h>
25: #include "gmp.h"
26: #include "gmp-impl.h"
27: #include "longlong.h"
28: #include "mpfr.h"
1.1.1.2 ! ohara 29: #include "mpfr-impl.h"
1.1 maekawa 30:
31: /* Currently the number should be of the form +/- xxxx.xxxxxxEyy, with
32: decimal exponent. The mantissa of x is supposed to be large enough
33: to hold all the bits of str. */
34:
35: void
1.1.1.2 ! ohara 36: mpfr_set_str_raw (mpfr_ptr x, char *str)
1.1 maekawa 37: {
38: char *str2, *str0, negative = 0;
1.1.1.2 ! ohara 39: unsigned long j, l, k = 0, xsize, cnt, alloc; mp_limb_t *xp;
1.1 maekawa 40: long expn = 0, e; char *endstr2;
41:
1.1.1.2 ! ohara 42: xp = MPFR_MANT(x);
! 43: xsize = 1 + (MPFR_PREC(x) - 1) / BITS_PER_MP_LIMB;
! 44: alloc = (strlen(str) + 1) * sizeof(char);
1.1 maekawa 45:
1.1.1.2 ! ohara 46: if (*str == '-')
! 47: {
! 48: negative = 1;
! 49: str++;
! 50: }
! 51: else if (*str == '+')
! 52: str++;
! 53:
! 54: if (*str == 'N')
! 55: {
! 56: MPFR_SET_NAN(x);
! 57: __mpfr_flags |= MPFR_FLAGS_NAN;
! 58: return;
! 59: }
! 60:
! 61: if (*str == 'I')
! 62: {
! 63: MPFR_CLEAR_NAN(x);
! 64: MPFR_SET_INF(x);
! 65: if (MPFR_ISNEG(x) != negative)
! 66: MPFR_CHANGE_SIGN(x);
! 67: return;
! 68: }
! 69:
! 70: MPFR_CLEAR_FLAGS(x);
! 71:
! 72: str0 = str2 = (char *) (*__gmp_allocate_func) (alloc);
1.1 maekawa 73:
74: while (*str == '0') { str++; }
75:
76: while (*str == '0' || *str == '1')
77: { *(str2++) = *(str++); k++; }
78:
79: if (*str == '.')
80: {
81: str++;
82: while (*str == '0' || *str == '1')
83: { *(str2++) = *(str++); }
84:
85: if (*str == '[') { while (*str != ']') str++; }
86: }
87:
88: if (*str == '[') { while (*str != ']') str++; }
89:
90: if (*str == 'e' || *str == 'E')
91: {
92: e = atol(++str); /* signed exponent after 'e' or 'E' */
93: expn = k + e;
94: if (expn < e)
95: fprintf(stderr, "Warning: overflow in exponent in Str -> mpfr\n");
96: }
97: else expn=k;
98:
99: endstr2 = str2;
1.1.1.2 ! ohara 100: l = endstr2 - str0; /* length of mantissa */
! 101: if (l == 0) { /* input is zero */
! 102: MPFR_SET_ZERO(x);
! 103: }
! 104: else {
! 105: l = (l - 1) / BITS_PER_MP_LIMB + 1;
! 106: str2 = str0;
! 107:
! 108: if (l > xsize) {
! 109: fprintf (stderr, "Error: mantissa larger than precision of destination variable in mpfr_set_str_raw\n");
! 110: exit (1);
1.1 maekawa 111: }
112:
1.1.1.2 ! ohara 113: /* str2[0]..endstr2[-1] contains the mantissa */
! 114: for (k = 1; k <= l; k++)
! 115: {
! 116: j = 0;
! 117: xp[xsize - k] = 0;
! 118: while ((str2 < endstr2) && (j < BITS_PER_MP_LIMB))
! 119: {
! 120: xp[xsize - k] = (xp[xsize - k] << 1) + (*str2 - '0');
! 121: str2++; j++;
! 122: }
! 123: xp[xsize - k] <<= (BITS_PER_MP_LIMB - j);
! 124: }
! 125:
! 126: for (; k <= xsize; k++) { xp[xsize - k] = 0; }
! 127:
! 128: count_leading_zeros(cnt, xp[xsize - 1]);
! 129: if (cnt) mpn_lshift(xp, xp, xsize, cnt);
! 130:
! 131: MPFR_EXP(x) = expn - cnt;
! 132: if (MPFR_ISNEG(x) != negative)
! 133: MPFR_CHANGE_SIGN(x);
! 134: }
1.1 maekawa 135:
1.1.1.2 ! ohara 136: (*__gmp_free_func) (str0, alloc);
1.1 maekawa 137:
138: /* May change to take into account : syntax errors, overflow in exponent,
139: string truncated because of size of x */
140: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>