Lines Matching +full:zero +full:- +full:point
1 //===----------------------------------------------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
18 // clang-format off
51 static constexpr int32_t _Exponent_bits = sizeof(float) * CHAR_BIT - FLT_MANT_DIG;
53 static constexpr int32_t _Maximum_binary_exponent = FLT_MAX_EXP - 1;
54 static constexpr int32_t _Minimum_binary_exponent = FLT_MIN_EXP - 1;
58 static constexpr int32_t _Sign_shift = _Exponent_bits + _Mantissa_bits - 1;
59 static constexpr int32_t _Exponent_shift = _Mantissa_bits - 1;
63 static constexpr uint32_t _Exponent_mask = (1u << _Exponent_bits) - 1;
64 static constexpr uint32_t _Normal_mantissa_mask = (1u << _Mantissa_bits) - 1;
65 static constexpr uint32_t _Denormal_mantissa_mask = (1u << (_Mantissa_bits - 1)) - 1;
66 static constexpr uint32_t _Special_nan_mantissa_mask = 1u << (_Mantissa_bits - 2);
74 static constexpr int32_t _Exponent_bits = sizeof(double) * CHAR_BIT - DBL_MANT_DIG;
76 static constexpr int32_t _Maximum_binary_exponent = DBL_MAX_EXP - 1;
77 static constexpr int32_t _Minimum_binary_exponent = DBL_MIN_EXP - 1;
81 static constexpr int32_t _Sign_shift = _Exponent_bits + _Mantissa_bits - 1;
82 static constexpr int32_t _Exponent_shift = _Mantissa_bits - 1;
86 static constexpr uint64_t _Exponent_mask = (1ULL << _Exponent_bits) - 1;
87 static constexpr uint64_t _Normal_mantissa_mask = (1ULL << _Mantissa_bits) - 1;
88 static constexpr uint64_t _Denormal_mantissa_mask = (1ULL << (_Mantissa_bits - 1)) - 1;
89 static constexpr uint64_t _Special_nan_mantissa_mask = 1ULL << (_Mantissa_bits - 2);
96 // FUNCTION to_chars (FLOATING-POINT TO STRING)
103 // * Later, we'll decrement _Precision when printing each hexit after the decimal point.
105 // The hexits after the decimal point correspond to the explicitly stored fraction bits.
127 … // * with the implicit bit restored (0 for zero values and subnormal values, 1 for normal values).
128 …// * Also calculate the _Unbiased_exponent. This unifies the processing of zero, subnormal, and no…
139 if (_Ieee_exponent == 0) { // zero or subnormal
142 if (_Ieee_mantissa == 0) { // zero
143 // C11 7.21.6.1 "The fprintf function"/8: "If the value is zero, the exponent is zero."
146 _Unbiased_exponent = 1 - _Traits::_Exponent_bias;
150 _Unbiased_exponent = _Ieee_exponent - _Traits::_Exponent_bias;
153 // _Unbiased_exponent is within [-126, 127] for float, [-1022, 1023] for double.
160 _Sign_character = '-';
161 _Absolute_exponent = static_cast<uint32_t>(-_Unbiased_exponent);
186 ptrdiff_t _Buffer_size = _Last - _First;
192 _Buffer_size -= _Precision;
195 … + static_cast<int32_t>(_Precision > 0) // possible decimal point
196 … // excluding `+ _Precision`, hexits after decimal point
197 + 2 // "p+" or "p-"
210 const int _Dropped_bits = (_Full_precision - _Precision) * 4;
212 // Perform rounding by adding an appropriately-shifted bit.
224 …// // If there are no insignificant set bits, the value is exactly-representable and should not…
228 …// // than the midpoint between two exactly-representable values or [2] the value is exactly th…
229 …// // between two exactly-representable values and the greater of the two is even (this is "rou…
233 … // const bool _Round_bit = (_Adjusted_mantissa & (_Uint_type{1} << (_Dropped_bits - 1))) != 0;
234 …nst bool _Has_tail_bits = (_Adjusted_mantissa & ((_Uint_type{1} << (_Dropped_bits - 1)) - 1)) != 0;
257 …// If all of the trailing bits T are 0, then `_Round_bit - 1` will produce 0 for H (due to R being…
258 …// If any of the trailing bits T are 1, then `_Round_bit - 1` will produce 1 for H (due to R being…
259 const _Uint_type _Has_tail_bits = _Round_bit - 1;
261 // Finally, we can use _Should_round_up() logic with bitwise-AND and bitwise-OR,
262 …// selecting just the bit at index _Dropped_bits. This is the appropriately-shifted bit that we wa…
277 constexpr _Uint_type _Mask = (_Uint_type{1} << _Adjusted_explicit_bits) - 1;
281 // * Print the decimal point and trailing hexits.
284 … // "if the precision is zero and the # flag is not specified, no decimal-point character appears."
293 _Number_of_bits_remaining -= 4;
302 --_Precision;
317 const _Uint_type _Mask = (_Uint_type{1} << _Number_of_bits_remaining) - 1;
332 …// We've already printed '-' if necessary, so uint32_t _Absolute_exponent avoids testing that agai…
341 // This prints "1.728p+0" instead of "2.e5p-1".
342 // This prints "0.000002p-126" instead of "1p-149" for float.
343 // This prints "0.0000000000001p-1022" instead of "1p-1074" for double.
344 …// This prioritizes being consistent with printf's de facto behavior (and hex-precision's behavior)
352 if (_Uint_value == 0) { // zero detected; write "0p+0" and return
353 // C11 7.21.6.1 "The fprintf function"/8: "If the value is zero, the exponent is zero."
354 // Special-casing zero is necessary because of the exponent.
358 if (_Last - _First < static_cast<ptrdiff_t>(_Len)) {
375 _Unbiased_exponent = 1 - _Traits::_Exponent_bias;
378 _Unbiased_exponent = _Ieee_exponent - _Traits::_Exponent_bias;
381 … // Performance note: Consider avoiding per-character bounds checking when there's plenty of space.
390 // The fraction bits are all 0. Trim them away, including the decimal point.
398 // The hexits after the decimal point correspond to the explicitly stored fraction bits.
412 …// do-while: The condition _Adjusted_mantissa != 0 is initially true - we have nonzero fraction bi…
413 …// printed the decimal point. Each iteration, we print a hexit, mask it away, and keep looping if …
420 _Number_of_bits_remaining -= 4;
432 const _Uint_type _Mask = (_Uint_type{1} << _Number_of_bits_remaining) - 1;
443 // float: _Unbiased_exponent is within [-126, 127].
444 // double: _Unbiased_exponent is within [-1022, 1023].
446 if (_Last - _First < 2) {
453 *_First++ = '-';
454 _Unbiased_exponent = -_Unbiased_exponent;
459 // We've already printed '-' if necessary, so static_cast<uint32_t> avoids testing that again.
475 // * Too-small integers, like 7, 70, and 99, will cause lower_bound() to return the D == 2 row. (If…
478 // * Too-large integers, like 1'000'000 and above, will cause lower_bound() to return the end of th…
481 // Floating-point is slightly more complicated.
483 // The ordinary lookup tables are for X within [-5, 38] for float, and [-5, 308] for double.
484 // (-5 absorbs too-negative exponents, outside the P > X >= -4 criterion. 38 and 308 are the maximu…
485 // Due to the P > X condition, we can use a subset of the table for X within [-5, P - 1], suitably …
495 // and floating-point granularity limits the number of consecutive '9' digits that can appear.
498 // These tables have varying lengths due to the P > X >= -4 criterion. For example:
499 // For P == 1, need table entries for X: -5, -4, -3, -2, -1, 0
500 // For P == 2, need table entries for X: -5, -4, -3, -2, -1, 0, 1
501 // For P == 3, need table entries for X: -5, -4, -3, -2, -1, 0, 1, 2
502 // For P == 4, need table entries for X: -5, -4, -3, -2, -1, 0, 1, 2, 3
505 // The table for P begins at index (P - 1) * (P + 10) / 2 with length P + 5.
507 … the ordinary and special lookup tables, after an index I is returned by lower_bound(), X is I - 5.
509 // We need to special-case the floating-point value 0.0, which is considered to have X == 0.
512 // Finally, because we're working with positive floating-point values,
513 // representation comparisons behave identically to floating-point comparisons.
540 for (UInt n = last - first; n > 0;) {
546 n = n - n2 - 1;
559 …// C11 7.21.6.1 "The fprintf function"/8 performs trial formatting with scientific precision P - 1.
560 const auto to_result = to_chars(buf, end(buf), flt, chars_format::scientific, P - 1);
607 for (int X = -5; X < P; ++X) {
608 … constexpr Floating first = static_cast<Floating>(9e-5); // well below 9.5e-5, otherwise arbitrary
615 P_X_LargestValWithX[P][X] = val_beyond_X - 1;
632 for (int X = -5; X < P; ++X) {
641 for (int X = -5; X < P; ++X) {
651 for (int X = -5; X < MaxP; ++X) {
667 for (int X = -5; X < P; ++X) {
668 if (P <= MaxSpecialP || P == 25 || P == MaxP || X == P - 1) {
844 …if (_Uint_value == 0) { // zero detected; write "0" and return; _Precision is irrelevant due to ze…
857 // or 1 if the precision is zero."
869 // Due to general notation's zero-trimming behavior, we can simply clamp _Precision.
877 // - if P > X >= -4, the conversion is with style f (or F) and precision P - (X + 1).
878 // - otherwise, the conversion is with style e (or E) and precision P - 1."
881 // and the decimal-point character is removed if there is no fractional portion remaining."
889 _Table_begin = _Tables::_Special_X_table + (_Precision - 1) * (_Precision + 10) / 2;
908 const ptrdiff_t _Table_index = _Table_lower_bound - _Table_begin;
909 const int _Scientific_exponent_X = static_cast<int>(_Table_index - 5);
910 …t bool _Use_fixed_notation = _Precision > _Scientific_exponent_X && _Scientific_exponent_X >= -4;
912 …// Performance note: it might (or might not) be faster to modify Ryu Printf to perform zero-trimmi…
915 …// Here, we're using a simpler approach: writing into a local buffer, manually zero-trimming, and …
916 …// the output range. The necessary buffer size is reasonably small, the zero-trimming logic is sim…
920 …IsSame<_Floating, float>::value ? 117 : 773; // cases: 0x1.fffffep-126f and 0x1.fffffffffffffp-1022
922 … _IsSame<_Floating, float>::value ? 37 : 66; // cases: 0x1.fffffep-14f and 0x1.fffffffffffffp-14
924 …IsSame<_Floating, float>::value ? 111 : 766; // cases: 0x1.fffffep-126f and 0x1.fffffffffffffp-1022
933 const char* _Exponent_first = nullptr; // e.g. "e-05"
935 int _Effective_precision; // number of digits printed after the decimal point, before trimming
940 … _Effective_precision = std::min(_Precision - (_Scientific_exponent_X + 1), _Max_fixed_precision);
946 _Effective_precision = std::min(_Precision - 1, _Max_scientific_precision);
955 // If we printed a decimal point followed by digits, perform zero-trimming.
957 while (_Significand_last[-1] == '0') { // will stop at '.' or a nonzero digit
958 --_Significand_last;
961 if (_Significand_last[-1] == '.') {
962 --_Significand_last;
967 const ptrdiff_t _Significand_distance = _Significand_last - _Significand_first;
968 if (_Last - _First < _Significand_distance) {
976 const ptrdiff_t _Exponent_distance = _Exponent_last - _Exponent_first;
977 if (_Last - _First < _Exponent_distance) {
1014 *_First++ = '-';
1032 … // the UCRT interprets it to mean "indeterminate", and indicates this by printing "-nan(ind)".
1043 if (_Last - _First < static_cast<ptrdiff_t>(_Len)) {
1075 // clang-format on