Coverage Report

Created: 2026-09-13 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boost/boost/charconv/detail/integer_search_trees.hpp
Line
Count
Source
1
// Copyright 2023 Matt Borland
2
// Distributed under the Boost Software License, Version 1.0.
3
// https://www.boost.org/LICENSE_1_0.txt
4
5
#ifndef BOOST_CHARCONV_DETAIL_INTEGER_SEARCH_TREES_HPP
6
#define BOOST_CHARCONV_DETAIL_INTEGER_SEARCH_TREES_HPP
7
8
// https://stackoverflow.com/questions/1489830/efficient-way-to-determine-number-of-digits-in-an-integer?page=1&tab=scoredesc#tab-top
9
// https://graphics.stanford.edu/~seander/bithacks.html
10
11
#include <boost/charconv/detail/config.hpp>
12
#include <boost/charconv/detail/emulated128.hpp>
13
#include <limits>
14
#include <array>
15
#include <cstdint>
16
17
namespace boost { namespace charconv { namespace detail {
18
19
// Generic solution
20
template <typename T>
21
BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(T x) noexcept
22
3.96M
{
23
3.96M
    int digits = 0;
24
25
11.8M
    while (x)
26
7.85M
    {
27
7.85M
        x /= static_cast<T>(10);
28
7.85M
        ++digits;
29
7.85M
    }
30
31
3.96M
    return digits;
32
3.96M
}
33
34
template <>
35
BOOST_CHARCONV_HOST_DEVICE BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(std::uint32_t x) noexcept
36
0
{
37
0
    if (x >= UINT32_C(10000))
38
0
    {
39
0
        if (x >= UINT32_C(10000000))
40
0
        {
41
0
            if (x >= UINT32_C(100000000))
42
0
            {
43
0
                if (x >= UINT32_C(1000000000))
44
0
                {
45
0
                    return 10;
46
0
                }
47
0
                return 9;
48
0
            }
49
0
            return 8;
50
0
        }
51
52
0
        else if (x >= UINT32_C(100000))
53
0
        {
54
0
            if (x >= UINT32_C(1000000))
55
0
            {
56
0
                return 7;
57
0
            }
58
0
            return 6;
59
0
        }
60
0
        return 5;
61
0
    }
62
0
    else if (x >= UINT32_C(100))
63
0
    {
64
0
        if (x >= UINT32_C(1000))
65
0
        {
66
0
            return 4;
67
0
        }
68
0
        return 3;
69
0
    }
70
0
    else if (x >= UINT32_C(10))
71
0
    {
72
0
        return 2;
73
0
    }
74
75
0
    return 1;
76
0
}
77
78
template <>
79
BOOST_CHARCONV_HOST_DEVICE BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(std::uint64_t x) noexcept
80
0
{
81
0
    if (x >= UINT64_C(10000000000))
82
0
    {
83
0
        if (x >= UINT64_C(100000000000000))
84
0
        {
85
0
            if (x >= UINT64_C(10000000000000000))
86
0
            {
87
0
                if (x >= UINT64_C(100000000000000000)) 
88
0
                {
89
0
                    if (x >= UINT64_C(1000000000000000000))
90
0
                    {
91
0
                        if (x >= UINT64_C(10000000000000000000))
92
0
                        {
93
0
                            return 20;
94
0
                        }
95
0
                        return 19;
96
0
                    }
97
0
                    return 18;
98
0
                }
99
0
                return 17;
100
0
            }
101
0
            else if (x >= UINT64_C(1000000000000000))
102
0
            {
103
0
                return 16;
104
0
            }
105
0
            return 15;
106
0
        } 
107
0
        if (x >= UINT64_C(1000000000000))
108
0
        {
109
0
            if (x >= UINT64_C(10000000000000))
110
0
            {
111
0
                return 14;
112
0
            }
113
0
            return 13;
114
0
        }
115
0
        if (x >= UINT64_C(100000000000))
116
0
        {
117
0
            return 12;
118
0
        }
119
0
        return 11;
120
0
    }
121
0
    else if (x >= UINT64_C(100000))
122
0
    {
123
0
        if (x >= UINT64_C(10000000))
124
0
        {
125
0
            if (x >= UINT64_C(100000000))
126
0
            {
127
0
                if (x >= UINT64_C(1000000000))
128
0
                {
129
0
                    return 10;
130
0
                }
131
0
                return 9;
132
0
            }
133
0
            return 8;
134
0
        }
135
0
        if (x >= UINT64_C(1000000))
136
0
        {
137
0
            return 7;
138
0
        }
139
0
        return 6;
140
0
    }
141
0
    if (x >= UINT64_C(100))
142
0
    {
143
0
        if (x >= UINT64_C(1000))
144
0
        {
145
0
            if (x >= UINT64_C(10000))
146
0
            {
147
0
                return 5;
148
0
            }
149
0
            return 4;
150
0
        }
151
0
        return 3;
152
0
    }
153
0
    if (x >= UINT64_C(10))
154
0
    {
155
0
        return 2;
156
0
    }
157
0
    return 1;
158
0
}
159
160
#ifdef BOOST_MSVC
161
# pragma warning(push)
162
# pragma warning(disable: 4307) // MSVC 14.1 warns of intergral constant overflow
163
#endif
164
165
BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(uint128 x) noexcept
166
0
{
167
0
    if (x.high == 0)
168
0
    {
169
0
        return num_digits(x.low);
170
0
    }
171
0
172
0
    BOOST_CHARCONV_CXX14_CONSTEXPR_NO_INLINE uint128 digits_39 = static_cast<uint128>(UINT64_C(10000000000000000000)) *
173
0
                                                                 static_cast<uint128>(UINT64_C(10000000000000000000));
174
0
    uint128 current_power_of_10 = digits_39;
175
0
176
0
    for (int i = 39; i > 0; --i)
177
0
    {
178
0
        if (x >= current_power_of_10)
179
0
        {
180
0
            return i;
181
0
        }
182
0
183
0
        current_power_of_10 /= 10U;
184
0
    }
185
0
186
0
    return 1;
187
0
}
188
189
#ifdef BOOST_MSVC
190
# pragma warning(pop)
191
#endif
192
193
#ifdef BOOST_CHARCONV_HAS_INT128
194
static constexpr std::array<std::uint64_t, 20> powers_of_10 =
195
{{
196
    UINT64_C(1), UINT64_C(10), UINT64_C(100), UINT64_C(1000), UINT64_C(10000), UINT64_C(100000), UINT64_C(1000000), 
197
    UINT64_C(10000000), UINT64_C(100000000), UINT64_C(1000000000), UINT64_C(10000000000), UINT64_C(100000000000), 
198
    UINT64_C(1000000000000), UINT64_C(10000000000000), UINT64_C(100000000000000), UINT64_C(1000000000000000), 
199
    UINT64_C(10000000000000000), UINT64_C(100000000000000000), UINT64_C(1000000000000000000), UINT64_C(10000000000000000000)
200
}};
201
202
// Assume that if someone is using 128 bit ints they are favoring the top end of the range
203
// Max value is 340,282,366,920,938,463,463,374,607,431,768,211,455 (39 digits)
204
BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(boost::uint128_type x) noexcept
205
0
{
206
    // There is no literal for boost::uint128_type, so we need to calculate them using the max value of the
207
    // std::uint64_t powers of 10
208
0
    constexpr boost::uint128_type digits_39 = static_cast<boost::uint128_type>(UINT64_C(10000000000000000000)) * 
209
0
                                              static_cast<boost::uint128_type>(UINT64_C(10000000000000000000));
210
211
0
    constexpr boost::uint128_type digits_38 = digits_39 / 10;
212
0
    constexpr boost::uint128_type digits_37 = digits_38 / 10;
213
0
    constexpr boost::uint128_type digits_36 = digits_37 / 10;
214
0
    constexpr boost::uint128_type digits_35 = digits_36 / 10;
215
0
    constexpr boost::uint128_type digits_34 = digits_35 / 10;
216
0
    constexpr boost::uint128_type digits_33 = digits_34 / 10;
217
0
    constexpr boost::uint128_type digits_32 = digits_33 / 10;
218
0
    constexpr boost::uint128_type digits_31 = digits_32 / 10;
219
0
    constexpr boost::uint128_type digits_30 = digits_31 / 10;
220
0
    constexpr boost::uint128_type digits_29 = digits_30 / 10;
221
0
    constexpr boost::uint128_type digits_28 = digits_29 / 10;
222
0
    constexpr boost::uint128_type digits_27 = digits_28 / 10;
223
0
    constexpr boost::uint128_type digits_26 = digits_27 / 10;
224
0
    constexpr boost::uint128_type digits_25 = digits_26 / 10;
225
0
    constexpr boost::uint128_type digits_24 = digits_25 / 10;
226
0
    constexpr boost::uint128_type digits_23 = digits_24 / 10;
227
0
    constexpr boost::uint128_type digits_22 = digits_23 / 10;
228
0
    constexpr boost::uint128_type digits_21 = digits_22 / 10;
229
230
0
    return (x >= digits_39) ? 39 :
231
0
           (x >= digits_38) ? 38 :
232
0
           (x >= digits_37) ? 37 :
233
0
           (x >= digits_36) ? 36 :
234
0
           (x >= digits_35) ? 35 :
235
0
           (x >= digits_34) ? 34 :
236
0
           (x >= digits_33) ? 33 :
237
0
           (x >= digits_32) ? 32 :
238
0
           (x >= digits_31) ? 31 :
239
0
           (x >= digits_30) ? 30 :
240
0
           (x >= digits_29) ? 29 :
241
0
           (x >= digits_28) ? 28 :
242
0
           (x >= digits_27) ? 27 :
243
0
           (x >= digits_26) ? 26 :
244
0
           (x >= digits_25) ? 25 :
245
0
           (x >= digits_24) ? 24 :
246
0
           (x >= digits_23) ? 23 :
247
0
           (x >= digits_22) ? 22 :
248
0
           (x >= digits_21) ? 21 :
249
0
           (x >= powers_of_10[19]) ? 20 :
250
0
           (x >= powers_of_10[18]) ? 19 :
251
0
           (x >= powers_of_10[17]) ? 18 :
252
0
           (x >= powers_of_10[16]) ? 17 :
253
0
           (x >= powers_of_10[15]) ? 16 :
254
0
           (x >= powers_of_10[14]) ? 15 :
255
0
           (x >= powers_of_10[13]) ? 14 :
256
0
           (x >= powers_of_10[12]) ? 13 :
257
0
           (x >= powers_of_10[11]) ? 12 :
258
0
           (x >= powers_of_10[10]) ? 11 :
259
0
           (x >= powers_of_10[9])  ? 10 :
260
0
           (x >= powers_of_10[8])  ?  9 :
261
0
           (x >= powers_of_10[7])  ?  8 :
262
0
           (x >= powers_of_10[6])  ?  7 :
263
0
           (x >= powers_of_10[5])  ?  6 :
264
0
           (x >= powers_of_10[4])  ?  5 :
265
0
           (x >= powers_of_10[3])  ?  4 :
266
0
           (x >= powers_of_10[2])  ?  3 :
267
0
           (x >= powers_of_10[1])  ?  2 :
268
0
           (x >= powers_of_10[0])  ?  1 : 0;
269
0
}
270
#endif
271
272
}}} // Namespace boost::charconv::detail
273
274
#endif // BOOST_CHARCONV_DETAIL_INTEGER_SEARCH_TREES_HPP