use core::arch::asm;
static POW10: [f64; 23] = [
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17,
1e18, 1e19, 1e20, 1e21, 1e22,
];
pub fn scale(m: u64, e: i32) -> f64 {
let mi = m as i64;
let mut out = 0f64;
let mut cw = [0u16; 2];
unsafe {
asm!(
"fnstcw word ptr [{cw}]",
"movzx {t:e}, word ptr [{cw}]",
"and {t:e}, 0xF3FF",
"or {t:e}, 0x0300",
"mov word ptr [{cw} + 2], {t:x}",
"fldcw word ptr [{cw} + 2]",
"fld1",
"2:",
"cmp {n:e}, 22",
"jbe 3f",
"fmul qword ptr [{tab} + 176]",
"sub {n:e}, 22",
"jmp 2b",
"3:",
"fmul qword ptr [{tab} + {n} * 8]",
"fild qword ptr [{m}]",
"test {neg:e}, {neg:e}",
"jnz 4f",
"fmul st(0), st(1)",
"jmp 5f",
"4:",
"fdiv st(0), st(1)",
"5:",
"fstp st(1)",
"fstp qword ptr [{out}]",
"fldcw word ptr [{cw}]",
cw = in(reg) cw.as_mut_ptr(),
t = out(reg) _,
n = inout(reg) e.clamp(-5000, 5000).unsigned_abs() as u64 => _,
tab = in(reg) POW10.as_ptr(),
m = in(reg) &mi,
neg = in(reg) (e < 0) as u32,
out = in(reg) &mut out,
out("st(0)") _, out("st(1)") _, out("st(2)") _, out("st(3)") _,
out("st(4)") _, out("st(5)") _, out("st(6)") _, out("st(7)") _,
);
}
out
}
pub fn scale_round(v: f64, k: i32) -> i64 {
let mut out = 0i64;
let mut cw = [0u16; 2];
unsafe {
asm!(
"fnstcw word ptr [{cw}]",
"movzx {t:e}, word ptr [{cw}]",
"and {t:e}, 0xF3FF",
"or {t:e}, 0x0300",
"mov word ptr [{cw} + 2], {t:x}",
"fldcw word ptr [{cw} + 2]",
"fld1",
"2:",
"cmp {n:e}, 22",
"jbe 3f",
"fmul qword ptr [{tab} + 176]",
"sub {n:e}, 22",
"jmp 2b",
"3:",
"fmul qword ptr [{tab} + {n} * 8]",
"fld qword ptr [{v}]",
"test {neg:e}, {neg:e}",
"jnz 4f",
"fmul st(0), st(1)",
"jmp 5f",
"4:",
"fdiv st(0), st(1)",
"5:",
"fstp st(1)",
"frndint",
"fistp qword ptr [{out}]",
"fldcw word ptr [{cw}]",
cw = in(reg) cw.as_mut_ptr(),
t = out(reg) _,
n = inout(reg) k.clamp(-5000, 5000).unsigned_abs() as u64 => _,
tab = in(reg) POW10.as_ptr(),
v = in(reg) &v,
neg = in(reg) (k < 0) as u32,
out = in(reg) &mut out,
out("st(0)") _, out("st(1)") _, out("st(2)") _, out("st(3)") _,
out("st(4)") _, out("st(5)") _, out("st(6)") _, out("st(7)") _,
);
}
out
}
pub fn log10_round(v: f64) -> i32 {
let mut out = 0i32;
let mut cw = [0u16; 2];
unsafe {
asm!(
"fnstcw word ptr [{cw}]",
"movzx {t:e}, word ptr [{cw}]",
"and {t:e}, 0xF3FF",
"or {t:e}, 0x0300",
"mov word ptr [{cw} + 2], {t:x}",
"fldcw word ptr [{cw} + 2]",
"fldlg2",
"fld qword ptr [{v}]",
"fyl2x",
"fistp dword ptr [{out}]",
"fldcw word ptr [{cw}]",
cw = in(reg) cw.as_mut_ptr(),
t = out(reg) _,
v = in(reg) &v,
out = in(reg) &mut out,
out("st(0)") _, out("st(1)") _, out("st(2)") _, out("st(3)") _,
out("st(4)") _, out("st(5)") _, out("st(6)") _, out("st(7)") _,
);
}
out
}
pub fn atan2(y: f64, x: f64) -> f64 {
let mut out = 0f64;
unsafe {
asm!(
"fld qword ptr [{y}]",
"fld qword ptr [{x}]",
"fpatan",
"fstp qword ptr [{out}]",
y = in(reg) &y,
x = in(reg) &x,
out = in(reg) &mut out,
out("st(0)") _, out("st(1)") _, out("st(2)") _, out("st(3)") _,
out("st(4)") _, out("st(5)") _, out("st(6)") _, out("st(7)") _,
);
}
out
}
pub fn sincos(x: f64) -> (f64, f64) {
let mut c = 0f64;
let mut s = 0f64;
unsafe {
asm!(
"fld qword ptr [{x}]",
"fsincos",
"fstp qword ptr [{c}]",
"fstp qword ptr [{s}]",
x = in(reg) &x,
c = in(reg) &mut c,
s = in(reg) &mut s,
out("st(0)") _, out("st(1)") _, out("st(2)") _, out("st(3)") _,
out("st(4)") _, out("st(5)") _, out("st(6)") _, out("st(7)") _,
);
}
(c, s)
}
pub fn pow(x: f64, y: f64) -> f64 {
const NAN: u64 = 0x7FF8000000000000;
if y.is_nan() {
return f64::from_bits(NAN);
}
if y == 0.0 {
return 1.0;
}
if x.is_nan() {
return f64::from_bits(NAN);
}
if x <= 0.0 {
return 0.0;
}
if y == 1.0 {
return x;
}
let mut out = 0f64;
let mut cw = [0u16; 2];
unsafe {
asm!(
"fnstcw word ptr [{cw}]",
"movzx {t:e}, word ptr [{cw}]",
"and {t:e}, 0xF3FF",
"or {t:e}, 0x0300",
"mov word ptr [{cw} + 2], {t:x}",
"fldcw word ptr [{cw} + 2]",
"fld qword ptr [{y}]",
"fld qword ptr [{x}]",
"fyl2x",
"fld st(0)",
"frndint",
"fsub st(1), st(0)",
"fxch",
"f2xm1",
"fld1",
"faddp",
"fscale",
"fstp st(1)",
"fstp qword ptr [{out}]",
"fldcw word ptr [{cw}]",
cw = in(reg) cw.as_mut_ptr(),
t = out(reg) _,
x = in(reg) &x,
y = in(reg) &y,
out = in(reg) &mut out,
out("st(0)") _, out("st(1)") _, out("st(2)") _, out("st(3)") _,
out("st(4)") _, out("st(5)") _, out("st(6)") _, out("st(7)") _,
);
}
out
}
pub fn rz_log2(x: f64) -> f64 {
let mut out = 0f64;
unsafe {
asm!(
"fld1",
"fld qword ptr [{x}]",
"fyl2x",
"fstp qword ptr [{out}]",
x = in(reg) &x,
out = in(reg) &mut out,
out("st(0)") _, out("st(1)") _, out("st(2)") _, out("st(3)") _,
out("st(4)") _, out("st(5)") _, out("st(6)") _, out("st(7)") _,
);
}
out
}
pub fn rz_exp2(x: f64) -> f64 {
let i = crate::fx::cvtd(x);
let f = x - i as f64;
let mut out = 0f64;
unsafe {
asm!(
"fild dword ptr [{i}]",
"fld qword ptr [{f}]",
"f2xm1",
"fld1",
"faddp",
"fscale",
"fstp qword ptr [{out}]",
"fstp st(0)",
i = in(reg) &i,
f = in(reg) &f,
out = in(reg) &mut out,
out("st(0)") _, out("st(1)") _, out("st(2)") _, out("st(3)") _,
out("st(4)") _, out("st(5)") _, out("st(6)") _, out("st(7)") _,
);
}
out
}
macro_rules! x87_unary {
($(#[$m:meta])* $name:ident, $($ins:literal),*) => {
$(#[$m])*
pub fn $name(x: f64) -> f64 {
let mut out = 0f64;
let mut cw = [0u16; 2];
unsafe {
asm!(
"fnstcw word ptr [{cw}]",
"movzx {t:e}, word ptr [{cw}]",
"and {t:e}, 0xF3FF",
"or {t:e}, 0x0300",
"mov word ptr [{cw} + 2], {t:x}",
"fldcw word ptr [{cw} + 2]",
$($ins,)*
"fstp qword ptr [{out}]",
"fldcw word ptr [{cw}]",
cw = in(reg) cw.as_mut_ptr(),
t = out(reg) _,
x = in(reg) &x,
out = in(reg) &mut out,
out("st(0)") _, out("st(1)") _, out("st(2)") _, out("st(3)") _,
out("st(4)") _, out("st(5)") _, out("st(6)") _, out("st(7)") _,
);
}
out
}
};
}
x87_unary!(
exp2, "fld qword ptr [{x}]", "fld st(0)", "frndint", "fsub st(1), st(0)", "fxch", "f2xm1", "fld1",
"faddp", "fscale", "fstp st(1)"
);
x87_unary!(
log2, "fld1", "fld qword ptr [{x}]", "fyl2x"
);
x87_unary!(
cos, "fld qword ptr [{x}]", "fcos"
);
x87_unary!(
sin, "fld qword ptr [{x}]", "fsin"
);