surrealdb-expr 4.0.0

A scalable, distributed, collaborative, document-graph database, for the realtime web
Documentation
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
//! Guarded decimal arithmetic over a 128-bit coefficient.
//!
//! Values are `±digits · 10^-scale` with a 7-digit *guard* fraction below the
//! last coefficient digit. The guard is truncated, never rounded, and only its
//! first digit takes part in a final rounding. Operations keep the guard
//! through rescaling, addition, multiplication and long division, and correct
//! a product or quotient for the guards of its operands with further guarded
//! operations.
//!
//! This is the arithmetic behind the float conversions in `float.rs`, whose
//! output is persisted in index keys, so it is specified by its exact
//! results: every operation, including its asymmetries between positive and
//! negative intermediates, is part of the format. Do not simplify it towards
//! mathematically exact results.

use std::cmp::Ordering;

/// Number of guard digits carried below the coefficient.
pub(super) const GUARD_DIGITS: u32 = 7;
/// `10^GUARD_DIGITS`, one unit of the coefficient expressed in guard units.
pub(super) const GUARD_CARRY: u64 = 10_000_000;
/// Width of the guard field; guard values are stored masked to it.
pub(super) const GUARD_MASK: u32 = 0x00FF_FFFF;
/// Largest `p` such that `10^p` fits a `u64`.
pub(super) const DIGIT_POWER_10: u32 = 19;

pub(super) const POW10_U64: [u64; 20] = {
	let mut t = [1u64; 20];
	let mut i = 1;
	while i < 20 {
		t[i] = t[i - 1] * 10;
		i += 1;
	}
	t
};

pub(super) const POW10_U128: [u128; 39] = {
	let mut t = [1u128; 39];
	let mut i = 1;
	while i < 39 {
		t[i] = t[i - 1] * 10;
		i += 1;
	}
	t
};

// ---------------------------------------------------------------------------
// Guarded decimal value
// ---------------------------------------------------------------------------

/// A decimal `±digits · 10^-scale` with a truncated guard fraction `guard /
/// 10^7` of one coefficient unit.
#[derive(Clone, Copy, Debug)]
pub(super) struct D {
	pub(super) digits: u128,
	pub(super) scale: i16,
	pub(super) neg: bool,
	pub(super) guard: u32,
}

impl D {
	pub(super) const ZERO: D = D {
		digits: 0,
		scale: 0,
		neg: false,
		guard: 0,
	};

	pub(super) fn new(digits: u128, scale: i16, neg: bool) -> Self {
		D {
			digits,
			scale,
			neg,
			guard: 0,
		}
	}

	pub(super) fn construct(digits: u128, exp: i32, neg: bool, guard: Guard) -> Self {
		D {
			digits,
			scale: (-exp) as i16,
			neg,
			guard: guard.digits & GUARD_MASK,
		}
	}

	pub(super) fn exponent(&self) -> i32 {
		-(self.scale as i32)
	}

	pub(super) fn is_zero(&self) -> bool {
		self.digits == 0 && self.guard == 0
	}

	pub(super) fn is_one(&self) -> bool {
		self.digits == 1 && self.scale == 0 && !self.neg && self.guard == 0
	}

	pub(super) fn neg(mut self) -> Self {
		self.neg = !self.neg;
		self
	}

	pub(super) fn without_guard(mut self) -> Self {
		self.guard = 0;
		self
	}

	pub(super) fn guard(&self) -> Guard {
		Guard::from_digits(self.guard as u64, GUARD_DIGITS)
	}

	pub(super) fn take_guard(&mut self) -> Guard {
		let g = self.guard();
		self.guard = 0;
		g
	}

	pub(super) fn set_guard(&mut self, g: Guard) {
		self.guard = g.digits & GUARD_MASK;
	}

