-
Notifications
You must be signed in to change notification settings - Fork 0
/
any_int.c
86 lines (69 loc) · 1.75 KB
/
any_int.c
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
/* This file is part of the module ANY_INT.
Written by Dick Grune, [email protected]
$Id: any_int.c,v 1.4 2017-01-22 14:50:00 Gebruiker Exp $
*/
#include "any_int.h"
#define N_INDEPENDENT_CALLS 12
#define MAX_ANY_UINT_DIGITS 40 /* good for 128 bits, including sign */
/*Library module source prelude */
#undef _ANY_UINT_CODE_
#ifndef lint
#define _ANY_UINT_CODE_
#endif
#ifdef LIB
#define _ANY_UINT_CODE_
#endif
#ifdef _ANY_UINT_CODE_
/* Library module source code */
/* circular list of buffers */
static char buff[N_INDEPENDENT_CALLS][MAX_ANY_UINT_DIGITS+1];
static int next_buff_cnt = 0;
static char *
next_buff(void) {
if (next_buff_cnt == N_INDEPENDENT_CALLS) next_buff_cnt = 0;
return buff[next_buff_cnt++];
}
static const char *
int2string(vlong_uint val, int neg, int size) {
char *res = next_buff() + MAX_ANY_UINT_DIGITS; /* end of new buffer */
*res = '\0'; /* insert EOS */
/* protect size */
if (size < 0 || size > MAX_ANY_UINT_DIGITS) size = 0;
do { /* one decimal character, the first always */
*--res = "0123456789ABCDEF"[val % 10];
size--;
val = val / 10;
} while (val > 0);
if (neg) {
*--res = '-';
size--;
}
while (size > 0) { /* fill up to size */
*--res = ' ';
size--;
}
return res;
}
const char * /* transient * N_INDEPENDENT_CALLS */
any_int2string(vlong_int val, int size) {
int neg = 0;
if (val < 0) {
val = - val;
neg = 1;
}
return int2string((vlong_uint)val, neg, size);
}
const char * /* transient * N_INDEPENDENT_CALLS */
any_uint2string(vlong_uint val, int size) {
return int2string(val, 0, size);
}
/* End library module source code */
#endif /* _ANY_UINT_CODE_ */
#ifdef lint
static void
satisfy_lint(void *x) {
any_int2string(0, 0);
any_uint2string(0, 0);
satisfy_lint(x);
}
#endif /* lint */