Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/include/ryu/ryu.h
Line
Count
Source (jump to first uncovered line)
1
// -*- C++ -*-
2
//===----------------------------------------------------------------------===//
3
//
4
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5
// See https://llvm.org/LICENSE.txt for license information.
6
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7
//
8
//===----------------------------------------------------------------------===//
9
10
// Copyright (c) Microsoft Corporation.
11
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12
13
14
// Copyright 2018 Ulf Adams
15
// Copyright (c) Microsoft Corporation. All rights reserved.
16
17
// Boost Software License - Version 1.0 - August 17th, 2003
18
19
// Permission is hereby granted, free of charge, to any person or organization
20
// obtaining a copy of the software and accompanying documentation covered by
21
// this license (the "Software") to use, reproduce, display, distribute,
22
// execute, and transmit the Software, and to prepare derivative works of the
23
// Software, and to permit third-parties to whom the Software is furnished to
24
// do so, all subject to the following:
25
26
// The copyright notices in the Software and this entire statement, including
27
// the above license grant, this restriction and the following disclaimer,
28
// must be included in all copies of the Software, in whole or in part, and
29
// all derivative works of the Software, unless such copies or derivative
30
// works are solely in the form of machine-executable object code generated by
31
// a source language processor.
32
33
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
36
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
37
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
38
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
39
// DEALINGS IN THE SOFTWARE.
40
41
#ifndef _LIBCPP_SRC_INCLUDE_RYU_RYU_H
42
#define _LIBCPP_SRC_INCLUDE_RYU_RYU_H
43
44
// Avoid formatting to keep the changes with the original code minimal.
45
// clang-format off
46
47
#include "__charconv/chars_format.h"
48
#include "__charconv/to_chars_result.h"
49
#include "__config"
50
#include "__debug"
51
#include "__errc"
52
#include "cstdint"
53
#include "cstring"
54
#include "type_traits"
55
#include "include/ryu/f2s.h"
56
#include "include/ryu/d2s.h"
57
#include "include/ryu/d2fixed.h"
58
59
#if defined(_M_X64) && defined(_LIBCPP_COMPILER_MSVC)
60
#include <intrin0.h> // for _umul128() and __shiftright128()
61
#endif // defined(_M_X64) && defined(_LIBCPP_COMPILER_MSVC)
62
63
#if defined(_WIN64) || defined(_M_AMD64) || defined(__x86_64__) ||  defined(__aarch64__)
64
#define _LIBCPP_64_BIT
65
#endif
66
67
_LIBCPP_BEGIN_NAMESPACE_STD
68
69
// https://github.com/ulfjack/ryu/tree/59661c3/ryu
70
71
#if !defined(_LIBCPP_COMPILER_MSVC)
72
0
_LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward64(unsigned long* __index, unsigned long long __mask) {
73
0
  if (__mask == 0) {
74
0
    return false;
75
0
  }
76
0
  *__index = __builtin_ctzll(__mask);
77
0
  return true;
78
0
}
79
80
0
_LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward(unsigned long* __index, unsigned int __mask) {
81
0
  if (__mask == 0) {
82
0
    return false;
83
0
  }
84
0
  *__index = __builtin_ctz(__mask);
85
0
  return true;
86
0
}
87
#endif  // _LIBCPP_COMPILER_MSVC
88
89
template <class _Floating>
90
[[nodiscard]] to_chars_result _Floating_to_chars_ryu(
91
0
    char* const _First, char* const _Last, const _Floating _Value, const chars_format _Fmt) noexcept {
92
0
    if constexpr (_IsSame<_Floating, float>::value) {
93
0
        return __f2s_buffered_n(_First, _Last, _Value, _Fmt);
94
0
    } else {
95
0
        return __d2s_buffered_n(_First, _Last, _Value, _Fmt);
96
0
    }
97
0
}
Unexecuted instantiation: std::__1::to_chars_result std::__1::_Floating_to_chars_ryu<float>(char*, char*, float, std::__1::chars_format)
Unexecuted instantiation: std::__1::to_chars_result std::__1::_Floating_to_chars_ryu<double>(char*, char*, double, std::__1::chars_format)
98
99
template <class _Floating>
100
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI to_chars_result _Floating_to_chars_scientific_precision(
101
0
    char* const _First, char* const _Last, const _Floating _Value, int _Precision) noexcept {
102
103
    // C11 7.21.6.1 "The fprintf function"/5:
104
    // "A negative precision argument is taken as if the precision were omitted."
105
    // /8: "e,E [...] if the precision is missing, it is taken as 6"
106
107
0
    if (_Precision < 0) {
108
0
        _Precision = 6;
109
0
    } else if (_Precision < 1'000'000'000) { // Match ' to fix compilation with GCC in C++11 mode
110
        // _Precision is ok.
111
0
    } else {
112
        // Avoid integer overflow.
113
        // (This defensive check is slightly nonconformant; it can be carefully improved in the future.)
114
0
        return {_Last, errc::value_too_large};
115
0
    }
116
117
0
    return __d2exp_buffered_n(_First, _Last, _Value, static_cast<uint32_t>(_Precision));
118
0
}
Unexecuted instantiation: std::__1::to_chars_result std::__1::_Floating_to_chars_scientific_precision<float>(char*, char*, float, int)
Unexecuted instantiation: std::__1::to_chars_result std::__1::_Floating_to_chars_scientific_precision<double>(char*, char*, double, int)
119
120
template <class _Floating>
121
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI to_chars_result _Floating_to_chars_fixed_precision(
122
0
    char* const _First, char* const _Last, const _Floating _Value, int _Precision) noexcept {
123
124
    // C11 7.21.6.1 "The fprintf function"/5:
125
    // "A negative precision argument is taken as if the precision were omitted."
126
    // /8: "f,F [...] If the precision is missing, it is taken as 6"
127
128
0
    if (_Precision < 0) {
129
0
        _Precision = 6;
130
0
    } else if (_Precision < 1'000'000'000) { // Match ' to fix compilation with GCC in C++11 mode
131
        // _Precision is ok.
132
0
    } else {
133
        // Avoid integer overflow.
134
        // (This defensive check is slightly nonconformant; it can be carefully improved in the future.)
135
0
        return {_Last, errc::value_too_large};
136
0
    }
137
138
0
    return __d2fixed_buffered_n(_First, _Last, _Value, static_cast<uint32_t>(_Precision));
139
0
}
Unexecuted instantiation: std::__1::to_chars_result std::__1::_Floating_to_chars_fixed_precision<float>(char*, char*, float, int)
Unexecuted instantiation: std::__1::to_chars_result std::__1::_Floating_to_chars_fixed_precision<double>(char*, char*, double, int)
140
141
#undef _LIBCPP_64_BIT
142
#undef _LIBCPP_INTRINSIC128
143
144
_LIBCPP_END_NAMESPACE_STD
145
146
// clang-format on
147
148
#endif // _LIBCPP_SRC_INCLUDE_RYU_RYU_H