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
use super::bigint::BigInt;
use super::float::Float;
use std::fmt::Display;
type BigNum = BigInt<50>;
impl<const EXPONENT: usize, const MANTISSA: usize> Float<EXPONENT, MANTISSA> {
fn convert_to_integer(&self) -> (BigNum, i64) {
let mut exp = self.get_exp() - MANTISSA as i64;
let mut mantissa: BigNum = self.get_mantissa().cast();
match exp.cmp(&0) {
std::cmp::Ordering::Less => {
let five = BigInt::from_u64(5);
let e5 = five.powi((-exp) as u64);
let overflow = mantissa.inplace_mul(e5);
debug_assert!(!overflow);
exp = -exp;
}
std::cmp::Ordering::Equal | std::cmp::Ordering::Greater => {
mantissa.shift_left(exp as usize);
exp = 0;
}
}
(mantissa, exp)
}
pub fn get_decimal_accuracy() -> usize {
2 + (MANTISSA * 59) / 196
}
fn reduce_printed_integer_length(integer: &mut BigNum, exp: &mut i64) {
let bits = integer.msb_index();
if bits <= MANTISSA {
return;
};
let needed_bits = bits - MANTISSA;
let mut digits_to_remove = ((needed_bits * 59) / 196) as i64;
if digits_to_remove > *exp {
digits_to_remove = *exp;
}
*exp -= digits_to_remove;
let ten = BigInt::from_u64(10);
let divisor = ten.powi(digits_to_remove as u64);
integer.inplace_div(divisor);
}
fn convert_normal_to_string(&self) -> String {
let (mut integer, mut exp) = self.convert_to_integer();
let mut buff = Vec::new();
let ten = BigNum::from_u64(10);
Self::reduce_printed_integer_length(&mut integer, &mut exp);
let chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
while !integer.is_zero() {
let rem = integer.inplace_div(ten);
let ch = chars[rem.as_u64() as usize];
buff.insert(0, ch);
}
debug_assert!(exp >= 0);
while buff.len() < exp as usize {
buff.insert(0, '0');
}
buff.insert(buff.len() - exp as usize, '.');
while !buff.is_empty() && buff[buff.len() - 1] == '0' {
buff.pop();
}
String::from_iter(buff)
}
fn convert_to_string(&self) -> String {
let result = if self.get_sign() { "-" } else { "" };
let mut result: String = result.to_string();
let body: String = match self.get_category() {
super::float::Category::Infinity => "Inf".to_string(),
super::float::Category::NaN => "NaN".to_string(),
super::float::Category::Normal => self.convert_normal_to_string(),
super::float::Category::Zero => "0.0".to_string(),
};
result.push_str(&body);
result
}
}
impl<const EXPONENT: usize, const MANTISSA: usize> Display
for Float<EXPONENT, MANTISSA>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.convert_to_string())
}
}
#[test]
fn test_convert_to_string() {
use crate::FP16;
use crate::FP64;
fn to_str_w_fp16(val: f64) -> String {
format!("{}", FP16::from_f64(val))
}
fn to_str_w_fp64(val: f64) -> String {
format!("{}", FP64::from_f64(val))
}
assert_eq!("-0.0", to_str_w_fp16(-0.));
assert_eq!(".3", to_str_w_fp16(0.3));
assert_eq!("4.5", to_str_w_fp16(4.5));
assert_eq!("256.", to_str_w_fp16(256.));
assert_eq!("Inf", to_str_w_fp16(65534.));
assert_eq!("-Inf", to_str_w_fp16(-65534.));
assert_eq!(".0999", to_str_w_fp16(0.1));
assert_eq!(".1", to_str_w_fp64(0.1));
assert_eq!(".29999999999999998", to_str_w_fp64(0.3));
assert_eq!("2251799813685248.", to_str_w_fp64((1u64 << 51) as f64));
assert_eq!("1995.1994999999999", to_str_w_fp64(1995.1995));
}
#[test]
fn test_fuzz_printing() {
use crate::utils;
use crate::FP64;
let mut lfsr = utils::Lfsr::new();
for _ in 0..50 {
let v0 = lfsr.get64();
let f0 = f64::from_bits(v0);
let fp0 = FP64::from_f64(f0);
fp0.to_string();
}
}
#[test]
fn test_print_sqrt() {
type FP = crate::FP128;
let n = FP::from_u64(5);
let half = FP::from_u64(2);
let mut x = n;
for _ in 0..100 {
x = half * (x + (n / x));
}
println!("{}", x);
}
#[test]
fn test_readme_example() {
type FP128 = Float<15, 112>;
let n = FP128::from_u64(5);
let two = FP128::from_u64(2);
let mut x = n;
for _ in 0..1000 {
x = (x + (n / x)) / two;
}
println!("fp128: {}", x);
println!("fp64: {}", x.as_f64());
use crate::FP16;
let fp = FP16::from_i64(15);
fp.dump();
}
#[test]
fn test_decimal_accuracy_for_type() {
use crate::{FP128, FP16, FP256, FP32, FP64};
assert_eq!(FP16::get_decimal_accuracy(), 5);
assert_eq!(FP32::get_decimal_accuracy(), 8);
assert_eq!(FP64::get_decimal_accuracy(), 17);
assert_eq!(FP128::get_decimal_accuracy(), 35);
assert_eq!(FP256::get_decimal_accuracy(), 73);
}