-
Notifications
You must be signed in to change notification settings - Fork 86
/
branchlut2.cpp
83 lines (70 loc) · 1.9 KB
/
branchlut2.cpp
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
#include <stdint.h>
#include "digitslut.h"
#include "test.h"
#define BEGIN2(n) \
do { \
int t = (n); \
if(t < 10) *p++ = '0' + t; \
else { \
t *= 2; \
*p++ = gDigitsLut[t]; \
*p++ = gDigitsLut[t + 1]; \
} \
} while(0)
#define MIDDLE2(n) \
do { \
int t = (n) * 2; \
*p++ = gDigitsLut[t]; \
*p++ = gDigitsLut[t + 1]; \
} while(0)
#define BEGIN4(n) \
do { \
int t4 = (n); \
if(t4 < 100) BEGIN2(t4); \
else { BEGIN2(t4 / 100); MIDDLE2(t4 % 100); } \
} while(0)
#define MIDDLE4(n) \
do { \
int t4 = (n); \
MIDDLE2(t4 / 100); MIDDLE2(t4 % 100); \
} while(0)
#define BEGIN8(n) \
do { \
uint32_t t8 = (n); \
if(t8 < 10000) BEGIN4(t8); \
else { BEGIN4(t8 / 10000); MIDDLE4(t8 % 10000); } \
} while(0)
#define MIDDLE8(n) \
do { \
uint32_t t8 = (n); \
MIDDLE4(t8 / 10000); MIDDLE4(t8 % 10000); \
} while(0)
#define MIDDLE16(n) \
do { \
uint64_t t16 = (n); \
MIDDLE8(t16 / 100000000); MIDDLE8(t16 % 100000000); \
} while(0)
void u32toa_branchlut2(uint32_t x, char* p) {
if(x < 100000000) BEGIN8(x);
else { BEGIN2(x / 100000000); MIDDLE8(x % 100000000); }
*p = 0;
}
void i32toa_branchlut2(int32_t x, char* p) {
uint64_t t;
if(x >= 0) t = x;
else *p++ = '-', t = -uint32_t(x);
u32toa_branchlut2(t, p);
}
void u64toa_branchlut2(uint64_t x, char* p) {
if(x < 100000000) BEGIN8(x);
else if(x < 10000000000000000) { BEGIN8(x / 100000000); MIDDLE8(x % 100000000); }
else { BEGIN4(x / 10000000000000000); MIDDLE16(x % 10000000000000000); }
*p = 0;
}
void i64toa_branchlut2(int64_t x, char* p) {
uint64_t t;
if(x >= 0) t = x;
else *p++ = '-', t = -uint64_t(x);
u64toa_branchlut2(t, p);
}
REGISTER_TEST(branchlut2);