1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Shared near-pole guard-digit sizing for the `tan` kernels.
//!
//! `tan(x)` is computed as `sin(r)/cos(r)` on the range-reduced residue
//! `r`. When the input lies close to an odd multiple of π/2 the residue
//! folds toward ±π/2, where `cos(r) → 0` and the quotient diverges. A
//! division at a fixed working scale `w` then amplifies the per-op
//! working-scale rounding error by the quotient magnitude
//! `1/cos(r) ≈ |tan|`. Holding the 0.5 ULP storage contract therefore
//! requires the working scale to carry roughly `log10(|tan|)` extra
//! guard digits beyond the magnitude-1 baseline — the same
//! conditioning class as the exp `2^k` reassembly budget.
//!
//! Reference: Muller, *Elementary Functions: Algorithms and
//! Implementation* (3rd ed., 2016), §11.1 — range-reduction error
//! budget when the reduced function has a pole inside the reduction
//! interval.
/// Extra working-scale guard digits a near-pole `tan` evaluation needs,
/// derived from the bit length of a base-width probe quotient.
///
/// The probe `q = sin(r)/cos(r)` is held at working scale `w0`, i.e.
/// `q ≈ |tan| · 10^{w0}`. Its bit length is
/// `bit_length(q) ≈ log2(|tan|) + w0 · log2(10)`, so
/// `log10(|tan|) ≈ bit_length(q) / log2(10) − w0`.
///
/// `bit_length / log2(10)` is computed in integers via the rational
/// `1 / log2(10) ≈ 100_000 / 332_193`. Anything ≤ 0 means the result is
/// magnitude-1 or smaller (no near-pole amplification) and the caller
/// keeps the base width. A small constant margin absorbs the
/// recompute-stage rounding (one extra `sin_cos_fixed` + `div` at the
/// lifted scale) plus the final narrowing half-LSB.
///
/// Scaling with the *measured* magnitude (not a flat constant) is what
/// keeps the lift cheap for near-pole-but-modest inputs while still
/// covering extreme inputs where `|tan|` is many digits large.
pub