fastmash 0.3.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Checked Rust log/exp shared by geometric mean and normality probabilities.
use super::numerics::{NumericFailure, Numerics, Value};
use astro_float_num::{BigFloat, Consts, RoundingMode, Sign};
use fastmash_numeric_contract::{Raw80, Value80, ValueClass};

pub(super) struct Transcendentals {
    constants: Consts,
    logarithms: Option<super::log_cache::LogCache>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use rustc_apfloat::{Float, Round, ieee::X87DoubleExtended as Extended};

    #[test]
    fn adapter_cache_and_fallback_match_independent_mpfr() {
        let mut math = Transcendentals::new().unwrap();
        for line in include_str!("testdata/log-mpfr.tsv")
            .lines()
            .filter(|s| !s.starts_with('#'))
        {
            let (input, expected) = line.split_once('\t').unwrap();
            let bits = u128::from_str_radix(input, 16).unwrap();
            let input =
                Numerics::admit(Value80::from_raw(Raw80::try_from_bits(bits).unwrap()).unwrap())
                    .unwrap();
            let expected = u128::from_str_radix(expected, 16).unwrap();
            for _ in 0..2 {
                assert_eq!(
                    Numerics::value80(math.log(input).unwrap()).raw().to_bits(),
                    expected,
                    "{bits:020x}"
                );
            }
        }
        math.log(Numerics::promote_u64(2)).unwrap();
        FAIL_ALLOCATION.with(|flag| flag.set(true));
        assert!(matches!(
            math.log(Numerics::promote_u64(2)),
            Err(NumericFailure::Allocation)
        ));
    }

    #[test]
    fn exponential_adapter_matches_independent_mpfr() {
        let mut math = Transcendentals::new().unwrap();
        let mut retained = fastmash_portable_numerics::PortableNumerics::new().unwrap();
        for line in include_str!("testdata/exp-mpfr.tsv")
            .lines()
            .filter(|line| !line.starts_with('#'))
        {
            let (input, expected) = line.split_once('\t').unwrap();
            let bits = u128::from_str_radix(input, 16).unwrap();
            let input =
                Numerics::admit(Value80::from_raw(Raw80::try_from_bits(bits).unwrap()).unwrap())
                    .unwrap();
            let expected = u128::from_str_radix(expected, 16).unwrap();
            let actual = math.exp(input).unwrap();
            let actual = actual
                .map(Numerics::value80)
                .unwrap_or_else(|| retained.exp(input).unwrap());
            assert_eq!(actual.raw().to_bits(), expected, "{bits:020x}");
        }
    }

    #[test]
    fn table_exp_agrees_with_arbitrary_precision_wherever_it_answers() {
        agreement(200_000, 0x243f_6a88_85a3_08d3_1319_8a2e_0370_7344);
    }

    #[test]
    fn table_exp_agrees_at_the_edges_of_its_domain() {
        let mut math = Transcendentals::new().unwrap();
        let mut inputs = Vec::new();
        // |x| near 2^-40 (the kernel's lower edge), 2^14 (its upper edge), the
        // smallest normal result (x near -11355.1) and the largest finite
        // result (x near 11356.5), with neighbouring significands.
        for (exponent, base) in [
            (16343u16, 1u64 << 63),
            (16342, u64::MAX),
            (16396, u64::MAX),
            (16396, 0xb162_0000_0000_0000),
            (16396, 0xb172_0000_0000_0000),
            (16396, 0xb16c_0000_0000_0000),
        ] {
            for delta in 0..512u64 {
                for sign in [0u128, 1] {
                    let significand = (base - delta * 0x1_0000_0000) | (1 << 63);
                    inputs.push(
                        (sign << 79) | (u128::from(exponent) << 64) | u128::from(significand),
                    );
                }
            }
        }
        let mut certified = 0;
        for bits in inputs {
            let Some(fast) = super::super::guarded_exp::exp(bits) else {
                continue;
            };
            certified += 1;
            let input = Value80::from_raw(Raw80::try_from_bits(bits).unwrap()).unwrap();
            if let Some(slow) = math.arbitrary_exp(input).unwrap() {
                assert_eq!(Numerics::value80(slow).raw().to_bits(), fast, "{bits:020x}");
            }
        }
        assert!(certified > 1000, "{certified}");
    }

