xref: /freebsd/contrib/llvm-project/libcxx/src/charconv.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
1*349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "charconv"
100b57cec5SDimitry Andric #include <string.h>
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric namespace __itoa
150b57cec5SDimitry Andric {
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric static constexpr char cDigitsLut[200] = {
180b57cec5SDimitry Andric     '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0',
190b57cec5SDimitry Andric     '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3', '1', '4',
200b57cec5SDimitry Andric     '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0', '2', '1', '2',
210b57cec5SDimitry Andric     '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9',
220b57cec5SDimitry Andric     '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3',
230b57cec5SDimitry Andric     '7', '3', '8', '3', '9', '4', '0', '4', '1', '4', '2', '4', '3', '4', '4',
240b57cec5SDimitry Andric     '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', '5', '0', '5', '1', '5',
250b57cec5SDimitry Andric     '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9',
260b57cec5SDimitry Andric     '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6',
270b57cec5SDimitry Andric     '7', '6', '8', '6', '9', '7', '0', '7', '1', '7', '2', '7', '3', '7', '4',
280b57cec5SDimitry Andric     '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8',
290b57cec5SDimitry Andric     '2', '8', '3', '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9',
300b57cec5SDimitry Andric     '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9',
310b57cec5SDimitry Andric     '7', '9', '8', '9', '9'};
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric template <typename T>
340b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY char*
355ffd83dbSDimitry Andric append1(char* buffer, T i) noexcept
360b57cec5SDimitry Andric {
370b57cec5SDimitry Andric     *buffer = '0' + static_cast<char>(i);
380b57cec5SDimitry Andric     return buffer + 1;
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric template <typename T>
420b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY char*
435ffd83dbSDimitry Andric append2(char* buffer, T i) noexcept
440b57cec5SDimitry Andric {
450b57cec5SDimitry Andric     memcpy(buffer, &cDigitsLut[(i)*2], 2);
460b57cec5SDimitry Andric     return buffer + 2;
470b57cec5SDimitry Andric }
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric template <typename T>
500b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY char*
515ffd83dbSDimitry Andric append3(char* buffer, T i) noexcept
520b57cec5SDimitry Andric {
530b57cec5SDimitry Andric     return append2(append1(buffer, (i) / 100), (i) % 100);
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric template <typename T>
570b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY char*
585ffd83dbSDimitry Andric append4(char* buffer, T i) noexcept
590b57cec5SDimitry Andric {
600b57cec5SDimitry Andric     return append2(append2(buffer, (i) / 100), (i) % 100);
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric template <typename T>
640b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY char*
655ffd83dbSDimitry Andric append2_no_zeros(char* buffer, T v) noexcept
660b57cec5SDimitry Andric {
670b57cec5SDimitry Andric     if (v < 10)
680b57cec5SDimitry Andric         return append1(buffer, v);
690b57cec5SDimitry Andric     else
700b57cec5SDimitry Andric         return append2(buffer, v);
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric template <typename T>
740b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY char*
755ffd83dbSDimitry Andric append4_no_zeros(char* buffer, T v) noexcept
760b57cec5SDimitry Andric {
770b57cec5SDimitry Andric     if (v < 100)
780b57cec5SDimitry Andric         return append2_no_zeros(buffer, v);
790b57cec5SDimitry Andric     else if (v < 1000)
800b57cec5SDimitry Andric         return append3(buffer, v);
810b57cec5SDimitry Andric     else
820b57cec5SDimitry Andric         return append4(buffer, v);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric template <typename T>
860b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY char*
875ffd83dbSDimitry Andric append8_no_zeros(char* buffer, T v) noexcept
880b57cec5SDimitry Andric {
890b57cec5SDimitry Andric     if (v < 10000)
900b57cec5SDimitry Andric     {
910b57cec5SDimitry Andric         buffer = append4_no_zeros(buffer, v);
920b57cec5SDimitry Andric     }
930b57cec5SDimitry Andric     else
940b57cec5SDimitry Andric     {
950b57cec5SDimitry Andric         buffer = append4_no_zeros(buffer, v / 10000);
960b57cec5SDimitry Andric         buffer = append4(buffer, v % 10000);
970b57cec5SDimitry Andric     }
980b57cec5SDimitry Andric     return buffer;
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric char*
102fe6060f1SDimitry Andric __u32toa(uint32_t value, char* buffer) noexcept
1030b57cec5SDimitry Andric {
1040b57cec5SDimitry Andric     if (value < 100000000)
1050b57cec5SDimitry Andric     {
1060b57cec5SDimitry Andric         buffer = append8_no_zeros(buffer, value);
1070b57cec5SDimitry Andric     }
1080b57cec5SDimitry Andric     else
1090b57cec5SDimitry Andric     {
1100b57cec5SDimitry Andric         // value = aabbbbcccc in decimal
1110b57cec5SDimitry Andric         const uint32_t a = value / 100000000;  // 1 to 42
1120b57cec5SDimitry Andric         value %= 100000000;
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric         buffer = append2_no_zeros(buffer, a);
1150b57cec5SDimitry Andric         buffer = append4(buffer, value / 10000);
1160b57cec5SDimitry Andric         buffer = append4(buffer, value % 10000);
1170b57cec5SDimitry Andric     }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric     return buffer;
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric char*
123fe6060f1SDimitry Andric __u64toa(uint64_t value, char* buffer) noexcept
1240b57cec5SDimitry Andric {
1250b57cec5SDimitry Andric     if (value < 100000000)
1260b57cec5SDimitry Andric     {
1270b57cec5SDimitry Andric         uint32_t v = static_cast<uint32_t>(value);
1280b57cec5SDimitry Andric         buffer = append8_no_zeros(buffer, v);
1290b57cec5SDimitry Andric     }
1300b57cec5SDimitry Andric     else if (value < 10000000000000000)
1310b57cec5SDimitry Andric     {
1320b57cec5SDimitry Andric         const uint32_t v0 = static_cast<uint32_t>(value / 100000000);
1330b57cec5SDimitry Andric         const uint32_t v1 = static_cast<uint32_t>(value % 100000000);
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric         buffer = append8_no_zeros(buffer, v0);
1360b57cec5SDimitry Andric         buffer = append4(buffer, v1 / 10000);
1370b57cec5SDimitry Andric         buffer = append4(buffer, v1 % 10000);
1380b57cec5SDimitry Andric     }
1390b57cec5SDimitry Andric     else
1400b57cec5SDimitry Andric     {
1410b57cec5SDimitry Andric         const uint32_t a =
1420b57cec5SDimitry Andric             static_cast<uint32_t>(value / 10000000000000000);  // 1 to 1844
1430b57cec5SDimitry Andric         value %= 10000000000000000;
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric         buffer = append4_no_zeros(buffer, a);
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric         const uint32_t v0 = static_cast<uint32_t>(value / 100000000);
1480b57cec5SDimitry Andric         const uint32_t v1 = static_cast<uint32_t>(value % 100000000);
1490b57cec5SDimitry Andric         buffer = append4(buffer, v0 / 10000);
1500b57cec5SDimitry Andric         buffer = append4(buffer, v0 % 10000);
1510b57cec5SDimitry Andric         buffer = append4(buffer, v1 / 10000);
1520b57cec5SDimitry Andric         buffer = append4(buffer, v1 % 10000);
1530b57cec5SDimitry Andric     }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric     return buffer;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric }  // namespace __itoa
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
161