use crate::dd::{two_sum, DD};
use crate::dv::{self, DV};
use crate::dw;
use crate::od::{Limb, OD};
use crate::ov::OV;
use crate::qd::QD;
use crate::qv::{self, QV};
use crate::simd::Simd;
use crate::v8::{M8, V8};
pub mod mutate {
pub const RED_TAIL: u32 = 1;
pub const TAYLOR_HALF: u32 = 2;
pub const LN_STEP: u32 = 3;
pub const LN_START: u32 = 4;
pub const LN_SERIES: u32 = 5;
pub const N: u32 = 5;
pub const LANE_STEP: u32 = 6;
pub const LANE_POW2_POLY: u32 = 7;
pub const LANE_POW2_FT: u32 = 8;
pub const LANE_POW2_SERIES: u32 = 9;
pub const LANE_NAN: u32 = 10;
pub const NAMES: [&str; 11] = [
"none",
"exp: ln 2's tail cut to one part",
"exp: Taylor polynomial at half its degree",
"ln: Newton step skipped",
"ln: start off by 2^-20",
"ln: log(1 + c) cut to c",
"lanes: first Horner step skipped",
"lanes: lane 0's powers of two in the Horner sum",
"lanes: lane 0's powers of two in ln's f t",
"lanes: lane 0's powers of two in ln's series",
"lanes: NaN lanes from the lanes' arithmetic, NaN signs flipped",
];
#[cfg(feature = "mutate")]
thread_local!(static ID: std::cell::Cell<u32> = const { std::cell::Cell::new(0) });
#[cfg(feature = "mutate")]
pub fn set(id: u32) {
ID.with(|m| m.set(id));
}
#[cfg(feature = "mutate")]
#[inline(always)]
pub fn on(id: u32) -> bool {
ID.with(|m| m.get() == id)
}
#[cfg(not(feature = "mutate"))]
#[inline(always)]
pub fn on(_id: u32) -> bool {
false
}
}
const EXP_MAX: f64 = 709.79;
const EXP_MIN: f64 = -745.2;
const LN2_HI: f64 = f64::from_bits(0x3fe62e42fefa3800);
const LN2_LO: [f64; 8] = [
f64::from_bits(0x3d2ef35793c76730),
f64::from_bits(0x398f97b57a079a19),
f64::from_bits(0x3619ca62d8b62834),
f64::from_bits(0x32b75b8baafa2be8),
f64::from_bits(0xaf51e277e48514da),
f64::from_bits(0x2bd56554bed2be87),
f64::from_bits(0xa86de25a2a3947d9),
f64::from_bits(0x24c0a213ab9d9489),
];
const LN2_DD: [f64; 2] = [f64::from_bits(0x3fe62e42fefa39ef), f64::from_bits(0x3c7abc9e3b39803f)];
const INV_FACT: [[f64; 8]; 44] = [
[
f64::from_bits(0x3ff0000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
],
[
f64::from_bits(0x3ff0000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
],
[
f64::from_bits(0x3fe0000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
f64::from_bits(0x0000000000000000),
],
[
f64::from_bits(0x3fc5555555555555),
f64::from_bits(0x3c65555555555555),
f64::from_bits(0x3905555555555555),
f64::from_bits(0x35a5555555555555),
f64::from_bits(0x3245555555555555),
f64::from_bits(0x2ee5555555555555),
f64::from_bits(0x2b85555555555555),
f64::from_bits(0x2825555555555555),
],
[
f64::from_bits(0x3fa5555555555555),
f64::from_bits(0x3c45555555555555),
f64::from_bits(0x38e5555555555555),
f64::from_bits(0x3585555555555555),
f64::from_bits(0x3225555555555555),
f64::from_bits(0x2ec5555555555555),
f64::from_bits(0x2b65555555555555),
f64::from_bits(0x2805555555555555),
],
[
f64::from_bits(0x3f81111111111111),
f64::from_bits(0x3c01111111111111),
f64::from_bits(0x3881111111111111),
f64::from_bits(0x3501111111111111),
f64::from_bits(0x3181111111111111),
f64::from_bits(0x2e01111111111111),
f64::from_bits(0x2a81111111111111),
f64::from_bits(0x2701111111111111),
],
[
f64::from_bits(0x3f56c16c16c16c17),
f64::from_bits(0xbbef49f49f49f49f),
f64::from_bits(0xb8827d27d27d27d2),
f64::from_bits(0xb52f49f49f49f49f),
f64::from_bits(0xb1c27d27d27d27d2),
f64::from_bits(0xae6f49f49f49f49f),
f64::from_bits(0xab027d27d27d27d2),
f64::from_bits(0xa7af49f49f49f49f),
],
[
f64::from_bits(0x3f2a01a01a01a01a),
f64::from_bits(0x3b6a01a01a01a01a),
f64::from_bits(0x37aa01a01a01a01a),
f64::from_bits(0x33ea01a01a01a01a),
f64::from_bits(0x302a01a01a01a01a),
f64::from_bits(0x2c6a01a01a01a01a),
f64::from_bits(0x28aa01a01a01a01a),
f64::from_bits(0x24ea01a01a01a01a),
],
[
f64::from_bits(0x3efa01a01a01a01a),
f64::from_bits(0x3b3a01a01a01a01a),
f64::from_bits(0x377a01a01a01a01a),
f64::from_bits(0x33ba01a01a01a01a),
f64::from_bits(0x2ffa01a01a01a01a),
f64::from_bits(0x2c3a01a01a01a01a),
f64::from_bits(0x287a01a01a01a01a),
f64::from_bits(0x24ba01a01a01a01a),
],
[
f64::from_bits(0x3ec71de3a556c734),
f64::from_bits(0xbb6c154f8ddc6c00),
f64::from_bits(0x38071de3a556c734),
f64::from_bits(0xb4ac154f8ddc6c00),
f64::from_bits(0x31471de3a556c734),
f64::from_bits(0xadec154f8ddc6c00),
f64::from_bits(0x2a871de3a556c734),
f64::from_bits(0xa72c154f8ddc6c00),
],
[
f64::from_bits(0x3e927e4fb7789f5c),
f64::from_bits(0x3b3cbbc05b4fa99a),
f64::from_bits(0xb7bc6d278883e8f5),
f64::from_bits(0x34595567d3a50cce),
f64::from_bits(0xb0c69c6f77527ad8),
f64::from_bits(0xad310c717d2332e9),
f64::from_bits(0xa9ccfa2aa85ae0b4),
f64::from_bits(0x26611a5038eccd61),
],
[
f64::from_bits(0x3e5ae64567f544e4),
f64::from_bits(0xbafc062e06d1f209),
f64::from_bits(0x377c7880adcbc46e),
f64::from_bits(0xb415553a6f0fed60),
f64::from_bits(0xb09071c56e0d709d),
f64::from_bits(0xad1d78e373529e26),
f64::from_bits(0xa9b0e7aaaa3f1195),
f64::from_bits(0xa625ab2e38d61b16),
],
[
f64::from_bits(0x3e21eed8eff8d898),
f64::from_bits(0xbac2aec959e14c06),
f64::from_bits(0x3742fb0073dd2d9e),
f64::from_bits(0x33ec71d90b4ab715),
f64::from_bits(0x308297b46da86d3b),
f64::from_bits(0x2d12e097b663c827),
f64::from_bits(0x29be975c71d013de),
f64::from_bits(0x264f8c6f097b8a1a),
],
[
f64::from_bits(0x3de6124613a86d09),
f64::from_bits(0x3a8f28e0cc748ebe),
f64::from_bits(0xb727b2c4c8a840bc),
f64::from_bits(0x33cc71cca1034c07),
f64::from_bits(0xb06cbda1195605c6),
f64::from_bits(0xad0aa25ed03f6b0e),
f64::from_bits(0x29a8660e38e4ef5d),
f64::from_bits(0x2641322fed0980c7),
],
[
f64::from_bits(0x3da93974a8c07c9d),
f64::from_bits(0x3a305d6f8a2efd1f),
f64::from_bits(0x36c3aa3346236a5d),
f64::from_bits(0x336d75f096ea801e),
f64::from_bits(0xafdb17039e931c58),
f64::from_bits(0xac77321dbfeab8cd),
f64::from_bits(0xa91f228a2870173f),
f64::from_bits(0xa5b4424bda122cb9),
],
[
f64::from_bits(0x3d6ae7f3e733b81f),
f64::from_bits(0x39e1d8656b0ee8cb),
f64::from_bits(0xb626e142a138f825),
f64::from_bits(0x32b43c0c38ccdcc6),
f64::from_bits(0xaf5b225fe7ac8e6d),
f64::from_bits(0x2bfa868cce37f24d),
f64::from_bits(0x285f6ca1aa91de98),
f64::from_bits(0x24ffaf1764148fe6),
],
[
f64::from_bits(0x3d2ae7f3e733b81f),
f64::from_bits(0x39a1d8656b0ee8cb),
f64::from_bits(0xb5e6e142a138f825),
f64::from_bits(0x32743c0c38ccdcc6),
f64::from_bits(0xaf1b225fe7ac8e6d),
f64::from_bits(0x2bba868cce37f24d),
f64::from_bits(0x281f6ca1aa91de98),
f64::from_bits(0x24bfaf1764148fe6),
],
[
f64::from_bits(0x3ce952c77030ad4a),
f64::from_bits(0x398ac981465ddc6c),
f64::from_bits(0xb5a588b72e53bc5f),
f64::from_bits(0x3227079e8909271a),
f64::from_bits(0xaecc7cf0c326b1bd),
f64::from_bits(0xab6599514ea5bf6f),
f64::from_bits(0xa7815664674f395b),
f64::from_bits(0x242fe4a25cc5a297),
],
[
f64::from_bits(0x3ca6827863b97d97),
f64::from_bits(0x394eec01221a8b0b),
f64::from_bits(0xb5e568798662118b),
f64::from_bits(0x321f00d8b9e49291),
f64::from_bits(0x2e8f90f119a4b758),
f64::from_bits(0xab2332f2f09354d4),
f64::from_bits(0xa7cc8130593f62de),
f64::from_bits(0x24668e20905276c9),
],
[
f64::from_bits(0x3c62f49b46814157),
f64::from_bits(0x38f2650f61dbdcb4),
f64::from_bits(0xb5969502917cbf3b),
f64::from_bits(0x320e35fbddac4553),
f64::from_bits(0xaea729e1e158c7b1),
f64::from_bits(0xab47d4e1832a5c10),
f64::from_bits(0xa7e137983711ad17),
f64::from_bits(0xa4703fa0d3c7ef66),
],
[
f64::from_bits(0x3c1e542ba4020225),
f64::from_bits(0x387ea72b4afe3c2f),
f64::from_bits(0xb5144020dfd65c8c),
f64::from_bits(0xb186e69b50fc88ab),
f64::from_bits(0xadf0c0c089e97a26),
f64::from_bits(0xaa80b468eeb00d04),
f64::from_bits(0x270ae5b62dd61c65),
f64::from_bits(0xa398690c19647b2a),
],
[
f64::from_bits(0x3bd71b8ef6dcf572),
f64::from_bits(0xb87d043ae40c4647),
f64::from_bits(0x34f486121e81d5fe),
f64::from_bits(0xb192d4ba8e1e64c7),
f64::from_bits(0xae1eacf61a05d3bd),
f64::from_bits(0xaaba0112e90a30d7),
f64::from_bits(0x273ea2e421cbf0ee),
f64::from_bits(0x23d41e29de668edf),
],
[
f64::from_bits(0x3b90ce396db7f853),
f64::from_bits(0xb83aebcdbd20331c),
f64::from_bits(0xb4d38a88578b4d75),
f64::from_bits(0x316c0fbc29694fb8),
f64::from_bits(0xae0a0fb9482f1340),
f64::from_bits(0x2a9e2e59a7156b7c),
f64::from_bits(0xa7247cd3fced75bc),
f64::from_bits(0x23baee36286fbce3),
],
[
f64::from_bits(0x3b4761b41316381a),
f64::from_bits(0xb7d3423c7d91404f),
f64::from_bits(0x346e6135bfc1194a),
f64::from_bits(0xb10ba7b1a3077b39),
f64::from_bits(0x2d9b7bf8dd95d7a8),
f64::from_bits(0x2a31330fbaaae84d),
f64::from_bits(0x26ddee26f924ece4),
f64::from_bits(0x237ddd5d53c82a5b),
],
[
f64::from_bits(0x3aff2cf01972f578),
f64::from_bits(0xb789ada5fcc1ab14),
f64::from_bits(0x342440ce7fd610dc),
f64::from_bits(0xb0c26fcbc204fcd1),
f64::from_bits(0x2d6e7ea849dc9d38),
f64::from_bits(0x29e6eebfa38e8b12),
f64::from_bits(0xa65613b59e762126),
f64::from_bits(0x22f3e3e37dac6e75),
],
[
f64::from_bits(0x3ab3f3ccdd165fa9),
f64::from_bits(0xb7458ddadf344487),
f64::from_bits(0xb3ee8ed8001ad67e),
f64::from_bits(0x30880a5edffcced7),
f64::from_bits(0xad22e223a7c4ba1a),
f64::from_bits(0x29cd44eb76543f36),
f64::from_bits(0x2642b6be453e3aca),
f64::from_bits(0xa2db899e29550d0c),
],
[
f64::from_bits(0x3a688e85fc6a4e5a),
f64::from_bits(0xb7071c37ebd16540),
f64::from_bits(0x33a494676265a364),
f64::from_bits(0xb02397b40007db79),
f64::from_bits(0xacc5dde19cf7f17a),
f64::from_bits(0x2938fe6b9053a1b3),
f64::from_bits(0xa5d519e12143cae4),
f64::from_bits(0x227ead3f9a42560a),
],
[
f64::from_bits(0x3a1d1ab1c2dccea3),
f64::from_bits(0x36a054d0c78aea14),
f64::from_bits(0xb34196bf16c33a56),
f64::from_bits(0x2fef0e65ed04d346),
f64::from_bits(0x2c843a24f850a04f),
f64::from_bits(0x292beceb85f33b63),
f64::from_bits(0xa5cba31a2863d624),
f64::from_bits(0x225781f675ab0344),
],
[
f64::from_bits(0x39d0a18a2635085d),
f64::from_bits(0x366b9e2e28e1aa54),
f64::from_bits(0x330a8549a9d99586),
f64::from_bits(0xaf5141dcc8cc5668),
f64::from_bits(0x2bd09cd93b9b8469),
f64::from_bits(0x284d880d99ac4f03),
f64::from_bits(0x24e4567f1bcd64fe),
f64::from_bits(0xa1800ae73c8e8deb),
],
[
f64::from_bits(0x398259f98b4358ad),
f64::from_bits(0x362eaf8c39dd9bc5),
f64::from_bits(0xb2c6e29990a26fb6),
f64::from_bits(0xaf42d867809b5568),
f64::from_bits(0xabe15e662f862571),
f64::from_bits(0x285afdee944e015d),
f64::from_bits(0xa4f033796509b339),
f64::from_bits(0xa19ac265171cf0cb),
],
[
f64::from_bits(0x3933932c5047d60e),
f64::from_bits(0x35d832b7b530a627),
f64::from_bits(0x3255d2c61f6d124c),
f64::from_bits(0x2eff192b328d82c4),
f64::from_bits(0xab575c254d35b232),
f64::from_bits(0xa7f3379ca17bb8d4),
f64::from_bits(0x2490a3414a95f91f),
f64::from_bits(0x211ba74c079192d8),
],
[
f64::from_bits(0x38e434d2e783f5bc),
f64::from_bits(0x3580b87b91be9aff),
f64::from_bits(0x31fc89db1796db75),
f64::from_bits(0xae98923b7699c8be),
f64::from_bits(0x2af1d668aaebfb77),
f64::from_bits(0x277f3d030c866cc8),
f64::from_bits(0x2415c84cba83e83b),
f64::from_bits(0xa0b34bc621ec3c31),
],
[
f64::from_bits(0x389434d2e783f5bc),
f64::from_bits(0x3530b87b91be9aff),
f64::from_bits(0x31ac89db1796db75),
f64::from_bits(0xae48923b7699c8be),
f64::from_bits(0x2aa1d668aaebfb77),
f64::from_bits(0x272f3d030c866cc8),
f64::from_bits(0x23c5c84cba83e83b),
f64::from_bits(0xa0634bc621ec3c31),
],
[
f64::from_bits(0x3843981254dd0d52),
f64::from_bits(0xb4e2b1f4c8015a2f),
f64::from_bits(0xb18d82af23edb6db),
f64::from_bits(0x2e2a1cd20123a99b),
f64::from_bits(0x2ac958e5a4343645),
f64::from_bits(0xa75c00bd1450f203),
f64::from_bits(0xa3deb344a7d736aa),
f64::from_bits(0x206c819525c289d0),
],
[
f64::from_bits(0x37f2710231c0fd7a),
f64::from_bits(0x3473f8a2b4af9d6b),
f64::from_bits(0x311c32215a9f317e),
f64::from_bits(0xadad451e158a1205),
f64::from_bits(0x2a4cf7b201892024),
f64::from_bits(0xa6363ea01c78d66f),
f64::from_bits(0xa2dc5f4a18322b80),
f64::from_bits(0x1f57d50b7184b411),
],
[
f64::from_bits(0x37a0dc59c716d91f),
f64::from_bits(0x343419e3fad3f031),
f64::from_bits(0x30bd9d7ed1981ffc),
f64::from_bits(0xad3345ea5d66a84b),
f64::from_bits(0xa9c285e2b2f07400),
f64::from_bits(0xa5e45683c242982b),
f64::from_bits(0xa2881c9b83d61924),
f64::from_bits(0x9f2f1fc2debd18ac),
],
[
f64::from_bits(0x374df983290c2ca9),
f64::from_bits(0x33d5835c6895393b),
f64::from_bits(0xb060578f45b1aaae),
f64::from_bits(0xad0281508688972d),
f64::from_bits(0xa99256a427c3a800),
f64::from_bits(0xa59214035757a3b5),
f64::from_bits(0x222c3eeb8766efbf),
f64::from_bits(0x1eb1562eaf26c4ba),
],
[
f64::from_bits(0x36f9ec8d1c94e85b),
f64::from_bits(0xb36670e9d4784ec6),
f64::from_bits(0x30079fe5954939a2),
f64::from_bits(0x2c882e418d9b0c9e),
f64::from_bits(0x29126a441d4e7c8a),
f64::from_bits(0x25b81814bbf34cbb),
f64::from_bits(0x22546b74b00666dd),
f64::from_bits(0x1ee7d7a47b670cc6),
],
[
f64::from_bits(0x36a5d4acb9c0c3ab),
f64::from_bits(0xb326ec2c8f5b13b2),
f64::from_bits(0x2fce2860aaa59188),
f64::from_bits(0xac6866eba0408569),
f64::from_bits(0x29088c546d54f915),
f64::from_bits(0xa59f25e2debdf7ec),
f64::from_bits(0xa2320fa2dfff537d),
f64::from_bits(0x1eb27e3d933e1da3),
],
[
f64::from_bits(0x3651e99449a4bace),
f64::from_bits(0xb2dfefbb89514b3c),
f64::from_bits(0xaf753433f743a2d9),
f64::from_bits(0x2bf25f70d1395dd7),
f64::from_bits(0xa89a192c8c09ef53),
f64::from_bits(0x252334e2e34edf6f),
f64::from_bits(0xa1c588a6f96d60c8),
f64::from_bits(0x1e305d79ede680e5),
],
[
f64::from_bits(0x35fca8ed42a12ae3),
f64::from_bits(0x31fa07244abad2ab),
f64::from_bits(0x2e9facdac6fb71b7),
f64::from_bits(0xaacca2f486d514e1),
f64::from_bits(0x273f1ff01aaea3dc),
f64::from_bits(0x23dbe90be5f2217e),
f64::from_bits(0xa056515e027fe5d1),
f64::from_bits(0x1ce4be5200b75141),
],
[
f64::from_bits(0x35a65e61c39d0241),
f64::from_bits(0xb23c0ed181727269),
f64::from_bits(0xaebabbd2f56bbc2f),
f64::from_bits(0xab518ff57fdc2e4e),
f64::from_bits(0xa7f36ae11f3e65fa),
f64::from_bits(0xa4925ae2605f425d),
f64::from_bits(0x2126250e608382a2),
f64::from_bits(0x1dc72185254caee6),
],
[
f64::from_bits(0x35510af527530de8),
f64::from_bits(0x31eb626c912ee5c8),
f64::from_bits(0x2e8349f032c6e859),
f64::from_bits(0xab2ec616617f45c6),
f64::from_bits(0x27bd2c9e0c7a83d1),
f64::from_bits(0x245a880b4900483e),
f64::from_bits(0x20e4a06700321962),
f64::from_bits(0x1d8b18f5c51105ad),
],
[
f64::from_bits(0x34f95db45257e512),
f64::from_bits(0x3186e5d72b6f79b9),
f64::from_bits(0x2dcb830cf0b5b5c6),
f64::from_bits(0x2a629276833f5728),
f64::from_bits(0xa70b6b869bcfa969),
f64::from_bits(0xa3a1da2df40a5f99),
f64::from_bits(0xa021b5f4052c22d2),
f64::from_bits(0x1cc237ba717f229d),
],
];
const THIRD: [f64; 8] = [
f64::from_bits(0x3fd5555555555555),
f64::from_bits(0x3c75555555555555),
f64::from_bits(0x3915555555555555),
f64::from_bits(0x35b5555555555555),
f64::from_bits(0x3255555555555555),
f64::from_bits(0x2ef5555555555555),
f64::from_bits(0x2b95555555555555),
f64::from_bits(0x2835555555555555),
];
const LN2_LO_CUT: [f64; 8] = [LN2_LO[0], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
trait Prec: Copy {
const S: i32;
const DEG: usize;
const Q: usize;
const B: i32;
const L: usize = 1;
const ALL: u8 = ((1u32 << Self::L) - 1) as u8;
type M: Copy;
type C: Copy;
fn from_f64(x: f64) -> Self;
#[inline(always)]
fn from_f64s(v: &[f64; 8]) -> Self {
Self::from_f64(v[0])
}
fn his(self) -> [f64; 8];
fn abs_sums(self) -> [f64; 8];
fn konst(c: &[f64; 8]) -> Self;
#[inline(always)]
fn konsts(c: &[&[f64; 8]; 8]) -> Self {
Self::konst(c[0])
}
fn plus(self, b: Self) -> Self;
fn times(self, b: Self) -> Self;
fn plus_one(self) -> Self;
fn times_pwr2(self, b: f64) -> Self;
#[inline(always)]
fn times_pwr2s(self, b: &[f64; 8]) -> Self {
self.times_pwr2(b[0])
}
fn neg(self) -> Self;
fn reduce(self, k: &[f64; 8], lo: &[f64; 8]) -> Self;
fn sub_one(self) -> Self;
fn ln_start(self) -> Self;
fn ln_finish(y0: Self, corr: Self) -> Self;
fn mask(m: u8) -> Self::M;
fn counts(c: &[i32; 8]) -> Self::C;
fn above(c: Self::C, i: i32) -> Self::M;
fn sel(m: Self::M, a: Self, b: Self) -> Self;
}
#[inline(always)]
fn pow2(k: i32) -> f64 {
f64::from_bits(((k + 1023) as u64) << 52)
}
#[inline(always)]
fn expo(h: f64) -> i32 {
((h.to_bits() >> 52) & 0x7ff) as i32 - 1023
}
#[inline(always)]
fn bound_expos<T: Prec>(v: T) -> [i32; 8] {
let s = v.abs_sums();
let mut e = [0; 8];
for j in 0..T::L {
e[j] = expo(s[j] * (1.0 + 2f64.powi(-48)));
}
e
}
#[inline(always)]
fn times_scaled<T: Prec>(a: T, p: &([f64; 8], [f64; 8]), b: T) -> T {
a.times_pwr2s(&p.0).times(b).times_pwr2s(&p.1)
}
#[inline(always)]
fn scale<T: Prec>(x: T, k: &[i32; 8]) -> T {
let (mut a, mut b) = ([1.0; 8], [1.0; 8]);
for j in 0..T::L {
let k1 = k[j] / 2;
(a[j], b[j]) = (pow2(k1), pow2(k[j] - k1));
}
x.times_pwr2s(&a).times_pwr2s(&b)
}
#[inline(always)]
fn most<T: Prec>(c: &[i32; 8]) -> i32 {
let mut m = 0;
for j in 0..T::L {
m = m.max(c[j]);
}
m
}
#[inline(always)]
fn step<T: Prec>(c: T::C, i: i32, new: T, old: T) -> T {
if T::L == 1 {
new
} else {
T::sel(T::above(c, i), new, old)
}
}
#[inline(always)]
fn lane0_pow2<T: Prec>(id: u32, p: &mut ([f64; 8], [f64; 8])) {
if T::L > 1 && mutate::on(id) {
*p = ([p.0[0]; 8], [p.1[0]; 8]);
}
}
#[inline(always)]
fn expm1_red<T: Prec>(x: T) -> ([i32; 8], T) {
let h = x.his();
let (mut k, mut red) = ([0.0; 8], 0u8);
for j in 0..T::L {
k[j] = (h[j] * std::f64::consts::LOG2_E).round();
red |= ((k[j] != 0.0) as u8) << j;
}
let r = if red == 0 {
x
} else {
let lo = if mutate::on(mutate::RED_TAIL) {
&LN2_LO_CUT
} else {
&LN2_LO
};
T::sel(T::mask(red), x.reduce(&k, lo), x)
};
let e = bound_expos(r);
let (mut m, mut er, mut d) = ([0; 8], [0; 8], [1; 8]);
let (mut sc, mut pm) = (0u8, [1.0; 8]);
for j in 0..T::L {
m[j] = (e[j] + T::S + 1).max(0);
if m[j] > 0 {
(sc, pm[j]) = (sc | 1 << j, pow2(-m[j]));
}
er[j] = e[j] - m[j];
let dj = if er[j] >= -1 - T::B {
T::DEG.min((T::B / (-er[j] - 1) + (T::B % (-er[j] - 1) != 0) as i32) as usize) as i32
} else {
1
};
d[j] = if mutate::on(mutate::TAYLOR_HALF) {
(dj / 2).max(1)
} else {
dj
};
}
let r = if sc == 0 {
r
} else {
T::sel(T::mask(sc), r.times_pwr2s(&pm), r)
};
let mut t = r;
let dmax = most::<T>(&d);
if dmax > 1 {
let (mut c, mut poly) = ([&INV_FACT[1]; 8], 0u8);
let mut pr = ([1.0; 8], [1.0; 8]);
for j in 0..T::L {
c[j] = &INV_FACT[d[j] as usize];
if d[j] > 1 {
(poly, pr.0[j], pr.1[j]) = (poly | 1 << j, pow2(-er[j]), pow2(er[j]));
}
}
lane0_pow2::<T>(mutate::LANE_POW2_POLY, &mut pr);
let skip = (T::L > 1 && mutate::on(mutate::LANE_STEP)) as i32;
let dc = T::counts(&d);
let mut q = T::konsts(&c);
for i in (2..dmax).rev() {
let s = T::konst(&INV_FACT[i as usize]).plus(r.times(q));
q = step(dc, i + skip, s, q);
}
let w = r.times(q);
let rs = r.times_pwr2s(&pr.0);
t = T::sel(T::mask(poly), rs.plus(rs.times(w)).times_pwr2s(&pr.1), r);
}
let mc = T::counts(&m);
for i in 0..most::<T>(&m) {
t = step(mc, i, t.times_pwr2(2.0).plus(t.times(t)), t);
}
let mut ki = [0; 8];
for j in 0..T::L {
ki[j] = k[j] as i32;
}
(ki, t)
}
#[inline(always)]
fn exp_of<T: Prec>(x: T) -> T {
let h = x.his();
let (mut sp, mut v) = (0u8, [0.0; 8]);
for j in 0..T::L {
let s = if h[j].is_nan() {
h[j]
} else if h[j] > EXP_MAX {
f64::INFINITY
} else if h[j] <= EXP_MIN {
0.0
} else {
continue;
};
(sp, v[j]) = (sp | 1 << j, s);
}
if sp == T::ALL {
return T::from_f64s(&v);
}
let x = if sp == 0 {
x
} else {
T::sel(T::mask(sp), T::from_f64(0.0), x)
};
let (k, t) = expm1_red(x);
let y = scale(t.plus_one(), &k);
let yh = y.his();
let mut out = sp;
for j in 0..T::L {
if yh[j] == f64::INFINITY && sp >> j & 1 == 0 {
(out, v[j]) = (out | 1 << j, f64::INFINITY);
}
}
if out == 0 {
y
} else {
T::sel(T::mask(out), T::from_f64s(&v), y)
}
}
#[inline(always)]
fn series<T: Prec>(i: usize) -> T {
match i {
2 => T::from_f64(-0.5),
3 => T::konst(&THIRD),
_ => T::from_f64(-0.25),
}
}
#[inline(always)]
fn ln_of<T: Prec>(x: T) -> T {
let h = x.his();
let (mut sp, mut v) = (0u8, [0.0; 8]);
for j in 0..T::L {
let s = if h[j].is_nan() || h[j] < 0.0 {
f64::NAN
} else if h[j] == 0.0 {
f64::NEG_INFINITY
} else if h[j] == f64::INFINITY {
f64::INFINITY
} else {
continue;
};
(sp, v[j]) = (sp | 1 << j, s);
}
if sp == T::ALL {
return T::from_f64s(&v);
}
let x = if sp == 0 {
x
} else {
T::sel(T::mask(sp), T::from_f64(1.0), x)
};
let special = |y: T| {
if sp == 0 {
y
} else {
T::sel(T::mask(sp), T::from_f64s(&v), y)
}
};
let y0 = x.ln_start();
if mutate::on(mutate::LN_STEP) {
return special(y0);
}
let (k, t) = expm1_red(y0.neg());
let f = scale(x, &k).sub_one();
let (ef, et) = (bound_expos(f), bound_expos(t));
let (mut ft, mut pf, mut pt) = (0u8, ([1.0; 8], [1.0; 8]), ([1.0; 8], [1.0; 8]));
for j in 0..T::L {
if ef[j].min(et[j]) >= -T::B - 13 {
ft |= 1 << j;
(pf.0[j], pf.1[j]) = (pow2(-ef[j]), pow2(ef[j]));
(pt.0[j], pt.1[j]) = (pow2(-et[j]), pow2(et[j]));
}
}
lane0_pow2::<T>(mutate::LANE_POW2_FT, &mut pf);
lane0_pow2::<T>(mutate::LANE_POW2_FT, &mut pt);
let s = f.plus(t);
let c = if ft == 0 {
s
} else {
let p = f.times_pwr2s(&pf.0).times(t.times_pwr2s(&pt.0));
T::sel(T::mask(ft), s.plus(p.times_pwr2s(&pf.1).times_pwr2s(&pt.1)), s)
};
let ec = bound_expos(c);
let (mut ser, mut pc) = (0u8, ([1.0; 8], [1.0; 8]));
for j in 0..T::L {
if !(ec[j] < -T::B - 13 || mutate::on(mutate::LN_SERIES)) {
(ser, pc.0[j], pc.1[j]) = (ser | 1 << j, pow2(-ec[j]), pow2(ec[j]));
}
}
lane0_pow2::<T>(mutate::LANE_POW2_SERIES, &mut pc);
let corr = if ser == 0 {
c
} else {
let mut h = series::<T>(T::Q);
for i in (2..T::Q).rev() {
h = series::<T>(i).plus(times_scaled(c, &pc, h));
}
T::sel(
T::mask(ser),
times_scaled(c, &pc, times_scaled(c, &pc, h).plus_one()),
c,
)
};
special(T::ln_finish(y0, corr))
}
#[inline(always)]
fn start_dd(x0: f64, dd: impl Fn() -> DD, half: impl Fn() -> DD) -> DD {
if x0 >= pow2(1023) {
half().ln()
+ DD {
hi: LN2_DD[0],
lo: LN2_DD[1],
}
} else {
dd().ln()
}
}
#[inline(always)]
fn start_dv<S: Simd>(x0: V8<S>, dd: impl Fn() -> DV<S>, half: impl Fn() -> DV<S>) -> DV<S> {
let big = x0.ge(V8::splat(pow2(1023)));
if big.bits() == 0 {
return dd().ln();
}
let y = <DV<S> as Prec>::sel(big, half(), dd()).ln();
let l2 = DV::splat(DD {
hi: LN2_DD[0],
lo: LN2_DD[1],
});
<DV<S> as Prec>::sel(big, y + l2, y)
}
#[doc(hidden)]
#[inline(always)]
pub fn ln_f64(h: f64) -> f64 {
let (m, e) = ln_f64_split(h);
ln_f64_sum(m, e as f64)
}
#[inline(always)]
fn ln_f64_split(h: f64) -> (f64, i32) {
let (h, e0) = if h < f64::MIN_POSITIVE {
(h * pow2(54), -54)
} else {
(h, 0)
};
let mut e = expo(h) + e0;
let mut m = f64::from_bits((h.to_bits() & 0x000f_ffff_ffff_ffff) | 0x3ff0_0000_0000_0000);
if m > std::f64::consts::SQRT_2 {
m *= 0.5;
e += 1;
}
(m, e)
}
#[inline(always)]
fn ln_f64_sum<L: Limb>(m: L, e: L) -> L {
let one = L::splat(1.0);
let s = m.sub(one).div(m.add(one));
let z = s.mul(s);
let mut v = L::splat(LN_SERIES[LN_SERIES.len() - 1]);
for &c in LN_SERIES[..LN_SERIES.len() - 1].iter().rev() {
v = z.fma(v, L::splat(c));
}
let y = e.fma(L::splat(std::f64::consts::LN_2), s.add(s).mul(v));
if mutate::on(mutate::LN_START) {
y.mul(L::splat(1.0 + 2f64.powi(-20)))
} else {
y
}
}
#[inline(always)]
fn ln_f64_lanes<S: Simd>(h: V8<S>) -> V8<S> {
let h = h.to_array();
let (mut m, mut e) = ([0.0; 8], [0.0; 8]);
for j in 0..8 {
let (mj, ej) = ln_f64_split(h[j]);
(m[j], e[j]) = (mj, ej as f64);
}
ln_f64_sum(V8::<S>::from_array(m), V8::from_array(e))
}
const LN_SERIES: [f64; 10] = [
1.0,
1.0 / 3.0,
1.0 / 5.0,
1.0 / 7.0,
1.0 / 9.0,
1.0 / 11.0,
1.0 / 13.0,
1.0 / 15.0,
1.0 / 17.0,
1.0 / 19.0,
];
macro_rules! one_lane {
() => {
type M = bool;
type C = i32;
#[inline(always)]
fn mask(m: u8) -> bool {
m & 1 != 0
}
#[inline(always)]
fn counts(c: &[i32; 8]) -> i32 {
c[0]
}
#[inline(always)]
fn above(c: i32, i: i32) -> bool {
c > i
}
#[inline(always)]
fn sel(m: bool, a: Self, b: Self) -> Self {
if m {
a
} else {
b
}
}
};
}
impl Prec for DD {
const S: i32 = 1;
const DEG: usize = 27;
const Q: usize = 3;
const B: i32 = 118;
one_lane!();
#[inline(always)]
fn from_f64(x: f64) -> DD {
DD::from_f64(x)
}
#[inline(always)]
fn his(self) -> [f64; 8] {
[self.hi; 8]
}
#[inline(always)]
fn abs_sums(self) -> [f64; 8] {
[self.hi.abs() + self.lo.abs(); 8]
}
#[inline(always)]
fn konst(c: &[f64; 8]) -> DD {
DD { hi: c[0], lo: c[1] }
}
#[inline(always)]
fn plus(self, b: DD) -> DD {
self + b
}
#[inline(always)]
fn times(self, b: DD) -> DD {
let (hi, lo) = dw::dw_times_dw2((self.hi, self.lo), (b.hi, b.lo));
DD { hi, lo }
}
#[inline(always)]
fn plus_one(self) -> DD {
let (hi, lo) = dw::dw_plus_fp((self.hi, self.lo), 1.0);
DD { hi, lo }
}
#[inline(always)]
fn times_pwr2(self, b: f64) -> DD {
self.mul_pwr2(b)
}
#[inline(always)]
fn neg(self) -> DD {
-self
}
#[inline(always)]
fn reduce(self, k: &[f64; 8], lo: &[f64; 8]) -> DD {
let k = k[0];
let a = self.hi - k * LN2_HI;
let p = DD { hi: lo[0], lo: lo[1] }.mul_f(-k);
let (hi, lo) = dw::dw_plus_fp(dw::dw_plus_fp((p.hi, p.lo), self.lo), a);
DD { hi, lo }
}
#[inline(always)]
fn sub_one(self) -> DD {
let (h, l) = two_sum(self.hi - 1.0, self.lo);
DD { hi: h, lo: l }
}
#[inline(always)]
fn ln_start(self) -> DD {
DD::from_f64(ln_f64(self.hi) + self.lo / self.hi)
}
#[inline(always)]
fn ln_finish(y0: DD, corr: DD) -> DD {
let (hi, lo) = dw::dw_plus_fp((corr.hi, corr.lo), y0.hi);
DD { hi, lo }
}
}
impl Prec for QD {
const S: i32 = 6;
const DEG: usize = 24;
const Q: usize = 2;
const B: i32 = 224;
one_lane!();
#[inline(always)]
fn from_f64(x: f64) -> QD {
QD::from_f64(x)
}
#[inline(always)]
fn his(self) -> [f64; 8] {
[self.x[0]; 8]
}
#[inline(always)]
fn abs_sums(self) -> [f64; 8] {
[self.x.iter().fold(0.0, |a, v| a + v.abs()); 8]
}
#[inline(always)]
fn konst(c: &[f64; 8]) -> QD {
QD {
x: [c[0], c[1], c[2], c[3]],
}
}
#[inline(always)]
fn plus(self, b: QD) -> QD {
self + b
}
#[inline(always)]
fn times(self, b: QD) -> QD {
self * b
}
#[inline(always)]
fn plus_one(self) -> QD {
self.add_f(1.0)
}
#[inline(always)]
fn times_pwr2(self, b: f64) -> QD {
self.mul_pwr2(b)
}
#[inline(always)]
fn neg(self) -> QD {
-self
}
#[inline(always)]
fn reduce(self, k: &[f64; 8], lo: &[f64; 8]) -> QD {
let (x, k) = (self.x, k[0]);
QD::from_sum(x[0] - k * LN2_HI, x[1], x[2], x[3]) + QD::konst(lo).mul_f(-k)
}
#[inline(always)]
fn sub_one(self) -> QD {
let x = self.x;
QD::from_sum(x[0] - 1.0, x[1], x[2], x[3])
}
#[inline(always)]
fn ln_start(self) -> QD {
QD::from_dd(start_dd(self.x[0], || self.to_dd(), || self.times_pwr2(0.5).to_dd()))
}
#[inline(always)]
fn ln_finish(y0: QD, corr: QD) -> QD {
y0 + corr
}
}
impl Prec for OD {
const S: i32 = 6;
const DEG: usize = 43;
const Q: usize = 4;
const B: i32 = 436;
one_lane!();
#[inline(always)]
fn from_f64(x: f64) -> OD {
OD::from_f64(x)
}
#[inline(always)]
fn his(self) -> [f64; 8] {
[self.x[0]; 8]
}
#[inline(always)]
fn abs_sums(self) -> [f64; 8] {
[self.x.iter().fold(0.0, |a, v| a + v.abs()); 8]
}
#[inline(always)]
fn konst(c: &[f64; 8]) -> OD {
OD { x: *c }
}
#[inline(always)]
fn plus(self, b: OD) -> OD {
self + b
}
#[inline(always)]
fn times(self, b: OD) -> OD {
self * b
}
#[inline(always)]
fn plus_one(self) -> OD {
self.add_f(1.0)
}
#[inline(always)]
fn times_pwr2(self, b: f64) -> OD {
self.mul_pwr2(b)
}
#[inline(always)]
fn neg(self) -> OD {
-self
}
#[inline(always)]
fn reduce(self, k: &[f64; 8], lo: &[f64; 8]) -> OD {
let (mut x, k) = (self, k[0]);
x.x[0] = self.x[0] - k * LN2_HI;
x + OD { x: *lo }.mul_f(-k)
}
#[inline(always)]
fn sub_one(self) -> OD {
self.add_f(-1.0)
}
#[inline(always)]
fn ln_start(self) -> OD {
OD::from_dd(start_dd(self.x[0], || self.to_dd(), || self.times_pwr2(0.5).to_dd()))
}
#[inline(always)]
fn ln_finish(y0: OD, corr: OD) -> OD {
corr.add_dd(DD {
hi: y0.x[0],
lo: y0.x[1],
})
}
}
#[inline(always)]
fn lane_mask<S: Simd>(m: u8) -> M8<S> {
let mut a = [0.0; 8];
for j in 0..8 {
a[j] = (m >> j & 1) as f64;
}
V8::<S>::from_array(a).gt(V8::zero())
}
#[inline(always)]
fn from_f64s_parts<S: Simd, const N: usize>(v: &[f64; 8]) -> [V8<S>; N] {
let mut r = [V8::zero(); N];
r[0] = V8::from_array(*v);
r
}
#[inline(always)]
fn abs_sums_parts<S: Simd, const N: usize>(x: &[V8<S>; N]) -> [f64; 8] {
let mut s = V8::zero();
for i in 0..N {
s = s.add(x[i].abs());
}
s.to_array()
}
#[inline(always)]
fn konsts_parts<S: Simd, const N: usize>(c: &[&[f64; 8]; 8]) -> [V8<S>; N] {
let mut r = [V8::zero(); N];
for i in 0..N {
let mut a = [0.0; 8];
for j in 0..8 {
a[j] = c[j][i];
}
r[i] = V8::from_array(a);
}
r
}
#[inline(always)]
fn pwr2_parts<S: Simd, const N: usize>(x: &[V8<S>; N], b: &[f64; 8]) -> [V8<S>; N] {
debug_assert!(
b.iter().all(|&v| crate::dd::is_pwr2(v)),
"mul_pwr2: {b:?} are not all powers of two"
);
let b = V8::from_array(*b);
let mut r = *x;
for i in 0..N {
r[i] = x[i].mul(b);
}
r
}
#[inline(always)]
fn sel_parts<S: Simd, const N: usize>(m: M8<S>, a: &[V8<S>; N], b: &[V8<S>; N]) -> [V8<S>; N] {
let mut r = *b;
for i in 0..N {
r[i] = m.sel(a[i], b[i]);
}
r
}
macro_rules! eight_lanes {
($s:ident) => {
const L: usize = 8;
type M = M8<$s>;
type C = V8<$s>;
#[inline(always)]
fn mask(m: u8) -> M8<$s> {
lane_mask(m)
}
#[inline(always)]
fn counts(c: &[i32; 8]) -> V8<$s> {
let mut a = [0.0; 8];
for j in 0..8 {
a[j] = c[j] as f64;
}
V8::from_array(a)
}
#[inline(always)]
fn above(c: V8<$s>, i: i32) -> M8<$s> {
c.gt(V8::splat(i as f64))
}
};
}
impl<S: Simd> Prec for DV<S> {
const S: i32 = <DD as Prec>::S;
const DEG: usize = <DD as Prec>::DEG;
const Q: usize = <DD as Prec>::Q;
const B: i32 = <DD as Prec>::B;
eight_lanes!(S);
#[inline(always)]
fn from_f64(x: f64) -> Self {
DV::splat_f(x)
}
#[inline(always)]
fn from_f64s(v: &[f64; 8]) -> Self {
let [hi, lo] = from_f64s_parts(v);
DV { hi, lo }
}
#[inline(always)]
fn his(self) -> [f64; 8] {
self.hi.to_array()
}
#[inline(always)]
fn abs_sums(self) -> [f64; 8] {
self.hi.abs().add(self.lo.abs()).to_array()
}
#[inline(always)]
fn konst(c: &[f64; 8]) -> Self {
DV::splat(DD { hi: c[0], lo: c[1] })
}
#[inline(always)]
fn konsts(c: &[&[f64; 8]; 8]) -> Self {
let [hi, lo] = konsts_parts(c);
DV { hi, lo }
}
#[inline(always)]
fn plus(self, b: Self) -> Self {
self + b
}
#[inline(always)]
fn times(self, b: Self) -> Self {
let (hi, lo) = dw::dw_times_dw2((self.hi, self.lo), (b.hi, b.lo));
DV { hi, lo }
}
#[inline(always)]
fn plus_one(self) -> Self {
let (hi, lo) = dw::dw_plus_fp((self.hi, self.lo), V8::splat(1.0));
DV { hi, lo }
}
#[inline(always)]
fn times_pwr2(self, b: f64) -> Self {
self.mul_pwr2(b)
}
#[inline(always)]
fn times_pwr2s(self, b: &[f64; 8]) -> Self {
let [hi, lo] = pwr2_parts(&[self.hi, self.lo], b);
DV { hi, lo }
}
#[inline(always)]
fn neg(self) -> Self {
-self
}
#[inline(always)]
fn reduce(self, k: &[f64; 8], lo: &[f64; 8]) -> Self {
let k = V8::from_array(*k);
let a = self.hi.sub(k.mul(V8::splat(LN2_HI)));
let p = DV::splat(DD { hi: lo[0], lo: lo[1] }).mul_v(k.neg());
let (hi, lo) = dw::dw_plus_fp(dw::dw_plus_fp((p.hi, p.lo), self.lo), a);
DV { hi, lo }
}
#[inline(always)]
fn sub_one(self) -> Self {
let (h, l) = dv::two_sum(self.hi.sub(V8::splat(1.0)), self.lo);
DV { hi: h, lo: l }
}
#[inline(always)]
fn ln_start(self) -> Self {
DV {
hi: ln_f64_lanes(self.hi).add(self.lo.div(self.hi)),
lo: V8::zero(),
}
}
#[inline(always)]
fn ln_finish(y0: Self, corr: Self) -> Self {
let (hi, lo) = dw::dw_plus_fp((corr.hi, corr.lo), y0.hi);
DV { hi, lo }
}
#[inline(always)]
fn sel(m: M8<S>, a: Self, b: Self) -> Self {
let [hi, lo] = sel_parts(m, &[a.hi, a.lo], &[b.hi, b.lo]);
DV { hi, lo }
}
}
impl<S: Simd> Prec for QV<S> {
const S: i32 = <QD as Prec>::S;
const DEG: usize = <QD as Prec>::DEG;
const Q: usize = <QD as Prec>::Q;
const B: i32 = <QD as Prec>::B;
eight_lanes!(S);
#[inline(always)]
fn from_f64(x: f64) -> Self {
QV::splat_f(x)
}
#[inline(always)]
fn from_f64s(v: &[f64; 8]) -> Self {
QV { c: from_f64s_parts(v) }
}
#[inline(always)]
fn his(self) -> [f64; 8] {
self.c[0].to_array()
}
#[inline(always)]
fn abs_sums(self) -> [f64; 8] {
abs_sums_parts(&self.c)
}
#[inline(always)]
fn konst(c: &[f64; 8]) -> Self {
QV::splat(<QD as Prec>::konst(c))
}
#[inline(always)]
fn konsts(c: &[&[f64; 8]; 8]) -> Self {
QV { c: konsts_parts(c) }
}
#[inline(always)]
fn plus(self, b: Self) -> Self {
self + b
}
#[inline(always)]
fn times(self, b: Self) -> Self {
self * b
}
#[inline(always)]
fn plus_one(self) -> Self {
self.add_f(1.0)
}
#[inline(always)]
fn times_pwr2(self, b: f64) -> Self {
self.mul_pwr2(b)
}
#[inline(always)]
fn times_pwr2s(self, b: &[f64; 8]) -> Self {
QV {
c: pwr2_parts(&self.c, b),
}
}
#[inline(always)]
fn neg(self) -> Self {
-self
}
#[inline(always)]
fn reduce(self, k: &[f64; 8], lo: &[f64; 8]) -> Self {
let (x, k) = (self.c, V8::from_array(*k));
QV {
c: qv::renorm4_any(x[0].sub(k.mul(V8::splat(LN2_HI))), x[1], x[2], x[3]),
} + QV::splat(<QD as Prec>::konst(lo)).mul_v(k.neg())
}
#[inline(always)]
fn sub_one(self) -> Self {
let x = self.c;
QV {
c: qv::renorm4_any(x[0].sub(V8::splat(1.0)), x[1], x[2], x[3]),
}
}
#[inline(always)]
fn ln_start(self) -> Self {
QV::from_dv(start_dv(self.c[0], || self.to_dv(), || self.mul_pwr2(0.5).to_dv()))
}
#[inline(always)]
fn ln_finish(y0: Self, corr: Self) -> Self {
y0 + corr
}
#[inline(always)]
fn sel(m: M8<S>, a: Self, b: Self) -> Self {
QV {
c: sel_parts(m, &a.c, &b.c),
}
}
}
impl<S: Simd> Prec for OV<S> {
const S: i32 = <OD as Prec>::S;
const DEG: usize = <OD as Prec>::DEG;
const Q: usize = <OD as Prec>::Q;
const B: i32 = <OD as Prec>::B;
eight_lanes!(S);
#[inline(always)]
fn from_f64(x: f64) -> Self {
OV::splat_f(x)
}
#[inline(always)]
fn from_f64s(v: &[f64; 8]) -> Self {
OV { c: from_f64s_parts(v) }
}
#[inline(always)]
fn his(self) -> [f64; 8] {
self.c[0].to_array()
}
#[inline(always)]
fn abs_sums(self) -> [f64; 8] {
abs_sums_parts(&self.c)
}
#[inline(always)]
fn konst(c: &[f64; 8]) -> Self {
OV::splat(OD { x: *c })
}
#[inline(always)]
fn konsts(c: &[&[f64; 8]; 8]) -> Self {
OV { c: konsts_parts(c) }
}
#[inline(always)]
fn plus(self, b: Self) -> Self {
self + b
}
#[inline(always)]
fn times(self, b: Self) -> Self {
self * b
}
#[inline(always)]
fn plus_one(self) -> Self {
self.add_f(1.0)
}
#[inline(always)]
fn times_pwr2(self, b: f64) -> Self {
self.mul_pwr2(b)
}
#[inline(always)]
fn times_pwr2s(self, b: &[f64; 8]) -> Self {
OV {
c: pwr2_parts(&self.c, b),
}
}
#[inline(always)]
fn neg(self) -> Self {
-self
}
#[inline(always)]
fn reduce(self, k: &[f64; 8], lo: &[f64; 8]) -> Self {
let (mut x, k) = (self, V8::from_array(*k));
x.c[0] = self.c[0].sub(k.mul(V8::splat(LN2_HI)));
x + OV::splat(OD { x: *lo }).mul_v(k.neg())
}
#[inline(always)]
fn sub_one(self) -> Self {
self.add_f(-1.0)
}
#[inline(always)]
fn ln_start(self) -> Self {
OV::from_dv(start_dv(self.c[0], || self.to_dv(), || self.mul_pwr2(0.5).to_dv()))
}
#[inline(always)]
fn ln_finish(y0: Self, corr: Self) -> Self {
corr.add_dv(DV {
hi: y0.c[0],
lo: y0.c[1],
})
}
#[inline(always)]
fn sel(m: M8<S>, a: Self, b: Self) -> Self {
OV {
c: sel_parts(m, &a.c, &b.c),
}
}
}
impl DD {
pub fn exp(self) -> DD {
exp_of(self)
}
pub fn ln(self) -> DD {
ln_of(self)
}
}
impl QD {
pub fn exp(self) -> QD {
exp_of(self)
}
pub fn ln(self) -> QD {
ln_of(self)
}
}
impl OD {
pub fn exp(self) -> OD {
exp_of(self)
}
pub fn ln(self) -> OD {
ln_of(self)
}
}
trait NanLanes: Copy {
type Scalar: Copy;
fn nan_lanes(self) -> u8;
fn flip_nans(self) -> Self;
fn set_lanes(self, m: u8, x: Self, f: fn(Self::Scalar) -> Self::Scalar) -> Self;
}
#[inline(always)]
fn nan_parts<S: Simd, const N: usize>(p: &[V8<S>; N]) -> u8 {
let mut m = p[0].ne(p[0]);
for i in 1..N {
m = m.or(p[i].ne(p[i]));
}
m.bits()
}
fn flip_parts<S: Simd, const N: usize>(p: &[V8<S>; N]) -> [V8<S>; N] {
let mut r = *p;
for i in 0..N {
r[i] = p[i].ne(p[i]).sel(p[i].neg(), p[i]);
}
r
}
impl<S: Simd> NanLanes for DV<S> {
type Scalar = DD;
#[inline(always)]
fn nan_lanes(self) -> u8 {
nan_parts(&[self.hi, self.lo])
}
fn flip_nans(self) -> Self {
let [hi, lo] = flip_parts(&[self.hi, self.lo]);
DV { hi, lo }
}
fn set_lanes(self, m: u8, x: Self, f: fn(DD) -> DD) -> Self {
let (mut hi, mut lo) = (self.hi.to_array(), self.lo.to_array());
for j in 0..8 {
if m >> j & 1 == 1 {
let y = f(x.lane(j));
(hi[j], lo[j]) = (y.hi, y.lo);
}
}
DV {
hi: V8::from_array(hi),
lo: V8::from_array(lo),
}
}
}
impl<S: Simd> NanLanes for QV<S> {
type Scalar = QD;
#[inline(always)]
fn nan_lanes(self) -> u8 {
nan_parts(&self.c)
}
fn flip_nans(self) -> Self {
QV { c: flip_parts(&self.c) }
}
fn set_lanes(self, m: u8, x: Self, f: fn(QD) -> QD) -> Self {
let (mut v, x) = (self.lanes(), x.lanes());
for j in 0..8 {
if m >> j & 1 == 1 {
v[j] = f(x[j]);
}
}
QV::from_lanes(&v)
}
}
impl<S: Simd> NanLanes for OV<S> {
type Scalar = OD;
#[inline(always)]
fn nan_lanes(self) -> u8 {
nan_parts(&self.c)
}
fn flip_nans(self) -> Self {
OV { c: flip_parts(&self.c) }
}
fn set_lanes(self, m: u8, x: Self, f: fn(OD) -> OD) -> Self {
let (mut v, x) = (self.lanes(), x.lanes());
for j in 0..8 {
if m >> j & 1 == 1 {
v[j] = f(x[j]);
}
}
OV::from_lanes(&v)
}
}
#[inline(always)]
fn scalar_nans<T: NanLanes>(x: T, v: T, f: fn(T::Scalar) -> T::Scalar) -> T {
let m = v.nan_lanes();
if m == 0 {
return v;
}
if mutate::on(mutate::LANE_NAN) {
return v.flip_nans();
}
v.set_lanes(m, x, std::hint::black_box(f))
}
impl<S: Simd> DV<S> {
#[inline]
pub fn exp(self) -> Self {
S::vectorize(
#[inline(always)]
|| scalar_nans(self, exp_of(self), DD::exp),
)
}
#[inline]
pub fn ln(self) -> Self {
S::vectorize(
#[inline(always)]
|| scalar_nans(self, ln_of(self), DD::ln),
)
}
}
impl<S: Simd> QV<S> {
#[inline]
pub fn exp(self) -> Self {
S::vectorize(
#[inline(always)]
|| scalar_nans(self, exp_of(self), QD::exp),
)
}
#[inline]
pub fn ln(self) -> Self {
S::vectorize(
#[inline(always)]
|| scalar_nans(self, ln_of(self), QD::ln),
)
}
}
impl<S: Simd> OV<S> {
#[inline]
pub fn exp(self) -> Self {
S::vectorize(
#[inline(always)]
|| scalar_nans(self, exp_of(self), OD::exp),
)
}
#[inline]
pub fn ln(self) -> Self {
S::vectorize(
#[inline(always)]
|| scalar_nans(self, ln_of(self), OD::ln),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::exact::{BigU, Dy};
fn within(parts: &[f64], num: &Dy, den: &Dy, lim2: i64) -> bool {
let d = Dy::sum(parts).mul(den).sub(num).abs();
d.ldexp(-lim2).cmp_abs(num) != std::cmp::Ordering::Greater
}
fn ln2() -> Dy {
let p = 1600u64;
let mut s = BigU::zero();
for j in 1..=p {
s = s.add(&BigU::from_u64(1).shl(p - j).divrem_small(j).0);
}
Dy {
neg: false,
m: s,
e: -(p as i64),
}
}
#[test]
fn constants_are_exact_roundings() {
let one = Dy::from_f64(1.0);
let mut f = Dy::from_f64(1.0);
for (j, c) in INV_FACT.iter().enumerate() {
if j > 0 {
f = f.mul(&Dy::from_f64(j as f64));
}
assert!(within(c, &one, &f, -423), "1/{j}!");
let mut bad = *c;
bad[7] += 2f64.powi(-421) * c[0];
assert!(!within(&bad, &one, &f, -423), "control 1/{j}!");
}
assert!(within(&THIRD, &one, &Dy::from_f64(3.0), -423));
let mut bad = THIRD;
bad[7] += 2f64.powi(-421) * THIRD[0];
assert!(!within(&bad, &one, &Dy::from_f64(3.0), -423), "control 1/3");
let mut lo = vec![LN2_HI];
lo.extend_from_slice(&LN2_LO);
let l2 = ln2();
assert!(within(&lo, &l2, &one, -466), "ln 2");
lo[8] += 2f64.powi(-460);
assert!(!within(&lo, &l2, &one, -466), "control ln 2");
assert_eq!(LN2_HI.to_bits() & ((1 << 11) - 1), 0, "LN2_HI has more than 42 bits");
assert!(within(&LN2_DD, &l2, &one, -107), "LN2_DD");
let mut bad = LN2_DD;
bad[1] += 2f64.powi(-104);
assert!(!within(&bad, &l2, &one, -107), "control LN2_DD");
assert!((LN2_HI - std::f64::consts::LN_2).abs() < 2f64.powi(-42));
}
}