omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / research / atemporal / at1 / evaluator / src / dd.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Double-double arithmetic (about 106 bits), deterministic on every IEEE-754 |
| 17 | //! platform: only +, -, *, / and fused multiply-add (f64::mul_add, which is |
| 18 | //! correctly rounded by definition). Used by the shadow model when a phase is |
| 19 | //! not a quarter turn (then exact Gaussian-rational arithmetic is impossible). |
| 20 | |
| 21 | use crate::big::Int; |
| 22 | use crate::rat::Q; |
| 23 | |
| 24 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 25 | pub struct Dd { |
| 26 | pub hi: f64, |
| 27 | pub lo: f64, |
| 28 | } |
| 29 | |
| 30 | fn two_sum(a: f64, b: f64) -> (f64, f64) { |
| 31 | let s = a + b; |
| 32 | let bb = s - a; |
| 33 | (s, (a - (s - bb)) + (b - bb)) |
| 34 | } |
| 35 | fn quick_two_sum(a: f64, b: f64) -> (f64, f64) { |
| 36 | let s = a + b; |
| 37 | (s, b - (s - a)) |
| 38 | } |
| 39 | fn two_prod(a: f64, b: f64) -> (f64, f64) { |
| 40 | let p = a * b; |
| 41 | (p, a.mul_add(b, -p)) |
| 42 | } |
| 43 | |
| 44 | pub fn ldexp(mut x: f64, mut e: i32) -> f64 { |
| 45 | while e > 1000 { |
| 46 | x *= f64::from_bits(((1000 + 1023) as u64) << 52); |
| 47 | e -= 1000; |
| 48 | } |
| 49 | while e < -1000 { |
| 50 | x *= f64::from_bits(((-1000i32 + 1023) as u64) << 52); |
| 51 | e += 1000; |
| 52 | } |
| 53 | x * f64::from_bits(((e + 1023) as u64) << 52) |
| 54 | } |
| 55 | |
| 56 | pub const PI2: Dd = Dd { hi: 6.283185307179586232e+00, lo: 2.449293598294706414e-16 }; |
| 57 | |
| 58 | impl Dd { |
| 59 | pub fn new(hi: f64) -> Dd { |
| 60 | Dd { hi, lo: 0.0 } |
| 61 | } |
| 62 | pub fn zero() -> Dd { |
| 63 | Dd::new(0.0) |
| 64 | } |
| 65 | pub fn one() -> Dd { |
| 66 | Dd::new(1.0) |
| 67 | } |
| 68 | pub fn add(self, o: Dd) -> Dd { |
| 69 | let (s, e) = two_sum(self.hi, o.hi); |
| 70 | let (t, f) = two_sum(self.lo, o.lo); |
| 71 | let e = e + t; |
| 72 | let (s, e) = quick_two_sum(s, e); |
| 73 | let e = e + f; |
| 74 | let (hi, lo) = quick_two_sum(s, e); |
| 75 | Dd { hi, lo } |
| 76 | } |
| 77 | pub fn neg(self) -> Dd { |
| 78 | Dd { hi: -self.hi, lo: -self.lo } |
| 79 | } |
| 80 | pub fn sub(self, o: Dd) -> Dd { |
| 81 | self.add(o.neg()) |
| 82 | } |
| 83 | pub fn mul(self, o: Dd) -> Dd { |
| 84 | let (p, e) = two_prod(self.hi, o.hi); |
| 85 | let e = e + (self.hi * o.lo + self.lo * o.hi); |
| 86 | let (hi, lo) = quick_two_sum(p, e); |
| 87 | Dd { hi, lo } |
| 88 | } |
| 89 | pub fn mul_f(self, f: f64) -> Dd { |
| 90 | self.mul(Dd::new(f)) |
| 91 | } |
| 92 | pub fn div(self, o: Dd) -> Dd { |
| 93 | let q1 = self.hi / o.hi; |
| 94 | let r = self.sub(o.mul_f(q1)); |
| 95 | let q2 = r.hi / o.hi; |
| 96 | let r = r.sub(o.mul_f(q2)); |
| 97 | let q3 = r.hi / o.hi; |
| 98 | let (hi, lo) = quick_two_sum(q1, q2); |
| 99 | Dd { hi, lo }.add(Dd::new(q3)) |
| 100 | } |
| 101 | pub fn sqrt(self) -> Dd { |
| 102 | if self.hi <= 0.0 { |
| 103 | return Dd::zero(); |
| 104 | } |
| 105 | let x = 1.0 / self.hi.sqrt(); |
| 106 | let ax = self.hi * x; |
| 107 | let (p, e) = two_prod(ax, ax); |
| 108 | let diff = self.sub(Dd { hi: p, lo: e }); |
| 109 | Dd::new(ax).add(Dd::new(diff.hi * (x * 0.5))) |
| 110 | } |
| 111 | pub fn abs(self) -> Dd { |
| 112 | if self.hi < 0.0 || (self.hi == 0.0 && self.lo < 0.0) { |
| 113 | self.neg() |
| 114 | } else { |
| 115 | self |
| 116 | } |
| 117 | } |
| 118 | pub fn to_q(self) -> Q { |
| 119 | Q::from_f64_bits(self.hi.to_bits()).add(&Q::from_f64_bits(self.lo.to_bits())) |
| 120 | } |
| 121 | /// Conversion of an exact rational, relative error about 2^-110. |
| 122 | pub fn from_q(q: &Q) -> Dd { |
| 123 | if q.is_zero() { |
| 124 | return Dd::zero(); |
| 125 | } |
| 126 | let neg = q.n.is_neg(); |
| 127 | let n = q.n.abs(); |
| 128 | let d = &q.d; |
| 129 | let k: i64 = 120 + d.bits() as i64 - n.bits() as i64; |
| 130 | let big = if k >= 0 { n.shl(k as u32).divrem(d).0 } else { n.divrem(&d.shl((-k) as u32)).0 }; |
| 131 | let (top, sh) = big.top_bits(53); |
| 132 | let hi_int = Int::from_u64(top).shl(sh as u32); |
| 133 | let rest = big.sub(&hi_int); |
| 134 | let (rtop, rsh) = rest.top_bits(53); |
| 135 | let hi = ldexp(top as f64, sh - k as i32); |
| 136 | let lo = ldexp(rtop as f64, rsh - k as i32); |
| 137 | let r = Dd::new(hi).add(Dd::new(lo)); |
| 138 | if neg { |
| 139 | r.neg() |
| 140 | } else { |
| 141 | r |
| 142 | } |
| 143 | } |
| 144 | /// floor of hi part as an exact integer adjustment for turn reduction. |
| 145 | pub fn frac_turn(self) -> Dd { |
| 146 | let f = self.hi.floor(); |
| 147 | let r = self.sub(Dd::new(f)); |
| 148 | if r.hi < 0.0 { |
| 149 | r.add(Dd::one()) |
| 150 | } else if r.hi >= 1.0 { |
| 151 | r.sub(Dd::one()) |
| 152 | } else { |
| 153 | r |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// (cos, sin) of 2*pi*x for x in [0, 1/4] by Taylor series in double-double. |
| 159 | fn cos_sin_quarter(x: Dd) -> (Dd, Dd) { |
| 160 | let t = PI2.mul(x); |
| 161 | let t2 = t.mul(t); |
| 162 | // sin |
| 163 | let mut term = t; |
| 164 | let mut s = t; |
| 165 | let mut n = 1.0; |
| 166 | for _ in 0..40 { |
| 167 | term = term.mul(t2).div(Dd::new((n + 1.0) * (n + 2.0))).neg(); |
| 168 | n += 2.0; |
| 169 | s = s.add(term); |
| 170 | if term.hi.abs() < 1e-36 { |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | let mut term = Dd::one(); |
| 175 | let mut c = Dd::one(); |
| 176 | let mut n = 0.0; |
| 177 | for _ in 0..40 { |
| 178 | term = term.mul(t2).div(Dd::new((n + 1.0) * (n + 2.0))).neg(); |
| 179 | n += 2.0; |
| 180 | c = c.add(term); |
| 181 | if term.hi.abs() < 1e-36 { |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | (c, s) |
| 186 | } |
| 187 | |
| 188 | /// (cos, sin) of 2*pi*turns for any turn count (reduced modulo 1 first). |
| 189 | pub fn cos_sin_turns(turns: Dd) -> (Dd, Dd) { |
| 190 | let f = turns.frac_turn(); |
| 191 | let q4 = (f.hi * 4.0).floor().max(0.0).min(3.0); |
| 192 | let r = f.sub(Dd::new(q4 * 0.25)); |
| 193 | let (c, s) = cos_sin_quarter(r); |
| 194 | match q4 as i32 { |
| 195 | 0 => (c, s), |
| 196 | 1 => (s.neg(), c), |
| 197 | 2 => (c.neg(), s.neg()), |
| 198 | _ => (s, c.neg()), |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /// Exact reduction of a rational turn count, then (cos, sin). |
| 203 | pub fn cos_sin_turns_q(turns: &Q) -> (Dd, Dd) { |
| 204 | let f = turns.mod1(); |
| 205 | let f4 = f.mul(&Q::int(4)); |
| 206 | if f4.is_int() { |
| 207 | let k = f4.n.low_u64(); |
| 208 | return match k { |
| 209 | 0 => (Dd::one(), Dd::zero()), |
| 210 | 1 => (Dd::zero(), Dd::one()), |
| 211 | 2 => (Dd::new(-1.0), Dd::zero()), |
| 212 | _ => (Dd::zero(), Dd::new(-1.0)), |
| 213 | }; |
| 214 | } |
| 215 | cos_sin_turns(Dd::from_q(&f)) |
| 216 | } |
| 217 | |
| 218 | #[cfg(test)] |
| 219 | mod tests { |
| 220 | use super::*; |
| 221 | #[test] |
| 222 | fn dd_basics() { |
| 223 | let third = Dd::from_q(&Q::frac(1, 3)); |
| 224 | let back = third.mul_f(3.0).sub(Dd::one()); |
| 225 | assert!(back.hi.abs() < 1e-31); |
| 226 | let s2 = Dd::new(2.0).sqrt(); |
| 227 | assert!(s2.mul(s2).sub(Dd::new(2.0)).hi.abs() < 1e-30); |
| 228 | // cos(2 pi / 3) = -1/2, sin = sqrt(3)/2 |
| 229 | let (c, s) = cos_sin_turns_q(&Q::frac(1, 3)); |
| 230 | assert!(c.add(Dd::new(0.5)).hi.abs() < 1e-30); |
| 231 | let s3 = Dd::new(3.0).sqrt().mul_f(0.5); |
| 232 | assert!(s.sub(s3).hi.abs() < 1e-30); |
| 233 | let (c, s) = cos_sin_turns_q(&Q::frac(1, 8)); |
| 234 | assert!(c.sub(s).hi.abs() < 1e-30); |
| 235 | let (c, _) = cos_sin_turns_q(&Q::frac(-7, 3)); |
| 236 | assert!(c.add(Dd::new(0.5)).hi.abs() < 1e-30); |
| 237 | let big = Dd::from_q(&Q::frac(524181, 2096716)); |
| 238 | assert!(big.to_q().sub(&Q::frac(524181, 2096716)).abs().cmp(&Q::frac(1, 1i64 << 62)) == std::cmp::Ordering::Less); |
| 239 | } |
| 240 | } |