yeptris 0.6.64.0-riscv64-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/README.adoc +150 -0
  3. data/ext/build_windows_native.rb +60 -0
  4. data/ext/libyeptris/extconf.rb +57 -0
  5. data/ext/yeptris_native/cbor_ruby.c +198 -0
  6. data/ext/yeptris_native/extconf.rb +99 -0
  7. data/ext/yeptris_native/json_ruby.c +414 -0
  8. data/ext/yeptris_native/version.h +29 -0
  9. data/ext/yeptris_native/yeptris_native.c +507 -0
  10. data/lib/yeptris/3.3/native.so +0 -0
  11. data/lib/yeptris/cbor.rb +169 -0
  12. data/lib/yeptris/document.rb +319 -0
  13. data/lib/yeptris/ffi.rb +572 -0
  14. data/lib/yeptris/json/descriptor.rb +266 -0
  15. data/lib/yeptris/json.rb +502 -0
  16. data/lib/yeptris/materializer.rb +389 -0
  17. data/lib/yeptris/node.rb +482 -0
  18. data/lib/yeptris/psych/class_loader.rb +24 -0
  19. data/lib/yeptris/psych/coder_shim.rb +55 -0
  20. data/lib/yeptris/psych/drop_in.rb +63 -0
  21. data/lib/yeptris/psych/encodable.rb +14 -0
  22. data/lib/yeptris/psych/handler.rb +95 -0
  23. data/lib/yeptris/psych/parser.rb +114 -0
  24. data/lib/yeptris/psych/scalar_scanner.rb +149 -0
  25. data/lib/yeptris/psych/tree_builder.rb +119 -0
  26. data/lib/yeptris/psych/visitors.rb +670 -0
  27. data/lib/yeptris/psych.rb +796 -0
  28. data/lib/yeptris/schema.rb +253 -0
  29. data/lib/yeptris/validation.rb +320 -0
  30. data/lib/yeptris/valueml.rb +477 -0
  31. data/lib/yeptris/yaml/descriptor.rb +56 -0
  32. data/lib/yeptris/yaml.rb +668 -0
  33. data/lib/yeptris.rb +130 -0
  34. data/libyeptris.so +0 -0
  35. data/vendor/libyeptris/CMakeLists.txt +280 -0
  36. data/vendor/libyeptris/cmake/yeptris-config.cmake.in +5 -0
  37. data/vendor/libyeptris/cmake/yeptris.pc.in +11 -0
  38. data/vendor/libyeptris/src/CMakeLists.txt +183 -0
  39. data/vendor/libyeptris/src/include/yeptris/api.h +32 -0
  40. data/vendor/libyeptris/src/include/yeptris/cbor.h +174 -0
  41. data/vendor/libyeptris/src/include/yeptris/dom.h +196 -0
  42. data/vendor/libyeptris/src/include/yeptris/emit.h +128 -0
  43. data/vendor/libyeptris/src/include/yeptris/error.h +42 -0
  44. data/vendor/libyeptris/src/include/yeptris/events.h +171 -0
  45. data/vendor/libyeptris/src/include/yeptris/json.h +57 -0
  46. data/vendor/libyeptris/src/include/yeptris/json.hpp +258 -0
  47. data/vendor/libyeptris/src/include/yeptris/jsonc_compat.h +115 -0
  48. data/vendor/libyeptris/src/include/yeptris/marshal.h +75 -0
  49. data/vendor/libyeptris/src/include/yeptris/parse.h +121 -0
  50. data/vendor/libyeptris/src/include/yeptris/plan.h +100 -0
  51. data/vendor/libyeptris/src/include/yeptris/resolve.h +81 -0
  52. data/vendor/libyeptris/src/include/yeptris/schema.h +121 -0
  53. data/vendor/libyeptris/src/include/yeptris/tape.h +128 -0
  54. data/vendor/libyeptris/src/include/yeptris/types.h +38 -0
  55. data/vendor/libyeptris/src/include/yeptris/validate.h +401 -0
  56. data/vendor/libyeptris/src/include/yeptris/values.h +108 -0
  57. data/vendor/libyeptris/src/include/yeptris/version.h.in +29 -0
  58. data/vendor/libyeptris/src/include/yeptris/visit.h +77 -0
  59. data/vendor/libyeptris/src/include/yeptris/yajl_compat.h +135 -0
  60. data/vendor/libyeptris/src/include/yeptris.h +24 -0
  61. data/vendor/libyeptris/src/yeptris/build.c +393 -0
  62. data/vendor/libyeptris/src/yeptris/cbor/cbor.h +35 -0
  63. data/vendor/libyeptris/src/yeptris/cbor/decode.c +1054 -0
  64. data/vendor/libyeptris/src/yeptris/cbor/encode.c +836 -0
  65. data/vendor/libyeptris/src/yeptris/cbor/encode_priv.h +32 -0
  66. data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +982 -0
  67. data/vendor/libyeptris/src/yeptris/cbor/sink.h +36 -0
  68. data/vendor/libyeptris/src/yeptris/common/chartype.c +41 -0
  69. data/vendor/libyeptris/src/yeptris/common/chartype.h +77 -0
  70. data/vendor/libyeptris/src/yeptris/common/cpu.c +81 -0
  71. data/vendor/libyeptris/src/yeptris/common/cpu.h +40 -0
  72. data/vendor/libyeptris/src/yeptris/common/error.c +65 -0
  73. data/vendor/libyeptris/src/yeptris/common/error.h +81 -0
  74. data/vendor/libyeptris/src/yeptris/common/mutex.h +45 -0
  75. data/vendor/libyeptris/src/yeptris/common/nametab.c +275 -0
  76. data/vendor/libyeptris/src/yeptris/common/nametab.h +68 -0
  77. data/vendor/libyeptris/src/yeptris/common/port.h +74 -0
  78. data/vendor/libyeptris/src/yeptris/common/simd_text.c +49 -0
  79. data/vendor/libyeptris/src/yeptris/common/simd_text.h +342 -0
  80. data/vendor/libyeptris/src/yeptris/common/simd_text_avx2.c +672 -0
  81. data/vendor/libyeptris/src/yeptris/common/simd_text_neon.c +793 -0
  82. data/vendor/libyeptris/src/yeptris/common/simd_text_scalar.c +324 -0
  83. data/vendor/libyeptris/src/yeptris/common/string_view.c +34 -0
  84. data/vendor/libyeptris/src/yeptris/common/string_view.h +93 -0
  85. data/vendor/libyeptris/src/yeptris/doc.h +86 -0
  86. data/vendor/libyeptris/src/yeptris/dom/dom.c +1312 -0
  87. data/vendor/libyeptris/src/yeptris/dom/dom.h +332 -0
  88. data/vendor/libyeptris/src/yeptris/dom/hpool.c +107 -0
  89. data/vendor/libyeptris/src/yeptris/dom/mapindex.c +173 -0
  90. data/vendor/libyeptris/src/yeptris/dom/mapindex.h +61 -0
  91. data/vendor/libyeptris/src/yeptris/dom/mutate.c +431 -0
  92. data/vendor/libyeptris/src/yeptris/dom/ytape.c +2323 -0
  93. data/vendor/libyeptris/src/yeptris/dom/ytape.h +96 -0
  94. data/vendor/libyeptris/src/yeptris/emit/emit.c +670 -0
  95. data/vendor/libyeptris/src/yeptris/emit/float/api.h +43 -0
  96. data/vendor/libyeptris/src/yeptris/emit/float/dragon.c +405 -0
  97. data/vendor/libyeptris/src/yeptris/emit/float/floatint.h +231 -0
  98. data/vendor/libyeptris/src/yeptris/emit/float/fpconv.c +428 -0
  99. data/vendor/libyeptris/src/yeptris/emit/float/print.c +396 -0
  100. data/vendor/libyeptris/src/yeptris/emit/style.c +21 -0
  101. data/vendor/libyeptris/src/yeptris/emit/style.h +32 -0
  102. data/vendor/libyeptris/src/yeptris/emit/writer.c +1951 -0
  103. data/vendor/libyeptris/src/yeptris/emit/writer.h +111 -0
  104. data/vendor/libyeptris/src/yeptris/encoding/bom.c +31 -0
  105. data/vendor/libyeptris/src/yeptris/encoding/encoding.h +63 -0
  106. data/vendor/libyeptris/src/yeptris/encoding/transcode.c +157 -0
  107. data/vendor/libyeptris/src/yeptris/encoding/utf8_validate.c +178 -0
  108. data/vendor/libyeptris/src/yeptris/events/capture.c +166 -0
  109. data/vendor/libyeptris/src/yeptris/events/capture.h +39 -0
  110. data/vendor/libyeptris/src/yeptris/events/iterparse.c +298 -0
  111. data/vendor/libyeptris/src/yeptris/events/pull.c +117 -0
  112. data/vendor/libyeptris/src/yeptris/events/push.c +151 -0
  113. data/vendor/libyeptris/src/yeptris/events/recorder.c +144 -0
  114. data/vendor/libyeptris/src/yeptris/events/values.c +630 -0
  115. data/vendor/libyeptris/src/yeptris/events/values_priv.h +53 -0
  116. data/vendor/libyeptris/src/yeptris/events/yaml_compat.c +86 -0
  117. data/vendor/libyeptris/src/yeptris/events/yaml_compat.h +90 -0
  118. data/vendor/libyeptris/src/yeptris/jsonapi/jsonc_compat.c +687 -0
  119. data/vendor/libyeptris/src/yeptris/jsonapi/yajl_compat.c +728 -0
  120. data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
  121. data/vendor/libyeptris/src/yeptris/marshal.c +988 -0
  122. data/vendor/libyeptris/src/yeptris/memory/allocator.c +20 -0
  123. data/vendor/libyeptris/src/yeptris/memory/allocator.h +43 -0
  124. data/vendor/libyeptris/src/yeptris/memory/arena.c +150 -0
  125. data/vendor/libyeptris/src/yeptris/memory/arena.h +52 -0
  126. data/vendor/libyeptris/src/yeptris/memory/pool.c +133 -0
  127. data/vendor/libyeptris/src/yeptris/memory/pool.h +36 -0
  128. data/vendor/libyeptris/src/yeptris/parse/engine.c +6528 -0
  129. data/vendor/libyeptris/src/yeptris/parse/engine.h +96 -0
  130. data/vendor/libyeptris/src/yeptris/parse/events.h +137 -0
  131. data/vendor/libyeptris/src/yeptris/parse/numbers.c +374 -0
  132. data/vendor/libyeptris/src/yeptris/parse/numbers.h +33 -0
  133. data/vendor/libyeptris/src/yeptris/parse/scalars.c +435 -0
  134. data/vendor/libyeptris/src/yeptris/parse/scalars.h +87 -0
  135. data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +358 -0
  136. data/vendor/libyeptris/src/yeptris/parse.c +1074 -0
  137. data/vendor/libyeptris/src/yeptris/plan.c +824 -0
  138. data/vendor/libyeptris/src/yeptris/resolve/compat11.c +339 -0
  139. data/vendor/libyeptris/src/yeptris/resolve/core12.c +153 -0
  140. data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
  141. data/vendor/libyeptris/src/yeptris/resolve/json12.c +109 -0
  142. data/vendor/libyeptris/src/yeptris/resolve/resolver.h +103 -0
  143. data/vendor/libyeptris/src/yeptris/resolve/tags.c +250 -0
  144. data/vendor/libyeptris/src/yeptris/scan/json.c +839 -0
  145. data/vendor/libyeptris/src/yeptris/scan/json.h +116 -0
  146. data/vendor/libyeptris/src/yeptris/scan/scan.c +862 -0
  147. data/vendor/libyeptris/src/yeptris/scan/scan.h +177 -0
  148. data/vendor/libyeptris/src/yeptris/schema.c +309 -0
  149. data/vendor/libyeptris/src/yeptris/tape.c +2375 -0
  150. data/vendor/libyeptris/src/yeptris/tape_in.h +39 -0
  151. data/vendor/libyeptris/src/yeptris/validate/compile.c +306 -0
  152. data/vendor/libyeptris/src/yeptris/validate/internal.h +347 -0
  153. data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +1922 -0
  154. data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
  155. data/vendor/libyeptris/src/yeptris/validate/regexp.c +1600 -0
  156. data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
  157. data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
  158. data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
  159. data/vendor/libyeptris/src/yeptris/validate/validate.c +1043 -0
  160. data/vendor/libyeptris/src/yeptris/version.c +7 -0
  161. data/vendor/libyeptris/src/yeptris/visit/dom_visit.c +115 -0
  162. data/vendor/libyeptris/src/yeptris/visit/json_visit.c +293 -0
  163. data/vendor/libyeptris/src/yeptris/visit/yaml_visit.c +127 -0
  164. metadata +221 -0
