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
/*
Copyright (C) 2010, 2011 Sebastian Pancratz
Copyright (C) 2023, 2026 Fredrik Johansson
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "gmpcompat.h"
#include "fmpz.h"
#include "fmpz/impl.h"
#include "mpn_extras.h"
char * fmpz_get_str(char * str, int b, const fmpz_t f)
{
FLINT_ASSERT(b >= 2 && b <= 62);
if (!COEFF_IS_MPZ(*f))
{
fmpz c;
ulong d;
c = *f;
d = FLINT_ABS(c);
/* Need a special case for zero, which may as well handle
single digits. */
if ((slong) d < FLINT_MIN(b, 10))
{
if (str == NULL)
str = flint_malloc(3);
str[0] = '-';
str[0 + (c < 0)] = d + '0';
str[1 + (c < 0)] = '\0';
}
else if (b == 10)
{
unsigned char tmp[FLINT_BITS + 3];
slong i, len;
/* The compiler might generate faster code for 32-bit divisions */
unsigned int dl;
len = 0;
#if FLINT_BITS == 64
while (d >= (UWORD(1) << 32))
{
tmp[len] = d % 10;
d /= 10;
len++;
}
#endif
dl = d;
while (dl != 0)
{
tmp[len] = dl % 10;
dl /= 10;
len++;
}
if (str == NULL)
str = flint_malloc(len + 2);
str[0] = '-';
for (i = 0; i < len; i++)
str[i + (c < 0)] = tmp[len - 1 - i] + '0';
str[len + (c < 0)] = '\0';
return str;
}
else
{
mpz_t z;
z->_mp_d = &d;
z->_mp_alloc = 1;
z->_mp_size = (c > 1) ? 1 : -1;
if (str == NULL)
str = flint_malloc(mpz_sizeinbase(z, b) + 2);
str = mpz_get_str(str, b, z);
}
}
else
{
__mpz_struct * z;
z = COEFF_TO_PTR(*f);
str = flint_mpn_get_str(str, b, z->_mp_d, FLINT_ABS(z->_mp_size), z->_mp_size < 0);
}
return str;
}