language: C++ 4.7.2 (gcc-4.7.2)
date: 897 days 4 hours ago
link:
visibility: public
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
 
#include <iostream>
#include <limits.h>
#include <memory.h>
 
 
const int GROUPING = 4; //1-4 (4 is the fastest but the lookup table takes 40kB)
const int BUFFER_SIZE = 16;
const int DIVISOR = 10*(GROUPING>1?10:1)*(GROUPING>2?10:1)*(GROUPING>3?10:1);
 
char digits[DIVISOR*GROUPING];
void init_digits()
{
        char* p = digits;
        for(int i=0; i<DIVISOR; i++) {
                int tmp = i;
                for(int i2=GROUPING-1; i2>=0; i2--) {
                        *(p+i2) = '0'+tmp%10;
                        tmp /= 10;
                }
                p += GROUPING;
        }
}
int fooinit = (init_digits(),0);
 
std::string itostr(int val)
{
        char buf[BUFFER_SIZE];
        char *it = &buf[BUFFER_SIZE-GROUPING];
 
        if(val>=0) {
                int div = val/DIVISOR;
                while(div) {
                        memcpy(it, &digits[GROUPING*(val-div*DIVISOR)], GROUPING);
                        val = div;
                        it -= GROUPING;
                        div = val/DIVISOR;
                }
                memcpy(it, &digits[GROUPING*val], GROUPING);
 
                if(GROUPING==4)
                        { if(val<1000) { it++; if(val<100) { it++; if(val<10) it++;     } } }
                else if(GROUPING==3)
                        { if(val<100) { it++; if(val<10) it++;  }}
                else if(GROUPING==2)
                        { if(val<10) it++; }
 
        } else {
                int div = val/DIVISOR;
                while(div) {
                        memcpy(it, &digits[-GROUPING*(val-div*DIVISOR)], GROUPING);
                        val = div;
                        it -= GROUPING;
                        div = val/DIVISOR;
                }
                memcpy(it, &digits[-GROUPING*val], GROUPING);
 
                if(GROUPING==4)
                        { if(val>-1000) { it++; if(val>-100) it++; } }
                else if(GROUPING==3)
                        { if(val>-100) it++; }
                else if(GROUPING==2)
                        { }
 
                if(GROUPING==1 || val<=-10)
                        it--;
 
                *it = '-';
        }
 
        return std::string(it,&buf[BUFFER_SIZE]-it);
}
 
std::string itostr(unsigned int val)
{
        char buf[BUFFER_SIZE];
        char *it = &buf[BUFFER_SIZE-GROUPING];
 
        int div = val/DIVISOR;
        while(div) {
                memcpy(it, &digits[GROUPING*(val-div*DIVISOR)], GROUPING);
                val = div;
                it -= GROUPING;
                div = val/DIVISOR;
        }
        memcpy(it, &digits[GROUPING*val], GROUPING);
 
        if(GROUPING==4)
                { if(val<1000) { it++; if(val<100) { it++; if(val<10) it++;     } } }
        else if(GROUPING==3)
                { if(val<100) { it++; if(val<10) it++;  }}
        else if(GROUPING==2)
                { if(val<10) it++; }
 
        return std::string(it,&buf[BUFFER_SIZE]-it);
}
 
 
int main(void)
{
    int i = 0;
    i += itostr(INT_MIN).size();
    i += itostr(INT_MAX).size();
 
    i += itostr(unsigned(INT_MAX) + 1U).size();
    i += itostr(unsigned(~0)).size(); 
 
    char c = CHAR_MIN;
    do {
        i += itostr(c).size();
                i += itostr((11111111 * c)).size();
 
                if (c >= CHAR_MAX) break;
 
        c++;
    } while (1);
 
    while (i < 100000000)
        i += itostr(i).size();
 
    std::cout << i << std::endl;
 
    return i;
}