Coverage Report

Created: 2024-02-25 06:16

/src/gmp-6.2.1/mpn/set_str.c
Line
Count
Source (jump to first uncovered line)
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 limb
3
   vector pointed to by RES_PTR.  Return the number of limbs in RES_PTR.
4
5
   Contributed to the GNU project by Torbjorn Granlund.
6
7
   THE FUNCTIONS IN THIS FILE, EXCEPT mpn_set_str, ARE INTERNAL WITH MUTABLE
8
   INTERFACES.  IT IS ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.
9
   IN FACT, IT IS ALMOST GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A
10
   FUTURE GNU MP RELEASE.
11
12
Copyright 1991-2017 Free Software Foundation, Inc.
13
14
This file is part of the GNU MP Library.
15
16
The GNU MP Library is free software; you can redistribute it and/or modify
17
it under the terms of either:
18
19
  * the GNU Lesser General Public License as published by the Free
20
    Software Foundation; either version 3 of the License, or (at your
21
    option) any later version.
22
23
or
24
25
  * the GNU General Public License as published by the Free Software
26
    Foundation; either version 2 of the License, or (at your option) any
27
    later version.
28
29
or both in parallel, as here.
30
31
The GNU MP Library is distributed in the hope that it will be useful, but
32
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
33
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
34
for more details.
35
36
You should have received copies of the GNU General Public License and the
37
GNU Lesser General Public License along with the GNU MP Library.  If not,
38
see https://www.gnu.org/licenses/.  */
39
40
41
/* TODO:
42
43
      Perhaps do not compute the highest power?
44
      Instead, multiply twice by the 2nd highest power:
45
46
         _______
47
        |_______|  hp
48
        |_______|  pow
49
       _______________
50
      |_______________|  final result
51
52
53
         _______
54
        |_______|  hp
55
      |___|  pow[-1]
56
     ___________
57
    |___________|  intermediate result
58
      |___|  pow[-1]
59
       _______________
60
      |_______________|  final result
61
62
      Generalizing that idea, perhaps we should make powtab contain successive
63
      cubes, not squares.
64
*/
65
66
#include "gmp-impl.h"
67
68
mp_size_t
69
mpn_set_str (mp_ptr rp, const unsigned char *str, size_t str_len, int base)
70
4.02k
{
71
4.02k
  if (POW2_P (base))
72
0
    {
73
      /* The base is a power of 2.  Read the input string from least to most
74
   significant character/digit.  */
75
76
0
      const unsigned char *s;
77
0
      int next_bitpos;
78
0
      mp_limb_t res_digit;
79
0
      mp_size_t size;
80
0
      int bits_per_indigit = mp_bases[base].big_base;
81
82
0
      size = 0;
83
0
      res_digit = 0;
84
0
      next_bitpos = 0;
85
86
0
      for (s = str + str_len - 1; s >= str; s--)
87
0
  {
88
0
    int inp_digit = *s;
89
90
0
    res_digit |= ((mp_limb_t) inp_digit << next_bitpos) & GMP_NUMB_MASK;
91
0
    next_bitpos += bits_per_indigit;
92
0
    if (next_bitpos >= GMP_NUMB_BITS)
93
0
      {
94
0
        rp[size++] = res_digit;
95
0
        next_bitpos -= GMP_NUMB_BITS;
96
0
        res_digit = inp_digit >> (bits_per_indigit - next_bitpos);
97
0
      }
98
0
  }
99
100
0
      if (res_digit != 0)
101
0
  rp[size++] = res_digit;
102
0
      return size;
103
0
    }
104
105
4.02k
  if (BELOW_THRESHOLD (str_len, SET_STR_PRECOMPUTE_THRESHOLD))
106
3.01k
    return mpn_bc_set_str (rp, str, str_len, base);
107
1.01k
  else
108
1.01k
    {
109
1.01k
      mp_ptr powtab_mem, tp;
110
1.01k
      powers_t powtab[GMP_LIMB_BITS];
111
1.01k
      int chars_per_limb;
112
1.01k
      powers_t *pt;
113
1.01k
      size_t n_pows;
114
1.01k
      mp_size_t size;
115
1.01k
      mp_size_t un;
116
1.01k
      TMP_DECL;
117
118
1.01k
      TMP_MARK;
119
120
1.01k
      chars_per_limb = mp_bases[base].chars_per_limb;
121
122
1.01k
      un = str_len / chars_per_limb + 1; /* FIXME: scalar integer division */
123
124
      /* Allocate one large block for the powers of big_base.  */
125
1.01k
      powtab_mem = TMP_BALLOC_LIMBS (mpn_str_powtab_alloc (un));
126
127
1.01k
      n_pows = mpn_compute_powtab (powtab, powtab_mem, un, base);
128
1.01k
      pt = powtab + n_pows;
129
130
1.01k
      tp = TMP_BALLOC_LIMBS (mpn_dc_set_str_itch (un));
131
1.01k
      size = mpn_dc_set_str (rp, str, str_len, pt, tp);
132
133
1.01k
      TMP_FREE;
134
1.01k
      return size;
135
1.01k
    }
136
4.02k
}
137
138
mp_size_t
139
mpn_dc_set_str (mp_ptr rp, const unsigned char *str, size_t str_len,
140
    const powers_t *powtab, mp_ptr tp)
