fix(memhub,gzip): 内存池 OOM 归属画像与 free 拒绝现场;gzip 注解 *= 词法粘连
memhub: alloc 失败打印归属 site/ctx 与存活块 order 分布(新增 dump_alive_orders);free 被 _is_valid_ptr 拒绝时首次打印现场;接通 big_alloc_count gzip: ub: uchar*= buf 补空格(*= 被词法合并为单 OP,注解止于 uchar) 新增 traceback 模块;删除 dbgstack.vp;其余模块随新语法与尺寸口径同步
This commit is contained in:
1 parent
093ff5f66d
commit
db3be6968f
32 files changed
+6870
-3058
No files matched your search
@@ -7,8 +7,9 @@
|
||||
# * Scope and feature set:
|
||||
# * - Fundamental mathematical constants: π, e, π/2, π/4, reciprocal π, 2/π, ln(2), ln(10), 2/sqrt(π), DBL_MAX.
|
||||
# * - Angle conversion: radians ↔ degrees helpers.
|
||||
# * - Trigonometric functions: sin, cos, tan, sinf, cosf, tanf. Argument reduction is performed using subtraction of
|
||||
# * 2π until the value falls into [-π, π]; polynomial approximations are truncated series tuned for the target interval.
|
||||
# * - Trigonometric functions: sin, cos, tan, sinf, cosf, tanf. Argument reduction is O(1): a three-constant
|
||||
# * Cody-Waite step for |x| < 2^20, and a Payne-Hanek step (20-word table of 2/π) above. Both yield r ∈ [-π/4, π/4]
|
||||
# * as a double-double fed to the truncated Taylor series.
|
||||
# * - Inverse trigonometric functions: asin, acos, atan, atan2. asin uses a minimax polynomial suitable for [-1, 1];
|
||||
# * atan relies on a Taylor core limited to |x| ≤ 1 combined with range folding via reciprocal and quadrant logic.
|
||||
# * - Hyperbolic trigonometry: sinh, cosh, tanh, built atop exp and exp(-x).
|
||||
@@ -69,30 +70,33 @@ define U_M_2_SQRT_PI: double = 1.77245385090551602730
|
||||
define DBL_MAX: double = 1.7976931348623157e308
|
||||
define NEG_1E308: double = -1e308
|
||||
|
||||
# Argument reduction constants for sin/cos/tan: pi/2 split into HI+MID+LO and 2/pi (all exact doubles),
|
||||
# plus the 2^20 limit below which Cody-Waite applies (so each fn*HK product stays exact in 53 bits).
|
||||
define U_M_PIO2_HI: double = 1.57079632673412561417e+00
|
||||
define U_M_PIO2_MID: double = 6.07710050650619224932e-11
|
||||
define U_M_PIO2_LO: double = 3.52155986518320018165e-27
|
||||
define U_M_INV_PIO2: double = 6.36619772367581382433e-01
|
||||
define U_M_REDUCE_LIMIT: double = 1048576.0
|
||||
|
||||
def radians(degrees: double) -> double: return degrees * (U_M_PI / 180.0)
|
||||
def degrees(radians: double) -> double: return radians * (180.0 / U_M_PI)
|
||||
|
||||
def sin(x: double) -> double:
|
||||
while x > U_M_PI:
|
||||
x -= 2.0 * U_M_PI
|
||||
while x < -U_M_PI:
|
||||
x += 2.0 * U_M_PI
|
||||
def _sin_core(x: double) -> double:
|
||||
"""sin(r) Taylor series, 8 terms, valid on |r| <= pi/4."""
|
||||
x2: double = x * x
|
||||
result: double = x
|
||||
term: double = x
|
||||
coeffs: const [8]double = [6.0, 20.0, 42.0, 72.0, 110.0, 156.0, 210.0, 272.0]
|
||||
sign: double = -1.0
|
||||
coeffs: const [8]double = [6.0, 20.0, 42.0, 72.0, 110.0, 156.0, 210.0, 272.0]
|
||||
for c in coeffs:
|
||||
term = term * x2 / c
|
||||
result += sign * term
|
||||
sign = -sign
|
||||
return result
|
||||
|
||||
def cos(x: double) -> double:
|
||||
while x > U_M_PI:
|
||||
x -= 2.0 * U_M_PI
|
||||
while x < -U_M_PI:
|
||||
x += 2.0 * U_M_PI
|
||||
|
||||
def _cos_core(x: double) -> double:
|
||||
"""cos(r) Taylor series, 8 terms, valid on |r| <= pi/4."""
|
||||
x2: double = x * x
|
||||
result: double = 1.0
|
||||
term: double = 1.0
|
||||
@@ -104,6 +108,154 @@ def cos(x: double) -> double:
|
||||
sign = -sign
|
||||
return result
|
||||
|
||||
|
||||
def _mul_add(a: uint64, b: uint64, c: uint64, lo: rawptr[uint64], hi: rawptr[uint64]):
|
||||
"""Write (hi:lo) = a*b + c using 32-bit limbs for the full 128-bit product."""
|
||||
a0: uint64 = a & 0xFFFFFFFF
|
||||
a1: uint64 = a >> 32
|
||||
b0: uint64 = b & 0xFFFFFFFF
|
||||
b1: uint64 = b >> 32
|
||||
w0: uint64 = a0 * b0
|
||||
t: uint64 = a1 * b0 + (w0 >> 32)
|
||||
w1: uint64 = t & 0xFFFFFFFF
|
||||
w2: uint64 = t >> 32
|
||||
w1 = a0 * b1 + w1
|
||||
h: uint64 = a1 * b1 + w2 + (w1 >> 32)
|
||||
l: uint64 = (w1 << 32) | (w0 & 0xFFFFFFFF)
|
||||
l2: uint64 = l + c
|
||||
if l2 < l: h = h + 1
|
||||
*lo = l2
|
||||
*hi = h
|
||||
|
||||
|
||||
def _two_prod(a: double, b: double, ph: rawptr[double], pl: rawptr[double]):
|
||||
"""Write a*b as the exact double-double (ph + pl) via Dekker splitting (no FMA required)."""
|
||||
split: double = 134217729.0
|
||||
p: double = a * b
|
||||
ca: double = split * a
|
||||
ahi: double = ca - (ca - a)
|
||||
alo: double = a - ahi
|
||||
cb: double = split * b
|
||||
bhi: double = cb - (cb - b)
|
||||
blo: double = b - bhi
|
||||
*ph = p
|
||||
*pl = ((ahi * bhi - p) + ahi * blo + alo * bhi) + alo * blo
|
||||
|
||||
|
||||
def _rem_pio2(x: double, rh: rawptr[double], rl: rawptr[double]) -> int:
|
||||
"""O(1) reduction of finite x >= 0: x = n*(pi/2) + r with |r| <= pi/4; returns n mod 4, writes r = rh + rl."""
|
||||
if x < U_M_REDUCE_LIMIT:
|
||||
# Cody-Waite three-step with pi/2 = HI + MID + LO; each fn*HK product is exact for fn < 2^20
|
||||
q: double = x * U_M_INV_PIO2
|
||||
k: int = int(q)
|
||||
if q - double(k) > 0.5: k = k + 1
|
||||
fn: double = double(k)
|
||||
r: double = x - fn * U_M_PIO2_HI
|
||||
w1: double = fn * U_M_PIO2_MID
|
||||
y0: double = r - w1
|
||||
w2: double = fn * U_M_PIO2_LO
|
||||
rhv: double = y0 - w2
|
||||
*rh = rhv
|
||||
*rl = ((r - y0) - w1) + ((y0 - rhv) - w2)
|
||||
return k & 3
|
||||
# Payne-Hanek: a 20-word (1280-bit) 2/pi table; multiply the 53-bit mantissa and read off the fraction
|
||||
twopi: const [20]uint64 = [
|
||||
0xA2F9836E4E441529, 0xFC2757D1F534DDC0, 0xDB6295993C439041, 0xFE5163ABDEBBC561,
|
||||
0xB7246E3A424DD2E0, 0x06492EEA09D1921C, 0xFE1DEB1CB129A73E, 0xE88235F52EBB4484,
|
||||
0xE99C7026B45F7E41, 0x3991D639835339F4, 0x9C845F8BBDF9283B, 0x1FF897FFDE05980F,
|
||||
0xEF2F118B5A0A6D1F, 0x6D367ECF27CB09B7, 0x4F463F669E5FEA2D, 0x7527BAC7EBE5F17B,
|
||||
0x3D0739F78A5292EA, 0x6BFB5FB11F8D5D08, 0x56033046FC7B6BAB, 0xF0CFBC209AF4361D
|
||||
]
|
||||
xv: double = x
|
||||
xb: uint64 = 0
|
||||
*&xb = *&xv
|
||||
ef: uint64 = (xb >> 52) & 0x7FF
|
||||
m: uint64 = (xb & 0xFFFFFFFFFFFFF) | 0x10000000000000
|
||||
e: int = int(ef) - 1075
|
||||
shift: int = 1280 - e
|
||||
qw: int = shift >> 6
|
||||
s: int = shift & 63
|
||||
b: [21]uint64
|
||||
carry: uint64 = 0
|
||||
for j in range(20):
|
||||
lo: uint64 = 0
|
||||
hi: uint64 = 0
|
||||
_mul_add(m, twopi[19 - j], carry, &lo, &hi)
|
||||
b[j] = lo
|
||||
carry = hi
|
||||
b[20] = carry
|
||||
lo2: uint64 = b[qw] >> s
|
||||
if s == 63: lo2 = (b[qw] >> 63) | (b[qw + 1] << 1)
|
||||
k: int = int(lo2 & 3)
|
||||
a1: uint64 = b[qw] & ((uint64(1) << s) - 1)
|
||||
a2: uint64 = b[qw - 1]
|
||||
a3: uint64 = b[qw - 2]
|
||||
fhi: uint64 = a2
|
||||
flo: uint64 = a3
|
||||
if s != 0:
|
||||
sh: int = 64 - s
|
||||
fhi = (a1 << sh) | (a2 >> s)
|
||||
flo = (a2 << sh) | (a3 >> s)
|
||||
# fractional part f = fhi*2^-64 + flo*2^-128; split off the top 53 bits so df is exact
|
||||
f53: uint64 = fhi >> 11
|
||||
df: double = double(f53)
|
||||
de: double = double(fhi - (f53 << 11))
|
||||
fh: double = df * 1.1102230246251565e-16
|
||||
fl: double = de * 5.421010862427522e-20 + double(flo) * 2.9387358770557188e-39
|
||||
r0h: double = fh
|
||||
r0l: double = fl
|
||||
n: int = k
|
||||
if fh > 0.5 or (fh == 0.5 and fl > 0.0):
|
||||
n = (k + 1) & 3
|
||||
r0h = fh - 1.0
|
||||
ph: double = 0.0
|
||||
pl: double = 0.0
|
||||
_two_prod(r0h, U_M_PIO2_HI, &ph, &pl)
|
||||
*rh = ph
|
||||
*rl = pl + r0h * U_M_PIO2_MID + r0h * U_M_PIO2_LO + r0l * U_M_PIO2_HI
|
||||
return n
|
||||
|
||||
|
||||
def sin(x: double) -> double:
|
||||
u: uint64 = 0
|
||||
xv: double = x
|
||||
*&u = *&xv
|
||||
ef: uint64 = (u >> 52) & 0x7FF
|
||||
if ef == 0x7FF: return x - x # inf/nan -> NaN
|
||||
neg: int = int(u >> 63)
|
||||
ax: double = x if neg == 0 else -x
|
||||
rh: double = 0.0
|
||||
rl: double = 0.0
|
||||
n: int = _rem_pio2(ax, &rh, &rl)
|
||||
sr: double = _sin_core(rh) + _cos_core(rh) * rl
|
||||
cr: double = _cos_core(rh) - _sin_core(rh) * rl
|
||||
res: double = 0.0
|
||||
if n == 0: res = sr
|
||||
elif n == 1: res = cr
|
||||
elif n == 2: res = -sr
|
||||
else: res = -cr
|
||||
return -res if neg != 0 else res
|
||||
|
||||
|
||||
def cos(x: double) -> double:
|
||||
u: uint64 = 0
|
||||
xv: double = x
|
||||
*&u = *&xv
|
||||
ef: uint64 = (u >> 52) & 0x7FF
|
||||
if ef == 0x7FF: return x - x # inf/nan -> NaN
|
||||
neg: int = int(u >> 63)
|
||||
ax: double = x if neg == 0 else -x
|
||||
rh: double = 0.0
|
||||
rl: double = 0.0
|
||||
n: int = _rem_pio2(ax, &rh, &rl)
|
||||
sr: double = _sin_core(rh) + _cos_core(rh) * rl
|
||||
cr: double = _cos_core(rh) - _sin_core(rh) * rl
|
||||
if n == 0: return cr
|
||||
elif n == 1: return -sr
|
||||
elif n == 2: return -cr
|
||||
else: return sr
|
||||
|
||||
|
||||
def tan(x: double) -> double: return sin(x) / cos(x)
|
||||
|
||||
|
||||
@@ -364,38 +516,8 @@ def sqrtf(x: float) -> float:
|
||||
guess = (guess + x / guess) / 2.0
|
||||
return guess
|
||||
|
||||
def sinf(x: float) -> float:
|
||||
while x > 3.14159265:
|
||||
x -= 6.28318530
|
||||
while x < -3.14159265:
|
||||
x += 6.28318530
|
||||
x2: float = x * x
|
||||
result: float = x
|
||||
term: float = x
|
||||
sign: float = -1.0
|
||||
coeffs: const [8]float = [6.0, 20.0, 42.0, 72.0, 110.0, 156.0, 210.0, 272.0]
|
||||
for c in coeffs:
|
||||
term = term * x2 / c
|
||||
result += sign * term
|
||||
sign = -sign
|
||||
return result
|
||||
|
||||
|
||||
def cosf(x: float) -> float:
|
||||
while x > 3.14159265:
|
||||
x -= 6.28318530
|
||||
while x < -3.14159265:
|
||||
x += 6.28318530
|
||||
x2: float = x * x
|
||||
result: float = 1.0
|
||||
term: float = 1.0
|
||||
sign: float = -1.0
|
||||
coeffs: const [8]float = [2.0, 12.0, 30.0, 56.0, 90.0, 132.0, 182.0, 240.0]
|
||||
for c in coeffs:
|
||||
term = term * x2 / c
|
||||
result += sign * term
|
||||
sign = -sign
|
||||
return result
|
||||
def sinf(x: float) -> float: return float(sin(double(x)))
|
||||
def cosf(x: float) -> float: return float(cos(double(x)))
|
||||
|
||||
def tanf(x: float) -> float:
|
||||
c: float = cosf(x)
|
||||
|
||||
Reference in new issue
Block a user