isotree 0.2.2 → 0.3.0

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 (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -1
  3. data/LICENSE.txt +2 -2
  4. data/README.md +32 -14
  5. data/ext/isotree/ext.cpp +144 -31
  6. data/ext/isotree/extconf.rb +7 -7
  7. data/lib/isotree/isolation_forest.rb +110 -30
  8. data/lib/isotree/version.rb +1 -1
  9. data/vendor/isotree/LICENSE +1 -1
  10. data/vendor/isotree/README.md +165 -27
  11. data/vendor/isotree/include/isotree.hpp +2111 -0
  12. data/vendor/isotree/include/isotree_oop.hpp +394 -0
  13. data/vendor/isotree/inst/COPYRIGHTS +62 -0
  14. data/vendor/isotree/src/RcppExports.cpp +525 -52
  15. data/vendor/isotree/src/Rwrapper.cpp +1931 -268
  16. data/vendor/isotree/src/c_interface.cpp +953 -0
  17. data/vendor/isotree/src/crit.hpp +4232 -0
  18. data/vendor/isotree/src/dist.hpp +1886 -0
  19. data/vendor/isotree/src/exp_depth_table.hpp +134 -0
  20. data/vendor/isotree/src/extended.hpp +1444 -0
  21. data/vendor/isotree/src/external_facing_generic.hpp +399 -0
  22. data/vendor/isotree/src/fit_model.hpp +2401 -0
  23. data/vendor/isotree/src/{dealloc.cpp → headers_joined.hpp} +38 -22
  24. data/vendor/isotree/src/helpers_iforest.hpp +813 -0
  25. data/vendor/isotree/src/{impute.cpp → impute.hpp} +353 -122
  26. data/vendor/isotree/src/indexer.cpp +515 -0
  27. data/vendor/isotree/src/instantiate_template_headers.cpp +118 -0
  28. data/vendor/isotree/src/instantiate_template_headers.hpp +240 -0
  29. data/vendor/isotree/src/isoforest.hpp +1659 -0
  30. data/vendor/isotree/src/isotree.hpp +1804 -392
  31. data/vendor/isotree/src/isotree_exportable.hpp +99 -0
  32. data/vendor/isotree/src/merge_models.cpp +159 -16
  33. data/vendor/isotree/src/mult.hpp +1321 -0
  34. data/vendor/isotree/src/oop_interface.cpp +842 -0
  35. data/vendor/isotree/src/oop_interface.hpp +278 -0
  36. data/vendor/isotree/src/other_helpers.hpp +219 -0
  37. data/vendor/isotree/src/predict.hpp +1932 -0
  38. data/vendor/isotree/src/python_helpers.hpp +134 -0
  39. data/vendor/isotree/src/ref_indexer.hpp +154 -0
  40. data/vendor/isotree/src/robinmap/LICENSE +21 -0
  41. data/vendor/isotree/src/robinmap/README.md +483 -0
  42. data/vendor/isotree/src/robinmap/include/tsl/robin_growth_policy.h +406 -0
  43. data/vendor/isotree/src/robinmap/include/tsl/robin_hash.h +1620 -0
  44. data/vendor/isotree/src/robinmap/include/tsl/robin_map.h +807 -0
  45. data/vendor/isotree/src/robinmap/include/tsl/robin_set.h +660 -0
  46. data/vendor/isotree/src/serialize.cpp +4300 -139
  47. data/vendor/isotree/src/sql.cpp +141 -59
  48. data/vendor/isotree/src/subset_models.cpp +174 -0
  49. data/vendor/isotree/src/utils.hpp +3808 -0
  50. data/vendor/isotree/src/xoshiro.hpp +467 -0
  51. data/vendor/isotree/src/ziggurat.hpp +405 -0
  52. metadata +38 -104
  53. data/vendor/cereal/LICENSE +0 -24
  54. data/vendor/cereal/README.md +0 -85
  55. data/vendor/cereal/include/cereal/access.hpp +0 -351
  56. data/vendor/cereal/include/cereal/archives/adapters.hpp +0 -163
  57. data/vendor/cereal/include/cereal/archives/binary.hpp +0 -169
  58. data/vendor/cereal/include/cereal/archives/json.hpp +0 -1019
  59. data/vendor/cereal/include/cereal/archives/portable_binary.hpp +0 -334
  60. data/vendor/cereal/include/cereal/archives/xml.hpp +0 -956
  61. data/vendor/cereal/include/cereal/cereal.hpp +0 -1089
  62. data/vendor/cereal/include/cereal/details/helpers.hpp +0 -422
  63. data/vendor/cereal/include/cereal/details/polymorphic_impl.hpp +0 -796
  64. data/vendor/cereal/include/cereal/details/polymorphic_impl_fwd.hpp +0 -65
  65. data/vendor/cereal/include/cereal/details/static_object.hpp +0 -127
  66. data/vendor/cereal/include/cereal/details/traits.hpp +0 -1411
  67. data/vendor/cereal/include/cereal/details/util.hpp +0 -84
  68. data/vendor/cereal/include/cereal/external/base64.hpp +0 -134
  69. data/vendor/cereal/include/cereal/external/rapidjson/allocators.h +0 -284
  70. data/vendor/cereal/include/cereal/external/rapidjson/cursorstreamwrapper.h +0 -78
  71. data/vendor/cereal/include/cereal/external/rapidjson/document.h +0 -2652
  72. data/vendor/cereal/include/cereal/external/rapidjson/encodedstream.h +0 -299
  73. data/vendor/cereal/include/cereal/external/rapidjson/encodings.h +0 -716
  74. data/vendor/cereal/include/cereal/external/rapidjson/error/en.h +0 -74
  75. data/vendor/cereal/include/cereal/external/rapidjson/error/error.h +0 -161
  76. data/vendor/cereal/include/cereal/external/rapidjson/filereadstream.h +0 -99
  77. data/vendor/cereal/include/cereal/external/rapidjson/filewritestream.h +0 -104
  78. data/vendor/cereal/include/cereal/external/rapidjson/fwd.h +0 -151
  79. data/vendor/cereal/include/cereal/external/rapidjson/internal/biginteger.h +0 -290
  80. data/vendor/cereal/include/cereal/external/rapidjson/internal/diyfp.h +0 -271
  81. data/vendor/cereal/include/cereal/external/rapidjson/internal/dtoa.h +0 -245
  82. data/vendor/cereal/include/cereal/external/rapidjson/internal/ieee754.h +0 -78
  83. data/vendor/cereal/include/cereal/external/rapidjson/internal/itoa.h +0 -308
  84. data/vendor/cereal/include/cereal/external/rapidjson/internal/meta.h +0 -186
  85. data/vendor/cereal/include/cereal/external/rapidjson/internal/pow10.h +0 -55
  86. data/vendor/cereal/include/cereal/external/rapidjson/internal/regex.h +0 -740
  87. data/vendor/cereal/include/cereal/external/rapidjson/internal/stack.h +0 -232
  88. data/vendor/cereal/include/cereal/external/rapidjson/internal/strfunc.h +0 -69
  89. data/vendor/cereal/include/cereal/external/rapidjson/internal/strtod.h +0 -290
  90. data/vendor/cereal/include/cereal/external/rapidjson/internal/swap.h +0 -46
  91. data/vendor/cereal/include/cereal/external/rapidjson/istreamwrapper.h +0 -128
  92. data/vendor/cereal/include/cereal/external/rapidjson/memorybuffer.h +0 -70
  93. data/vendor/cereal/include/cereal/external/rapidjson/memorystream.h +0 -71
  94. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/inttypes.h +0 -316
  95. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/stdint.h +0 -300
  96. data/vendor/cereal/include/cereal/external/rapidjson/ostreamwrapper.h +0 -81
  97. data/vendor/cereal/include/cereal/external/rapidjson/pointer.h +0 -1414
  98. data/vendor/cereal/include/cereal/external/rapidjson/prettywriter.h +0 -277
  99. data/vendor/cereal/include/cereal/external/rapidjson/rapidjson.h +0 -656
  100. data/vendor/cereal/include/cereal/external/rapidjson/reader.h +0 -2230
  101. data/vendor/cereal/include/cereal/external/rapidjson/schema.h +0 -2497
  102. data/vendor/cereal/include/cereal/external/rapidjson/stream.h +0 -223
  103. data/vendor/cereal/include/cereal/external/rapidjson/stringbuffer.h +0 -121
  104. data/vendor/cereal/include/cereal/external/rapidjson/writer.h +0 -709
  105. data/vendor/cereal/include/cereal/external/rapidxml/license.txt +0 -52
  106. data/vendor/cereal/include/cereal/external/rapidxml/manual.html +0 -406
  107. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml.hpp +0 -2624
  108. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_iterators.hpp +0 -175
  109. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_print.hpp +0 -428
  110. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_utils.hpp +0 -123
  111. data/vendor/cereal/include/cereal/macros.hpp +0 -154
  112. data/vendor/cereal/include/cereal/specialize.hpp +0 -139
  113. data/vendor/cereal/include/cereal/types/array.hpp +0 -79
  114. data/vendor/cereal/include/cereal/types/atomic.hpp +0 -55
  115. data/vendor/cereal/include/cereal/types/base_class.hpp +0 -203
  116. data/vendor/cereal/include/cereal/types/bitset.hpp +0 -176
  117. data/vendor/cereal/include/cereal/types/boost_variant.hpp +0 -164
  118. data/vendor/cereal/include/cereal/types/chrono.hpp +0 -72
  119. data/vendor/cereal/include/cereal/types/common.hpp +0 -129
  120. data/vendor/cereal/include/cereal/types/complex.hpp +0 -56
  121. data/vendor/cereal/include/cereal/types/concepts/pair_associative_container.hpp +0 -73
  122. data/vendor/cereal/include/cereal/types/deque.hpp +0 -62
  123. data/vendor/cereal/include/cereal/types/forward_list.hpp +0 -68
  124. data/vendor/cereal/include/cereal/types/functional.hpp +0 -43
  125. data/vendor/cereal/include/cereal/types/list.hpp +0 -62
  126. data/vendor/cereal/include/cereal/types/map.hpp +0 -36
  127. data/vendor/cereal/include/cereal/types/memory.hpp +0 -425
  128. data/vendor/cereal/include/cereal/types/optional.hpp +0 -66
  129. data/vendor/cereal/include/cereal/types/polymorphic.hpp +0 -483
  130. data/vendor/cereal/include/cereal/types/queue.hpp +0 -132
  131. data/vendor/cereal/include/cereal/types/set.hpp +0 -103
  132. data/vendor/cereal/include/cereal/types/stack.hpp +0 -76
  133. data/vendor/cereal/include/cereal/types/string.hpp +0 -61
  134. data/vendor/cereal/include/cereal/types/tuple.hpp +0 -123
  135. data/vendor/cereal/include/cereal/types/unordered_map.hpp +0 -36
  136. data/vendor/cereal/include/cereal/types/unordered_set.hpp +0 -99
  137. data/vendor/cereal/include/cereal/types/utility.hpp +0 -47
  138. data/vendor/cereal/include/cereal/types/valarray.hpp +0 -89
  139. data/vendor/cereal/include/cereal/types/variant.hpp +0 -109
  140. data/vendor/cereal/include/cereal/types/vector.hpp +0 -112
  141. data/vendor/cereal/include/cereal/version.hpp +0 -52
  142. data/vendor/isotree/src/Makevars +0 -4
  143. data/vendor/isotree/src/crit.cpp +0 -912
  144. data/vendor/isotree/src/dist.cpp +0 -749
  145. data/vendor/isotree/src/extended.cpp +0 -790
  146. data/vendor/isotree/src/fit_model.cpp +0 -1090
  147. data/vendor/isotree/src/helpers_iforest.cpp +0 -324
  148. data/vendor/isotree/src/isoforest.cpp +0 -771
  149. data/vendor/isotree/src/mult.cpp +0 -607
  150. data/vendor/isotree/src/predict.cpp +0 -853
  151. data/vendor/isotree/src/utils.cpp +0 -1566
@@ -1,290 +0,0 @@
1
- // Tencent is pleased to support the open source community by making RapidJSON available.
2
- //
3
- // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4
- //
5
- // Licensed under the MIT License (the "License"); you may not use this file except
6
- // in compliance with the License. You may obtain a copy of the License at
7
- //
8
- // http://opensource.org/licenses/MIT
9
- //
10
- // Unless required by applicable law or agreed to in writing, software distributed
11
- // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
- // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
- // specific language governing permissions and limitations under the License.
14
-
15
- #ifndef CEREAL_RAPIDJSON_BIGINTEGER_H_
16
- #define CEREAL_RAPIDJSON_BIGINTEGER_H_
17
-
18
- #include "../rapidjson.h"
19
-
20
- #if defined(_MSC_VER) && !__INTEL_COMPILER && defined(_M_AMD64)
21
- #include <intrin.h> // for _umul128
22
- #pragma intrinsic(_umul128)
23
- #endif
24
-
25
- CEREAL_RAPIDJSON_NAMESPACE_BEGIN
26
- namespace internal {
27
-
28
- class BigInteger {
29
- public:
30
- typedef uint64_t Type;
31
-
32
- BigInteger(const BigInteger& rhs) : count_(rhs.count_) {
33
- std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
34
- }
35
-
36
- explicit BigInteger(uint64_t u) : count_(1) {
37
- digits_[0] = u;
38
- }
39
-
40
- BigInteger(const char* decimals, size_t length) : count_(1) {
41
- CEREAL_RAPIDJSON_ASSERT(length > 0);
42
- digits_[0] = 0;
43
- size_t i = 0;
44
- const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19
45
- while (length >= kMaxDigitPerIteration) {
46
- AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
47
- length -= kMaxDigitPerIteration;
48
- i += kMaxDigitPerIteration;
49
- }
50
-
51
- if (length > 0)
52
- AppendDecimal64(decimals + i, decimals + i + length);
53
- }
54
-
55
- BigInteger& operator=(const BigInteger &rhs)
56
- {
57
- if (this != &rhs) {
58
- count_ = rhs.count_;
59
- std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
60
- }
61
- return *this;
62
- }
63
-
64
- BigInteger& operator=(uint64_t u) {
65
- digits_[0] = u;
66
- count_ = 1;
67
- return *this;
68
- }
69
-
70
- BigInteger& operator+=(uint64_t u) {
71
- Type backup = digits_[0];
72
- digits_[0] += u;
73
- for (size_t i = 0; i < count_ - 1; i++) {
74
- if (digits_[i] >= backup)
75
- return *this; // no carry
76
- backup = digits_[i + 1];
77
- digits_[i + 1] += 1;
78
- }
79
-
80
- // Last carry
81
- if (digits_[count_ - 1] < backup)
82
- PushBack(1);
83
-
84
- return *this;
85
- }
86
-
87
- BigInteger& operator*=(uint64_t u) {
88
- if (u == 0) return *this = 0;
89
- if (u == 1) return *this;
90
- if (*this == 1) return *this = u;
91
-
92
- uint64_t k = 0;
93
- for (size_t i = 0; i < count_; i++) {
94
- uint64_t hi;
95
- digits_[i] = MulAdd64(digits_[i], u, k, &hi);
96
- k = hi;
97
- }
98
-
99
- if (k > 0)
100
- PushBack(k);
101
-
102
- return *this;
103
- }
104
-
105
- BigInteger& operator*=(uint32_t u) {
106
- if (u == 0) return *this = 0;
107
- if (u == 1) return *this;
108
- if (*this == 1) return *this = u;
109
-
110
- uint64_t k = 0;
111
- for (size_t i = 0; i < count_; i++) {
112
- const uint64_t c = digits_[i] >> 32;
113
- const uint64_t d = digits_[i] & 0xFFFFFFFF;
114
- const uint64_t uc = u * c;
115
- const uint64_t ud = u * d;
116
- const uint64_t p0 = ud + k;
117
- const uint64_t p1 = uc + (p0 >> 32);
118
- digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
119
- k = p1 >> 32;
120
- }
121
-
122
- if (k > 0)
123
- PushBack(k);
124
-
125
- return *this;
126
- }
127
-
128
- BigInteger& operator<<=(size_t shift) {
129
- if (IsZero() || shift == 0) return *this;
130
-
131
- size_t offset = shift / kTypeBit;
132
- size_t interShift = shift % kTypeBit;
133
- CEREAL_RAPIDJSON_ASSERT(count_ + offset <= kCapacity);
134
-
135
- if (interShift == 0) {
136
- std::memmove(digits_ + offset, digits_, count_ * sizeof(Type));
137
- count_ += offset;
138
- }
139
- else {
140
- digits_[count_] = 0;
141
- for (size_t i = count_; i > 0; i--)
142
- digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
143
- digits_[offset] = digits_[0] << interShift;
144
- count_ += offset;
145
- if (digits_[count_])
146
- count_++;
147
- }
148
-
149
- std::memset(digits_, 0, offset * sizeof(Type));
150
-
151
- return *this;
152
- }
153
-
154
- bool operator==(const BigInteger& rhs) const {
155
- return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0;
156
- }
157
-
158
- bool operator==(const Type rhs) const {
159
- return count_ == 1 && digits_[0] == rhs;
160
- }
161
-
162
- BigInteger& MultiplyPow5(unsigned exp) {
163
- static const uint32_t kPow5[12] = {
164
- 5,
165
- 5 * 5,
166
- 5 * 5 * 5,
167
- 5 * 5 * 5 * 5,
168
- 5 * 5 * 5 * 5 * 5,
169
- 5 * 5 * 5 * 5 * 5 * 5,
170
- 5 * 5 * 5 * 5 * 5 * 5 * 5,
171
- 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
172
- 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
173
- 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
174
- 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
175
- 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5
176
- };
177
- if (exp == 0) return *this;
178
- for (; exp >= 27; exp -= 27) *this *= CEREAL_RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27
179
- for (; exp >= 13; exp -= 13) *this *= static_cast<uint32_t>(1220703125u); // 5^13
180
- if (exp > 0) *this *= kPow5[exp - 1];
181
- return *this;
182
- }
183
-
184
- // Compute absolute difference of this and rhs.
185
- // Assume this != rhs
186
- bool Difference(const BigInteger& rhs, BigInteger* out) const {
187
- int cmp = Compare(rhs);
188
- CEREAL_RAPIDJSON_ASSERT(cmp != 0);
189
- const BigInteger *a, *b; // Makes a > b
190
- bool ret;
191
- if (cmp < 0) { a = &rhs; b = this; ret = true; }
192
- else { a = this; b = &rhs; ret = false; }
193
-
194
- Type borrow = 0;
195
- for (size_t i = 0; i < a->count_; i++) {
196
- Type d = a->digits_[i] - borrow;
197
- if (i < b->count_)
198
- d -= b->digits_[i];
199
- borrow = (d > a->digits_[i]) ? 1 : 0;
200
- out->digits_[i] = d;
201
- if (d != 0)
202
- out->count_ = i + 1;
203
- }
204
-
205
- return ret;
206
- }
207
-
208
- int Compare(const BigInteger& rhs) const {
209
- if (count_ != rhs.count_)
210
- return count_ < rhs.count_ ? -1 : 1;
211
-
212
- for (size_t i = count_; i-- > 0;)
213
- if (digits_[i] != rhs.digits_[i])
214
- return digits_[i] < rhs.digits_[i] ? -1 : 1;
215
-
216
- return 0;
217
- }
218
-
219
- size_t GetCount() const { return count_; }
220
- Type GetDigit(size_t index) const { CEREAL_RAPIDJSON_ASSERT(index < count_); return digits_[index]; }
221
- bool IsZero() const { return count_ == 1 && digits_[0] == 0; }
222
-
223
- private:
224
- void AppendDecimal64(const char* begin, const char* end) {
225
- uint64_t u = ParseUint64(begin, end);
226
- if (IsZero())
227
- *this = u;
228
- else {
229
- unsigned exp = static_cast<unsigned>(end - begin);
230
- (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u
231
- }
232
- }
233
-
234
- void PushBack(Type digit) {
235
- CEREAL_RAPIDJSON_ASSERT(count_ < kCapacity);
236
- digits_[count_++] = digit;
237
- }
238
-
239
- static uint64_t ParseUint64(const char* begin, const char* end) {
240
- uint64_t r = 0;
241
- for (const char* p = begin; p != end; ++p) {
242
- CEREAL_RAPIDJSON_ASSERT(*p >= '0' && *p <= '9');
243
- r = r * 10u + static_cast<unsigned>(*p - '0');
244
- }
245
- return r;
246
- }
247
-
248
- // Assume a * b + k < 2^128
249
- static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) {
250
- #if defined(_MSC_VER) && defined(_M_AMD64)
251
- uint64_t low = _umul128(a, b, outHigh) + k;
252
- if (low < k)
253
- (*outHigh)++;
254
- return low;
255
- #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
256
- __extension__ typedef unsigned __int128 uint128;
257
- uint128 p = static_cast<uint128>(a) * static_cast<uint128>(b);
258
- p += k;
259
- *outHigh = static_cast<uint64_t>(p >> 64);
260
- return static_cast<uint64_t>(p);
261
- #else
262
- const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
263
- uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
264
- x1 += (x0 >> 32); // can't give carry
265
- x1 += x2;
266
- if (x1 < x2)
267
- x3 += (static_cast<uint64_t>(1) << 32);
268
- uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
269
- uint64_t hi = x3 + (x1 >> 32);
270
-
271
- lo += k;
272
- if (lo < k)
273
- hi++;
274
- *outHigh = hi;
275
- return lo;
276
- #endif
277
- }
278
-
279
- static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000
280
- static const size_t kCapacity = kBitCount / sizeof(Type);
281
- static const size_t kTypeBit = sizeof(Type) * 8;
282
-
283
- Type digits_[kCapacity];
284
- size_t count_;
285
- };
286
-
287
- } // namespace internal
288
- CEREAL_RAPIDJSON_NAMESPACE_END
289
-
290
- #endif // CEREAL_RAPIDJSON_BIGINTEGER_H_
@@ -1,271 +0,0 @@
1
- // Tencent is pleased to support the open source community by making RapidJSON available.
2
- //
3
- // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4
- //
5
- // Licensed under the MIT License (the "License"); you may not use this file except
6
- // in compliance with the License. You may obtain a copy of the License at
7
- //
8
- // http://opensource.org/licenses/MIT
9
- //
10
- // Unless required by applicable law or agreed to in writing, software distributed
11
- // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
- // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
- // specific language governing permissions and limitations under the License.
14
-
15
- // This is a C++ header-only implementation of Grisu2 algorithm from the publication:
16
- // Loitsch, Florian. "Printing floating-point numbers quickly and accurately with
17
- // integers." ACM Sigplan Notices 45.6 (2010): 233-243.
18
-
19
- #ifndef CEREAL_RAPIDJSON_DIYFP_H_
20
- #define CEREAL_RAPIDJSON_DIYFP_H_
21
-
22
- #include "../rapidjson.h"
23
- #include <limits>
24
-
25
- #if defined(_MSC_VER) && defined(_M_AMD64) && !defined(__INTEL_COMPILER)
26
- #include <intrin.h>
27
- #pragma intrinsic(_BitScanReverse64)
28
- #pragma intrinsic(_umul128)
29
- #endif
30
-
31
- CEREAL_RAPIDJSON_NAMESPACE_BEGIN
32
- namespace internal {
33
-
34
- #ifdef __GNUC__
35
- CEREAL_RAPIDJSON_DIAG_PUSH
36
- CEREAL_RAPIDJSON_DIAG_OFF(effc++)
37
- #endif
38
-
39
- #ifdef __clang__
40
- CEREAL_RAPIDJSON_DIAG_PUSH
41
- CEREAL_RAPIDJSON_DIAG_OFF(padded)
42
- #endif
43
-
44
- struct DiyFp {
45
- DiyFp() : f(), e() {}
46
-
47
- DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {}
48
-
49
- explicit DiyFp(double d) {
50
- union {
51
- double d;
52
- uint64_t u64;
53
- } u = { d };
54
-
55
- int biased_e = static_cast<int>((u.u64 & kDpExponentMask) >> kDpSignificandSize);
56
- uint64_t significand = (u.u64 & kDpSignificandMask);
57
- if (biased_e != 0) {
58
- f = significand + kDpHiddenBit;
59
- e = biased_e - kDpExponentBias;
60
- }
61
- else {
62
- f = significand;
63
- e = kDpMinExponent + 1;
64
- }
65
- }
66
-
67
- DiyFp operator-(const DiyFp& rhs) const {
68
- return DiyFp(f - rhs.f, e);
69
- }
70
-
71
- DiyFp operator*(const DiyFp& rhs) const {
72
- #if defined(_MSC_VER) && defined(_M_AMD64)
73
- uint64_t h;
74
- uint64_t l = _umul128(f, rhs.f, &h);
75
- if (l & (uint64_t(1) << 63)) // rounding
76
- h++;
77
- return DiyFp(h, e + rhs.e + 64);
78
- #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
79
- __extension__ typedef unsigned __int128 uint128;
80
- uint128 p = static_cast<uint128>(f) * static_cast<uint128>(rhs.f);
81
- uint64_t h = static_cast<uint64_t>(p >> 64);
82
- uint64_t l = static_cast<uint64_t>(p);
83
- if (l & (uint64_t(1) << 63)) // rounding
84
- h++;
85
- return DiyFp(h, e + rhs.e + 64);
86
- #else
87
- const uint64_t M32 = 0xFFFFFFFF;
88
- const uint64_t a = f >> 32;
89
- const uint64_t b = f & M32;
90
- const uint64_t c = rhs.f >> 32;
91
- const uint64_t d = rhs.f & M32;
92
- const uint64_t ac = a * c;
93
- const uint64_t bc = b * c;
94
- const uint64_t ad = a * d;
95
- const uint64_t bd = b * d;
96
- uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32);
97
- tmp += 1U << 31; /// mult_round
98
- return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64);
99
- #endif
100
- }
101
-
102
- DiyFp Normalize() const {
103
- CEREAL_RAPIDJSON_ASSERT(f != 0); // https://stackoverflow.com/a/26809183/291737
104
- #if defined(_MSC_VER) && defined(_M_AMD64)
105
- unsigned long index;
106
- _BitScanReverse64(&index, f);
107
- return DiyFp(f << (63 - index), e - (63 - index));
108
- #elif defined(__GNUC__) && __GNUC__ >= 4
109
- int s = __builtin_clzll(f);
110
- return DiyFp(f << s, e - s);
111
- #else
112
- DiyFp res = *this;
113
- while (!(res.f & (static_cast<uint64_t>(1) << 63))) {
114
- res.f <<= 1;
115
- res.e--;
116
- }
117
- return res;
118
- #endif
119
- }
120
-
121
- DiyFp NormalizeBoundary() const {
122
- DiyFp res = *this;
123
- while (!(res.f & (kDpHiddenBit << 1))) {
124
- res.f <<= 1;
125
- res.e--;
126
- }
127
- res.f <<= (kDiySignificandSize - kDpSignificandSize - 2);
128
- res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2);
129
- return res;
130
- }
131
-
132
- void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const {
133
- DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary();
134
- DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1);
135
- mi.f <<= mi.e - pl.e;
136
- mi.e = pl.e;
137
- *plus = pl;
138
- *minus = mi;
139
- }
140
-
141
- double ToDouble() const {
142
- union {
143
- double d;
144
- uint64_t u64;
145
- }u;
146
- CEREAL_RAPIDJSON_ASSERT(f <= kDpHiddenBit + kDpSignificandMask);
147
- if (e < kDpDenormalExponent) {
148
- // Underflow.
149
- return 0.0;
150
- }
151
- if (e >= kDpMaxExponent) {
152
- // Overflow.
153
- return std::numeric_limits<double>::infinity();
154
- }
155
- const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 :
156
- static_cast<uint64_t>(e + kDpExponentBias);
157
- u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize);
158
- return u.d;
159
- }
160
-
161
- static const int kDiySignificandSize = 64;
162
- static const int kDpSignificandSize = 52;
163
- static const int kDpExponentBias = 0x3FF + kDpSignificandSize;
164
- static const int kDpMaxExponent = 0x7FF - kDpExponentBias;
165
- static const int kDpMinExponent = -kDpExponentBias;
166
- static const int kDpDenormalExponent = -kDpExponentBias + 1;
167
- static const uint64_t kDpExponentMask = CEREAL_RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000);
168
- static const uint64_t kDpSignificandMask = CEREAL_RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
169
- static const uint64_t kDpHiddenBit = CEREAL_RAPIDJSON_UINT64_C2(0x00100000, 0x00000000);
170
-
171
- uint64_t f;
172
- int e;
173
- };
174
-
175
- inline DiyFp GetCachedPowerByIndex(size_t index) {
176
- // 10^-348, 10^-340, ..., 10^340
177
- static const uint64_t kCachedPowers_F[] = {
178
- CEREAL_RAPIDJSON_UINT64_C2(0xfa8fd5a0, 0x081c0288), CEREAL_RAPIDJSON_UINT64_C2(0xbaaee17f, 0xa23ebf76),
179
- CEREAL_RAPIDJSON_UINT64_C2(0x8b16fb20, 0x3055ac76), CEREAL_RAPIDJSON_UINT64_C2(0xcf42894a, 0x5dce35ea),
180
- CEREAL_RAPIDJSON_UINT64_C2(0x9a6bb0aa, 0x55653b2d), CEREAL_RAPIDJSON_UINT64_C2(0xe61acf03, 0x3d1a45df),
181
- CEREAL_RAPIDJSON_UINT64_C2(0xab70fe17, 0xc79ac6ca), CEREAL_RAPIDJSON_UINT64_C2(0xff77b1fc, 0xbebcdc4f),
182
- CEREAL_RAPIDJSON_UINT64_C2(0xbe5691ef, 0x416bd60c), CEREAL_RAPIDJSON_UINT64_C2(0x8dd01fad, 0x907ffc3c),
183
- CEREAL_RAPIDJSON_UINT64_C2(0xd3515c28, 0x31559a83), CEREAL_RAPIDJSON_UINT64_C2(0x9d71ac8f, 0xada6c9b5),
184
- CEREAL_RAPIDJSON_UINT64_C2(0xea9c2277, 0x23ee8bcb), CEREAL_RAPIDJSON_UINT64_C2(0xaecc4991, 0x4078536d),
185
- CEREAL_RAPIDJSON_UINT64_C2(0x823c1279, 0x5db6ce57), CEREAL_RAPIDJSON_UINT64_C2(0xc2109436, 0x4dfb5637),
186
- CEREAL_RAPIDJSON_UINT64_C2(0x9096ea6f, 0x3848984f), CEREAL_RAPIDJSON_UINT64_C2(0xd77485cb, 0x25823ac7),
187
- CEREAL_RAPIDJSON_UINT64_C2(0xa086cfcd, 0x97bf97f4), CEREAL_RAPIDJSON_UINT64_C2(0xef340a98, 0x172aace5),
188
- CEREAL_RAPIDJSON_UINT64_C2(0xb23867fb, 0x2a35b28e), CEREAL_RAPIDJSON_UINT64_C2(0x84c8d4df, 0xd2c63f3b),
189
- CEREAL_RAPIDJSON_UINT64_C2(0xc5dd4427, 0x1ad3cdba), CEREAL_RAPIDJSON_UINT64_C2(0x936b9fce, 0xbb25c996),
190
- CEREAL_RAPIDJSON_UINT64_C2(0xdbac6c24, 0x7d62a584), CEREAL_RAPIDJSON_UINT64_C2(0xa3ab6658, 0x0d5fdaf6),
191
- CEREAL_RAPIDJSON_UINT64_C2(0xf3e2f893, 0xdec3f126), CEREAL_RAPIDJSON_UINT64_C2(0xb5b5ada8, 0xaaff80b8),
192
- CEREAL_RAPIDJSON_UINT64_C2(0x87625f05, 0x6c7c4a8b), CEREAL_RAPIDJSON_UINT64_C2(0xc9bcff60, 0x34c13053),
193
- CEREAL_RAPIDJSON_UINT64_C2(0x964e858c, 0x91ba2655), CEREAL_RAPIDJSON_UINT64_C2(0xdff97724, 0x70297ebd),
194
- CEREAL_RAPIDJSON_UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), CEREAL_RAPIDJSON_UINT64_C2(0xf8a95fcf, 0x88747d94),
195
- CEREAL_RAPIDJSON_UINT64_C2(0xb9447093, 0x8fa89bcf), CEREAL_RAPIDJSON_UINT64_C2(0x8a08f0f8, 0xbf0f156b),
196
- CEREAL_RAPIDJSON_UINT64_C2(0xcdb02555, 0x653131b6), CEREAL_RAPIDJSON_UINT64_C2(0x993fe2c6, 0xd07b7fac),
197
- CEREAL_RAPIDJSON_UINT64_C2(0xe45c10c4, 0x2a2b3b06), CEREAL_RAPIDJSON_UINT64_C2(0xaa242499, 0x697392d3),
198
- CEREAL_RAPIDJSON_UINT64_C2(0xfd87b5f2, 0x8300ca0e), CEREAL_RAPIDJSON_UINT64_C2(0xbce50864, 0x92111aeb),
199
- CEREAL_RAPIDJSON_UINT64_C2(0x8cbccc09, 0x6f5088cc), CEREAL_RAPIDJSON_UINT64_C2(0xd1b71758, 0xe219652c),
200
- CEREAL_RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), CEREAL_RAPIDJSON_UINT64_C2(0xe8d4a510, 0x00000000),
201
- CEREAL_RAPIDJSON_UINT64_C2(0xad78ebc5, 0xac620000), CEREAL_RAPIDJSON_UINT64_C2(0x813f3978, 0xf8940984),
202
- CEREAL_RAPIDJSON_UINT64_C2(0xc097ce7b, 0xc90715b3), CEREAL_RAPIDJSON_UINT64_C2(0x8f7e32ce, 0x7bea5c70),
203
- CEREAL_RAPIDJSON_UINT64_C2(0xd5d238a4, 0xabe98068), CEREAL_RAPIDJSON_UINT64_C2(0x9f4f2726, 0x179a2245),
204
- CEREAL_RAPIDJSON_UINT64_C2(0xed63a231, 0xd4c4fb27), CEREAL_RAPIDJSON_UINT64_C2(0xb0de6538, 0x8cc8ada8),
205
- CEREAL_RAPIDJSON_UINT64_C2(0x83c7088e, 0x1aab65db), CEREAL_RAPIDJSON_UINT64_C2(0xc45d1df9, 0x42711d9a),
206
- CEREAL_RAPIDJSON_UINT64_C2(0x924d692c, 0xa61be758), CEREAL_RAPIDJSON_UINT64_C2(0xda01ee64, 0x1a708dea),
207
- CEREAL_RAPIDJSON_UINT64_C2(0xa26da399, 0x9aef774a), CEREAL_RAPIDJSON_UINT64_C2(0xf209787b, 0xb47d6b85),
208
- CEREAL_RAPIDJSON_UINT64_C2(0xb454e4a1, 0x79dd1877), CEREAL_RAPIDJSON_UINT64_C2(0x865b8692, 0x5b9bc5c2),
209
- CEREAL_RAPIDJSON_UINT64_C2(0xc83553c5, 0xc8965d3d), CEREAL_RAPIDJSON_UINT64_C2(0x952ab45c, 0xfa97a0b3),
210
- CEREAL_RAPIDJSON_UINT64_C2(0xde469fbd, 0x99a05fe3), CEREAL_RAPIDJSON_UINT64_C2(0xa59bc234, 0xdb398c25),
211
- CEREAL_RAPIDJSON_UINT64_C2(0xf6c69a72, 0xa3989f5c), CEREAL_RAPIDJSON_UINT64_C2(0xb7dcbf53, 0x54e9bece),
212
- CEREAL_RAPIDJSON_UINT64_C2(0x88fcf317, 0xf22241e2), CEREAL_RAPIDJSON_UINT64_C2(0xcc20ce9b, 0xd35c78a5),
213
- CEREAL_RAPIDJSON_UINT64_C2(0x98165af3, 0x7b2153df), CEREAL_RAPIDJSON_UINT64_C2(0xe2a0b5dc, 0x971f303a),
214
- CEREAL_RAPIDJSON_UINT64_C2(0xa8d9d153, 0x5ce3b396), CEREAL_RAPIDJSON_UINT64_C2(0xfb9b7cd9, 0xa4a7443c),
215
- CEREAL_RAPIDJSON_UINT64_C2(0xbb764c4c, 0xa7a44410), CEREAL_RAPIDJSON_UINT64_C2(0x8bab8eef, 0xb6409c1a),
216
- CEREAL_RAPIDJSON_UINT64_C2(0xd01fef10, 0xa657842c), CEREAL_RAPIDJSON_UINT64_C2(0x9b10a4e5, 0xe9913129),
217
- CEREAL_RAPIDJSON_UINT64_C2(0xe7109bfb, 0xa19c0c9d), CEREAL_RAPIDJSON_UINT64_C2(0xac2820d9, 0x623bf429),
218
- CEREAL_RAPIDJSON_UINT64_C2(0x80444b5e, 0x7aa7cf85), CEREAL_RAPIDJSON_UINT64_C2(0xbf21e440, 0x03acdd2d),
219
- CEREAL_RAPIDJSON_UINT64_C2(0x8e679c2f, 0x5e44ff8f), CEREAL_RAPIDJSON_UINT64_C2(0xd433179d, 0x9c8cb841),
220
- CEREAL_RAPIDJSON_UINT64_C2(0x9e19db92, 0xb4e31ba9), CEREAL_RAPIDJSON_UINT64_C2(0xeb96bf6e, 0xbadf77d9),
221
- CEREAL_RAPIDJSON_UINT64_C2(0xaf87023b, 0x9bf0ee6b)
222
- };
223
- static const int16_t kCachedPowers_E[] = {
224
- -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980,
225
- -954, -927, -901, -874, -847, -821, -794, -768, -741, -715,
226
- -688, -661, -635, -608, -582, -555, -529, -502, -475, -449,
227
- -422, -396, -369, -343, -316, -289, -263, -236, -210, -183,
228
- -157, -130, -103, -77, -50, -24, 3, 30, 56, 83,
229
- 109, 136, 162, 189, 216, 242, 269, 295, 322, 348,
230
- 375, 402, 428, 455, 481, 508, 534, 561, 588, 614,
231
- 641, 667, 694, 720, 747, 774, 800, 827, 853, 880,
232
- 907, 933, 960, 986, 1013, 1039, 1066
233
- };
234
- CEREAL_RAPIDJSON_ASSERT(index < 87);
235
- return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]);
236
- }
237
-
238
- inline DiyFp GetCachedPower(int e, int* K) {
239
-
240
- //int k = static_cast<int>(ceil((-61 - e) * 0.30102999566398114)) + 374;
241
- double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive
242
- int k = static_cast<int>(dk);
243
- if (dk - k > 0.0)
244
- k++;
245
-
246
- unsigned index = static_cast<unsigned>((k >> 3) + 1);
247
- *K = -(-348 + static_cast<int>(index << 3)); // decimal exponent no need lookup table
248
-
249
- return GetCachedPowerByIndex(index);
250
- }
251
-
252
- inline DiyFp GetCachedPower10(int exp, int *outExp) {
253
- CEREAL_RAPIDJSON_ASSERT(exp >= -348);
254
- unsigned index = static_cast<unsigned>(exp + 348) / 8u;
255
- *outExp = -348 + static_cast<int>(index) * 8;
256
- return GetCachedPowerByIndex(index);
257
- }
258
-
259
- #ifdef __GNUC__
260
- CEREAL_RAPIDJSON_DIAG_POP
261
- #endif
262
-
263
- #ifdef __clang__
264
- CEREAL_RAPIDJSON_DIAG_POP
265
- CEREAL_RAPIDJSON_DIAG_OFF(padded)
266
- #endif
267
-
268
- } // namespace internal
269
- CEREAL_RAPIDJSON_NAMESPACE_END
270
-
271
- #endif // CEREAL_RAPIDJSON_DIYFP_H_