fastmash 0.6.0

Fast command-line statistics on text data, compatible with GNU datamash
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Exact finite conversion rounded directly onto the binary80 target grid.

use crate::numeric_contract::{Raw80, Value80};
use num_bigint::BigUint;
use num_traits::Zero;
use std::cmp::Ordering;

use crate::conversion::bytes::{Exponent, FiniteToken};
use crate::conversion::integer::{self, Ratio, RemainderRelation};
use crate::conversion::profile::{EXPONENT_CAP, Error};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Direction {
    Overflow,
    UnderflowToZero,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum MagnitudeBound {
    ExponentDominates {
        absolute_exponent_greater_than: u32,
    },
    DecimalOrder {
        significant_digits_plus_exponent: i32,
    },
    HexLeadingExponent {
        exponent: i32,
    },
}

#[derive(Clone, Debug)]
pub(crate) enum RoundEvidence {
    Zero,
    Ratio {
        quantum: i32,
        remainder: RemainderRelation,
        quotient_odd: bool,
        carry: bool,
    },
    MagnitudeShortcut {
        direction: Direction,
        exponent: Exponent,
        significant_digits: usize,
        fraction_digits: usize,
        bound: MagnitudeBound,
    },
}

#[derive(Clone, Debug)]
pub(crate) struct Conversion {
    pub value: Value80,
    pub rounding: RoundEvidence,
    pub exact_zero: bool,
    pub overflow: bool,
    pub target_inexact: bool,
    pub ordinary_p64_tiny: bool,
    pub exact_at_least_min_normal: bool,
    pub shortcut: Option<Direction>,
}

#[derive(Clone, Debug, Default)]
pub(crate) struct ConversionPartial {
    pub value: Option<Value80>,
    pub rounding: Option<RoundEvidence>,
}

#[derive(Clone, Debug)]
pub(crate) struct ConversionFailure {
    pub error: Error,
    pub partial: ConversionPartial,
}

impl From<Error> for ConversionFailure {
    fn from(error: Error) -> Self {
        Self {
            error,
            partial: ConversionPartial::default(),
        }
    }
}

pub(crate) fn zero(negative: bool) -> Result<Value80, Error> {
    Ok(Value80::from_raw(Raw80::new(
        if negative { 0x8000 } else { 0 },
        0,
    ))?)
}

pub(crate) fn infinity(negative: bool) -> Result<Value80, Error> {
    Ok(Value80::from_raw(Raw80::new(
        if negative { 0xffff } else { 0x7fff },
        1 << 63,
    ))?)
}

pub(crate) fn quiet_nan(negative: bool) -> Result<Value80, Error> {
    Ok(Value80::from_raw(Raw80::new(
        if negative { 0xffff } else { 0x7fff },
        0xc000_0000_0000_0000,
    ))?)
}

fn shortcut(
    token: &FiniteToken<'_>,
    direction: Direction,
    significant_digits: usize,
    bound: MagnitudeBound,
) -> Result<Conversion, ConversionFailure> {
    let overflow = direction == Direction::Overflow;
    Ok(Conversion {
        value: if overflow {
            infinity(token.negative())?
        } else {
            zero(token.negative())?
        },
        rounding: RoundEvidence::MagnitudeShortcut {
            direction,
            exponent: token.exponent(),
            significant_digits,
            fraction_digits: token.fraction_digits(),
            bound,
        },
        exact_zero: false,
        overflow,
        target_inexact: true,
        ordinary_p64_tiny: !overflow,
        exact_at_least_min_normal: overflow,
        shortcut: Some(direction),
    })
}

pub(crate) fn finite(token: &FiniteToken<'_>) -> Result<Conversion, ConversionFailure> {
    let first_nonzero = token.digits().position(|d| d != 0);
    let Some(first_nonzero) = first_nonzero else {
        return Ok(Conversion {
            value: zero(token.negative())?,
            rounding: RoundEvidence::Zero,
            exact_zero: true,
            overflow: false,
            target_inexact: false,
            ordinary_p64_tiny: false,
            exact_at_least_min_normal: false,
            shortcut: None,
        });
    };
    let significant_digits = token.digit_count() - first_nonzero;
    let exponent = match token.exponent() {
        Exponent::Finite(exponent) => exponent,
        Exponent::Greater { negative } => {
            // The old evidence-view bound implied exponent dominance. Borrowed
            // command tokens can be longer, so establish that bound explicitly.
            if token.digit_count().saturating_mul(4).saturating_add(20000) >= EXPONENT_CAP as usize
            {
                return Err(Error::IntermediateCapacity.into());
            }
            return shortcut(
                token,
                if negative {
                    Direction::UnderflowToZero
                } else {
                    Direction::Overflow
                },
                significant_digits,
                MagnitudeBound::ExponentDominates {
                    absolute_exponent_greater_than: EXPONENT_CAP,
                },
            );
        }
    };
    let fractional = i32::try_from(token.fraction_digits()).map_err(|_| Error::InputCapacity)?;
    let fractional = if token.radix() == 16 {
        fractional
            .checked_mul(4)
            .ok_or(Error::IntermediateCapacity)?
    } else {
        fractional
    };
    let adjusted = exponent
        .checked_sub(fractional)
        .ok_or(Error::IntermediateCapacity)?;
    if token.radix() == 10 {
        let order = i32::try_from(significant_digits)
            .map_err(|_| Error::InputCapacity)?
            .checked_add(adjusted)
            .ok_or(Error::IntermediateCapacity)?;
        if order >= 5001 {
            return shortcut(
                token,
                Direction::Overflow,
                significant_digits,
                MagnitudeBound::DecimalOrder {
                    significant_digits_plus_exponent: order,
                },
            );
        }
        if order <= -5000 {
            return shortcut(
                token,
                Direction::UnderflowToZero,
                significant_digits,
                MagnitudeBound::DecimalOrder {
                    significant_digits_plus_exponent: order,
                },
            );
        }
    }
    let mut digits = Vec::new();
    digits
        .try_reserve_exact(significant_digits)
        .map_err(|_| Error::Allocation)?;
    digits.extend(token.digits().skip(first_nonzero));
    let coefficient = integer::coefficient(&digits, token.radix())?;
    let ratio = if token.radix() == 16 {
        let k = i32::try_from(coefficient.bits())
            .map_err(|_| Error::CoefficientCapacity)?
            .checked_sub(1)
            .and_then(|k| k.checked_add(adjusted))
            .ok_or(Error::IntermediateCapacity)?;
        if k >= 16384 {
            return shortcut(
                token,
                Direction::Overflow,
                significant_digits,
                MagnitudeBound::HexLeadingExponent { exponent: k },
            );
        }
        if k < -16446 {
            return shortcut(
                token,
                Direction::UnderflowToZero,
                significant_digits,
                MagnitudeBound::HexLeadingExponent { exponent: k },
            );
        }
        Ratio::shifted(coefficient, adjusted)?
    } else {
        let five = integer::power(5, adjusted.unsigned_abs())?;
        if adjusted >= 0 {
            Ratio::shifted(integer::mul(&coefficient, &five)?, adjusted)?
        } else {
            Ratio::new(coefficient, integer::shl(&five, adjusted.unsigned_abs())?)?
        }
    };
    round_ratio(&ratio, token.negative())
}

/// Whether the decimal of `digits` (most significant first, each below ten)
/// times 10^`scale` is exactly `significand` times 2^`exponent`, in
/// magnitude. For deciding whether a conversion was exact where a range
/// error depends on it: `significand` times 2^`exponent` is a binary80 value
/// (below 2^16384, and a multiple of 2^-16445), and `digits` are at most a
/// long decimal's kept digits. Outside those bounds it is an error, not an
/// answer.
pub(crate) fn decimal_equals(
    digits: &[u8],
    scale: i64,
    significand: u64,
    exponent: i64,
) -> Result<bool, Error> {
    let Some(start) = digits.iter().position(|&d| d != 0) else {
        return Ok(significand == 0);
    };
    if significand == 0 {
        return Ok(false);
    }
    // Trailing zeros move into the scale.
    let end = digits
        .iter()
        .rposition(|&d| d != 0)
        .map_or(start, |at| at + 1);
    let scale = scale
        .checked_add(i64::try_from(digits.len() - end).map_err(|_| Error::InputCapacity)?)
        .ok_or(Error::IntermediateCapacity)?;
    let digits = &digits[start..end];
    let twos = significand.trailing_zeros();
    let odd = BigUint::from(significand >> twos);
    let exponent = exponent
        .checked_add(i64::from(twos))
        .filter(|exponent| (-16445..16384).contains(exponent))
        .ok_or(Error::IntermediateCapacity)?;
    let coefficient = || BigUint::from_radix_be(digits, 10).ok_or(Error::InternalInvariant);
    if exponent < 0 {
        // An odd multiple of 2^-j is an odd multiple of 5^j over 10^j, whose
        // last digit is 5: exactly j fraction digits.
        if scale != exponent {
            return Ok(false);
        }
        let power = u32::try_from(-exponent).map_err(|_| Error::PowerCapacity)?;
        Ok(coefficient()? == integer::mul(&odd, &integer::power(5, power)?)?)
    } else {
        // An integer below 2^16384, so of at most 4,933 digits.
        let length = i64::try_from(digits.len()).map_err(|_| Error::InputCapacity)?;
        if scale < 0 || scale.saturating_add(length) > 4933 {
            return Ok(false);
        }
        let power = u32::try_from(scale).map_err(|_| Error::PowerCapacity)?;
        let decimal = integer::mul(&coefficient()?, &integer::power(10, power)?)?;
        let shift = u32::try_from(exponent).map_err(|_| Error::ShiftCapacity)?;
        Ok(decimal == integer::shl(&odd, shift)?)
    }
}

pub(crate) fn round_ratio(ratio: &Ratio, negative: bool) -> Result<Conversion, ConversionFailure> {
    let mut partial = ConversionPartial::default();
    let result = (|| -> Result<Conversion, Error> {
        let k = ratio.floor_log2()?;
        let quantum = (k - 63).max(-16445);
        let target = ratio.at_quantum(quantum)?;
        let inexact = target.relation != RemainderRelation::Zero;
        let bits = target.value.bits();
        let carry = bits > 64 || (k < -16382 && bits == 64);
        let rounding = RoundEvidence::Ratio {
            quantum,
            remainder: target.relation,
            quotient_odd: target.quotient_odd,
            carry,
        };
        partial.rounding = Some(rounding.clone());
        let (value, overflow) = if target.value.is_zero() {
            (zero(negative)?, false)
        } else {
            let result_k =
                i32::try_from(bits).map_err(|_| Error::IntermediateCapacity)? - 1 + quantum;
            if result_k > 16383 {
                (infinity(negative)?, true)
            } else if result_k < -16382 {
                if quantum != -16445 {
                    return Err(Error::InternalInvariant);
                }
                let sig = integer::as_u64(&target.value)?;
                (
                    Value80::from_raw(Raw80::new(if negative { 0x8000 } else { 0 }, sig))?,
                    false,
                )
            } else {
                let normalized = if bits > 64 {
                    &target.value >> (bits - 64) as usize
                } else {
                    integer::shl(&target.value, (64 - bits) as u32)?
                };
                let sig = integer::as_u64(&normalized)?;
                let exponent =
                    u16::try_from(result_k + 16383).map_err(|_| Error::InternalInvariant)?;
                (
                    Value80::from_raw(Raw80::new(
                        exponent | if negative { 0x8000 } else { 0 },
                        sig,
                    ))?,
                    false,
                )
            }
        };
        partial.value = Some(value);
        let exact_at_least_min_normal = ratio.compare_power_two(-16382)? != Ordering::Less;
        // This separate rounding determines source tininess only. It never becomes
        // the input to target rounding, including at the normal/subnormal boundary.
        // The completed target remains available if this later source work fails.
        let ordinary = ratio.at_quantum(k - 63)?;
        let ordinary_k = k + i32::from(ordinary.value.bits() > 64);
        let ordinary_p64_tiny = ordinary_k < -16382;
        Ok(Conversion {
            value,
            rounding,
            exact_zero: false,
            overflow,
            target_inexact: inexact || overflow,
            ordinary_p64_tiny,
            exact_at_least_min_normal,
            shortcut: None,
        })
    })();
    result.map_err(|error| ConversionFailure { error, partial })
}

pub(crate) fn value_ratio(value: Value80) -> Result<Ratio, Error> {
    if value.exponent() == 0x7fff {
        return Err(Error::InternalInvariant);
    }
    let exponent = if value.exponent() == 0 {
        -16445
    } else {
        i32::from(value.exponent()) - 16383 - 63
    };
    Ratio::shifted(BigUint::from(value.significand()), exponent)
}

pub(crate) fn integer_value(value: i64) -> Result<Value80, Error> {
    let magnitude = value.unsigned_abs();
    if magnitude == 0 {
        return zero(false);
    }
    let k = 63 - magnitude.leading_zeros();
    let sig = magnitude << (63 - k);
    let exponent = (k as u16) + 16383;
    Ok(Value80::from_raw(Raw80::new(
        exponent | if value < 0 { 0x8000 } else { 0 },
        sig,
    ))?)
}

#[cfg(test)]
mod tests {
    use super::decimal_equals;

    /// `n` as a significand and a power of two, as binary80 holds it.
    fn binary(n: u64) -> (u64, i64) {
        let shift = n.leading_zeros();
        (n << shift, -i64::from(shift))
    }

    #[test]
    fn decimals_equal_binary_values_exactly_or_not_at_all() {
        let half = (1u64 << 63, -64);
        assert_eq!(decimal_equals(&[5], -1, half.0, half.1), Ok(true));
        assert_eq!(decimal_equals(&[0, 5, 0, 0], -3, half.0, half.1), Ok(true));
        assert_eq!(decimal_equals(&[2, 5], -2, 1 << 63, -65), Ok(true));
        assert_eq!(decimal_equals(&[3], -1, half.0, half.1), Ok(false));
        assert_eq!(decimal_equals(&[5, 1], -2, half.0, half.1), Ok(false));
        let four_hundred = binary(400);
        assert_eq!(
            decimal_equals(&[4, 0, 0], 0, four_hundred.0, four_hundred.1),
            Ok(true)
        );
        assert_eq!(
            decimal_equals(&[4], 2, four_hundred.0, four_hundred.1),
            Ok(true)
        );
        assert_eq!(
            decimal_equals(&[4, 0, 1], 0, four_hundred.0, four_hundred.1),
            Ok(false)
        );
        assert_eq!(
            decimal_equals(&[4], -2, four_hundred.0, four_hundred.1),
            Ok(false)
        );
        assert_eq!(decimal_equals(&[0, 0], -99, 0, -16445), Ok(true));
        assert_eq!(decimal_equals(&[1], -99, 0, -16445), Ok(false));
        assert_eq!(decimal_equals(&[0], 0, 1, -16445), Ok(false));
        // The smallest subnormal, 2^-16445, has 16,445 fraction digits.
        let mut digits = vec![1u8];
        for _ in 0..16445 {
            let mut carry = 0;
            for digit in digits.iter_mut().rev() {
                let next = *digit * 5 + carry;
                *digit = next % 10;
                carry = next / 10;
            }
            while carry != 0 {
                digits.insert(0, carry % 10);
                carry /= 10;
            }
        }
        assert_eq!(decimal_equals(&digits, -16445, 1, -16445), Ok(true));
        assert_eq!(decimal_equals(&digits, -16444, 1, -16445), Ok(false));
        let last = digits.len() - 1;
        digits[last] -= 1;
        assert_eq!(decimal_equals(&digits, -16445, 1, -16445), Ok(false));
    }
}