原生结果series通过字节索引直传,C端使用Ryu精确回读编码和64 KiB批量写出;网页采用Float64缓存和CSV工作线程,减少结果处理与保存等待。 补充八路AME曲线核查、全流程分阶段计时、独立编码基准和复现工具,固定后续优化采用修正八路及rtol=1e-8。C写出1.1808→0.1638 s,点击到可查看8.0100→6.9756 s。 验证:最终10项编码专项、29项相关后端回归通过;8份原生结果逐位一致,16次网页结果/CSV/刷新恢复通过。前端构建及缓存/CSV专项在本轮结果处理工作中通过。环境、原始大结果与临时构建不纳入Git。
115 lines
3.6 KiB
C
115 lines
3.6 KiB
C
// Copyright 2018 Ulf Adams
|
|
//
|
|
// The contents of this file may be used under the terms of the Apache License,
|
|
// Version 2.0.
|
|
//
|
|
// (See accompanying file LICENSE-Apache or copy at
|
|
// http://www.apache.org/licenses/LICENSE-2.0)
|
|
//
|
|
// Alternatively, the contents of this file may be used under the terms of
|
|
// the Boost Software License, Version 1.0.
|
|
// (See accompanying file LICENSE-Boost or copy at
|
|
// https://www.boost.org/LICENSE_1_0.txt)
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, this software
|
|
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied.
|
|
#ifndef RYU_COMMON_H
|
|
#define RYU_COMMON_H
|
|
|
|
#include <assert.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#if defined(_M_IX86) || defined(_M_ARM)
|
|
#define RYU_32_BIT_PLATFORM
|
|
#endif
|
|
|
|
// Returns the number of decimal digits in v, which must not contain more than 9 digits.
|
|
static inline uint32_t decimalLength9(const uint32_t v) {
|
|
// Function precondition: v is not a 10-digit number.
|
|
// (f2s: 9 digits are sufficient for round-tripping.)
|
|
// (d2fixed: We print 9-digit blocks.)
|
|
assert(v < 1000000000);
|
|
if (v >= 100000000) { return 9; }
|
|
if (v >= 10000000) { return 8; }
|
|
if (v >= 1000000) { return 7; }
|
|
if (v >= 100000) { return 6; }
|
|
if (v >= 10000) { return 5; }
|
|
if (v >= 1000) { return 4; }
|
|
if (v >= 100) { return 3; }
|
|
if (v >= 10) { return 2; }
|
|
return 1;
|
|
}
|
|
|
|
// Returns e == 0 ? 1 : [log_2(5^e)]; requires 0 <= e <= 3528.
|
|
static inline int32_t log2pow5(const int32_t e) {
|
|
// This approximation works up to the point that the multiplication overflows at e = 3529.
|
|
// If the multiplication were done in 64 bits, it would fail at 5^4004 which is just greater
|
|
// than 2^9297.
|
|
assert(e >= 0);
|
|
assert(e <= 3528);
|
|
return (int32_t) ((((uint32_t) e) * 1217359) >> 19);
|
|
}
|
|
|
|
// Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
|
|
static inline int32_t pow5bits(const int32_t e) {
|
|
// This approximation works up to the point that the multiplication overflows at e = 3529.
|
|
// If the multiplication were done in 64 bits, it would fail at 5^4004 which is just greater
|
|
// than 2^9297.
|
|
assert(e >= 0);
|
|
assert(e <= 3528);
|
|
return (int32_t) (((((uint32_t) e) * 1217359) >> 19) + 1);
|
|
}
|
|
|
|
// Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
|
|
static inline int32_t ceil_log2pow5(const int32_t e) {
|
|
return log2pow5(e) + 1;
|
|
}
|
|
|
|
// Returns floor(log_10(2^e)); requires 0 <= e <= 1650.
|
|
static inline uint32_t log10Pow2(const int32_t e) {
|
|
// The first value this approximation fails for is 2^1651 which is just greater than 10^297.
|
|
assert(e >= 0);
|
|
assert(e <= 1650);
|
|
return (((uint32_t) e) * 78913) >> 18;
|
|
}
|
|
|
|
// Returns floor(log_10(5^e)); requires 0 <= e <= 2620.
|
|
static inline uint32_t log10Pow5(const int32_t e) {
|
|
// The first value this approximation fails for is 5^2621 which is just greater than 10^1832.
|
|
assert(e >= 0);
|
|
assert(e <= 2620);
|
|
return (((uint32_t) e) * 732923) >> 20;
|
|
}
|
|
|
|
static inline int copy_special_str(char * const result, const bool sign, const bool exponent, const bool mantissa) {
|
|
if (mantissa) {
|
|
memcpy(result, "NaN", 3);
|
|
return 3;
|
|
}
|
|
if (sign) {
|
|
result[0] = '-';
|
|
}
|
|
if (exponent) {
|
|
memcpy(result + sign, "Infinity", 8);
|
|
return sign + 8;
|
|
}
|
|
memcpy(result + sign, "0E0", 3);
|
|
return sign + 3;
|
|
}
|
|
|
|
static inline uint32_t float_to_bits(const float f) {
|
|
uint32_t bits = 0;
|
|
memcpy(&bits, &f, sizeof(float));
|
|
return bits;
|
|
}
|
|
|
|
static inline uint64_t double_to_bits(const double d) {
|
|
uint64_t bits = 0;
|
|
memcpy(&bits, &d, sizeof(double));
|
|
return bits;
|
|
}
|
|
|
|
#endif // RYU_COMMON_H
|