Coverage Report

Created: 2022-05-17 06:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/charconv.cpp
Line
Count
Source (jump to first uncovered line)
1
//===----------------------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include <charconv>
10
#include <string.h>
11
12
#include "include/ryu/digit_table.h"
13
#include "include/to_chars_floating_point.h"
14
15
_LIBCPP_BEGIN_NAMESPACE_STD
16
17
namespace __itoa
18
{
19
20
template <typename T>
21
inline _LIBCPP_INLINE_VISIBILITY char*
22
append1(char* buffer, T i) noexcept
23
440k
{
24
440k
    *buffer = '0' + static_cast<char>(i);
25
440k
    return buffer + 1;
26
440k
}
27
28
template <typename T>
29
inline _LIBCPP_INLINE_VISIBILITY char*
30
append2(char* buffer, T i) noexcept
31
1.08M
{
32
1.08M
    memcpy(buffer, &__DIGIT_TABLE[(i)*2], 2);
33
1.08M
    return buffer + 2;
34
1.08M
}
35
36
template <typename T>
37
inline _LIBCPP_INLINE_VISIBILITY char*
38
append3(char* buffer, T i) noexcept
39
108k
{
40
108k
    return append2(append1(buffer, (i) / 100), (i) % 100);
41
108k
}
42
43
template <typename T>
44
inline _LIBCPP_INLINE_VISIBILITY char*
45
append4(char* buffer, T i) noexcept
46
216k
{
47
216k
    return append2(append2(buffer, (i) / 100), (i) % 100);
48
216k
}
49
50
template <typename T>
51
inline _LIBCPP_INLINE_VISIBILITY char*
52
append2_no_zeros(char* buffer, T v) noexcept
53
874k
{
54
874k
    if (v < 10)
55
332k
        return append1(buffer, v);
56
542k
    else
57
542k
        return append2(buffer, v);
58
874k
}
59
60
template <typename T>
61
inline _LIBCPP_INLINE_VISIBILITY char*
62
append4_no_zeros(char* buffer, T v) noexcept
63
1.09M
{
64
1.09M
    if (v < 100)
65
874k
        return append2_no_zeros(buffer, v);
66
216k
    else if (v < 1000)
67
108k
        return append3(buffer, v);
68
108k
    else
69
108k
        return append4(buffer, v);
70
1.09M
}
71
72
template <typename T>
73
inline _LIBCPP_INLINE_VISIBILITY char*
74
append8_no_zeros(char* buffer, T v) noexcept
75
1.09M
{
76
1.09M
    if (v < 10000)
77
983k
    {
78
983k
        buffer = append4_no_zeros(buffer, v);
79
983k
    }
80
108k
    else
81
108k
    {
82
108k
        buffer = append4_no_zeros(buffer, v / 10000);
83
108k
        buffer = append4(buffer, v % 10000);
84
108k
    }
85
1.09M
    return buffer;
86
1.09M
}
87
88
char*
89
__u32toa(uint32_t value, char* buffer) noexcept
90
1.09M
{
91
1.09M
    if (value < 100000000)
92
1.09M
    {
93
1.09M
        buffer = append8_no_zeros(buffer, value);
94
1.09M
    }
95
0
    else
96
0
    {
97
        // value = aabbbbcccc in decimal
98
0
        const uint32_t a = value / 100000000;  // 1 to 42
99
0
        value %= 100000000;
100
101
0
        buffer = append2_no_zeros(buffer, a);
102
0
        buffer = append4(buffer, value / 10000);
103
0
        buffer = append4(buffer, value % 10000);
104
0
    }
105
106
1.09M
    return buffer;
107
1.09M
}
108
109
char*
110
__u64toa(uint64_t value, char* buffer) noexcept
111
16
{
112
16
    if (value < 100000000)
113
16
    {
114
16
        uint32_t v = static_cast<uint32_t>(value);
115
16
        buffer = append8_no_zeros(buffer, v);
116
16
    }
117
0
    else if (value < 10000000000000000)
118
0
    {
119
0
        const uint32_t v0 = static_cast<uint32_t>(value / 100000000);
120
0
        const uint32_t v1 = static_cast<uint32_t>(value % 100000000);
121
122
0
        buffer = append8_no_zeros(buffer, v0);
123
0
        buffer = append4(buffer, v1 / 10000);
124
0
        buffer = append4(buffer, v1 % 10000);
125
0
    }
126
0
    else
127
0
    {
128
0
        const uint32_t a =
129
0
            static_cast<uint32_t>(value / 10000000000000000);  // 1 to 1844
130
0
        value %= 10000000000000000;
131
132
0
        buffer = append4_no_zeros(buffer, a);
133
134
0
        const uint32_t v0 = static_cast<uint32_t>(value / 100000000);
135
0
        const uint32_t v1 = static_cast<uint32_t>(value % 100000000);
136
0
        buffer = append4(buffer, v0 / 10000);
137
0
        buffer = append4(buffer, v0 % 10000);
138
0
        buffer = append4(buffer, v1 / 10000);
139
0
        buffer = append4(buffer, v1 % 10000);
140
0
    }
141
142
16
    return buffer;
143
16
}
144
145
}  // namespace __itoa
146
147
// The original version of floating-point to_chars was written by Microsoft and
148
// contributed with the following license.
149
150
// Copyright (c) Microsoft Corporation.
151
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
152
153
// This implementation is dedicated to the memory of Mary and Thavatchai.
154
155
0
to_chars_result to_chars(char* __first, char* __last, float __value) {
156
0
  return _Floating_to_chars<_Floating_to_chars_overload::_Plain>(__first, __last, __value, chars_format{}, 0);
157
0
}
158
159
0
to_chars_result to_chars(char* __first, char* __last, double __value) {
160
0
  return _Floating_to_chars<_Floating_to_chars_overload::_Plain>(__first, __last, __value, chars_format{}, 0);
161
0
}
162
163
0
to_chars_result to_chars(char* __first, char* __last, long double __value) {
164
0
  return _Floating_to_chars<_Floating_to_chars_overload::_Plain>(__first, __last, static_cast<double>(__value),
165
0
                                                                 chars_format{}, 0);
166
0
}
167
168
0
to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt) {
169
0
  return _Floating_to_chars<_Floating_to_chars_overload::_Format_only>(__first, __last, __value, __fmt, 0);
170
0
}
171
172
0
to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt) {
173
0
  return _Floating_to_chars<_Floating_to_chars_overload::_Format_only>(__first, __last, __value, __fmt, 0);
174
0
}
175
176
0
to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt) {
177
0
  return _Floating_to_chars<_Floating_to_chars_overload::_Format_only>(__first, __last, static_cast<double>(__value),
178
0
                                                                       __fmt, 0);
179
0
}
180
181
0
to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt, int __precision) {
182
0
  return _Floating_to_chars<_Floating_to_chars_overload::_Format_precision>(__first, __last, __value, __fmt,
183
0
                                                                            __precision);
184
0
}
185
186
0
to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt, int __precision) {
187
0
  return _Floating_to_chars<_Floating_to_chars_overload::_Format_precision>(__first, __last, __value, __fmt,
188
0
                                                                            __precision);
189
0
}
190
191
0
to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt, int __precision) {
192
0
  return _Floating_to_chars<_Floating_to_chars_overload::_Format_precision>(
193
0
      __first, __last, static_cast<double>(__value), __fmt, __precision);
194
0
}
195
196
_LIBCPP_END_NAMESPACE_STD