	/// Removes the guard and returns it as a decimal value of its own.
	pub(super) fn take_guard_decimal(&mut self) -> D {
		let g = self.take_guard();
		if g.digits != 0 {
			D::construct(
				g.digits as u128,
				self.exponent() - GUARD_DIGITS as i32,
				self.neg,
				Guard::NONE,
			)
		} else {
			D {
				neg: self.neg,
				..D::ZERO
			}
		}
	}
}

/// A guard fraction in transit, with the number of meaningful digits it holds.
#[derive(Clone, Copy, Debug)]
pub(super) struct Guard {
	digits: u32,
	count: u32,
}

impl Guard {
	pub(super) const NONE: Guard = Guard {
		digits: 0,
		count: 0,
	};

	pub(super) fn from_digits(digits: u64, count: u32) -> Self {
		let (digits, count) = normalize_guard(digits, count);
		Guard {
			digits,
			count,
		}
	}

	pub(super) fn first_digit(self) -> u32 {
		self.digits / 1_000_000
	}

	pub(super) fn push_back(&mut self, tail: Guard) {
		match self.count {
			1..=6 => {
				self.digits += tail.digits / POW10_U64[self.count as usize] as u32;
				self.count += tail.count.min(GUARD_DIGITS - self.count);
			}
			0 => *self = tail,
			_ => {}
		}
	}

	pub(super) fn append(&mut self, tail: u64, count: u32) {
		if tail == 0 {
			return;
		}
		let (tail, count) = normalize_guard(tail, count);
		match self.count {
			1..=6 => {
				self.digits += tail / POW10_U64[self.count as usize] as u32;
				self.count += count.min(GUARD_DIGITS - self.count);
			}
			0 => {
				self.digits = tail;
				self.count = count;
			}
			_ => {}
		}
	}

	/// Shifts `power` leading guard digits out as an integer correction.
	pub(super) fn scale_up(&mut self, mut power: u32) -> Option<D> {
		if self.digits == 0 {
			return None;
		}
		let mut res = D::ZERO;
		let ten = D::new(10, 0, false);
		while power > 0 {
			let digit = self.digits / 1_000_000;
			res = round(add(round(mul(res, ten)), D::new(digit as u128, 0, false)));
			self.digits = (self.digits % 1_000_000) * 10;
			power -= 1;
			self.count = self.count.saturating_sub(1);
		}
		if res.is_zero() {
			None
		} else {
			Some(res)
		}
	}

	pub(super) fn overflowing_add(&mut self, other: Guard) -> bool {
		let res = self.digits as u64 + other.digits as u64;
		if res >= GUARD_CARRY {
			let res = res - GUARD_CARRY;
			self.digits = res as u32;
			self.count = count_digits(res);
			true
		} else {
			self.digits = res as u32;
			self.count = count_digits(res);
			false
		}
	}

	pub(super) fn overflowing_sub(&mut self, other: Guard) -> bool {
		if self.digits >= other.digits {
			self.digits -= other.digits;
			self.count = count_digits(self.digits as u64);
			false
		} else {
			let res = GUARD_CARRY - (other.digits - self.digits) as u64;
			self.digits = res as u32;
			self.count = count_digits(res);
			true
		}
	}
}

pub(super) fn count_digits(digits: u64) -> u32 {
	if digits == 0 {
		0
	} else {
		decimal_digits_64(digits).min(GUARD_DIGITS)
	}
}

pub(super) fn normalize_guard(mut digits: u64, mut count: u32) -> (u32, u32) {
	if count > GUARD_DIGITS {
		digits /= POW10_U64[(count - GUARD_DIGITS) as usize];
		count = GUARD_DIGITS;
	} else if count < GUARD_DIGITS {
		digits = digits.wrapping_mul(POW10_U64[(GUARD_DIGITS - count) as usize]);
	}
	(digits as u32, count)
}

// ---------------------------------------------------------------------------
// Integer helpers
// ---------------------------------------------------------------------------

pub(super) fn decimal_digits_64(n: u64) -> u32 {
	if n == 0 {
		0
	} else {
		n.ilog10() + 1
	}
}

