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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use core::{
fmt,
iter::{Product, Sum},
ops::*,
str::FromStr,
};
// TODO decide on how `to_string_general` should work
/// A common trait for a special case of fixed point numbers in the form of all
/// fractional bits.
pub trait Fracint:
Sized
+ Clone
+ Copy
+ Default
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ core::hash::Hash
+ fmt::Display
+ fmt::Debug
+ FromStr
+ Neg<Output = Self>
+ Add<Output = Self>
+ AddAssign
+ Sub<Output = Self>
+ SubAssign
+ Mul<Output = Self>
+ MulAssign
+ Div<Output = Self>
+ DivAssign
+ Shl<usize, Output = Self>
+ ShlAssign<usize>
+ Shr<usize, Output = Self>
+ ShrAssign<usize>
+ Not<Output = Self>
+ BitOr<Output = Self>
+ BitOrAssign
+ BitAnd<Output = Self>
+ BitAndAssign
+ BitXor<Output = Self>
+ BitXorAssign
+ Sum
+ Product
{
/// This is only for workarounds, to make this better we would need a
/// refined trait for normal integers.
type Int: Sized
+ Clone
+ Copy
+ TryInto<u128, Error: fmt::Debug>
+ TryFrom<u128, Error: fmt::Debug>
+ fmt::Debug;
/// The number of bits in this type
const BITS: usize;
/// The maximum value representable by `Self`
const MAX: Self;
/// The minimum value representable by `Self`
const MIN: Self;
/// For `fiN`, `ONE` and `MAX` are the same. Prefer to use `MAX` when
/// wanting to emphasize the true numeric bounds or ordered maximum,
/// and instead use `ONE` for numeric values.
const ONE: Self;
/// A numerical value of negative one.
///
/// Note that `NEG_ONE` is not equal to `MIN`, but to `MIN + ULP`.
/// The purpose of `NEG_ONE` in contrast to `MIN` is preventing
/// certain overflows, such as `fiN::MIN.wrapping_mul(fiN::MIN)` or
/// `fiN::wrapping_abs(fiN::MIN)`.
const NEG_ONE: Self;
/// One positive Unit in the Last Place
const ULP: Self;
/// Zero.
const ZERO: Self;
/// If this type is signed
const SIGNED: bool;
/// Casts from the `Self::Int` type
fn from_int(x: Self::Int) -> Self;
/// Casts to the `Self::Int` type
fn as_int(self) -> Self::Int;
fn is_zero(self) -> bool {
self == Self::ZERO
}
fn is_negative(self) -> bool {
self < Self::ZERO
}
fn is_positive(self) -> bool {
self >= Self::ZERO
}
/// Returns a value representing the sign of `self`.
///
/// - `fiN::NEG_ONE` if the value is negative
/// - `fiN::ZERO` if the value is zero
/// - `fiN::ONE` if the value is positive
fn signum(self) -> Self {
if self < Self::ZERO {
Self::NEG_ONE
} else if self == Self::ZERO {
Self::ZERO
} else {
Self::ONE
}
}
/// Wrapping absolute value of `self`.
///
/// # Overflow behavior
///
/// `Self::MIN.abs()` -> `Self::MIN`
fn wrapping_abs(self) -> Self {
if self.is_negative() {
self.wrapping_neg()
} else {
self
}
}
/// Returns a tuple of `self.wrapping_abs()` along with a boolean indicating
/// whether an overflow happened.
fn overflowing_abs(self) -> (Self, bool);
/// Same as `overflowing_abs` except it returns `None` on overflow
fn checked_abs(self) -> Option<Self> {
match self.overflowing_abs() {
(x, false) => Some(x),
(_, true) => None,
}
}
/// Saturating absolute value of `self`. It behaves the same way as
/// `wrapping_abs` except `Self::MIN.saturating_abs()` -> `Self::ONE`
fn saturating_abs(self) -> Self {
self.checked_abs().unwrap_or(Self::ONE)
}
/// Wrapping negation of `self`.
///
/// # Overflow behavior
///
/// `Self::MIN.wrapping_neg()` -> `Self::MIN`
fn wrapping_neg(self) -> Self;
/// Returns a tuple of `self.wrapping_neg()` along with a boolean indicating
/// whether an overflow happened.
fn overflowing_neg(self) -> (Self, bool);
/// Same as `overflowing_neg` except it returns `None` on overflow
fn checked_neg(self) -> Option<Self> {
match self.overflowing_neg() {
(x, false) => Some(x),
(_, true) => None,
}
}
/// Saturating negation of `self`. It behaves the same way as `wrapping_neg`
/// except `Self::MIN.saturating_neg()` -> `Self::ONE`
fn saturating_neg(self) -> Self;
/// Saturating inversion of `self`. It has the special cases
///
/// - `fiN::MIN` => `-1`
/// - `fiN::ZERO` => `iX::MAX`
///
/// Note that it does not panic on zero.
fn saturating_inv(self) -> Self::Int;
/// Wrapping (modular) addition.
///
/// # Overflow behavior
///
/// If the numeric value of `self` added to `rhs` is more than `fiN::MAX`
/// or less than `fiN::MIN` (if `fiN::MIN` should be avoided, use
/// `fiN::NEG_ONE` as the minimum value instead), modular overflow will
/// happen.
fn wrapping_add(self, rhs: Self) -> Self;
/// Returns a tuple of `self.wrapping_add(rhs)` along with a boolean
/// indicating whether an overflow happened.
/// Note that if `self.wrapping_add(rhs) == fiN::MIN`, it is counted as
/// overflow.
fn overflowing_add(self, rhs: Self) -> (Self, bool);
/// Same as `overflowing_add` except it returns `None` on overflow
fn checked_add(self, rhs: Self) -> Option<Self> {
match self.overflowing_add(rhs) {
(v, false) => Some(v),
(_, true) => None,
}
}
/// Saturating addition. Saturates at the numeric bounds `fiN::NEG_ONE` and
/// `fiN::ONE` instead of overflowing.
fn saturating_add(self, rhs: Self) -> Self;
/// Wrapping (modular) subtraction.
///
/// # Overflow behavior
///
/// If the numeric value of `self` subtracted by `rhs` is more than
/// `fiN::MAX` or less than `fiN::MIN` (if `fiN::MIN` should be avoided,
/// use `fiN::NEG_ONE` as the minimum value instead), modular overflow
/// will happen.
fn wrapping_sub(self, rhs: Self) -> Self;
/// Returns a tuple of `self.wrapping_sub(rhs)` along with a boolean
/// indicating whether an overflow happened.
/// Note that if `self.wrapping_sub(rhs) == fiN::MIN`, it is counted as
/// overflow.
fn overflowing_sub(self, rhs: Self) -> (Self, bool);
/// Same as `overflowing_sub` except it returns `None` on overflow
fn checked_sub(self, rhs: Self) -> Option<Self> {
match self.overflowing_sub(rhs) {
(v, false) => Some(v),
(_, true) => None,
}
}
/// Saturating subtraction. Saturates at the numeric bounds `fiN::NEG_ONE`
/// and `fiN::ONE` instead of overflowing.
fn saturating_sub(self, rhs: Self) -> Self;
/// Wrapping fracint multiplication.
///
/// # Overflow Behavior
///
/// There is only one case where overflow can occur:
/// `fiN::MIN.wrapping_mul(fiN::MIN)` -> `fiN::MIN`.
fn wrapping_mul(self, rhs: Self) -> Self;
/// Returns a tuple of `self.wrapping_mul(rhs)` along with a boolean
/// indicating whether an overflow happened.
///
/// Note that only the overflow possible is the corner case
/// `fiN::MIN.wrapping_mul(fiN::MIN)` -> `fiN::MIN`.
fn overflowing_mul(self, rhs: Self) -> (Self, bool);
/// Same as `overflowing_mul` except it returns `None` on overflow
fn checked_mul(self, rhs: Self) -> Option<Self> {
match self.overflowing_mul(rhs) {
(v, false) => Some(v),
(_, true) => None,
}
}
/// Saturating fracint multiplication. Saturates at the numeric bounds
/// `fiN::NEG_ONE` and `fiN::ONE` instead of overflowing.
fn saturating_mul(self, rhs: Self) -> Self;
/// Saturating fracint multiplication with an integer. Saturates at the
/// numeric bounds `fiN::NEG_ONE` and `fiN::ONE` instead of overflowing.
fn saturating_mul_int(self, rhs: Self::Int) -> Self;
/// Saturating fracint division. Saturates at the numeric bounds
/// `fiN::NEG_ONE` and `fiN::ONE` instead of overflowing.
///
/// Panics are prevented and saturation handled in this way:
/// - if `rhs == fiN::ZERO`, `self.signum()` is returned
/// - else if `self.saturating_abs() >= rhs.saturating_abs()`, it will
/// return `fiN::NEG_ONE` if their signs are not equal, `fiN::ONE`
/// otherwise (except for `rhs == fiN::ZERO` as shown above)
///
/// Note that it does not panic on zero
fn saturating_div(self, rhs: Self) -> Self;
/// Saturating fracint division with an integer. Saturates at the numeric
/// bounds `fiN::NEG_ONE` and `fiN::ONE` instead of overflowing.
///
/// Panics are prevented and saturation handled in this way:
/// - if `rhs == fiN::ZERO`, `self.signum()` is returned
/// - if `self == fiN::MIN` and `rhs == -1`, `Self::ONE` is returned
fn saturating_div_int(self, rhs: Self::Int) -> Self;
#[cfg(feature = "rand_support")]
fn rand<R: rand_core::Rng + ?Sized>(rng: &mut R) -> Self;
/// Converts from an `f32` to `Self`. Returns `None` if the absolute value
/// is greater than 1.0.
fn from_f32(f: f32) -> Option<Self>;
/// Converts from an `f64` to `Self`. Returns `None` if the absolute value
/// is greater than 1.0.
fn from_f64(f: f64) -> Option<Self>;
/// Converts to an `f32`
fn to_f32(self) -> f32;
/// Converts to an `f64`
fn to_f64(self) -> f64;
/// Slow way of calculating the truncated square root using bisection. This
/// will always underestimate the square root, with about as much bit error
/// as the number of leading zero bits.
fn sqrt_simple_bisection(self) -> Self {
let mut set_bit = if Self::SIGNED {
Self::ULP << (Self::BITS - 2)
} else {
Self::ULP << (Self::BITS - 1)
};
let mut res = Self::ZERO;
let mut prev_sqr = Self::ZERO;
loop {
if set_bit.is_zero() {
break res
}
let test = res | set_bit;
let sqr = test.saturating_mul(test);
if sqr <= self {
res = test;
if sqr == self {
break res
}
}
if sqr == prev_sqr {
// we have reached a point where the set bit no longer contributes
break res
}
prev_sqr = sqr;
set_bit >>= 1;
}
}
/// Fast way of calculating the square root to within a few ULPs of the true
/// value.
fn sqrt_fast(self) -> Self;
}
pub trait FracintDouble: Fracint {
/// The double-sized version of `Self`
type Double: Fracint + FracintHalf<Half = Self>;
/// Widens `self` by extending with zero bits
fn widen(self) -> Self::Double;
/// Saturating widening multiplication of `self` and `other`. All inputs
/// result in numerically _exact_ outputs, except for
/// `fiN::MIN.full_mul(fiN::MIN)` which has to be saturated to `fiM::ONE`.
///
/// # Examples
///
/// ```
/// use fracints::*;
///
/// // note: `from_str` and `to_string` sometimes have small
/// // round-to-even errors when converting and displaying, but
/// // the `full_mul` between those functions is truly exact
/// // (except for the corner case).
/// assert_eq!(
/// fi32!(0.123456789)
/// .saturating_widening_mul(fi32!(0.123456789))
/// .to_string(),
/// "0.0152415787947921023"
/// );
///
/// // the only overflow corner case
/// assert_eq!(fi32::MIN.saturating_widening_mul(fi32::MIN), fi64::ONE);
/// ```
fn saturating_widening_mul(self, rhs: Self) -> Self::Double;
/// This calculates a nearly exact truncated square root using bisection
/// with a larger type
fn sqrt_slow(self) -> Self {
self.widen().sqrt_simple_bisection().truncate()
}
}
pub trait FracintHalf: Fracint + From<Self::Half> {
/// The half-sized version of `Self`
type Half: Fracint + FracintDouble<Double = Self>;
/// Returns half sized low and high parts. The high part is effectively a
/// truncated version of `self`, and the low part is the remainder.
fn split(self) -> (Self::Half, Self::Half);
/// Truncates `self` down to a half sized type with the same value except
/// with less than 1 ULP of error.
fn truncate(self) -> Self::Half;
}