Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
16099{
16100 static_assert(kAlpha >= -60, "internal error");
16101 static_assert(kGamma <= -32, "internal error");
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113 JSON_ASSERT(M_plus.e >= kAlpha);
16114 JSON_ASSERT(M_plus.e <= kGamma);
16115 std::uint64_t delta = diyfp::sub(M_plus, M_minus).f;
16116 std::uint64_t dist = diyfp::sub(M_plus, w ).f;
16117
16118
16119
16120
16121
16122
16123 const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
16124 auto p1 = static_cast<std::uint32_t>(M_plus.f >> -one.e);
16125 std::uint64_t p2 = M_plus.f & (one.f - 1);
16126
16127
16128
16129 JSON_ASSERT(p1 > 0);
16130 std::uint32_t pow10{};
16131 const int k = find_largest_pow10(p1, pow10);
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149 int n = k;
16150
16151 while (n > 0)
16152 {
16153
16154
16155
16156
16157 const std::uint32_t d = p1 / pow10;
16158 const std::uint32_t r = p1 % pow10;
16159
16160
16161
16162
16163 JSON_ASSERT(d <= 9);
16164 buffer[length++] = static_cast<char>('0' + d);
16165
16166
16167
16168 p1 = r;
16169 n--;
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182 const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
16183
16184 if (rest <= delta)
16185 {
16186
16187 decimal_exponent += n;
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197 const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
16198 grisu2_round(buffer, length, dist, delta, rest, ten_n);
16199 return;
16200 }
16201
16202 pow10 /= 10;
16203
16204
16205
16206 }
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246 JSON_ASSERT(p2 > delta);
16247 int m = 0;
16248
16249 for (;;)
16250 {
16251
16252
16253
16254
16255
16256
16257 JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
16258 p2 *= 10;
16259 const std::uint64_t d = p2 >> -one.e;
16260 const std::uint64_t r = p2 & (one.f - 1);
16261
16262
16263
16264
16265
16266 JSON_ASSERT(d <= 9);
16267 buffer[length++] = static_cast<char>('0' + d);
16268
16269
16270
16271 p2 = r;
16272 m++;
16273
16274
16275
16276
16277
16278
16279
16280
16281 delta *= 10;
16282 dist *= 10;
16283
16284 if (p2 <= delta)
16285 {
16286 break;
16287 }
16288 }
16289
16290
16291 decimal_exponent -= m;
16292
16293
16294
16295
16296
16297
16298 const std::uint64_t ten_m = one.f;
16299 grisu2_round(buffer, length, dist, delta, p2, ten_m);
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313}