pub(super) fn decimal_digits(n: u128) -> u32 {
	if n == 0 {
		0
	} else {
		n.ilog10() + 1
	}
}

/// The largest `k` such that `n · 10^k` fits a `u128` (39 for zero).
pub(super) fn remaining_decimal_digits(n: u128) -> u32 {
	let dd = decimal_digits(n);
	let mut max_digits = 39;
	if dd != 0 && u128::MAX / pow10(max_digits - dd) < n {
		max_digits -= 1;
	}
	max_digits - dd
}

pub(super) fn can_scale_by_power_of_ten(n: u128, power: u32) -> bool {
	n <= u128::MAX / pow10(power)
}

pub(super) fn pow10(p: u32) -> u128 {
	if p < 39 {
		POW10_U128[p as usize]
	} else {
		// 10^39 exceeds u128; only reached through `u128::MAX / 10^p` style
		// bounds where the true quotient is zero.
		u128::MAX
	}
}

/// Full 256-bit product of two `u128`s as `(low, high)`.
pub(super) fn widening_mul(a: u128, b: u128) -> (u128, u128) {
	let (a0, a1) = (a as u64 as u128, a >> 64);
	let (b0, b1) = (b as u64 as u128, b >> 64);
	let p00 = a0 * b0;
	let p01 = a0 * b1;
	let p10 = a1 * b0;
	let p11 = a1 * b1;
	let mid = (p00 >> 64) + (p01 as u64 as u128) + (p10 as u64 as u128);
	let low = (p00 as u64 as u128) | (mid << 64);
	let high = p11 + (p01 >> 64) + (p10 >> 64) + (mid >> 64);
	(low, high)
}

/// Divides the 256-bit value `(low, high)` by `d`, returning the remainder.
pub(super) fn div_rem_256_by_u64(low: &mut u128, high: &mut u128, d: u64) -> u64 {
	let words = [*low as u64, (*low >> 64) as u64, *high as u64, (*high >> 64) as u64];
	let mut out = [0u64; 4];
	let mut rem: u128 = 0;
	for i in (0..4).rev() {
		let cur = (rem << 64) | words[i] as u128;
		let q = cur / d as u128;
		out[i] = q as u64;
		rem = cur - q * d as u128;
	}
	*low = out[0] as u128 | ((out[1] as u128) << 64);
	*high = out[2] as u128 | ((out[3] as u128) << 64);
	rem as u64
}

/// `(a · b) / d` and `(a · b) % d`, for operands where the quotient fits in
/// 64 bits.
pub(super) fn mul_div_rem(a: u128, b: u128, d: u128) -> (u64, u128) {
	let (low, high) = widening_mul(a, b);
	if high == 0 {
		let q = low / d;
		return (q as u64, low - q * d);
	}
	// A 128-bit product never needs the wide path, so `d` has a high word.
	debug_assert!(d >> 64 != 0);
	// Normalise so the divisor's top bit is set; the quotient is unchanged
	// and the remainder is shifted by the same amount.
	let s = d.leading_zeros();
	let d = d << s;
	let (hi, lo) = if s == 0 {
		(high, low)
	} else {
		((high << s) | (low >> (128 - s)), low << s)
	};
	// The numerator is below d · 2^64, so it has three significant words and
	// a single-word quotient estimated from the divisor's top word, at most
	// two too large.
	let top = (hi << 64) | (lo >> 64);
	let d1 = d >> 64;
	let mut q = (top / d1).min(u64::MAX as u128);
	let (mut p_lo, mut p_hi) = widening_mul(q, d);
	while (p_hi, p_lo) > (hi, lo) {
		q -= 1;
		let (l, borrow) = p_lo.overflowing_sub(d);
		p_lo = l;
		p_hi -= borrow as u128;
	}
	let r = lo.wrapping_sub(p_lo);
	(q as u64, r >> s)
}

// ---------------------------------------------------------------------------
// Rescaling
// ---------------------------------------------------------------------------