    /// Numerical admission run: `FASTMASH_EXP_AGREEMENT=<count> cargo test
    /// --release -- --ignored table_exp_admission`.
    #[test]
    #[ignore]
    fn table_exp_admission() {
        let count = std::env::var("FASTMASH_EXP_AGREEMENT")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(20_000_000);
        agreement(count, 0x9e37_79b9_7f4a_7c15_f39c_c060_5ced_c834);
    }

    fn agreement(count: u32, seed: u128) {
        let mut math = Transcendentals::new().unwrap();
        let (mut answered, mut total) = (0u32, 0u32);
        let mut state = seed;
        for _ in 0..count {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            // Exponents across the kernel 2^-40 <= |x| < 2^14 and a little beyond.
            let exponent = 16383 - 44 + (state >> 100) as u16 % 60;
            let significand = (state as u64) | (1 << 63);
            let sign = (state >> 90) as u16 & 1;
            let bits = (u128::from(sign << 15 | exponent) << 64) | u128::from(significand);
            let Some(fast) = super::super::guarded_exp::exp(bits) else {
                continue;
            };
            total += 1;
            let input = Value80::from_raw(Raw80::try_from_bits(bits).unwrap()).unwrap();
            let slow = math.arbitrary_exp(input).unwrap();
            if let Some(slow) = slow {
                assert_eq!(Numerics::value80(slow).raw().to_bits(), fast, "{bits:020x}");
                answered += 1;
            }
        }
        // The certificate settles nearly every rounding.
        eprintln!("exp agreement: {count} inputs, {total} certified, {answered} compared");
        assert!(
            total > count / 4 * 3 && answered > count / 4 * 3,
            "{total} {answered}"
        );
    }

    #[test]
    fn exp_only_owner_does_not_initialize_log_work() {
        let mut math = Transcendentals::new().unwrap();
        math.exp(Numerics::promote_u64(1)).unwrap();
        assert!(math.logarithms.is_none());
    }

    #[test]
    fn logarithms_preserve_retained_bits_and_wider_precision_consistency() {
        let mut log = Transcendentals::new().unwrap();
        let mut retained = fastmash_portable_numerics::PortableNumerics::new().unwrap();
        let mut constants = Consts::new().unwrap();
        let mut values = Vec::new();
        for exponent in [1, 2, 8192, 16318, 16382, 16383, 16384, 24576, 32765, 32766] {
            for significand in [1 << 63, (1 << 63) + 1, 0xc000_0000_0000_0000, u64::MAX] {
                values.push(Value80::from_raw(Raw80::new(exponent, significand)).unwrap());
            }
        }
        for width in 1..64 {
            for significand in [1u64 << (width - 1), (1u64 << width) - 1] {
                values.push(Value80::from_raw(Raw80::new(0, significand)).unwrap());
            }
        }
        for input in values {
            let actual = Numerics::value80(log.log(Numerics::admit(input).unwrap()).unwrap());
            if input.exponent() != 0 {
                assert_eq!(
                    actual.raw(),
                    retained.log(Numerics::admit(input).unwrap()).unwrap().raw(),
                    "{input:?}"
                );
            }
            if input.raw() == Raw80::new(0x3fff, 1 << 63) {
                assert_eq!(actual.raw(), Value80::signed_zero(false).raw());
                continue;
            }
            let shift = input.significand().leading_zeros();
            let source = BigFloat::from_raw_parts(
                &[input.significand() << shift],
                64,
                Sign::Pos,
                i32::from(input.exponent().max(1)) - 16382 - shift as i32,
                false,
            );
            // Cross-precision consistency is not an independent oracle. Normal
            // inputs above also compare with the independent retained kernel.
            for precision in [128, 256] {
                let high = source.ln(precision, RoundingMode::ToEven, &mut constants);
                checked(&high).unwrap();
                let (words, _, sign, exponent, _) = high.as_raw_parts().unwrap();
                let mut hex = if sign == Sign::Neg {
                    "-0x".to_owned()
                } else {
                    "0x".to_owned()
                };
                for word in words.iter().rev() {
                    hex.push_str(&format!("{word:016x}"));
                }
                hex.push_str(&format!("p{}", exponent - words.len() as i32 * 64));
                let expected = Extended::from_str_r(&hex, Round::NearestTiesToEven)
                    .unwrap()
                    .value
                    .to_bits();
                assert_eq!(actual.raw().to_bits(), expected, "{input:?} at {precision}");
            }
        }
    }

