Skip to main content

decimal_scaled/
consts_wide.rs

1//! Per-width raw constants for the wide-tier decimal types.
2//!
3//! The raw constants are computed at build time by `build.rs` using a
4//! hand-rolled multi-precision arithmetic core (no external deps).
5//! Each width gets its own `SCALE_REF` matched to its storage's max
6//! precision: D76 at 75 frac digits, D153 at 153, D307 at 307. The
7//! corresponding raw values land in `OUT_DIR/wide_consts.rs` as
8//! decimal-digit strings, parsed at compile time by
9//! `Int*::from_str_radix` (a `const fn`).
10//!
11//! This closes the SCALE > 37 panic in `D76<76>::pi()` etc. and
12//! tightens the 0.5 ULP contract on `DecimalConsts` for every
13//! wide-tier scale.
14
15// These imports are only reachable when at least one wide-tier
16// feature is enabled — every item in this module is per-tier
17// `#[cfg]`-gated below. Narrow-only builds compile the module
18// empty.
19#[cfg(any(feature = "d76", feature = "d153", feature = "d307", feature = "wide", feature = "x-wide"))]
20use crate::wide_int::{Int256, Int512, Int1024};
21#[cfg(any(feature = "d76", feature = "d153", feature = "d307", feature = "wide", feature = "x-wide"))]
22use crate::consts::DecimalConsts;
23
24include!(concat!(env!("OUT_DIR"), "/wide_consts.rs"));
25
26// ─── D76 ─────────────────────────────────────────────────────────────
27
28#[cfg(any(feature = "d76", feature = "wide"))]
29const D76_SCALE_REF: u32 = 75;
30
31#[cfg(any(feature = "d76", feature = "wide"))]
32const PI_RAW_D256: Int256 = match Int256::from_str_radix(PI_D76_S75, 10) {
33    Ok(v) => v,
34    Err(()) => panic!("consts_wide: PI_D76_S75 not parseable"),
35};
36#[cfg(any(feature = "d76", feature = "wide"))]
37const TAU_RAW_D256: Int256 = match Int256::from_str_radix(TAU_D76_S75, 10) {
38    Ok(v) => v,
39    Err(()) => panic!("consts_wide: TAU_D76_S75 not parseable"),
40};
41#[cfg(any(feature = "d76", feature = "wide"))]
42const HALF_PI_RAW_D256: Int256 = match Int256::from_str_radix(HALF_PI_D76_S75, 10) {
43    Ok(v) => v,
44    Err(()) => panic!("consts_wide: HALF_PI_D76_S75 not parseable"),
45};
46#[cfg(any(feature = "d76", feature = "wide"))]
47const QUARTER_PI_RAW_D256: Int256 = match Int256::from_str_radix(QUARTER_PI_D76_S75, 10) {
48    Ok(v) => v,
49    Err(()) => panic!("consts_wide: QUARTER_PI_D76_S75 not parseable"),
50};
51#[cfg(any(feature = "d76", feature = "wide"))]
52const E_RAW_D256: Int256 = match Int256::from_str_radix(E_D76_S75, 10) {
53    Ok(v) => v,
54    Err(()) => panic!("consts_wide: E_D76_S75 not parseable"),
55};
56#[cfg(any(feature = "d76", feature = "wide"))]
57const GOLDEN_RAW_D256: Int256 = match Int256::from_str_radix(GOLDEN_D76_S75, 10) {
58    Ok(v) => v,
59    Err(()) => panic!("consts_wide: GOLDEN_D76_S75 not parseable"),
60};
61
62#[cfg(any(feature = "d76", feature = "wide"))]
63pub(crate) fn pi_at_target_d76<const TARGET: u32>() -> Int256 {
64    use crate::core_type::D76;
65    D76::<D76_SCALE_REF>::from_bits(PI_RAW_D256)
66        .rescale::<TARGET>()
67        .to_bits()
68}
69#[cfg(any(feature = "d76", feature = "wide"))]
70pub(crate) fn tau_at_target_d76<const TARGET: u32>() -> Int256 {
71    use crate::core_type::D76;
72    D76::<D76_SCALE_REF>::from_bits(TAU_RAW_D256)
73        .rescale::<TARGET>()
74        .to_bits()
75}
76#[cfg(any(feature = "d76", feature = "wide"))]
77pub(crate) fn half_pi_at_target_d76<const TARGET: u32>() -> Int256 {
78    use crate::core_type::D76;
79    D76::<D76_SCALE_REF>::from_bits(HALF_PI_RAW_D256)
80        .rescale::<TARGET>()
81        .to_bits()
82}
83#[cfg(any(feature = "d76", feature = "wide"))]
84pub(crate) fn quarter_pi_at_target_d76<const TARGET: u32>() -> Int256 {
85    use crate::core_type::D76;
86    D76::<D76_SCALE_REF>::from_bits(QUARTER_PI_RAW_D256)
87        .rescale::<TARGET>()
88        .to_bits()
89}
90#[cfg(any(feature = "d76", feature = "wide"))]
91pub(crate) fn e_at_target_d76<const TARGET: u32>() -> Int256 {
92    use crate::core_type::D76;
93    D76::<D76_SCALE_REF>::from_bits(E_RAW_D256)
94        .rescale::<TARGET>()
95        .to_bits()
96}
97#[cfg(any(feature = "d76", feature = "wide"))]
98pub(crate) fn golden_at_target_d76<const TARGET: u32>() -> Int256 {
99    use crate::core_type::D76;
100    D76::<D76_SCALE_REF>::from_bits(GOLDEN_RAW_D256)
101        .rescale::<TARGET>()
102        .to_bits()
103}
104
105// ─── D153 ─────────────────────────────────────────────────────────────
106
107#[cfg(any(feature = "d153", feature = "wide"))]
108const D153_SCALE_REF: u32 = 153;
109
110#[cfg(any(feature = "d153", feature = "wide"))]
111const PI_RAW_D512: Int512 = match Int512::from_str_radix(PI_D153_S153, 10) {
112    Ok(v) => v,
113    Err(()) => panic!("consts_wide: PI_D153_S153 not parseable"),
114};
115#[cfg(any(feature = "d153", feature = "wide"))]
116const TAU_RAW_D512: Int512 = match Int512::from_str_radix(TAU_D153_S153, 10) {
117    Ok(v) => v,
118    Err(()) => panic!("consts_wide: TAU_D153_S153 not parseable"),
119};
120#[cfg(any(feature = "d153", feature = "wide"))]
121const HALF_PI_RAW_D512: Int512 = match Int512::from_str_radix(HALF_PI_D153_S153, 10) {
122    Ok(v) => v,
123    Err(()) => panic!("consts_wide: HALF_PI_D153_S153 not parseable"),
124};
125#[cfg(any(feature = "d153", feature = "wide"))]
126const QUARTER_PI_RAW_D512: Int512 = match Int512::from_str_radix(QUARTER_PI_D153_S153, 10) {
127    Ok(v) => v,
128    Err(()) => panic!("consts_wide: QUARTER_PI_D153_S153 not parseable"),
129};
130#[cfg(any(feature = "d153", feature = "wide"))]
131const E_RAW_D512: Int512 = match Int512::from_str_radix(E_D153_S153, 10) {
132    Ok(v) => v,
133    Err(()) => panic!("consts_wide: E_D153_S153 not parseable"),
134};
135#[cfg(any(feature = "d153", feature = "wide"))]
136const GOLDEN_RAW_D512: Int512 = match Int512::from_str_radix(GOLDEN_D153_S153, 10) {
137    Ok(v) => v,
138    Err(()) => panic!("consts_wide: GOLDEN_D153_S153 not parseable"),
139};
140
141#[cfg(any(feature = "d153", feature = "wide"))]
142pub(crate) fn pi_at_target_d153<const TARGET: u32>() -> Int512 {
143    use crate::core_type::D153;
144    D153::<D153_SCALE_REF>::from_bits(PI_RAW_D512)
145        .rescale::<TARGET>()
146        .to_bits()
147}
148#[cfg(any(feature = "d153", feature = "wide"))]
149pub(crate) fn tau_at_target_d153<const TARGET: u32>() -> Int512 {
150    use crate::core_type::D153;
151    D153::<D153_SCALE_REF>::from_bits(TAU_RAW_D512)
152        .rescale::<TARGET>()
153        .to_bits()
154}
155#[cfg(any(feature = "d153", feature = "wide"))]
156pub(crate) fn half_pi_at_target_d153<const TARGET: u32>() -> Int512 {
157    use crate::core_type::D153;
158    D153::<D153_SCALE_REF>::from_bits(HALF_PI_RAW_D512)
159        .rescale::<TARGET>()
160        .to_bits()
161}
162#[cfg(any(feature = "d153", feature = "wide"))]
163pub(crate) fn quarter_pi_at_target_d153<const TARGET: u32>() -> Int512 {
164    use crate::core_type::D153;
165    D153::<D153_SCALE_REF>::from_bits(QUARTER_PI_RAW_D512)
166        .rescale::<TARGET>()
167        .to_bits()
168}
169#[cfg(any(feature = "d153", feature = "wide"))]
170pub(crate) fn e_at_target_d153<const TARGET: u32>() -> Int512 {
171    use crate::core_type::D153;
172    D153::<D153_SCALE_REF>::from_bits(E_RAW_D512)
173        .rescale::<TARGET>()
174        .to_bits()
175}
176#[cfg(any(feature = "d153", feature = "wide"))]
177pub(crate) fn golden_at_target_d153<const TARGET: u32>() -> Int512 {
178    use crate::core_type::D153;
179    D153::<D153_SCALE_REF>::from_bits(GOLDEN_RAW_D512)
180        .rescale::<TARGET>()
181        .to_bits()
182}
183
184// ─── D307 ────────────────────────────────────────────────────────────
185
186#[cfg(any(feature = "d307", feature = "wide"))]
187const D307_SCALE_REF: u32 = 307;
188
189#[cfg(any(feature = "d307", feature = "wide"))]
190const PI_RAW_D1024: Int1024 = match Int1024::from_str_radix(PI_D307_S307, 10) {
191    Ok(v) => v,
192    Err(()) => panic!("consts_wide: PI_D307_S307 not parseable"),
193};
194#[cfg(any(feature = "d307", feature = "wide"))]
195const TAU_RAW_D1024: Int1024 = match Int1024::from_str_radix(TAU_D307_S307, 10) {
196    Ok(v) => v,
197    Err(()) => panic!("consts_wide: TAU_D307_S307 not parseable"),
198};
199#[cfg(any(feature = "d307", feature = "wide"))]
200const HALF_PI_RAW_D1024: Int1024 = match Int1024::from_str_radix(HALF_PI_D307_S307, 10) {
201    Ok(v) => v,
202    Err(()) => panic!("consts_wide: HALF_PI_D307_S307 not parseable"),
203};
204#[cfg(any(feature = "d307", feature = "wide"))]
205const QUARTER_PI_RAW_D1024: Int1024 = match Int1024::from_str_radix(QUARTER_PI_D307_S307, 10) {
206    Ok(v) => v,
207    Err(()) => panic!("consts_wide: QUARTER_PI_D307_S307 not parseable"),
208};
209#[cfg(any(feature = "d307", feature = "wide"))]
210const E_RAW_D1024: Int1024 = match Int1024::from_str_radix(E_D307_S307, 10) {
211    Ok(v) => v,
212    Err(()) => panic!("consts_wide: E_D307_S307 not parseable"),
213};
214#[cfg(any(feature = "d307", feature = "wide"))]
215const GOLDEN_RAW_D1024: Int1024 = match Int1024::from_str_radix(GOLDEN_D307_S307, 10) {
216    Ok(v) => v,
217    Err(()) => panic!("consts_wide: GOLDEN_D307_S307 not parseable"),
218};
219
220#[cfg(any(feature = "d307", feature = "wide"))]
221pub(crate) fn pi_at_target_d307<const TARGET: u32>() -> Int1024 {
222    use crate::core_type::D307;
223    D307::<D307_SCALE_REF>::from_bits(PI_RAW_D1024)
224        .rescale::<TARGET>()
225        .to_bits()
226}
227#[cfg(any(feature = "d307", feature = "wide"))]
228pub(crate) fn tau_at_target_d307<const TARGET: u32>() -> Int1024 {
229    use crate::core_type::D307;
230    D307::<D307_SCALE_REF>::from_bits(TAU_RAW_D1024)
231        .rescale::<TARGET>()
232        .to_bits()
233}
234#[cfg(any(feature = "d307", feature = "wide"))]
235pub(crate) fn half_pi_at_target_d307<const TARGET: u32>() -> Int1024 {
236    use crate::core_type::D307;
237    D307::<D307_SCALE_REF>::from_bits(HALF_PI_RAW_D1024)
238        .rescale::<TARGET>()
239        .to_bits()
240}
241#[cfg(any(feature = "d307", feature = "wide"))]
242pub(crate) fn quarter_pi_at_target_d307<const TARGET: u32>() -> Int1024 {
243    use crate::core_type::D307;
244    D307::<D307_SCALE_REF>::from_bits(QUARTER_PI_RAW_D1024)
245        .rescale::<TARGET>()
246        .to_bits()
247}
248#[cfg(any(feature = "d307", feature = "wide"))]
249pub(crate) fn e_at_target_d307<const TARGET: u32>() -> Int1024 {
250    use crate::core_type::D307;
251    D307::<D307_SCALE_REF>::from_bits(E_RAW_D1024)
252        .rescale::<TARGET>()
253        .to_bits()
254}
255#[cfg(any(feature = "d307", feature = "wide"))]
256pub(crate) fn golden_at_target_d307<const TARGET: u32>() -> Int1024 {
257    use crate::core_type::D307;
258    D307::<D307_SCALE_REF>::from_bits(GOLDEN_RAW_D1024)
259        .rescale::<TARGET>()
260        .to_bits()
261}
262
263// ─── DecimalConsts impls ──────────────────────────────────────────────
264//
265// These shadow the impls that `decl_decimal_consts!(wide …)` would
266// emit. To avoid duplicate trait impls, the wide-arm macro invocations
267// in `core_type.rs` were removed (search for `decl_decimal_consts!(wide`).
268
269#[cfg(any(feature = "d76", feature = "wide"))]
270impl<const SCALE: u32> DecimalConsts for crate::core_type::D76<SCALE> {
271    #[inline] fn pi() -> Self { Self(pi_at_target_d76::<SCALE>()) }
272    #[inline] fn tau() -> Self { Self(tau_at_target_d76::<SCALE>()) }
273    #[inline] fn half_pi() -> Self { Self(half_pi_at_target_d76::<SCALE>()) }
274    #[inline] fn quarter_pi() -> Self { Self(quarter_pi_at_target_d76::<SCALE>()) }
275    #[inline] fn golden() -> Self { Self(golden_at_target_d76::<SCALE>()) }
276    #[inline] fn e() -> Self { Self(e_at_target_d76::<SCALE>()) }
277}
278
279#[cfg(any(feature = "d153", feature = "wide"))]
280impl<const SCALE: u32> DecimalConsts for crate::core_type::D153<SCALE> {
281    #[inline] fn pi() -> Self { Self(pi_at_target_d153::<SCALE>()) }
282    #[inline] fn tau() -> Self { Self(tau_at_target_d153::<SCALE>()) }
283    #[inline] fn half_pi() -> Self { Self(half_pi_at_target_d153::<SCALE>()) }
284    #[inline] fn quarter_pi() -> Self { Self(quarter_pi_at_target_d153::<SCALE>()) }
285    #[inline] fn golden() -> Self { Self(golden_at_target_d153::<SCALE>()) }
286    #[inline] fn e() -> Self { Self(e_at_target_d153::<SCALE>()) }
287}
288
289#[cfg(any(feature = "d307", feature = "wide"))]
290impl<const SCALE: u32> DecimalConsts for crate::core_type::D307<SCALE> {
291    #[inline] fn pi() -> Self { Self(pi_at_target_d307::<SCALE>()) }
292    #[inline] fn tau() -> Self { Self(tau_at_target_d307::<SCALE>()) }
293    #[inline] fn half_pi() -> Self { Self(half_pi_at_target_d307::<SCALE>()) }
294    #[inline] fn quarter_pi() -> Self { Self(quarter_pi_at_target_d307::<SCALE>()) }
295    #[inline] fn golden() -> Self { Self(golden_at_target_d307::<SCALE>()) }
296    #[inline] fn e() -> Self { Self(e_at_target_d307::<SCALE>()) }
297}