/// Moves `d` to `new_scale`. Returns whether an upward rescale was clamped
/// short of the target because the coefficient ran out of room.
pub(super) fn rescale(d: &mut D, new_scale: i16) -> bool {
	match new_scale.cmp(&d.scale) {
		Ordering::Greater => rescale_up(d, new_scale),
		Ordering::Less => {
			rescale_down(d, new_scale);
			false
		}
		Ordering::Equal => false,
	}
}

pub(super) fn extend_scale_to(mut d: D, new_scale: i16) -> D {
	if new_scale > d.scale {
		rescale(&mut d, new_scale);
	}
	d
}

pub(super) fn rescale_up(d: &mut D, new_scale: i16) -> bool {
	let power = (new_scale as i32 - d.scale as i32) as u32;
	if d.digits == 0 {
		d.scale = new_scale;
		let mut g = d.guard();
		if let Some(c) = g.scale_up(power) {
			scale_correct(d, c);
		}
		d.set_guard(g);
		return false;
	}
	let remaining = remaining_decimal_digits(d.digits);
	if remaining == 0 {
		true
	} else if remaining < power {
		rescale_up_unchecked(d, remaining);
		true
	} else {
		rescale_up_unchecked(d, power);
		false
	}
}

pub(super) fn rescale_up_unchecked(d: &mut D, gap: u32) {
	d.digits *= pow10(gap);
	d.scale = (d.scale as i32 + gap as i32) as i16;
	let mut g = d.guard();
	if let Some(c) = g.scale_up(gap) {
		scale_correct(d, c);
	}
	d.set_guard(g);
}

pub(super) fn scale_correct(d: &mut D, mut c: D) {
	c.neg = d.neg;
	c.scale = (c.scale as i32 + d.scale as i32) as i16;
	*d = add(*d, c);
}

pub(super) fn rescale_down(d: &mut D, new_scale: i16) {
	let old = d.guard();
	let power = (d.scale as i32 - new_scale as i32) as u32;
	let dd = decimal_digits(d.digits);
	if dd <= power {
		let shift = power - dd;
		if shift >= GUARD_DIGITS {
			d.guard = 0;
		} else {
			let g = make_guard(d.digits, dd, shift);
			d.set_guard(g);
		}
		d.digits = 0;
	} else {
		let divisor = pow10(power);
		let extra = d.digits % divisor;
		d.digits /= divisor;
		if extra != 0 || old.digits != 0 {
			let mut g = make_guard(extra, power, 0);
			g.push_back(old);
			d.set_guard(g);
		}
	}
	d.scale = (d.scale as i32 - power as i32) as i16;
}

pub(super) fn make_guard(digits: u128, dd: u32, shift: u32) -> Guard {
	let power = dd.saturating_sub(GUARD_DIGITS - shift);
	let v = if power > 0 {
		(digits / pow10(power)) as u64
	} else {
		digits as u64
	};
	Guard::from_digits(v, dd - power + shift)
}

// ---------------------------------------------------------------------------
// Addition and subtraction
// ---------------------------------------------------------------------------

pub(super) fn add(lhs: D, rhs: D) -> D {
	match (lhs.neg, rhs.neg) {
		(false, false) => add_abs(lhs, rhs),
		(true, true) => add_abs(rhs.neg(), lhs.neg()).neg(),
		(false, true) => sub_abs(lhs, rhs.neg()),
		(true, false) => sub_abs(rhs, lhs.neg()),
	}
}

pub(super) fn sub(lhs: D, rhs: D) -> D {
	match (lhs.neg, rhs.neg) {
		(false, false) => sub_abs(lhs, rhs),
		(true, true) => sub_abs(rhs.neg(), lhs.neg()),
		(false, true) => add_abs(lhs, rhs.neg()),
		(true, false) => add_abs(lhs.neg(), rhs).neg(),
	}
}