    #[test]
    fn special_values_and_library_errors_are_distinct() {
        let mut log = Transcendentals::new().unwrap();
        for (input, expected) in [
            (Value80::signed_zero(false), Value80::infinity(true)),
            (Value80::signed_zero(true), Value80::infinity(true)),
            (Value80::infinity(false), Value80::infinity(false)),
            (Value80::infinity(true), Value80::canonical_quiet_nan()),
            (
                Value80::from_raw(Raw80::new(0x8000, 1)).unwrap(),
                Value80::canonical_quiet_nan(),
            ),
            (
                Value80::from_raw(Raw80::new(0xffff, 0xc000_0000_0000_0007)).unwrap(),
                Value80::from_raw(Raw80::new(0xffff, 0xc000_0000_0000_0007)).unwrap(),
            ),
        ] {
            assert_eq!(
                Numerics::value80(log.log(Numerics::admit(input).unwrap()).unwrap()).raw(),
                expected.raw()
            );
        }
        let invalid = BigFloat::from_raw_parts(&[1], 65, Sign::Pos, 0, false);
        assert_eq!(checked(&invalid), Err(NumericFailure::Invariant));
    }

    #[test]
    fn exponential_bits_and_boundary_selection() {
        let mut math = Transcendentals::new().unwrap();
        let mut retained = fastmash_portable_numerics::PortableNumerics::new().unwrap();
        for exponent in [2, 0x7ffe] {
            let boundary =
                Numerics::admit(Value80::from_raw(Raw80::new(exponent, 1 << 63)).unwrap()).unwrap();
            let logarithm = retained.log(boundary).unwrap().raw().to_bits();
            for bits in [logarithm - 1, logarithm, logarithm + 1] {
                let input = Numerics::admit(
                    Value80::from_raw(Raw80::try_from_bits(bits).unwrap()).unwrap(),
                )
                .unwrap();
                let actual = math.exp(input).unwrap();
                let expected = retained.exp(input).unwrap();
                assert_eq!(Numerics::value80(actual.unwrap()).raw(), expected.raw());
            }
        }
        let guard = Value80::exact_u64(16384).raw().to_bits();
        for sign in [0, 1u128 << 79] {
            for bits in [Raw80::new(16396, u64::MAX).to_bits(), guard, guard + 1] {
                let input = Numerics::admit(
                    Value80::from_raw(Raw80::try_from_bits(bits | sign).unwrap()).unwrap(),
                )
                .unwrap();
                let result = math.exp(input).unwrap();
                assert_eq!(result.is_none(), bits >= guard);
                if let Some(result) = result {
                    assert_eq!(
                        Numerics::value80(result).raw(),
                        retained.exp(input).unwrap().raw()
                    );
                }
            }
            let tiny = Numerics::admit(
                Value80::from_raw(Raw80::try_from_bits(1 | sign).unwrap()).unwrap(),
            )
            .unwrap();
            assert_eq!(
                Numerics::value80(math.exp(tiny).unwrap().unwrap()).raw(),
                Value80::exact_u64(1).raw()
            );
        }
        for text in [
            "-11354", "-100", "-1", "-0.00001", "0.00001", "1", "100", "11355",
        ] {
            let raw = Extended::from_str_r(text, Round::NearestTiesToEven)
                .unwrap()
                .value
                .to_bits();
            let input =
                Numerics::admit(Value80::from_raw(Raw80::try_from_bits(raw).unwrap()).unwrap())
                    .unwrap();
            assert_eq!(
                Numerics::value80(math.exp(input).unwrap().unwrap()).raw(),
                retained.exp(input).unwrap().raw()
            );
        }
        for text in [
            "-11400",
            "-11356",
            "-11355.2",
            "-11355.137111933024058",
            "11356",
            "11356.52340629414395",
            "11357",
            "16384",
            "-16384",
        ] {
            let raw = Extended::from_str_r(text, Round::NearestTiesToEven)
                .unwrap()
                .value
                .to_bits();
            let input =
                Numerics::admit(Value80::from_raw(Raw80::try_from_bits(raw).unwrap()).unwrap())
                    .unwrap();
            let actual = math.exp(input).unwrap();
            if text == "16384" || text == "-16384" {
                assert!(actual.is_none(), "{text}");
            } else {
                assert_eq!(
                    Numerics::value80(actual.unwrap()).raw(),
                    retained.exp(input).unwrap().raw()
                );
            }
        }
        for (input, expected) in [
            (Value80::signed_zero(false), Value80::exact_u64(1)),
            (Value80::signed_zero(true), Value80::exact_u64(1)),
            (Value80::infinity(false), Value80::infinity(false)),
            (Value80::infinity(true), Value80::signed_zero(false)),
            (
                Value80::canonical_quiet_nan().with_sign(true),
                Value80::canonical_quiet_nan().with_sign(true),
            ),
        ] {
            assert_eq!(
                Numerics::value80(math.exp(Numerics::admit(input).unwrap()).unwrap().unwrap())
                    .raw(),
                expected.raw()
            );
        }
    }
}
#[cfg(test)]
thread_local! {
    pub(super) static FAIL_ALLOCATION: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
fn checked(value: &BigFloat) -> Result<(), NumericFailure> {
    match value.err() {
        None => Ok(()),
        Some(astro_float_num::Error::MemoryAllocation) => Err(NumericFailure::Allocation),
        Some(_) => Err(NumericFailure::Invariant),
    }
}
fn exact_input(input: Value80) -> BigFloat {
    let shift = input.significand().leading_zeros();
    BigFloat::from_raw_parts(
        &[input.significand() << shift],
        64,
        if input.is_negative() {
            Sign::Neg
        } else {
            Sign::Pos
        },
        i32::from(input.exponent().max(1)) - 16382 - shift as i32,
        false,
    )
}
impl Transcendentals {
    pub fn new() -> Result<Self, NumericFailure> {
        Ok(Self {
            constants: Consts::new().map_err(|_| NumericFailure::Allocation)?,
            logarithms: None,
        })
    }
    pub fn log(&mut self, input: Value) -> Result<Value, NumericFailure> {
        #[cfg(test)]
        if FAIL_ALLOCATION.with(|flag| flag.replace(false)) {
            return Err(NumericFailure::Allocation);
        }
        let input = Numerics::value80(input);
        let special = match input.classify() {
            ValueClass::Nan { .. } => Some(input.quiet_nan()),
            ValueClass::Zero => Some(Value80::infinity(true)),
            _ if input.is_negative() => Some(Value80::canonical_quiet_nan()),
            ValueClass::Infinity => Some(input),
            _ => None,
        };
        if let Some(value) = special {
            return Numerics::admit(value);
        }

        let bits = input.raw().to_bits();
        let cache = self
            .logarithms
            .get_or_insert_with(super::log_cache::LogCache::new);
        if let Some(value) = cache.get(bits) {
            return Ok(value);
        }
        let value = self.finite_log(input)?;
        self.logarithms
            .as_mut()
            .expect("initialized cache")
            .insert(bits, value);
        Ok(value)
    }

    fn finite_log(&mut self, input: Value80) -> Result<Value, NumericFailure> {
        if let Some(bits) = super::guarded_log::log(input.raw().to_bits()) {
            let raw = Raw80::try_from_bits(bits).map_err(|_| NumericFailure::Invariant)?;
            return Numerics::admit(Value80::from_raw(raw).map_err(|_| NumericFailure::Invariant)?);
        }

        // Exact normalization, including all 63 subnormal significand widths.
        let source = exact_input(input);
        checked(&source)?;
        let result = source.ln(64, RoundingMode::ToEven, &mut self.constants);
        checked(&result)?;
        if result.is_zero() {
            return Numerics::admit(Value80::signed_zero(false));
        }
        let (mantissa, precision, sign, exponent, _) =
            result.as_raw_parts().ok_or(NumericFailure::Invariant)?;
        // For positive binary80 x != 1, ln(x) is a normal binary80 number:
        // even the closest inputs to 1 give magnitudes around 2^-64.
        if precision != 64 || mantissa.len() != 1 || !(-16381..=16384).contains(&exponent) {
            return Err(NumericFailure::Invariant);
        }
        let raw = Raw80::new(
            (exponent + 16382) as u16 | if sign == Sign::Neg { 0x8000 } else { 0 },
            mantissa[0],
        );
        Numerics::admit(Value80::from_raw(raw).map_err(|_| NumericFailure::Invariant)?)
    }

    /// None requests retained exp at range boundaries, not after a library error.
    pub fn exp(&mut self, input: Value) -> Result<Option<Value>, NumericFailure> {
        let input = Numerics::value80(input);
        let special = match input.classify() {
            ValueClass::Nan { .. } => Some(input.quiet_nan()),
            ValueClass::Zero => Some(Value80::exact_u64(1)),
            ValueClass::Infinity => Some(if input.is_negative() {
                Value80::signed_zero(false)
            } else {
                input
            }),
            _ => None,
        };
        if let Some(value) = special {
            return Numerics::admit(value).map(Some);
        }
        // |x| >= 2^14 is beyond binary80 exp's finite output range. Keep
        // those inputs on the retained path without asking Astro to expand them.
        if input.exponent() >= 16397 {
            return Ok(None);
        }
        if let Some(bits) = super::guarded_exp::exp(input.raw().to_bits()) {
            let raw = Raw80::try_from_bits(bits).map_err(|_| NumericFailure::Invariant)?;
            return Numerics::admit(Value80::from_raw(raw).map_err(|_| NumericFailure::Invariant)?)
                .map(Some);
        }
        self.arbitrary_exp(input)
    }

    /// exp in arbitrary precision, correctly rounded; `None` requests the
    /// retained path. The table-driven path above must agree wherever it answers.
    fn arbitrary_exp(&mut self, input: Value80) -> Result<Option<Value>, NumericFailure> {
        let source = exact_input(input);
        checked(&source)?;
        let result = source.exp(64, RoundingMode::ToEven, &mut self.constants);
        checked(&result)?;
        let Some((mantissa, precision, sign, exponent, _)) = result.as_raw_parts() else {
            return Ok(None);
        };
        // A direct conversion at range boundaries risks double rounding.
        // Certify those results with directed wider endpoints instead.
        if !(-16380..=16383).contains(&exponent) {
            return super::boundary_exp::exp(&source, &mut self.constants)
                .map_err(|error| match error {
                    astro_float_num::Error::MemoryAllocation => NumericFailure::Allocation,
                    _ => NumericFailure::Invariant,
                })?
                .map(|bits| {
                    let raw = Raw80::try_from_bits(bits).map_err(|_| NumericFailure::Invariant)?;
                    Numerics::admit(Value80::from_raw(raw).map_err(|_| NumericFailure::Invariant)?)
                })
                .transpose();
        }
        if precision != 64 || mantissa.len() != 1 || sign != Sign::Pos {
            return Err(NumericFailure::Invariant);
        }
        let raw = Raw80::new((exponent + 16382) as u16, mantissa[0]);
        Numerics::admit(Value80::from_raw(raw).map_err(|_| NumericFailure::Invariant)?).map(Some)
    }
}