1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use crate::Exponent;
use crate::common::consts::ONE;
use crate::common::consts::TWO;
use crate::common::util::get_add_cost;
use crate::common::util::get_mul_cost;
use crate::common::util::get_sqrt_cost;
use crate::defs::Word;
use crate::num::BigFloatNumber;
use crate::defs::RoundingMode;
use crate::defs::Error;
use crate::defs::Sign;
use crate::ops::series::PolycoeffGen;
use crate::ops::series::ArgReductionEstimator;
use crate::ops::series::series_run;
use crate::ops::series::series_cost_optimize;
use crate::ops::consts::Consts;
struct AtanhPolycoeffGen {
acc: BigFloatNumber,
one_full_p: BigFloatNumber,
val: BigFloatNumber,
iter_cost: usize,
}
impl AtanhPolycoeffGen {
fn new(p: usize) -> Result<Self, Error> {
let acc = BigFloatNumber::from_word(1, 1)?;
let one_full_p = BigFloatNumber::from_word(1, p)?;
let val = BigFloatNumber::from_word(1, p)?;
let iter_cost = get_add_cost(p) + get_add_cost(1); Ok(AtanhPolycoeffGen {
acc,
one_full_p,
val,
iter_cost,
})
}
}
impl PolycoeffGen for AtanhPolycoeffGen {
fn next(&mut self, rm: RoundingMode) -> Result<&BigFloatNumber, Error> {
self.acc = self.acc.add(&TWO, rm)?;
self.val = self.one_full_p.div(&self.acc, rm)?;
Ok(&self.val)
}
#[inline]
fn get_iter_cost(&self) -> usize {
self.iter_cost
}
}
struct LnArgReductionEstimator {}
impl ArgReductionEstimator for LnArgReductionEstimator {
fn get_reduction_cost(n: usize, p: usize) -> usize {
let cost_mul = get_mul_cost(p);
let cost_add = get_add_cost(p);
let sqrt_cost = get_sqrt_cost(p, cost_mul, cost_add);
n * sqrt_cost
}
#[inline]
fn reduction_effect(n: usize, m: isize) -> usize {
(m + n as isize) as usize
}
}
impl BigFloatNumber {
pub fn ln(&self, rm: RoundingMode, cc: &mut Consts) -> Result<Self, Error> {
if self.is_zero() || self.is_negative() {
return Err(Error::InvalidArgument);
}
let e = self.get_exponent();
let mut x = self.clone()?;
x.set_exponent(0);
if e != 0 {
x.set_precision(x.get_mantissa_max_bit_len() + 2, RoundingMode::None)?;
}
let p1 = Self::ln_series(x, RoundingMode::None)?;
let mut ret = if e == 0 {
p1
} else {
let p2 = cc.ln_2(self.get_mantissa_max_bit_len() + 2, RoundingMode::None)?;
let mut n = Self::from_word(e.unsigned_abs() as Word, 1)?;
if e < 0 {
n.set_sign(Sign::Neg);
}
let p2n = p2.mul(&n, RoundingMode::None)?;
p1.add(&p2n, RoundingMode::None)?
};
ret.set_precision(self.get_mantissa_max_bit_len(), rm)?;
Ok(ret)
}
fn ln_series(mut x: Self, rm: RoundingMode) -> Result<Self, Error> {
let p = x.get_mantissa_max_bit_len();
let mut polycoeff_gen = AtanhPolycoeffGen::new(p)?;
let (reduction_times, niter) = series_cost_optimize::<AtanhPolycoeffGen, LnArgReductionEstimator>(
p, &polycoeff_gen, 0, 2, false);
let err = niter * 3 + reduction_times + 4;
x.set_precision(x.get_mantissa_max_bit_len() + err, rm)?;
let arg = if reduction_times > 0 {
Self::ln_arg_reduce(x, reduction_times, rm)?
} else {
x
};
let x1 = arg.sub(&ONE, rm)?;
let x2 = arg.add(&ONE, rm)?;
let z = x1.div(&x2, rm)?;
let x_step = z.mul(&z, rm)?; let x_first = z.mul(&x_step, rm)?; let ret = series_run(z, x_first, x_step, niter, &mut polycoeff_gen, rm)?;
Self::ln_arg_restore(ret, reduction_times + 1)
}
fn ln_arg_reduce(mut x: Self, n: usize, rm: RoundingMode) -> Result<Self, Error> {
for _ in 0..n {
x = x.sqrt(rm)?;
}
Ok(x)
}
fn ln_arg_restore(mut x: Self, n: usize) -> Result<Self, Error> {
x.set_exponent(x.get_exponent() + n as Exponent);
Ok(x)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ln() {
let mut cc = Consts::new().unwrap();
let rm = RoundingMode::ToEven;
let n1 = BigFloatNumber::from_word(123,3200).unwrap();
let mut n2 = n1.ln(rm, &mut cc).unwrap();
n2.set_sign(Sign::Pos);
}
#[ignore]
#[test]
#[cfg(feature="std")]
fn ln_perf() {
let mut cc = Consts::new().unwrap();
let mut n = vec![];
for _ in 0..10000 {
let mut nn = BigFloatNumber::random_normal(133, -100, 100).unwrap();
nn.set_sign(Sign::Pos);
n.push(nn);
}
for _ in 0..5 {
let start_time = std::time::Instant::now();
for ni in n.iter() {
let _f = ni.ln(RoundingMode::ToEven, &mut cc).unwrap();
}
let time = start_time.elapsed();
println!("{}", time.as_millis());
}
}
}