pub(super) fn add_abs(lhs: D, rhs: D) -> D {
	if rhs.is_zero() {
		return extend_scale_to(lhs, rhs.scale);
	}
	if lhs.is_zero() {
		return extend_scale_to(rhs, lhs.scale);
	}
	match lhs.scale.cmp(&rhs.scale) {
		Ordering::Less => add_rescale(lhs, rhs),
		Ordering::Equal => add_aligned(lhs, rhs),
		Ordering::Greater => add_rescale(rhs, lhs),
	}
}

pub(super) fn add_rescale(mut lhs: D, mut rhs: D) -> D {
	if rescale(&mut lhs, rhs.scale) {
		rescale(&mut rhs, lhs.scale);
	}
	add_aligned(lhs, rhs)
}

pub(super) fn add_aligned(mut lhs: D, mut rhs: D) -> D {
	match lhs.digits.overflowing_add(rhs.digits) {
		(digits, false) => {
			lhs.digits = digits;
			let mut g = lhs.take_guard();
			let carry = g.overflowing_add(rhs.guard());
			lhs.set_guard(g);
			if carry {
				magnitude_inc(&mut lhs);
			}
			lhs
		}
		(_, true) => {
			let scale = lhs.scale - 1;
			rescale(&mut lhs, scale);
			rescale(&mut rhs, scale);
			add_aligned(lhs, rhs)
		}
	}
}

pub(super) fn sub_abs(mut lhs: D, mut rhs: D) -> D {
	if rhs.is_zero() {
		return extend_scale_to(lhs, rhs.scale);
	}
	if lhs.is_zero() {
		return extend_scale_to(rhs, lhs.scale).neg();
	}
	match lhs.scale.cmp(&rhs.scale) {
		Ordering::Equal => {}
		Ordering::Less => {
			if rescale(&mut lhs, rhs.scale) {
				rescale(&mut rhs, lhs.scale);
			}
		}
		Ordering::Greater => {
			if rescale(&mut rhs, lhs.scale) {
				rescale(&mut lhs, rhs.scale);
			}
		}
	}
	sub_aligned(lhs, rhs)
}

pub(super) fn sub_aligned(mut lhs: D, mut rhs: D) -> D {
	match lhs.digits.cmp(&rhs.digits) {
		Ordering::Less => {
			rhs.digits -= lhs.digits;
			sub_guard(&mut rhs, &lhs);
			rhs.neg()
		}
		Ordering::Equal => {
			lhs.digits = 0;
			sub_guard(&mut lhs, &rhs);
			lhs
		}
		Ordering::Greater => {
			lhs.digits -= rhs.digits;
			sub_guard(&mut lhs, &rhs);
			lhs
		}
	}
}

pub(super) fn sub_guard(this: &mut D, other: &D) {
	let mut g = this.take_guard();
	let borrow = g.overflowing_sub(other.guard());
	this.set_guard(g);
	if borrow {
		magnitude_dec(this);
	}
}

/// Steps the coefficient by one unit after a guard carry. For a negative
/// value this adds a negative unit through subtraction, which moves the
/// coefficient towards zero; that direction is part of the reproduced
/// algorithm.
pub(super) fn magnitude_inc(d: &mut D) {
	let one = D::new(1, d.scale, d.neg);
	*d = if d.neg {
		sub(*d, one)
	} else {
		add(*d, one)
	};
}

/// Steps the coefficient by one unit after a guard borrow, with the same sign
/// handling as [`magnitude_inc`].
pub(super) fn magnitude_dec(d: &mut D) {
	let one = D::new(1, d.scale, d.neg);
	*d = if d.neg {
		add(*d, one)
	} else {
		sub(*d, one)
	};
}

/// Adds a correction term unless it is zero.
pub(super) fn correct(d: D, correction: D) -> D {
	if correction.is_zero() {
		d
	} else {
		add(d, correction)
	}
}

// ---------------------------------------------------------------------------
// Multiplication
// ---------------------------------------------------------------------------