@@ -0,0 +1,428 @@
1
+ /* fpconv.c — the json-gem float byte shape (yeptris#651).
2
+ *
3
+ * Vendored from night-shift/fpconv in the variant carried by the Ruby
4
+ * json package (ruby/json ext/json/ext/vendor/fpconv.c), which appends
5
+ * ".0" to integral floats. JSON.generate on json >= 2.10 emits floats
6
+ * through exactly this code, so byte parity for the JSON writer
7
+ * (emit_canonical_scalar's JSON route) requires the same digits AND
8
+ * the same emit windows: grisu2 is not always-shortest, so the
9
+ * clean-room shortest printer cannot be reshaped to match.
10
+ *
11
+ * Boost Software License - Version 1.0 - August 17th, 2003.
12
+ * Permission is hereby granted, free of charge, to any person or
13
+ * organization obtaining a copy of this software (see
14
+ * https://www.boost.org/LICENSE_1_0.txt). Attribution preserved per
15
+ * the vendoring discipline (cf. the ryu port plan).
16
+ */
17
+
18
+ #include <stdbool.h>
19
+ #include <stdint.h>
20
+ #include <string.h>
21
+
22
+ #include "emit/float/api.h"
23
+
24
+ #define npowers 87
25
+ #define steppowers 8
26
+ #define firstpower -348 /* 10 ^ -348 */
27
+
28
+ #define expmax -32
29
+ #define expmin -60
30
+
31
+ typedef struct Fp {
32
+ uint64_t frac;
33
+ int exp;
34
+ } Fp;
35
+
36
+ static const Fp powers_ten[] = {
37
+ {18054884314459144840U, -1220}, {13451937075301367670U, -1193}, {10022474136428063862U, -1166},
38
+ {14934650266808366570U, -1140}, {11127181549972568877U, -1113}, {16580792590934885855U, -1087},
39
+ {12353653155963782858U, -1060}, {18408377700990114895U, -1034}, {13715310171984221708U, -1007},
40
+ {10218702384817765436U, -980}, {15227053142812498563U, -954}, {11345038669416679861U, -927},
41
+ {16905424996341287883U, -901}, {12595523146049147757U, -874}, {9384396036005875287U, -847},
42
+ {13983839803942852151U, -821}, {10418772551374772303U, -794}, {15525180923007089351U, -768},
43
+ {11567161174868858868U, -741}, {17236413322193710309U, -715}, {12842128665889583758U, -688},
44
+ {9568131466127621947U, -661}, {14257626930069360058U, -635}, {10622759856335341974U, -608},
45
+ {15829145694278690180U, -582}, {11793632577567316726U, -555}, {17573882009934360870U, -529},
46
+ {13093562431584567480U, -502}, {9755464219737475723U, -475}, {14536774485912137811U, -449},
47
+ {10830740992659433045U, -422}, {16139061738043178685U, -396}, {12024538023802026127U, -369},
48
+ {17917957937422433684U, -343}, {13349918974505688015U, -316}, {9946464728195732843U, -289},
49
+ {14821387422376473014U, -263}, {11042794154864902060U, -236}, {16455045573212060422U, -210},
50
+ {12259964326927110867U, -183}, {18268770466636286478U, -157}, {13611294676837538539U, -130},
51
+ {10141204801825835212U, -103}, {15111572745182864684U, -77}, {11258999068426240000U, -50},
52
+ {16777216000000000000U, -24}, {12500000000000000000U, 3}, {9313225746154785156U, 30},
53
+ {13877787807814456755U, 56}, {10339757656912845936U, 83}, {15407439555097886824U, 109},
54
+ {11479437019748901445U, 136}, {17105694144590052135U, 162}, {12744735289059618216U, 189},
55
+ {9495567745759798747U, 216}, {14149498560666738074U, 242}, {10542197943230523224U, 269},
56
+ {15709099088952724970U, 295}, {11704190886730495818U, 322}, {17440603504673385349U, 348},
57
+ {12994262207056124023U, 375}, {9681479787123295682U, 402}, {14426529090290212157U, 428},
58
+ {10748601772107342003U, 455}, {16016664761464807395U, 481}, {11933345169920330789U, 508},
59
+ {17782069995880619868U, 534}, {13248674568444952270U, 561}, {9871031767461413346U, 588},
60
+ {14708983551653345445U, 614}, {10959046745042015199U, 641}, {16330252207878254650U, 667},
61
+ {12166986024289022870U, 694}, {18130221999122236476U, 720}, {13508068024458167312U, 747},
62
+ {10064294952495520794U, 774}, {14996968138956309548U, 800}, {11173611982879273257U, 827},
63
+ {16649979327439178909U, 853}, {12405201291620119593U, 880}, {9242595204427927429U, 907},
64
+ {13772540099066387757U, 933}, {10261342003245940623U, 960}, {15290591125556738113U, 986},
65
+ {11392378155556871081U, 1013}, {16975966327722178521U, 1039}, {12648080533535911531U, 1066}};
66
+
67
+ static Fp find_cachedpow10(int exp, int* k) {
68
+ const double one_log_ten = 0.30102999566398114;
69
+
70
+ int approx = (int)(-(exp + npowers) * one_log_ten);
71
+ int idx = (approx - firstpower) / steppowers;
72
+
73
+ while (1) {
74
+ int current = exp + powers_ten[idx].exp + 64;
75
+
76
+ if (current < expmin) {
77
+ idx++;
78
+ continue;
79
+ }
80
+
81
+ if (current > expmax) {
82
+ idx--;
83
+ continue;
84
+ }
85
+
86
+ *k = (firstpower + idx * steppowers);
87
+
88
+ return powers_ten[idx];
89
+ }
90
+ }
91
+
92
+ #define fracmask 0x000FFFFFFFFFFFFFU
93
+ #define expmask 0x7FF0000000000000U
94
+ #define hiddenbit 0x0010000000000000U
95
+ #define signmask 0x8000000000000000U
96
+ #define expbias (1023 + 52)
97
+
98
+ #define absv(n) ((n) < 0 ? -(n) : (n))
99
+ #define minv(a, b) ((a) < (b) ? (a) : (b))
100
+
101
+ static const uint64_t tens[] = {10000000000000000000U,
102
+ 1000000000000000000U,
103
+ 100000000000000000U,
104
+ 10000000000000000U,
105
+ 1000000000000000U,
106
+ 100000000000000U,
107
+ 10000000000000U,
108
+ 1000000000000U,
109
+ 100000000000U,
110
+ 10000000000U,
111
+ 1000000000U,
112
+ 100000000U,
113
+ 10000000U,
114
+ 1000000U,
115
+ 100000U,
116
+ 10000U,
117
+ 1000U,
118
+ 100U,
119
+ 10U,
120
+ 1U};
121
+
122
+ static uint64_t get_dbits(double d) {
123
+ union {
124
+ double dbl;
125
+ uint64_t i;
126
+ } dbl_bits = {d};
127
+
128
+ return dbl_bits.i;
129
+ }
130
+
131
+ static Fp build_fp(double d) {
132
+ uint64_t bits = get_dbits(d);
133
+
134
+ Fp fp;
135
+ fp.frac = bits & fracmask;
136
+ fp.exp = (bits & expmask) >> 52;
137
+
138
+ if (fp.exp) {
139
+ fp.frac += hiddenbit;
140
+ fp.exp -= expbias;
141
+
142
+ } else {
143
+ fp.exp = -expbias + 1;
144
+ }
145
+
146
+ return fp;
147
+ }
148
+
149
+ static void normalize(Fp* fp) {
150
+ while ((fp->frac & hiddenbit) == 0) {
151
+ fp->frac <<= 1;
152
+ fp->exp--;
153
+ }
154
+
155
+ int shift = 64 - 52 - 1;
156
+ fp->frac <<= shift;
157
+ fp->exp -= shift;
158
+ }
159
+
160
+ static void get_normalized_boundaries(Fp* fp, Fp* lower, Fp* upper) {
161
+ upper->frac = (fp->frac << 1) + 1;
162
+ upper->exp = fp->exp - 1;
163
+
164
+ while ((upper->frac & (hiddenbit << 1)) == 0) {
165
+ upper->frac <<= 1;
166
+ upper->exp--;
167
+ }
168
+
169
+ int u_shift = 64 - 52 - 2;
170
+
171
+ upper->frac <<= u_shift;
172
+ upper->exp = upper->exp - u_shift;
173
+
174
+ int l_shift = fp->frac == hiddenbit ? 2 : 1;
175
+
176
+ lower->frac = (fp->frac << l_shift) - 1;
177
+ lower->exp = fp->exp - l_shift;
178
+
179
+ lower->frac <<= lower->exp - upper->exp;
180
+ lower->exp = upper->exp;
181
+ }
182
+
183
+ static Fp multiply(Fp* a, Fp* b) {
184
+ const uint64_t lomask = 0x00000000FFFFFFFF;
185
+
186
+ uint64_t ah_bl = (a->frac >> 32) * (b->frac & lomask);
187
+ uint64_t al_bh = (a->frac & lomask) * (b->frac >> 32);
188
+ uint64_t al_bl = (a->frac & lomask) * (b->frac & lomask);
189
+ uint64_t ah_bh = (a->frac >> 32) * (b->frac >> 32);
190
+
191
+ uint64_t tmp = (ah_bl & lomask) + (al_bh & lomask) + (al_bl >> 32);
192
+ /* round up */
193
+ tmp += 1U << 31;
194
+
195
+ Fp fp = {ah_bh + (ah_bl >> 32) + (al_bh >> 32) + (tmp >> 32), a->exp + b->exp + 64};
196
+
197
+ return fp;
198
+ }
199
+
200
+ static void round_digit(char* digits, int ndigits, uint64_t delta, uint64_t rem, uint64_t kappa,
201
+ uint64_t frac) {
202
+ while (rem < frac && delta - rem >= kappa &&
203
+ (rem + kappa < frac || frac - rem > rem + kappa - frac)) {
204
+ digits[ndigits - 1]--;
205
+ rem += kappa;
206
+ }
207
+ }
208
+
209
+ static int generate_digits(Fp* fp, Fp* upper, Fp* lower, char* digits, int* K) {
210
+ uint64_t wfrac = upper->frac - fp->frac;
211
+ uint64_t delta = upper->frac - lower->frac;
212
+
213
+ Fp one;
214
+ one.frac = 1ULL << -upper->exp;
215
+ one.exp = upper->exp;
216
+
217
+ uint64_t part1 = upper->frac >> -one.exp;
218
+ uint64_t part2 = upper->frac & (one.frac - 1);
219
+
220
+ int idx = 0, kappa = 10;
221
+ const uint64_t* divp;
222
+ /* 1000000000 */
223
+ for (divp = tens + 10; kappa > 0; divp++) {
224
+ uint64_t div = *divp;
225
+ unsigned digit = (unsigned)(part1 / div);
226
+
227
+ if (digit || idx) {
228
+ digits[idx++] = (char)(digit + '0');
229
+ }
230
+
231
+ part1 -= digit * div;
232
+ kappa--;
233
+
234
+ uint64_t tmp = (part1 << -one.exp) + part2;
235
+ if (tmp <= delta) {
236
+ *K += kappa;
237
+ round_digit(digits, idx, delta, tmp, div << -one.exp, wfrac);
238
+
239
+ return idx;
240
+ }
241
+ }
242
+
243
+ /* 10 */
244
+ const uint64_t* unit = tens + 18;
245
+
246
+ while (true) {
247
+ part2 *= 10;
248
+ delta *= 10;
249
+ kappa--;
250
+
251
+ unsigned digit = (unsigned)(part2 >> -one.exp);
252
+ if (digit || idx) {
253
+ digits[idx++] = (char)(digit + '0');
254
+ }
255
+
256
+ part2 &= one.frac - 1;
257
+ if (part2 < delta) {
258
+ *K += kappa;
259
+ round_digit(digits, idx, delta, part2, one.frac, wfrac * *unit);
260
+
261
+ return idx;
262
+ }
263
+
264
+ unit--;
265
+ }
266
+ }
267
+
268
+ static int grisu2(double d, char* digits, int* K) {
269
+ Fp w = build_fp(d);
270
+
271
+ Fp lower, upper;
272
+ get_normalized_boundaries(&w, &lower, &upper);
273
+
274
+ normalize(&w);
275
+
276
+ int k;
277
+ Fp cp = find_cachedpow10(upper.exp, &k);
278
+
279
+ w = multiply(&w, &cp);
280
+ upper = multiply(&upper, &cp);
281
+ lower = multiply(&lower, &cp);
282
+
283
+ lower.frac++;
284
+ upper.frac--;
285
+
286
+ *K = -k;
287
+
288
+ return generate_digits(&w, &upper, &lower, digits, K);
289
+ }
290
+
291
+ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg) {
292
+ int exp = absv(K + ndigits - 1);
293
+
294
+ if (K >= 0 && exp < 15) {
295
+ memcpy(dest, digits, (size_t)ndigits);
296
+ memset(dest + ndigits, '0', (size_t)K);
297
+
298
+ /* add a .0 to mark this as a float. */
299
+ dest[ndigits + K] = '.';
300
+ dest[ndigits + K + 1] = '0';
301
+
302
+ return ndigits + K + 2;
303
+ }
304
+
305
+ /* write decimal w/o scientific notation */
306
+ if (K < 0 && (K > -7 || exp < 10)) {
307
+ int offset = ndigits - absv(K);
308
+ /* fp < 1.0 -> write leading zero */
309
+ if (offset <= 0) {
310
+ offset = -offset;
311
+ dest[0] = '0';
312
+ dest[1] = '.';
313
+ memset(dest + 2, '0', (size_t)offset);
314
+ memcpy(dest + offset + 2, digits, (size_t)ndigits);
315
+
316
+ return ndigits + 2 + offset;
317
+
318
+ /* fp > 1.0 */
319
+ } else {
320
+ memcpy(dest, digits, (size_t)offset);
321
+ dest[offset] = '.';
322
+ memcpy(dest + offset + 1, digits + offset, (size_t)(ndigits - offset));
323
+
324
+ return ndigits + 1;
325
+ }
326
+ }
327
+
328
+ /* write decimal w/ scientific notation */
329
+ ndigits = minv(ndigits, 18 - neg);
330
+
331
+ int idx = 0;
332
+ dest[idx++] = digits[0];
333
+
334
+ if (ndigits > 1) {
335
+ dest[idx++] = '.';
336
+ memcpy(dest + idx, digits + 1, (size_t)(ndigits - 1));
337
+ idx += ndigits - 1;
338
+ }
339
+
340
+ dest[idx++] = 'e';
341
+
342
+ char sign = K + ndigits - 1 < 0 ? '-' : '+';
343
+ dest[idx++] = sign;
344
+
345
+ int cent = 0;
346
+
347
+ if (exp > 99) {
348
+ cent = exp / 100;
349
+ dest[idx++] = (char)(cent + '0');
350
+ exp -= cent * 100;
351
+ }
352
+ if (exp > 9) {
353
+ int dec = exp / 10;
354
+ dest[idx++] = (char)(dec + '0');
355
+ exp -= dec * 10;
356
+
357
+ } else if (cent) {
358
+ dest[idx++] = '0';
359
+ }
360
+
361
+ dest[idx++] = (char)(exp % 10 + '0');
362
+
363
+ return idx;
364
+ }
365
+
366
+ static int filter_special(double fp, char* dest) {
367
+ if (fp == 0.0) {
368
+ dest[0] = '0';
369
+ dest[1] = '.';
370
+ dest[2] = '0';
371
+ return 3;
372
+ }
373
+
374
+ uint64_t bits = get_dbits(fp);
375
+
376
+ bool nan = (bits & expmask) == expmask;
377
+
378
+ if (!nan) {
379
+ return 0;
380
+ }
381
+
382
+ if (bits & fracmask) {
383
+ dest[0] = 'n';
384
+ dest[1] = 'a';
385
+ dest[2] = 'n';
386
+
387
+ } else {
388
+ dest[0] = 'i';
389
+ dest[1] = 'n';
390
+ dest[2] = 'f';
391
+ }
392
+
393
+ return 3;
394
+ }
395
+
396
+ /* Fast and accurate double to string conversion based on Florian
397
+ * Loitsch's Grisu-algorithm. The generated string is never longer
398
+ * than 32 characters and is not null terminated. Returns the number
399
+ * of written characters. */
400
+ static int fpconv_dtoa(double d, char dest[32]) {
401
+ char digits[18];
402
+
403
+ int str_len = 0;
404
+ bool neg = false;
405
+
406
+ if (get_dbits(d) & signmask) {
407
+ dest[0] = '-';
408
+ str_len++;
409
+ neg = true;
410
+ }
411
+
412
+ int spec = filter_special(d, dest + str_len);
413
+
414
+ if (spec) {
415
+ return str_len + spec;
416
+ }
417
+
418
+ int K = 0;
419
+ int ndigits = grisu2(d, digits, &K);
420
+
421
+ str_len += emit_digits(digits, ndigits, dest + str_len, K, neg);
422
+
423
+ return str_len;
424
+ }
425
+
426
+ int yep_d2s_json(double d, char* buf) {
427
+ return fpconv_dtoa(d, buf);
428
+ }