141
37.8k
{
142
37.8k
  size_t len_lo, len_hi;
143
37.8k
  mp_limb_t cy;
144
37.8k
  mp_size_t ln, hn, n, sn;
145
146
37.8k
  len_lo = powtab->digits_in_base;
147
148
37.8k
  if (str_len <= len_lo)
149
0
    {
150
0
      if (BELOW_THRESHOLD (str_len, SET_STR_DC_THRESHOLD))
151
0
  return mpn_bc_set_str (rp, str, str_len, powtab->base);
152
0
      else
153
0
  return mpn_dc_set_str (rp, str, str_len, powtab - 1, tp);
154
0
    }
155
156
37.8k
  len_hi = str_len - len_lo;
157
37.8k
  ASSERT (len_lo >= len_hi);
158
159
37.8k
  if (BELOW_THRESHOLD (len_hi, SET_STR_DC_THRESHOLD))
160
20.0k
    hn = mpn_bc_set_str (tp, str, len_hi, powtab->base);
161
17.8k
  else
162
17.8k
    hn = mpn_dc_set_str (tp, str, len_hi, powtab - 1, rp);
163
164
37.8k
  sn = powtab->shift;
165
166
37.8k
  if (hn == 0)
167
9.51k
    {
168
      /* Zero +1 limb here, to avoid reading an allocated but uninitialised
169
   limb in mpn_incr_u below.  */
170
9.51k
      MPN_ZERO (rp, powtab->n + sn + 1);
171
9.51k
    }
172
28.3k
  else
173
28.3k
    {
174
28.3k
      if (powtab->n > hn)
175
8.46k
  mpn_mul (rp + sn, powtab->p, powtab->n, tp, hn);
176
19.8k
      else
177
19.8k
  mpn_mul (rp + sn, tp, hn, powtab->p, powtab->n);
178
28.3k
      MPN_ZERO (rp, sn);
179
28.3k
    }
180
181
37.8k
  str = str + str_len - len_lo;
182
37.8k
  if (BELOW_THRESHOLD (len_lo, SET_STR_DC_THRESHOLD))
183
18.8k
    ln = mpn_bc_set_str (tp, str, len_lo, powtab->base);
184
19.0k
  else
185
19.0k
    ln = mpn_dc_set_str (tp, str, len_lo, powtab - 1, tp + powtab->n + sn + 1);
186
187
37.8k
  if (ln != 0)
188
28.9k
    {
189
28.9k
      cy = mpn_add_n (rp, rp, tp, ln);
190
28.9k
      mpn_incr_u (rp + ln, cy);
191
28.9k
    }
192
37.8k
  n = hn + powtab->n + sn;
193
37.8k
  return n - (rp[n - 1] == 0);
194
37.8k
}
195
196
mp_size_t
197
mpn_bc_set_str (mp_ptr rp, const unsigned char *str, size_t str_len, int base)
198
41.8k
{
199
41.8k
  mp_size_t size;
200
41.8k
  size_t i;
201
41.8k
  long j;
202
41.8k
  mp_limb_t cy_limb;
203
204
41.8k
  mp_limb_t big_base;
205
41.8k
  int chars_per_limb;
206
41.8k
  mp_limb_t res_digit;
207
208
41.8k
  ASSERT (base >= 2);
209
41.8k
  ASSERT (base < numberof (mp_bases));
210
41.8k
  ASSERT (str_len >= 1);
211
212
41.8k
  big_base = mp_bases[base].big_base;
213
41.8k
  chars_per_limb = mp_bases[base].chars_per_limb;
214
215
41.8k
  size = 0;
216
353k
  for (i = chars_per_limb; i < str_len; i += chars_per_limb)
217
311k
    {
218
311k
      res_digit = *str++;
219
311k
      if (base == 10)
220
311k
  { /* This is a common case.
221
       Help the compiler to avoid multiplication.  */
222
5.91M
    for (j = MP_BASES_CHARS_PER_LIMB_10 - 1; j != 0; j--)
223
5.60M
      res_digit = res_digit * 10 + *str++;
224
311k
  }
225
0
      else
226
0
  {
227
0
    for (j = chars_per_limb - 1; j != 0; j--)
228
0
      res_digit = res_digit * base + *str++;
229
0
  }
230
231
311k
      if (size == 0)
232
189k
  {
233
189k
    if (res_digit != 0)
234
21.6k
      {
235
21.6k
        rp[0] = res_digit;
236
21.6k
        size = 1;
237
21.6k
      }
238
189k
  }
239
121k
      else
240
121k
  {
241
121k
#if HAVE_NATIVE_mpn_mul_1c
242
121k
    cy_limb = mpn_mul_1c (rp, rp, size, big_base, res_digit);
243
#else
244
    cy_limb = mpn_mul_1 (rp, rp, size, big_base);
245
    cy_limb += mpn_add_1 (rp, rp, size, res_digit);
246
#endif
247
121k
    if (cy_limb != 0)
248
121k
      rp[size++] = cy_limb;
249
121k
  }
250
311k
    }
251
252
41.8k
  big_base = base;
253
41.8k
  res_digit = *str++;
254
41.8k
  if (base == 10)
255
41.8k
    { /* This is a common case.
256
   Help the compiler to avoid multiplication.  */
257
747k
      for (j = str_len - (i - MP_BASES_CHARS_PER_LIMB_10) - 1; j > 0; j--)
258
705k
  {
259
705k
    res_digit = res_digit * 10 + *str++;
260
705k
    big_base *= 10;
261
705k
  }
262
41.8k
    }
263
0
  else
264
0
    {
265
0
      for (j = str_len - (i - chars_per_limb) - 1; j > 0; j--)
266
0
  {
267
0
    res_digit = res_digit * base + *str++;
268
0
    big_base *= base;
269
0
  }
270
0
    }
271
272
41.8k
  if (size == 0)
273
20.2k
    {
274
20.2k
      if (res_digit != 0)
275
1.77k
  {
276
1.77k
    rp[0] = res_digit;
277
1.77k
    size = 1;
278
1.77k
  }
279
20.2k
    }
280
21.6k
  else
281
21.6k
    {
282
21.6k
#if HAVE_NATIVE_mpn_mul_1c
283
21.6k
      cy_limb = mpn_mul_1c (rp, rp, size, big_base, res_digit);
284
#else
285
      cy_limb = mpn_mul_1 (rp, rp, size, big_base);
286
      cy_limb += mpn_add_1 (rp, rp, size, res_digit);
287
#endif
288
21.6k
      if (cy_limb != 0)
289
20.3k
  rp[size++] = cy_limb;
290
21.6k
    }
291
41.8k
  return size;
292
41.8k
}