pub(super) fn mul(mut lhs: D, mut rhs: D) -> D {
	let neg = lhs.neg != rhs.neg;
	let mut exp = lhs.exponent() + rhs.exponent();
	let mut guard = Guard::NONE;
	if lhs.is_zero() || rhs.is_zero() {
		return D::construct(0, exp, neg, guard);
	}
	let correction = if lhs.guard != 0 || rhs.guard != 0 {
		Some(mul_correction(&mut lhs, &mut rhs))
	} else {
		None
	};
	let (mut low, mut high) = widening_mul(lhs.digits, rhs.digits);
	// Drop low digits until the product fits, the dropped digits becoming the
	// guard. Each step removes at most as many digits as the excess bits
	// guarantee are surplus, so the product ends at the widest coefficient
	// that fits, exactly as dropping one digit at a time would.
	while high != 0 {
		let excess = 128 - high.leading_zeros();
		let j = (((excess as u64 * 77) >> 8) as u32).clamp(1, DIGIT_POWER_10);
		let rem = div_rem_256_by_u64(&mut low, &mut high, POW10_U64[j as usize]);
		let mut g = make_guard(rem as u128, j, 0);
		g.push_back(guard);
		guard = g;
		exp += j as i32;
	}
	let result = D::construct(low, exp, neg, guard);
	match correction {
		Some(c) => correct(result, c),
		None => result,
	}
}

pub(super) fn mul_correction(lhs: &mut D, rhs: &mut D) -> D {
	let xi_lhs = lhs.take_guard_decimal();
	let xi_rhs = rhs.take_guard_decimal();
	match (xi_lhs.is_zero(), xi_rhs.is_zero()) {
		(true, true) => D::ZERO,
		(true, false) => mul(*lhs, xi_rhs),
		(false, true) => mul(*rhs, xi_lhs),
		(false, false) => add(mul(*lhs, xi_rhs), add(mul(*rhs, xi_lhs), mul(xi_rhs, xi_lhs))),
	}
}

// ---------------------------------------------------------------------------
// Division
// ---------------------------------------------------------------------------

pub(super) fn div(mut dividend: D, mut divisor: D) -> D {
	let neg = dividend.neg != divisor.neg;
	debug_assert!(!divisor.is_zero());
	if dividend.is_zero() || divisor.is_one() {
		dividend.neg = neg;
		return dividend;
	}
	let correction = if dividend.guard != 0 || divisor.guard != 0 {
		Some(div_correction(&mut dividend, &mut divisor))
	} else {
		None
	};
	let exp = dividend.exponent() - divisor.exponent();
	let quotient = dividend.digits / divisor.digits;
	let result = LongDivision {
		digits: quotient,
		remainder: dividend.digits - quotient * divisor.digits,
		divisor: divisor.digits,
		neg,
		exp,
		guard: Guard::NONE,
	}
	.run();
	match correction {
		Some(c) => correct(result, c),
		None => result,
	}
}

pub(super) fn div_correction(dividend: &mut D, divisor: &mut D) -> D {
	let xi_dividend = dividend.take_guard_decimal();
	let xi_divisor = divisor.take_guard_decimal();
	if xi_dividend.is_zero() && xi_divisor.is_zero() {
		return D::ZERO;
	}
	if xi_divisor.is_zero() {
		return div(xi_dividend, *divisor);
	}
	let x = if xi_dividend.is_zero() {
		mul(*dividend, xi_divisor).neg().without_guard()
	} else {
		sub(mul(*divisor, xi_dividend), mul(*dividend, xi_divisor)).without_guard()
	};
	if x.is_zero() {
		return D::ZERO;
	}
	let y = add(mul(*divisor, *divisor), mul(*divisor, xi_divisor)).without_guard();
	div(x, y)
}

pub(super) struct LongDivision {
	digits: u128,
	remainder: u128,
	divisor: u128,
	neg: bool,
	exp: i32,
	guard: Guard,
}

