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, 2000 Free Software Foundation,
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or (at your
14 option) any later version.
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
23 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 MA 02111-1307, USA. */
31 mpn_set_str (mp_ptr xp, const unsigned char *str, size_t str_len, int base)
33 mpn_set_str (xp, str, str_len, base)
35 const unsigned char *str;
42 int indigits_per_limb;
45 big_base = __mp_bases[base].big_base;
46 indigits_per_limb = __mp_bases[base].chars_per_limb;
48 /* size = str_len / indigits_per_limb + 1; */
52 if ((base & (base - 1)) == 0)
54 /* The base is a power of 2. Read the input string from
55 least to most significant character/digit. */
57 const unsigned char *s;
59 int bits_per_indigit = big_base;
64 for (s = str + str_len - 1; s >= str; s--)
68 res_digit |= (mp_limb_t) inp_digit << next_bitpos;
69 next_bitpos += bits_per_indigit;
70 if (next_bitpos >= BITS_PER_MP_LIMB)
72 xp[size++] = res_digit;
73 next_bitpos -= BITS_PER_MP_LIMB;
74 res_digit = inp_digit >> (bits_per_indigit - next_bitpos);
79 xp[size++] = res_digit;
83 /* General case. The base is not a power of 2. */
89 for (i = indigits_per_limb; i < str_len; i += indigits_per_limb)
93 { /* This is a common case.
94 Help the compiler to avoid multiplication. */
95 for (j = 1; j < indigits_per_limb; j++)
96 res_digit = res_digit * 10 + *str++;
100 for (j = 1; j < indigits_per_limb; j++)
101 res_digit = res_digit * base + *str++;
114 cy_limb = mpn_mul_1 (xp, xp, size, big_base);
115 cy_limb += mpn_add_1 (xp, xp, size, res_digit);
117 xp[size++] = cy_limb;
124 { /* This is a common case.
125 Help the compiler to avoid multiplication. */
126 for (j = 1; j < str_len - (i - indigits_per_limb); j++)
128 res_digit = res_digit * 10 + *str++;
134 for (j = 1; j < str_len - (i - indigits_per_limb); j++)
136 res_digit = res_digit * base + *str++;
151 cy_limb = mpn_mul_1 (xp, xp, size, big_base);
152 cy_limb += mpn_add_1 (xp, xp, size, res_digit);
154 xp[size++] = cy_limb;