Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/grisu2/patches/0001-godot-changes.patch
9904 views
1
diff --git a/thirdparty/grisu2/grisu2.h b/thirdparty/grisu2/grisu2.h
2
index 19886cce47f..dbc09755fad 100644
3
--- a/thirdparty/grisu2/grisu2.h
4
+++ b/thirdparty/grisu2/grisu2.h
5
@@ -1,15 +1,12 @@
6
-#ifndef SIMDJSON_SRC_TO_CHARS_CPP
7
-#define SIMDJSON_SRC_TO_CHARS_CPP
8
-
9
-#include <base.h>
10
+#pragma once
11
12
#include <cstring>
13
#include <cstdint>
14
#include <array>
15
#include <cmath>
16
17
-namespace simdjson {
18
-namespace internal {
19
+namespace grisu2 {
20
/*!
21
implements the Grisu2 algorithm for binary to decimal floating-point
22
conversion.
23
@@ -26,7 +23,6 @@ PLDI 2010 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and
24
Accurately", Proceedings of the ACM SIGPLAN 1996 Conference on Programming
25
Language Design and Implementation, PLDI 1996
26
*/
27
-namespace dtoa_impl {
28
29
template <typename Target, typename Source>
30
Target reinterpret_bits(const Source source) {
31
@@ -718,7 +714,7 @@ v = buf * 10^decimal_exponent
32
len is the length of the buffer (number of decimal digits)
33
The buffer must be large enough, i.e. >= max_digits10.
34
*/
35
-inline void grisu2(char *buf, int &len, int &decimal_exponent, diyfp m_minus,
36
+inline void grisu2_core(char *buf, int &len, int &decimal_exponent, diyfp m_minus,
37
diyfp v, diyfp m_plus) {
38
39
// --------(-----------------------+-----------------------)-------- (A)
40
@@ -775,7 +771,7 @@ len is the length of the buffer (number of decimal digits)
41
The buffer must be large enough, i.e. >= max_digits10.
42
*/
43
template <typename FloatType>
44
-void grisu2(char *buf, int &len, int &decimal_exponent, FloatType value) {
45
+void grisu2_wrap(char *buf, int &len, int &decimal_exponent, FloatType value) {
46
static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
47
"internal error: not enough precision");
48
49
@@ -804,7 +800,7 @@ void grisu2(char *buf, int &len, int &decimal_exponent, FloatType value) {
50
const boundaries w = compute_boundaries(value);
51
#endif
52
53
- grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
54
+ grisu2_core(buf, len, decimal_exponent, w.minus, w.w, w.plus);
55
}
56
57
/*!
58
@@ -864,10 +860,7 @@ inline char *format_buffer(char *buf, int len, int decimal_exponent,
59
// len <= max_exp + 2
60
61
std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
62
- // Make it look like a floating-point number (#362, #378)
63
- buf[n + 0] = '.';
64
- buf[n + 1] = '0';
65
- return buf + (static_cast<size_t>(n)) + 2;
66
+ return buf + (static_cast<size_t>(n));
67
}
68
69
if (0 < n && n <= max_exp) {
70
@@ -909,8 +902,6 @@ inline char *format_buffer(char *buf, int len, int decimal_exponent,
71
return append_exponent(buf, n - 1);
72
}
73
74
-} // namespace dtoa_impl
75
-
76
/*!
77
The format of the resulting decimal representation is similar to printf's %g
78
format. Returns an iterator pointing past-the-end of the decimal representation.
79
@@ -918,19 +909,15 @@ format. Returns an iterator pointing past-the-end of the decimal representation.
80
@note The buffer must be large enough.
81
@note The result is NOT null-terminated.
82
*/
83
-char *to_chars(char *first, const char *last, double value) {
84
- static_cast<void>(last); // maybe unused - fix warning
85
+template <typename FloatType>
86
+char *to_chars(char *first, FloatType value) {
87
bool negative = std::signbit(value);
88
if (negative) {
89
value = -value;
90
*first++ = '-';
91
}
92
-
93
if (value == 0) // +-0
94
{
95
- *first++ = '0';
96
- // Make it look like a floating-point number (#362, #378)
97
- *first++ = '.';
98
*first++ = '0';
99
return first;
100
}
101
@@ -940,15 +927,13 @@ char *to_chars(char *first, const char *last, double value) {
102
// len is the length of the buffer, i.e. the number of decimal digits.
103
int len = 0;
104
int decimal_exponent = 0;
105
- dtoa_impl::grisu2(first, len, decimal_exponent, value);
106
+ grisu2_wrap(first, len, decimal_exponent, value);
107
// Format the buffer like printf("%.*g", prec, value)
108
constexpr int kMinExp = -4;
109
constexpr int kMaxExp = std::numeric_limits<double>::digits10;
110
111
- return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp,
112
- kMaxExp);
113
+ return format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
114
}
115
-} // namespace internal
116
-} // namespace simdjson
117
+} // namespace grisu2
118
119
-#endif // SIMDJSON_SRC_TO_CHARS_CPP
120
121