impl LongDivision {
	pub(super) fn run(mut self) -> D {
		if self.remainder != 0 {
			self.fractional_part();
		}
		D::construct(self.digits, self.exp, self.neg, self.guard)
	}

	/// Appends `quotient` (holding `mul_power + add_power` digits) to the
	/// coefficient, returning the `add_power` digits that did not fit.
	pub(super) fn mul_add(&mut self, quotient: u64, mul_power: u32, add_power: u32) -> u64 {
		self.digits *= POW10_U64[mul_power as usize] as u128;
		self.exp -= mul_power as i32;
		let qd = POW10_U64[add_power as usize];
		let (mut quotient, mut extra) = (quotient / qd, quotient % qd);
		match self.digits.overflowing_add(quotient as u128) {
			(digits, false) => self.digits = digits,
			(_, true) => {
				self.digits /= 10;
				(quotient, extra) = (quotient / 10, quotient % 10);
				self.digits += quotient as u128;
				self.exp += 1;
			}
		}
		extra
	}

	pub(super) fn fractional_part(&mut self) {
		let mut power = 0;
		let mut extra = 0;
		let mut carry = false;
		while self.remainder != 0 || carry {
			let remaining = remaining_decimal_digits(self.digits);
			if carry {
				if remaining != 0 {
					if remaining <= power {
						let p = power - remaining;
						extra = self.mul_add(extra, remaining, p);
						return self.set_guard(extra, p);
					}
					extra = self.mul_add(extra, power, 0);
					carry = false;
					continue;
				}
				return self.set_guard(extra, power);
			}
			let mut q_power = remaining.min(DIGIT_POWER_10);
			let max_power = (q_power + GUARD_DIGITS).min(DIGIT_POWER_10);
			let mut quotient = self.next_digits(max_power);
			if remaining == 0 {
				return self.set_guard(quotient, max_power);
			}
			if quotient == 0 {
				let p = max_power.min(q_power);
				if self.digits != 0 {
					self.digits *= POW10_U64[p as usize] as u128;
				}
				self.exp -= p as i32;
				continue;
			}
			power = decimal_digits_64(quotient);
			if q_power == 0 {
				return self.set_guard(quotient, power);
			}
			let leading_zeros = max_power - power;
			if self.remainder == 0 {
				while quotient >= 10 && quotient.is_multiple_of(10) {
					quotient /= 10;
					power -= 1;
				}
			}
			if power < q_power {
				q_power = power;
			}
			power = power - q_power + leading_zeros;
			extra = self.mul_add(quotient, q_power, power);
			if power != 0 {
				carry = true;
			}
		}
	}

	/// The next `power` quotient digits; advances the remainder.
	pub(super) fn next_digits(&mut self, power: u32) -> u64 {
		let (q, r) = self.peek_digits(power);
		self.remainder = r;
		q
	}

	pub(super) fn peek_digits(&self, power: u32) -> (u64, u128) {
		if can_scale_by_power_of_ten(self.remainder, power) {
			let n = self.remainder * pow10(power);
			let q = n / self.divisor;
			(q as u64, n - q * self.divisor)
		} else {
			mul_div_rem(self.remainder, pow10(power), self.divisor)
		}
	}

	pub(super) fn set_guard(&mut self, quotient: u64, power: u32) {
		let mut g = Guard::from_digits(quotient, power);
		let rest = GUARD_DIGITS.saturating_sub(power);
		if self.remainder != 0 && rest != 0 {
			let (q, _) = self.peek_digits(rest);
			g.append(q, rest);
		}
		if g.digits != 0 || self.remainder != 0 {
			self.guard = g;
		}
	}
}

/// Rounds half-up on the first guard digit, consuming the guard.
pub(super) fn round(mut d: D) -> D {
	let digit = d.take_guard().first_digit();
	if digit >= 5 {
		if d.digits == u128::MAX {
			d.digits /= 10;
			d.scale -= 1;
		}
		d.digits += 1;
	}
	d
}