1#pragma once
2
3#include <cstdint>
4#include <limits>
5
6
7// Also defined in Core/Defines.h
8#if !defined(NO_SANITIZE_UNDEFINED)
9#if defined(__clang__)
10 #define NO_SANITIZE_UNDEFINED __attribute__((__no_sanitize__("undefined")))
11#else
12 #define NO_SANITIZE_UNDEFINED
13#endif
14#endif
15
16
17/// On overlow, the function returns unspecified value.
18inline NO_SANITIZE_UNDEFINED uint64_t intExp2(int x)
19{
20 return 1ULL << x;
21}
22
23inline uint64_t intExp10(int x)
24{
25 if (x < 0)
26 return 0;
27 if (x > 19)
28 return std::numeric_limits<uint64_t>::max();
29
30 static const uint64_t table[20] =
31 {
32 1ULL, 10ULL, 100ULL,
33 1000ULL, 10000ULL, 100000ULL,
34 1000000ULL, 10000000ULL, 100000000ULL,
35 1000000000ULL, 10000000000ULL, 100000000000ULL,
36 1000000000000ULL, 10000000000000ULL, 100000000000000ULL,
37 1000000000000000ULL, 10000000000000000ULL, 100000000000000000ULL,
38 1000000000000000000ULL, 10000000000000000000ULL
39 };
40
41 return table[x];
42}
43
44namespace common
45{
46
47inline int exp10_i32(int x)
48{
49 static const int values[] = {
50 1,
51 10,
52 100,
53 1000,
54 10000,
55 100000,
56 1000000,
57 10000000,
58 100000000,
59 1000000000
60 };
61 return values[x];
62}
63
64inline int64_t exp10_i64(int x)
65{
66 static const int64_t values[] = {
67 1ll,
68 10ll,
69 100ll,
70 1000ll,
71 10000ll,
72 100000ll,
73 1000000ll,
74 10000000ll,
75 100000000ll,
76 1000000000ll,
77 10000000000ll,
78 100000000000ll,
79 1000000000000ll,
80 10000000000000ll,
81 100000000000000ll,
82 1000000000000000ll,
83 10000000000000000ll,
84 100000000000000000ll,
85 1000000000000000000ll
86 };
87 return values[x];
88}
89
90inline __int128 exp10_i128(int x)
91{
92 static const __int128 values[] = {
93 static_cast<__int128>(1ll),
94 static_cast<__int128>(10ll),
95 static_cast<__int128>(100ll),
96 static_cast<__int128>(1000ll),
97 static_cast<__int128>(10000ll),
98 static_cast<__int128>(100000ll),
99 static_cast<__int128>(1000000ll),
100 static_cast<__int128>(10000000ll),
101 static_cast<__int128>(100000000ll),
102 static_cast<__int128>(1000000000ll),
103 static_cast<__int128>(10000000000ll),
104 static_cast<__int128>(100000000000ll),
105 static_cast<__int128>(1000000000000ll),
106 static_cast<__int128>(10000000000000ll),
107 static_cast<__int128>(100000000000000ll),
108 static_cast<__int128>(1000000000000000ll),
109 static_cast<__int128>(10000000000000000ll),
110 static_cast<__int128>(100000000000000000ll),
111 static_cast<__int128>(1000000000000000000ll),
112 static_cast<__int128>(1000000000000000000ll) * 10ll,
113 static_cast<__int128>(1000000000000000000ll) * 100ll,
114 static_cast<__int128>(1000000000000000000ll) * 1000ll,
115 static_cast<__int128>(1000000000000000000ll) * 10000ll,
116 static_cast<__int128>(1000000000000000000ll) * 100000ll,
117 static_cast<__int128>(1000000000000000000ll) * 1000000ll,
118 static_cast<__int128>(1000000000000000000ll) * 10000000ll,
119 static_cast<__int128>(1000000000000000000ll) * 100000000ll,
120 static_cast<__int128>(1000000000000000000ll) * 1000000000ll,
121 static_cast<__int128>(1000000000000000000ll) * 10000000000ll,
122 static_cast<__int128>(1000000000000000000ll) * 100000000000ll,
123 static_cast<__int128>(1000000000000000000ll) * 1000000000000ll,
124 static_cast<__int128>(1000000000000000000ll) * 10000000000000ll,
125 static_cast<__int128>(1000000000000000000ll) * 100000000000000ll,
126 static_cast<__int128>(1000000000000000000ll) * 1000000000000000ll,
127 static_cast<__int128>(1000000000000000000ll) * 10000000000000000ll,
128 static_cast<__int128>(1000000000000000000ll) * 100000000000000000ll,
129 static_cast<__int128>(1000000000000000000ll) * 100000000000000000ll * 10ll,
130 static_cast<__int128>(1000000000000000000ll) * 100000000000000000ll * 100ll,
131 static_cast<__int128>(1000000000000000000ll) * 100000000000000000ll * 1000ll
132 };
133 return values[x];
134}
135
136}
137