ycpp 0.1.0__cp315-cp315-win_arm64.whl

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 (142) hide show
  1. ycpp/__init__.py +110 -0
  2. ycpp/_array.py +318 -0
  3. ycpp/_awareness.py +268 -0
  4. ycpp/_doc.py +718 -0
  5. ycpp/_event.py +188 -0
  6. ycpp/_id_map.py +620 -0
  7. ycpp/_map.py +267 -0
  8. ycpp/_provider.py +180 -0
  9. ycpp/_snapshot.py +28 -0
  10. ycpp/_sticky_index.py +59 -0
  11. ycpp/_stream.py +91 -0
  12. ycpp/_sync.py +192 -0
  13. ycpp/_text.py +239 -0
  14. ycpp/_transaction.py +287 -0
  15. ycpp/_typed.py +122 -0
  16. ycpp/_undo.py +201 -0
  17. ycpp/_update.py +18 -0
  18. ycpp/_xml.py +452 -0
  19. ycpp/_ycpp.cp315-win_arm64.lib +0 -0
  20. ycpp/_ycpp.cp315-win_arm64.pyd +0 -0
  21. ycpp-0.1.0.data/headers/fast_float/ascii_number.h +766 -0
  22. ycpp-0.1.0.data/headers/fast_float/bigint.h +638 -0
  23. ycpp-0.1.0.data/headers/fast_float/constexpr_feature_detect.h +53 -0
  24. ycpp-0.1.0.data/headers/fast_float/decimal_to_binary.h +212 -0
  25. ycpp-0.1.0.data/headers/fast_float/digit_comparison.h +455 -0
  26. ycpp-0.1.0.data/headers/fast_float/fast_float.h +91 -0
  27. ycpp-0.1.0.data/headers/fast_float/fast_table.h +708 -0
  28. ycpp-0.1.0.data/headers/fast_float/float_common.h +1432 -0
  29. ycpp-0.1.0.data/headers/fast_float/parse_number.h +488 -0
  30. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_arithmetic.hpp +267 -0
  31. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_bit.hpp +230 -0
  32. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_cast.hpp +43 -0
  33. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_complex.hpp +112 -0
  34. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_details.hpp +386 -0
  35. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_logical.hpp +240 -0
  36. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_math.hpp +2601 -0
  37. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_memory.hpp +1006 -0
  38. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_rounding.hpp +72 -0
  39. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_swizzle.hpp +260 -0
  40. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_trigo.hpp +996 -0
  41. ycpp-0.1.0.data/headers/xsimd/arch/utils/shifts.hpp +82 -0
  42. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx.hpp +2111 -0
  43. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx2.hpp +1591 -0
  44. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512bw.hpp +724 -0
  45. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512cd.hpp +28 -0
  46. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512dq.hpp +270 -0
  47. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512er.hpp +17 -0
  48. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512f.hpp +2919 -0
  49. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512ifma.hpp +17 -0
  50. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512pf.hpp +17 -0
  51. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vbmi.hpp +76 -0
  52. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vbmi2.hpp +130 -0
  53. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vnni_avx512bw.hpp +17 -0
  54. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vnni_avx512vbmi2.hpp +17 -0
  55. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avxvnni.hpp +17 -0
  56. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_common.hpp +26 -0
  57. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_common_fwd.hpp +127 -0
  58. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_constants.hpp +393 -0
  59. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_emulated.hpp +787 -0
  60. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma3_avx.hpp +93 -0
  61. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma3_avx2.hpp +46 -0
  62. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma3_sse.hpp +97 -0
  63. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma4.hpp +92 -0
  64. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_i8mm_neon64.hpp +17 -0
  65. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_isa.hpp +146 -0
  66. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_neon.hpp +3580 -0
  67. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_neon64.hpp +1614 -0
  68. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_rvv.hpp +1515 -0
  69. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_scalar.hpp +1210 -0
  70. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse2.hpp +2333 -0
  71. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse3.hpp +66 -0
  72. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse4_1.hpp +510 -0
  73. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse4_2.hpp +44 -0
  74. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_ssse3.hpp +186 -0
  75. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sve.hpp +1186 -0
  76. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_vsx.hpp +916 -0
  77. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_vxe.hpp +787 -0
  78. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_wasm.hpp +1819 -0
  79. ycpp-0.1.0.data/headers/xsimd/config/xsimd_arch.hpp +242 -0
  80. ycpp-0.1.0.data/headers/xsimd/config/xsimd_config.hpp +620 -0
  81. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features.hpp +50 -0
  82. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_arm.hpp +132 -0
  83. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_ppc.hpp +54 -0
  84. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_riscv.hpp +95 -0
  85. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_s390x.hpp +56 -0
  86. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_x86.hpp +1044 -0
  87. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpuid.hpp +144 -0
  88. ycpp-0.1.0.data/headers/xsimd/config/xsimd_getauxval.hpp +159 -0
  89. ycpp-0.1.0.data/headers/xsimd/config/xsimd_macros.hpp +76 -0
  90. ycpp-0.1.0.data/headers/xsimd/math/xsimd_rem_pio2.hpp +707 -0
  91. ycpp-0.1.0.data/headers/xsimd/memory/xsimd_aligned_allocator.hpp +349 -0
  92. ycpp-0.1.0.data/headers/xsimd/memory/xsimd_alignment.hpp +102 -0
  93. ycpp-0.1.0.data/headers/xsimd/types/xsimd_all_registers.hpp +57 -0
  94. ycpp-0.1.0.data/headers/xsimd/types/xsimd_api.hpp +3106 -0
  95. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx2_register.hpp +44 -0
  96. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512bw_register.hpp +51 -0
  97. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512cd_register.hpp +51 -0
  98. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512dq_register.hpp +51 -0
  99. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512er_register.hpp +51 -0
  100. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512f_register.hpp +77 -0
  101. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512ifma_register.hpp +51 -0
  102. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512pf_register.hpp +51 -0
  103. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vbmi2_register.hpp +51 -0
  104. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vbmi_register.hpp +51 -0
  105. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vnni_avx512bw_register.hpp +54 -0
  106. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vnni_avx512vbmi2_register.hpp +53 -0
  107. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx_register.hpp +64 -0
  108. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avxvnni_register.hpp +44 -0
  109. ycpp-0.1.0.data/headers/xsimd/types/xsimd_batch.hpp +1665 -0
  110. ycpp-0.1.0.data/headers/xsimd/types/xsimd_batch_constant.hpp +571 -0
  111. ycpp-0.1.0.data/headers/xsimd/types/xsimd_batch_fwd.hpp +41 -0
  112. ycpp-0.1.0.data/headers/xsimd/types/xsimd_common_arch.hpp +49 -0
  113. ycpp-0.1.0.data/headers/xsimd/types/xsimd_emulated_register.hpp +80 -0
  114. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma3_avx2_register.hpp +50 -0
  115. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma3_avx_register.hpp +50 -0
  116. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma3_sse_register.hpp +50 -0
  117. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma4_register.hpp +50 -0
  118. ycpp-0.1.0.data/headers/xsimd/types/xsimd_i8mm_neon64_register.hpp +55 -0
  119. ycpp-0.1.0.data/headers/xsimd/types/xsimd_neon64_register.hpp +55 -0
  120. ycpp-0.1.0.data/headers/xsimd/types/xsimd_neon_register.hpp +130 -0
  121. ycpp-0.1.0.data/headers/xsimd/types/xsimd_register.hpp +96 -0
  122. ycpp-0.1.0.data/headers/xsimd/types/xsimd_rvv_register.hpp +491 -0
  123. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse2_register.hpp +59 -0
  124. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse3_register.hpp +49 -0
  125. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse4_1_register.hpp +48 -0
  126. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse4_2_register.hpp +48 -0
  127. ycpp-0.1.0.data/headers/xsimd/types/xsimd_ssse3_register.hpp +48 -0
  128. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sve_register.hpp +216 -0
  129. ycpp-0.1.0.data/headers/xsimd/types/xsimd_traits.hpp +408 -0
  130. ycpp-0.1.0.data/headers/xsimd/types/xsimd_utils.hpp +462 -0
  131. ycpp-0.1.0.data/headers/xsimd/types/xsimd_vsx_register.hpp +77 -0
  132. ycpp-0.1.0.data/headers/xsimd/types/xsimd_vxe_register.hpp +86 -0
  133. ycpp-0.1.0.data/headers/xsimd/types/xsimd_wasm_register.hpp +59 -0
  134. ycpp-0.1.0.data/headers/xsimd/utils/bits.hpp +122 -0
  135. ycpp-0.1.0.data/headers/xsimd/utils/xsimd_type_traits.hpp +145 -0
  136. ycpp-0.1.0.data/headers/xsimd/xsimd.hpp +43 -0
  137. ycpp-0.1.0.dist-info/DELVEWHEEL +2 -0
  138. ycpp-0.1.0.dist-info/METADATA +15 -0
  139. ycpp-0.1.0.dist-info/RECORD +142 -0
  140. ycpp-0.1.0.dist-info/WHEEL +4 -0
  141. ycpp.libs/msvcp140-9cb625fcbc2fbbd6fca1aee8b24b0ebe.dll +0 -0
  142. ycpp.libs/msvcp140_1-84ac1bc84e09d193dd6ad98c84c0fd94.dll +0 -0
