use super::dyadic::{Dyadic, Jammed, constant, nearest64, truncate64, truncate67};
use super::polynomial::{horner, split};
const HALF_PI: u128 = 0x6_487ed511_0b4611a6;
const SINE: [Dyadic; 6] = [
constant(true, 0x5_55555555_55555555, -3),
constant(false, 0x4_44444444_44443e35, -7),
constant(true, 0x6_80680680_6773c774, -13),
constant(false, 0x5_c778e94f_50956d70, -19),
constant(true, 0x6_b991122e_fa0532f0, -26),
constant(false, 0x5_8303f026_14d5e4d8, -33),
];
const COSINE: [Dyadic; 6] = [
constant(true, 0x7_ffffffff_fffffffe, -2),
constant(false, 0x5_55555555_55554277, -5),
constant(true, 0x5_b05b05b0_5a18a1ba, -10),
constant(false, 0x6_80680675_b559f2cf, -16),
constant(true, 0x4_9f93af61_f5349300, -22),
constant(false, 0x4_7a4f2483_514c1af8, -29),
];
const SINE_NEAR: [Dyadic; 4] = [
constant(true, 0x5_55555555_55555445, -3),
constant(false, 0x4_44444444_43a3fdb6, -7),
constant(true, 0x6_8068060b_2044e9ae, -13),
constant(false, 0x5_c75716e6_0f321240, -19),
];
const COSINE_NEAR: [Dyadic; 4] = [
constant(true, 0x7_ffffffff_fffffa28, -2),
constant(false, 0x5_55555555_539cfae6, -5),
constant(true, 0x5_b05b050f_31b2e713, -10),
constant(false, 0x6_803988d5_6e3bff10, -16),
];
const SINE_POINTS: [Dyadic; 8] = [
constant(false, 0x4_4434312d_a70edd92, -1),
constant(false, 0x5_13ace073_ce1aac13, -1),
constant(false, 0x5_cedda037_a95df6ee, -1),
constant(false, 0x6_72daa6ef_3992b586, -1),
constant(false, 0x4_70df5931_ae1d9460, -2),
constant(false, 0x5_646f27e8_bd65cbe4, -2),
constant(false, 0x6_529afa7d_51b12963, -2),
constant(false, 0x7_3a74b8f5_2947b682, -2),
];
const COSINE_POINTS: [Dyadic; 8] = [
constant(false, 0x6_c4741058_a93188ef, -1),
constant(false, 0x6_2ec41697_72401864, -1),
constant(false, 0x5_806149bd_58f7d46d, -1),
constant(false, 0x4_bc044c99_08390c72, -1),
constant(false, 0x7_af8853dd_bbe9efd0, -1),
constant(false, 0x7_882fd26b_35b03d34, -1),
constant(false, 0x7_594fc1cf_900fe89e, -1),
constant(false, 0x7_2316fe33_86a10d5a, -1),
];
pub(super) fn component(x: Dyadic, cosine: bool) -> Jammed {
let (quadrant, r) = reduce(x.magnitude());
let a = r.magnitude();
let selected = (quadrant + u32::from(cosine)) & 3;
let (value, negative) = match (selected & 1 == 0, a.exponent >= -2) {
(true, true) => (Near::new(a).sine(), r.negative),
(false, true) => (Near::new(a).cosine(), false),
(true, false) => (split_sine(a), r.negative),
(false, false) => (split_cosine(a), false),
};
let odd = !cosine && x.negative;
value.signed(negative ^ (selected & 2 != 0) ^ odd)
}
pub(super) fn pair(x: Dyadic) -> (Jammed, Jammed) {
let (quadrant, r) = reduce(x.magnitude());
let a = r.magnitude();
let (s, c) = if a.exponent >= -2 {
let near = Near::new(a);
(near.sine(), near.cosine())
} else {
chained(a)
};
let s = s.signed(r.negative);
let (sine, cosine) = match quadrant {
0 => (s, c),
1 => (c, -s),
2 => (-s, -c),
_ => (-c, s),
};
(if x.negative { -sine } else { sine }, cosine)
}
pub(super) fn tangent(x: Dyadic) -> Jammed {
let (sine, cosine) = pair(x);
truncate67(sine) / truncate67(cosine)
}
fn reduce(x: Dyadic) -> (u32, Dyadic) {
if x.exponent < -1 {
return (0, x);
}
assert!(x.significand << 64 == 0 && x.exponent < 63);
let mut remainder = x.significand >> 64;
let mut quotient = 0u64;
let mut shift = (x.exponent + 3) as u32;
while shift > 0 {
let step = shift.min(60);
remainder <<= step;
quotient = (quotient << step).wrapping_add((remainder / HALF_PI) as u64);
remainder %= HALF_PI;
shift -= step;
}
assert!(remainder != 0 && remainder << 1 != HALF_PI);
let quadrant = (quotient & 3) as u32;
if remainder << 1 < HALF_PI {
(quadrant, Dyadic::new(false, remainder, -66))
} else {
(
(quadrant + 1) & 3,
Dyadic::new(true, HALF_PI - remainder, -66),
)
}
}
fn squares(a: Dyadic) -> (Dyadic, Dyadic) {
let q = truncate67(a * a);
(q, truncate67(q * truncate64(q)))
}
fn split_sine(a: Dyadic) -> Jammed {
let (q, q2) = squares(a);
a + truncate67(nearest64(split(&SINE, q, q2)) * a)
}
fn split_cosine(a: Dyadic) -> Jammed {
let (q, q2) = squares(a);
Dyadic::ONE + truncate67(split(&COSINE, q, q2))
}
fn chained(a: Dyadic) -> (Jammed, Jammed) {
let q = truncate67(a * a);
let sine = a + truncate67(nearest64(horner(&SINE, q) * q) * a);
let cosine = Dyadic::ONE + truncate67(horner(&COSINE, q) * q);
(sine, cosine)
}
struct Near {
sine_point: Dyadic,
cosine_point: Dyadic,
cosine_delta_minus_one: Dyadic,
sine_delta: Dyadic,
}
impl Near {
fn new(a: Dyadic) -> Near {
let (index, point) = if a.exponent < -1 {
let interval = a.floor(4);
(interval as usize, 4 * interval + 2)
} else {
let interval = a.floor(3);
(interval as usize - 4, 8 * interval + 4)
};
let d = (a - Dyadic::new(false, u128::from(point), -6)).exact();
let distance = d.magnitude();
let q = truncate67(distance * distance);
let cosine_delta_minus_one = nearest64(horner(&COSINE_NEAR, q) * q);
let tail = truncate67(truncate67(horner(&SINE_NEAR, q) * q) * distance);
Near {
sine_point: SINE_POINTS[index],
cosine_point: COSINE_POINTS[index],
cosine_delta_minus_one,
sine_delta: nearest64(distance + tail).signed(d.negative),
}
}
fn sine(&self) -> Jammed {
self.sum(self.sine_point, self.cosine_point, self.sine_delta)
}
fn cosine(&self) -> Jammed {
self.sum(self.cosine_point, self.sine_point, -self.sine_delta)
}
fn sum(&self, p: Dyadic, q: Dyadic, s: Dyadic) -> Jammed {
p + truncate67(truncate67(p * self.cosine_delta_minus_one) + truncate67(q * s))
}
}