1 /* mpn_set_str (mp_ptr res_ptr, const char *str, size_t str_len, int base)
2 -- Convert a STR_LEN long base BASE byte string pointed to by STR to a
3 limb vector pointed to by RES_PTR. Return the number of limbs in
6 Copyright (C) 1991, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
8 This file is part of the GNU MP Library.
10 The GNU MP Library is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Library General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or (at your
13 option) any later version.
15 The GNU MP Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
18 License for more details.
20 You should have received a copy of the GNU Library General Public License
21 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 MA 02111-1307, USA. */
29 mpn_set_str (xp, str, str_len, base)
31 const unsigned char *str;
37 int indigits_per_limb;
40 big_base = __mp_bases[base].big_base;
41 indigits_per_limb = __mp_bases[base].chars_per_limb;
43 /* size = str_len / indigits_per_limb + 1; */
47 if ((base & (base - 1)) == 0)
49 /* The base is a power of 2. Read the input string from
50 least to most significant character/digit. */
52 const unsigned char *s;
54 int bits_per_indigit = big_base;
59 for (s = str + str_len - 1; s >= str; s--)
63 res_digit |= (mp_limb_t) inp_digit << next_bitpos;
64 next_bitpos += bits_per_indigit;
65 if (next_bitpos >= BITS_PER_MP_LIMB)
67 xp[size++] = res_digit;
68 next_bitpos -= BITS_PER_MP_LIMB;
69 res_digit = inp_digit >> (bits_per_indigit - next_bitpos);
74 xp[size++] = res_digit;
78 /* General case. The base is not a power of 2. */
84 for (i = indigits_per_limb; i < str_len; i += indigits_per_limb)
88 { /* This is a common case.
89 Help the compiler to avoid multiplication. */
90 for (j = 1; j < indigits_per_limb; j++)
91 res_digit = res_digit * 10 + *str++;
95 for (j = 1; j < indigits_per_limb; j++)
96 res_digit = res_digit * base + *str++;
109 cy_limb = mpn_mul_1 (xp, xp, size, big_base);
110 cy_limb += mpn_add_1 (xp, xp, size, res_digit);
112 xp[size++] = cy_limb;
119 { /* This is a common case.
120 Help the compiler to avoid multiplication. */
121 for (j = 1; j < str_len - (i - indigits_per_limb); j++)
123 res_digit = res_digit * 10 + *str++;
129 for (j = 1; j < str_len - (i - indigits_per_limb); j++)
131 res_digit = res_digit * base + *str++;
146 cy_limb = mpn_mul_1 (xp, xp, size, big_base);
147 cy_limb += mpn_add_1 (xp, xp, size, res_digit);
149 xp[size++] = cy_limb;