@@ -0,0 +1,212 @@
1
+ #ifndef FASTFLOAT_DECIMAL_TO_BINARY_H
2
+ #define FASTFLOAT_DECIMAL_TO_BINARY_H
3
+
4
+ #include "float_common.h"
5
+ #include "fast_table.h"
6
+ #include <cfloat>
7
+ #include <cinttypes>
8
+ #include <cmath>
9
+ #include <cstdint>
10
+ #include <cstdlib>
11
+ #include <cstring>
12
+
13
+ namespace fast_float {
14
+
15
+ // This will compute or rather approximate w * 5**q and return a pair of 64-bit
16
+ // words approximating the result, with the "high" part corresponding to the
17
+ // most significant bits and the low part corresponding to the least significant
18
+ // bits.
19
+ //
20
+ template <int bit_precision>
21
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 value128
22
+ compute_product_approximation(int64_t q, uint64_t w) {
23
+ int const index = 2 * int(q - powers::smallest_power_of_five);
24
+ // For small values of q, e.g., q in [0,27], the answer is always exact
25
+ // because The line value128 firstproduct = full_multiplication(w,
26
+ // power_of_five_128[index]); gives the exact answer.
27
+ value128 firstproduct =
28
+ full_multiplication(w, powers::power_of_five_128[index]);
29
+ static_assert((bit_precision >= 0) && (bit_precision <= 64),
30
+ " precision should be in (0,64]");
31
+ constexpr uint64_t precision_mask =
32
+ (bit_precision < 64) ? (uint64_t(0xFFFFFFFFFFFFFFFF) >> bit_precision)
33
+ : uint64_t(0xFFFFFFFFFFFFFFFF);
34
+ if ((firstproduct.high & precision_mask) ==
35
+ precision_mask) { // could further guard with (lower + w < lower)
36
+ // regarding the second product, we only need secondproduct.high, but our
37
+ // expectation is that the compiler will optimize this extra work away if
38
+ // needed.
39
+ value128 secondproduct =
40
+ full_multiplication(w, powers::power_of_five_128[index + 1]);
41
+ firstproduct.low += secondproduct.high;
42
+ if (secondproduct.high > firstproduct.low) {
43
+ firstproduct.high++;
44
+ }
45
+ }
46
+ return firstproduct;
47
+ }
48
+
49
+ namespace detail {
50
+ /**
51
+ * For q in (0,350), we have that
52
+ * f = (((152170 + 65536) * q ) >> 16);
53
+ * is equal to
54
+ * floor(p) + q
55
+ * where
56
+ * p = log(5**q)/log(2) = q * log(5)/log(2)
57
+ *
58
+ * For negative values of q in (-400,0), we have that
59
+ * f = (((152170 + 65536) * q ) >> 16);
60
+ * is equal to
61
+ * -ceil(p) + q
62
+ * where
63
+ * p = log(5**-q)/log(2) = -q * log(5)/log(2)
64
+ */
65
+ constexpr fastfloat_really_inline int32_t power(int32_t q) noexcept {
66
+ return (((152170 + 65536) * q) >> 16) + 63;
67
+ }
68
+ } // namespace detail
69
+
70
+ // create an adjusted mantissa, biased by the invalid power2
71
+ // for significant digits already multiplied by 10 ** q.
72
+ template <typename binary>
73
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR14 adjusted_mantissa
74
+ compute_error_scaled(int64_t q, uint64_t w, int lz) noexcept {
75
+ int hilz = int(w >> 63) ^ 1;
76
+ adjusted_mantissa answer;
77
+ answer.mantissa = w << hilz;
78
+ int bias = binary::mantissa_explicit_bits() - binary::minimum_exponent();
79
+ answer.power2 = int32_t(detail::power(int32_t(q)) + bias - hilz - lz - 62 +
80
+ invalid_am_bias);
81
+ return answer;
82
+ }
83
+
84
+ // w * 10 ** q, without rounding the representation up.
85
+ // the power2 in the exponent will be adjusted by invalid_am_bias.
86
+ template <typename binary>
87
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
88
+ compute_error(int64_t q, uint64_t w) noexcept {
89
+ int lz = leading_zeroes(w);
90
+ w <<= lz;
91
+ value128 product =
92
+ compute_product_approximation<binary::mantissa_explicit_bits() + 3>(q, w);
93
+ return compute_error_scaled<binary>(q, product.high, lz);
94
+ }
95
+
96
+ // Computers w * 10 ** q.
97
+ // The returned value should be a valid number that simply needs to be
98
+ // packed. However, in some very rare cases, the computation will fail. In such
99
+ // cases, we return an adjusted_mantissa with a negative power of 2: the caller
100
+ // should recompute in such cases.
101
+ template <typename binary>
102
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
103
+ compute_float(int64_t q, uint64_t w) noexcept {
104
+ adjusted_mantissa answer;
105
+ if ((w == 0) || (q < binary::smallest_power_of_ten())) {
106
+ answer.power2 = 0;
107
+ answer.mantissa = 0;
108
+ // result should be zero
109
+ return answer;
110
+ }
111
+ if (q > binary::largest_power_of_ten()) {
112
+ // we want to get infinity:
113
+ answer.power2 = binary::infinite_power();
114
+ answer.mantissa = 0;
115
+ return answer;
116
+ }
117
+ // At this point in time q is in [powers::smallest_power_of_five,
118
+ // powers::largest_power_of_five].
119
+
120
+ // We want the most significant bit of i to be 1. Shift if needed.
121
+ int lz = leading_zeroes(w);
122
+ w <<= lz;
123
+
124
+ // The required precision is binary::mantissa_explicit_bits() + 3 because
125
+ // 1. We need the implicit bit
126
+ // 2. We need an extra bit for rounding purposes
127
+ // 3. We might lose a bit due to the "upperbit" routine (result too small,
128
+ // requiring a shift)
129
+
130
+ value128 product =
131
+ compute_product_approximation<binary::mantissa_explicit_bits() + 3>(q, w);
132
+ // The computed 'product' is always sufficient.
133
+ // Mathematical proof:
134
+ // Noble Mushtak and Daniel Lemire, Fast Number Parsing Without Fallback (to
135
+ // appear) See script/mushtak_lemire.py
136
+
137
+ // The "compute_product_approximation" function can be slightly slower than a
138
+ // branchless approach: value128 product = compute_product(q, w); but in
139
+ // practice, we can win big with the compute_product_approximation if its
140
+ // additional branch is easily predicted. Which is best is data specific.
141
+ int upperbit = int(product.high >> 63);
142
+ int shift = upperbit + 64 - binary::mantissa_explicit_bits() - 3;
143
+
144
+ answer.mantissa = product.high >> shift;
145
+
146
+ answer.power2 = int32_t(detail::power(int32_t(q)) + upperbit - lz -
147
+ binary::minimum_exponent());
148
+ if (answer.power2 <= 0) { // we have a subnormal?
149
+ // Here have that answer.power2 <= 0 so -answer.power2 >= 0
150
+ if (-answer.power2 + 1 >=
151
+ 64) { // if we have more than 64 bits below the minimum exponent, you
152
+ // have a zero for sure.
153
+ answer.power2 = 0;
154
+ answer.mantissa = 0;
155
+ // result should be zero
156
+ return answer;
157
+ }
158
+ // next line is safe because -answer.power2 + 1 < 64
159
+ answer.mantissa >>= -answer.power2 + 1;
160
+ // Thankfully, we can't have both "round-to-even" and subnormals because
161
+ // "round-to-even" only occurs for powers close to 0 in the 32-bit and
162
+ // and 64-bit case (with no more than 19 digits).
163
+ answer.mantissa += (answer.mantissa & 1); // round up
164
+ answer.mantissa >>= 1;
165
+ // There is a weird scenario where we don't have a subnormal but just.
166
+ // Suppose we start with 2.2250738585072013e-308, we end up
167
+ // with 0x3fffffffffffff x 2^-1023-53 which is technically subnormal
168
+ // whereas 0x40000000000000 x 2^-1023-53 is normal. Now, we need to round
169
+ // up 0x3fffffffffffff x 2^-1023-53 and once we do, we are no longer
170
+ // subnormal, but we can only know this after rounding.
171
+ // So we only declare a subnormal if we are smaller than the threshold.
172
+ answer.power2 =
173
+ (answer.mantissa < (uint64_t(1) << binary::mantissa_explicit_bits()))
174
+ ? 0
175
+ : 1;
176
+ return answer;
177
+ }
178
+
179
+ // usually, we round *up*, but if we fall right in between and and we have an
180
+ // even basis, we need to round down
181
+ // We are only concerned with the cases where 5**q fits in single 64-bit word.
182
+ if ((product.low <= 1) && (q >= binary::min_exponent_round_to_even()) &&
183
+ (q <= binary::max_exponent_round_to_even()) &&
184
+ ((answer.mantissa & 3) == 1)) { // we may fall between two floats!
185
+ // To be in-between two floats we need that in doing
186
+ // answer.mantissa = product.high >> (upperbit + 64 -
187
+ // binary::mantissa_explicit_bits() - 3);
188
+ // ... we dropped out only zeroes. But if this happened, then we can go
189
+ // back!!!
190
+ if ((answer.mantissa << shift) == product.high) {
191
+ answer.mantissa &= ~uint64_t(1); // flip it so that we do not round up
192
+ }
193
+ }
194
+
195
+ answer.mantissa += (answer.mantissa & 1); // round up
196
+ answer.mantissa >>= 1;
197
+ if (answer.mantissa >= (uint64_t(2) << binary::mantissa_explicit_bits())) {
198
+ answer.mantissa = (uint64_t(1) << binary::mantissa_explicit_bits());
199
+ answer.power2++; // undo previous addition
200
+ }
201
+
202
+ answer.mantissa &= ~(uint64_t(1) << binary::mantissa_explicit_bits());
203
+ if (answer.power2 >= binary::infinite_power()) { // infinity
204
+ answer.power2 = binary::infinite_power();
205
+ answer.mantissa = 0;
206
+ }
207
+ return answer;
208
+ }
209
+
210
+ } // namespace fast_float
211
+
212
+ #endif
@@ -0,0 +1,455 @@
1
+ #ifndef FASTFLOAT_DIGIT_COMPARISON_H
2
+ #define FASTFLOAT_DIGIT_COMPARISON_H
3
+
4
+ #include <algorithm>
5
+ #include <cstdint>
6
+ #include <cstring>
7
+ #include <iterator>
8
+
9
+ #include "float_common.h"
10
+ #include "bigint.h"
11
+ #include "ascii_number.h"
12
+
13
+ namespace fast_float {
14
+
15
+ // 1e0 to 1e19
16
+ constexpr static uint64_t powers_of_ten_uint64[] = {1UL,
17
+ 10UL,
18
+ 100UL,
19
+ 1000UL,
20
+ 10000UL,
21
+ 100000UL,
22
+ 1000000UL,
23
+ 10000000UL,
24
+ 100000000UL,
25
+ 1000000000UL,
26
+ 10000000000UL,
27
+ 100000000000UL,
28
+ 1000000000000UL,
29
+ 10000000000000UL,
30
+ 100000000000000UL,
31
+ 1000000000000000UL,
32
+ 10000000000000000UL,
33
+ 100000000000000000UL,
34
+ 1000000000000000000UL,
35
+ 10000000000000000000UL};
36
+
37
+ // calculate the exponent, in scientific notation, of the number.
38
+ // this algorithm is not even close to optimized, but it has no practical
39
+ // effect on performance: in order to have a faster algorithm, we'd need
40
+ // to slow down performance for faster algorithms, and this is still fast.
41
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR14 int32_t
42
+ scientific_exponent(uint64_t mantissa, int32_t exponent) noexcept {
43
+ while (mantissa >= 10000) {
44
+ mantissa /= 10000;
45
+ exponent += 4;
46
+ }
47
+ while (mantissa >= 100) {
48
+ mantissa /= 100;
49
+ exponent += 2;
50
+ }
51
+ while (mantissa >= 10) {
52
+ mantissa /= 10;
53
+ exponent += 1;
54
+ }
55
+ return exponent;
56
+ }
57
+
58
+ // this converts a native floating-point number to an extended-precision float.
59
+ template <typename T>
60
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
61
+ to_extended(T value) noexcept {
62
+ using equiv_uint = equiv_uint_t<T>;
63
+ constexpr equiv_uint exponent_mask = binary_format<T>::exponent_mask();
64
+ constexpr equiv_uint mantissa_mask = binary_format<T>::mantissa_mask();
65
+ constexpr equiv_uint hidden_bit_mask = binary_format<T>::hidden_bit_mask();
66
+
67
+ adjusted_mantissa am;
68
+ int32_t bias = binary_format<T>::mantissa_explicit_bits() -
69
+ binary_format<T>::minimum_exponent();
70
+ equiv_uint bits;
71
+ #if FASTFLOAT_HAS_BIT_CAST
72
+ bits = std::bit_cast<equiv_uint>(value);
73
+ #else
74
+ ::memcpy(&bits, &value, sizeof(T));
75
+ #endif
76
+ if ((bits & exponent_mask) == 0) {
77
+ // denormal
78
+ am.power2 = 1 - bias;
79
+ am.mantissa = bits & mantissa_mask;
80
+ } else {
81
+ // normal
82
+ am.power2 = int32_t((bits & exponent_mask) >>
83
+ binary_format<T>::mantissa_explicit_bits());
84
+ am.power2 -= bias;
85
+ am.mantissa = (bits & mantissa_mask) | hidden_bit_mask;
86
+ }
87
+
88
+ return am;
89
+ }
90
+
91
+ // get the extended precision value of the halfway point between b and b+u.
92
+ // we are given a native float that represents b, so we need to adjust it
93
+ // halfway between b and b+u.
94
+ template <typename T>
95
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
96
+ to_extended_halfway(T value) noexcept {
97
+ adjusted_mantissa am = to_extended(value);
98
+ am.mantissa <<= 1;
99
+ am.mantissa += 1;
100
+ am.power2 -= 1;
101
+ return am;
102
+ }
103
+
104
+ // round an extended-precision float to the nearest machine float.
105
+ template <typename T, typename callback>
106
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void round(adjusted_mantissa &am,
107
+ callback cb) noexcept {
108
+ int32_t mantissa_shift = 64 - binary_format<T>::mantissa_explicit_bits() - 1;
109
+ if (-am.power2 >= mantissa_shift) {
110
+ // have a denormal float
111
+ int32_t shift = -am.power2 + 1;
112
+ cb(am, std::min<int32_t>(shift, 64));
113
+ // check for round-up: if rounding-nearest carried us to the hidden bit.
114
+ am.power2 = (am.mantissa <
115
+ (uint64_t(1) << binary_format<T>::mantissa_explicit_bits()))
116
+ ? 0
117
+ : 1;
118
+ return;
119
+ }
120
+
121
+ // have a normal float, use the default shift.
122
+ cb(am, mantissa_shift);
123
+
124
+ // check for carry
125
+ if (am.mantissa >=
126
+ (uint64_t(2) << binary_format<T>::mantissa_explicit_bits())) {
127
+ am.mantissa = (uint64_t(1) << binary_format<T>::mantissa_explicit_bits());
128
+ am.power2++;
129
+ }
130
+
131
+ // check for infinite: we could have carried to an infinite power
132
+ am.mantissa &= ~(uint64_t(1) << binary_format<T>::mantissa_explicit_bits());
133
+ if (am.power2 >= binary_format<T>::infinite_power()) {
134
+ am.power2 = binary_format<T>::infinite_power();
135
+ am.mantissa = 0;
136
+ }
137
+ }
138
+
139
+ template <typename callback>
140
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void
141
+ round_nearest_tie_even(adjusted_mantissa &am, int32_t shift,
142
+ callback cb) noexcept {
143
+ uint64_t const mask = (shift == 64) ? UINT64_MAX : (uint64_t(1) << shift) - 1;
144
+ uint64_t const halfway = (shift == 0) ? 0 : uint64_t(1) << (shift - 1);
145
+ uint64_t truncated_bits = am.mantissa & mask;
146
+ bool is_above = truncated_bits > halfway;
147
+ bool is_halfway = truncated_bits == halfway;
148
+
149
+ // shift digits into position
150
+ if (shift == 64) {
151
+ am.mantissa = 0;
152
+ } else {
153
+ am.mantissa >>= shift;
154
+ }
155
+ am.power2 += shift;
156
+
157
+ bool is_odd = (am.mantissa & 1) == 1;
158
+ am.mantissa += uint64_t(cb(is_odd, is_halfway, is_above));
159
+ }
160
+
161
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void
162
+ round_down(adjusted_mantissa &am, int32_t shift) noexcept {
163
+ if (shift == 64) {
164
+ am.mantissa = 0;
165
+ } else {
166
+ am.mantissa >>= shift;
167
+ }
168
+ am.power2 += shift;
169
+ }
170
+
171
+ template <typename UC>
172
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
173
+ skip_zeros(UC const *&first, UC const *last) noexcept {
174
+ uint64_t val;
175
+ while (!cpp20_and_in_constexpr() &&
176
+ std::distance(first, last) >= int_cmp_len<UC>()) {
177
+ ::memcpy(&val, first, sizeof(uint64_t));
178
+ if (val != int_cmp_zeros<UC>()) {
179
+ break;
180
+ }
181
+ first += int_cmp_len<UC>();
182
+ }
183
+ while (first != last) {
184
+ if (*first != UC('0')) {
185
+ break;
186
+ }
187
+ first++;
188
+ }
189
+ }
190
+
191
+ // determine if any non-zero digits were truncated.
192
+ // all characters must be valid digits.
193
+ template <typename UC>
194
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 bool
195
+ is_truncated(UC const *first, UC const *last) noexcept {
196
+ // do 8-bit optimizations, can just compare to 8 literal 0s.
197
+ uint64_t val;
198
+ while (!cpp20_and_in_constexpr() &&
199
+ std::distance(first, last) >= int_cmp_len<UC>()) {
200
+ ::memcpy(&val, first, sizeof(uint64_t));
201
+ if (val != int_cmp_zeros<UC>()) {
202
+ return true;
203
+ }
204
+ first += int_cmp_len<UC>();
205
+ }
206
+ while (first != last) {
207
+ if (*first != UC('0')) {
208
+ return true;
209
+ }
210
+ ++first;
211
+ }
212
+ return false;
213
+ }
214
+
215
+ template <typename UC>
216
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 bool
217
+ is_truncated(span<UC const> s) noexcept {
218
+ return is_truncated(s.ptr, s.ptr + s.len());
219
+ }
220
+
221
+ template <typename UC>
222
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
223
+ parse_eight_digits(UC const *&p, limb &value, size_t &counter,
224
+ size_t &count) noexcept {
225
+ value = value * 100000000 + parse_eight_digits_unrolled(p);
226
+ p += 8;
227
+ counter += 8;
228
+ count += 8;
229
+ }
230
+
231
+ template <typename UC>
232
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void
233
+ parse_one_digit(UC const *&p, limb &value, size_t &counter,
234
+ size_t &count) noexcept {
235
+ value = value * 10 + limb(*p - UC('0'));
236
+ p++;
237
+ counter++;
238
+ count++;
239
+ }
240
+
241
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
242
+ add_native(bigint &big, limb power, limb value) noexcept {
243
+ big.mul(power);
244
+ big.add(value);
245
+ }
246
+
247
+ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
248
+ round_up_bigint(bigint &big, size_t &count) noexcept {
249
+ // need to round-up the digits, but need to avoid rounding
250
+ // ....9999 to ...10000, which could cause a false halfway point.
251
+ add_native(big, 10, 1);
252
+ count++;
253
+ }
254
+
255
+ // parse the significant digits into a big integer
256
+ template <typename UC>
257
+ inline FASTFLOAT_CONSTEXPR20 void
258
+ parse_mantissa(bigint &result, parsed_number_string_t<UC> &num,
259
+ size_t max_digits, size_t &digits) noexcept {
260
+ // try to minimize the number of big integer and scalar multiplication.
261
+ // therefore, try to parse 8 digits at a time, and multiply by the largest
262
+ // scalar value (9 or 19 digits) for each step.
263
+ size_t counter = 0;
264
+ digits = 0;
265
+ limb value = 0;
266
+ #ifdef FASTFLOAT_64BIT_LIMB
267
+ size_t step = 19;
268
+ #else
269
+ size_t step = 9;
270
+ #endif
271
+
272
+ // process all integer digits.
273
+ UC const *p = num.integer.ptr;
274
+ UC const *pend = p + num.integer.len();
275
+ skip_zeros(p, pend);
276
+ // process all digits, in increments of step per loop
277
+ while (p != pend) {
278
+ while ((std::distance(p, pend) >= 8) && (step - counter >= 8) &&
279
+ (max_digits - digits >= 8)) {
280
+ parse_eight_digits(p, value, counter, digits);
281
+ }
282
+ while (counter < step && p != pend && digits < max_digits) {
283
+ parse_one_digit(p, value, counter, digits);
284
+ }
285
+ if (digits == max_digits) {
286
+ // add the temporary value, then check if we've truncated any digits
287
+ add_native(result, limb(powers_of_ten_uint64[counter]), value);
288
+ bool truncated = is_truncated(p, pend);
289
+ if (num.fraction.ptr != nullptr) {
290
+ truncated |= is_truncated(num.fraction);
291
+ }
292
+ if (truncated) {
293
+ round_up_bigint(result, digits);
294
+ }
295
+ return;
296
+ } else {
297
+ add_native(result, limb(powers_of_ten_uint64[counter]), value);
298
+ counter = 0;
299
+ value = 0;
300
+ }
301
+ }
302
+
303
+ // add our fraction digits, if they're available.
304
+ if (num.fraction.ptr != nullptr) {
305
+ p = num.fraction.ptr;
306
+ pend = p + num.fraction.len();
307
+ if (digits == 0) {
308
+ skip_zeros(p, pend);
309
+ }
310
+ // process all digits, in increments of step per loop
311
+ while (p != pend) {
312
+ while ((std::distance(p, pend) >= 8) && (step - counter >= 8) &&
313
+ (max_digits - digits >= 8)) {
314
+ parse_eight_digits(p, value, counter, digits);
315
+ }
316
+ while (counter < step && p != pend && digits < max_digits) {
317
+ parse_one_digit(p, value, counter, digits);
318
+ }
319
+ if (digits == max_digits) {
320
+ // add the temporary value, then check if we've truncated any digits
321
+ add_native(result, limb(powers_of_ten_uint64[counter]), value);
322
+ bool truncated = is_truncated(p, pend);
323
+ if (truncated) {
324
+ round_up_bigint(result, digits);
325
+ }
326
+ return;
327
+ } else {
328
+ add_native(result, limb(powers_of_ten_uint64[counter]), value);
329
+ counter = 0;
330
+ value = 0;
331
+ }
332
+ }
333
+ }
334
+
335
+ if (counter != 0) {
336
+ add_native(result, limb(powers_of_ten_uint64[counter]), value);
337
+ }
338
+ }
339
+
340
+ template <typename T>
341
+ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
342
+ positive_digit_comp(bigint &bigmant, int32_t exponent) noexcept {
343
+ FASTFLOAT_ASSERT(bigmant.pow10(uint32_t(exponent)));
344
+ adjusted_mantissa answer;
345
+ bool truncated;
346
+ answer.mantissa = bigmant.hi64(truncated);
347
+ int bias = binary_format<T>::mantissa_explicit_bits() -
348
+ binary_format<T>::minimum_exponent();
349
+ answer.power2 = bigmant.bit_length() - 64 + bias;
350
+
351
+ round<T>(answer, [truncated](adjusted_mantissa &a, int32_t shift) {
352
+ round_nearest_tie_even(
353
+ a, shift,
354
+ [truncated](bool is_odd, bool is_halfway, bool is_above) -> bool {
355
+ return is_above || (is_halfway && truncated) ||
356
+ (is_odd && is_halfway);
357
+ });
358
+ });
359
+
360
+ return answer;
361
+ }
362
+
363
+ // the scaling here is quite simple: we have, for the real digits `m * 10^e`,
364
+ // and for the theoretical digits `n * 2^f`. Since `e` is always negative,
365
+ // to scale them identically, we do `n * 2^f * 5^-f`, so we now have `m * 2^e`.
366
+ // we then need to scale by `2^(f- e)`, and then the two significant digits
367
+ // are of the same magnitude.
368
+ template <typename T>
369
+ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp(
370
+ bigint &bigmant, adjusted_mantissa am, int32_t exponent) noexcept {
371
+ bigint &real_digits = bigmant;
372
+ int32_t real_exp = exponent;
373
+
374
+ // get the value of `b`, rounded down, and get a bigint representation of b+h
375
+ adjusted_mantissa am_b = am;
376
+ // gcc7 buf: use a lambda to remove the noexcept qualifier bug with
377
+ // -Wnoexcept-type.
378
+ round<T>(am_b,
379
+ [](adjusted_mantissa &a, int32_t shift) { round_down(a, shift); });
380
+ T b;
381
+ to_float(false, am_b, b);
382
+ adjusted_mantissa theor = to_extended_halfway(b);
383
+ bigint theor_digits(theor.mantissa);
384
+ int32_t theor_exp = theor.power2;
385
+
386
+ // scale real digits and theor digits to be same power.
387
+ int32_t pow2_exp = theor_exp - real_exp;
388
+ uint32_t pow5_exp = uint32_t(-real_exp);
389
+ if (pow5_exp != 0) {
390
+ FASTFLOAT_ASSERT(theor_digits.pow5(pow5_exp));
391
+ }
392
+ if (pow2_exp > 0) {
393
+ FASTFLOAT_ASSERT(theor_digits.pow2(uint32_t(pow2_exp)));
394
+ } else if (pow2_exp < 0) {
395
+ FASTFLOAT_ASSERT(real_digits.pow2(uint32_t(-pow2_exp)));
396
+ }
397
+
398
+ // compare digits, and use it to direct rounding
399
+ int ord = real_digits.compare(theor_digits);
400
+ adjusted_mantissa answer = am;
401
+ round<T>(answer, [ord](adjusted_mantissa &a, int32_t shift) {
402
+ round_nearest_tie_even(
403
+ a, shift, [ord](bool is_odd, bool _, bool __) -> bool {
404
+ (void)_; // not needed, since we've done our comparison
405
+ (void)__; // not needed, since we've done our comparison
406
+ if (ord > 0) {
407
+ return true;
408
+ } else if (ord < 0) {
409
+ return false;
410
+ } else {
411
+ return is_odd;
412
+ }
413
+ });
414
+ });
415
+
416
+ return answer;
417
+ }
418
+
419
+ // parse the significant digits as a big integer to unambiguously round
420
+ // the significant digits. here, we are trying to determine how to round
421
+ // an extended float representation close to `b+h`, halfway between `b`
422
+ // (the float rounded-down) and `b+u`, the next positive float. this
423
+ // algorithm is always correct, and uses one of two approaches. when
424
+ // the exponent is positive relative to the significant digits (such as
425
+ // 1234), we create a big-integer representation, get the high 64-bits,
426
+ // determine if any lower bits are truncated, and use that to direct
427
+ // rounding. in case of a negative exponent relative to the significant
428
+ // digits (such as 1.2345), we create a theoretical representation of
429
+ // `b` as a big-integer type, scaled to the same binary exponent as
430
+ // the actual digits. we then compare the big integer representations
431
+ // of both, and use that to direct rounding.
432
+ template <typename T, typename UC>
433
+ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
434
+ digit_comp(parsed_number_string_t<UC> &num, adjusted_mantissa am) noexcept {
435
+ // remove the invalid exponent bias
436
+ am.power2 -= invalid_am_bias;
437
+
438
+ int32_t sci_exp =
439
+ scientific_exponent(num.mantissa, static_cast<int32_t>(num.exponent));
440
+ size_t max_digits = binary_format<T>::max_digits();
441
+ size_t digits = 0;
442
+ bigint bigmant;
443
+ parse_mantissa(bigmant, num, max_digits, digits);
444
+ // can't underflow, since digits is at most max_digits.
445
+ int32_t exponent = sci_exp + 1 - int32_t(digits);
446
+ if (exponent >= 0) {
447
+ return positive_digit_comp<T>(bigmant, exponent);
448
+ } else {
449
+ return negative_digit_comp<T>(bigmant, am, exponent);
450
+ }
451
+ }
452
+
453
+ } // namespace fast_float
454
+
455
+ #endif