Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 194 additions & 0 deletions division_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package udecimal

import (
"fmt"
"math/big"
"testing"

"github.com/stretchr/testify/require"
)

func u256BigInt(u u256) *big.Int {
return new(big.Int).Or(new(big.Int).Lsh(u.carry.ToBigInt(), 128), u128{hi: u.hi, lo: u.lo}.ToBigInt())
}

func TestFastQuoTrialOverflow(t *testing.T) {
const highBit = uint64(1) << 63
testcases := []struct {
name string
u u256
v u128
wantErr bool
}{
{"three words", u256{carry: u128{lo: 1}}, u128{hi: 2}, false},
{"initial equality", u256{carry: u128{lo: 1}}, u128{hi: 1}, false},
{"four words", u256{carry: u128{hi: 1}, hi: 1}, u128{hi: highBit}, false},
{"first trial overflow", u256{carry: u128{hi: highBit}}, u128{hi: highBit, lo: 1}, true},
{"later trial overflow", u256{carry: u128{hi: 1}, hi: 1}, u128{hi: highBit, lo: 1}, true},
{"trial B plus one", u256{carry: u128{hi: 1, lo: 1}, hi: 1}, u128{hi: highBit, lo: highBit + 1}, true},
{"quotient overflow", u256{carry: u128{hi: 1}}, u128{hi: 1}, true},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
var q, r u128
var err error
require.NotPanics(t, func() { q, r, err = tc.u.fastQuo(tc.v) })
if tc.wantErr {
require.ErrorIs(t, err, errOverflow)
require.Equal(t, u128{}, q)
require.Equal(t, u128{}, r)
return
}
require.NoError(t, err)
wantQ, wantR := new(big.Int), new(big.Int)
wantQ.QuoRem(u256BigInt(tc.u), tc.v.ToBigInt(), wantR)
require.Zero(t, wantQ.Cmp(q.ToBigInt()))
require.Zero(t, wantR.Cmp(r.ToBigInt()))
})
}
}

func TestFastQuoNormalisation(t *testing.T) {
for shift := range 64 {
t.Run(fmt.Sprint(shift), func(t *testing.T) {
v := u128{hi: uint64(1) << (63 - shift), lo: 1}
wantQ := u128{hi: 1, lo: 3}
n := new(big.Int).Mul(v.ToBigInt(), wantQ.ToBigInt())
n.Add(n, big.NewInt(1))
words := new(big.Int).Set(n)
u := u256{lo: words.Uint64()}
words.Rsh(words, 64)
u.hi = words.Uint64()
words.Rsh(words, 64)
u.carry.lo = words.Uint64()
words.Rsh(words, 64)
u.carry.hi = words.Uint64()
q, r, err := u.fastQuo(v)
require.NoError(t, err)
require.Equal(t, wantQ, q)
require.Equal(t, u128{lo: 1}, r)
})
}
}

func TestDivTrialOverflow(t *testing.T) {
testcases := []struct {
name, a, b, want string
prec uint8
fallback bool
}{
{"reported", "340282366920938463463374607431768211455", "184467440737095516141553255926290448384", "1.8446744073709551617", 19, false},
{"small", "3.4028236692093846347", "1.8446744073709551616", "1.8446744073709551616", 19, false},
{"adjacent", "3.4028236692093846346", "1.8446744073709551616", "1.8446744073709551615", 19, false},
{"later trial", "100000000000000000000", "27105054312137610850.1863200217485427857", "3.6893488147419103231", 19, true},
{"reported precision 14", "340282366920938463463374607431768211455", "184467440737095516141553255926290448384", "1.84467440737095", 14, false},
{"small precision 14", "34028236692.09384634633747", "184467.44073709551616", "184467.44073709551616", 14, false},
}
for _, tc := range testcases {
for _, aneg := range []bool{false, true} {
for _, bneg := range []bool{false, true} {
for _, heap := range []bool{false, true} {
t.Run(fmt.Sprintf("%s/%t/%t/heap=%t", tc.name, aneg, bneg, heap), func(t *testing.T) {
previousPrec := defaultPrec
SetDefaultPrecision(tc.prec)
t.Cleanup(func() { SetDefaultPrecision(previousPrec) })
a, b := MustParse(tc.a), MustParse(tc.b)
a.neg, b.neg = aneg, bneg
if heap {
a.coef = bintFromBigInt(a.coef.GetBig())
b.coef = bintFromBigInt(b.coef.GetBig())
}
aAlias, bAlias := a, b
aCoef, bCoef := a.coef.GetBig(), b.coef.GetBig()
var got Decimal
var err error
require.NotPanics(t, func() { got, err = a.Div(b) })
require.NoError(t, err)
want := tc.want
if aneg != bneg {
want = "-" + want
}
require.Equal(t, want, got.String())
require.Equal(t, tc.prec, got.prec)
assertOverflow(t, got, heap || tc.fallback)
require.Equal(t, aAlias, a)
require.Equal(t, bAlias, b)
require.Equal(t, aCoef, aAlias.coef.GetBig())
require.Equal(t, bCoef, bAlias.coef.GetBig())
})
}
}
}
}
}

func TestQuoRemTrialOverflow(t *testing.T) {
for _, aneg := range []bool{false, true} {
for _, bneg := range []bool{false, true} {
t.Run(fmt.Sprintf("%t/%t", aneg, bneg), func(t *testing.T) {
a := MustParse("34028236692093846347")
b := MustParse("1.8446744073709551616")
a.neg, b.neg = aneg, bneg
var q, r Decimal
var err error
require.NotPanics(t, func() { q, r, err = a.QuoRem(b) })
require.NoError(t, err)
wantQ, wantR := "18446744073709551616", "0.6625392568231788544"
if aneg != bneg {
wantQ = "-" + wantQ
}
if aneg {
wantR = "-" + wantR
}
require.Equal(t, wantQ, q.String())
require.Equal(t, wantR, r.String())
require.Equal(t, uint8(0), q.prec)
require.Equal(t, uint8(19), r.prec)
assertOverflow(t, q, false)
assertOverflow(t, r, false)
})
}
}
}

var divisionBenchmarkResult Decimal

func TestDivTrialOverflowAllocations(t *testing.T) {
for _, left := range []string{"3.4028236692093846346", "3.4028236692093846347", "4"} {
t.Run(left, func(t *testing.T) {
a, b := MustParse(left), MustParse("1.8446744073709551616")
var err error
allocs := testing.AllocsPerRun(100, func() { divisionBenchmarkResult, err = a.Div(b) })
require.NoError(t, err)
require.Zero(t, allocs)
})
}
a := MustParse("340282366920938463463374607431768211455")
b := MustParse("184467440737095516141553255926290448384")
var err error
allocs := testing.AllocsPerRun(100, func() { divisionBenchmarkResult, err = a.Div(b) })
require.NoError(t, err)
require.Zero(t, allocs)
}

func BenchmarkDivTrialOverflow(b *testing.B) {
for _, tc := range []struct{ name, a, divisor string }{
{"adjacent", "3.4028236692093846346", "1.8446744073709551616"},
{"small", "3.4028236692093846347", "1.8446744073709551616"},
{"reported", "340282366920938463463374607431768211455", "184467440737095516141553255926290448384"},
{"fallback", "100000000000000000000", "27105054312137610850.1863200217485427857"},
} {
b.Run(tc.name, func(b *testing.B) {
a, divisor := MustParse(tc.a), MustParse(tc.divisor)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var err error
divisionBenchmarkResult, err = a.Div(divisor)
if err != nil {
b.Fatal(err)
}
}
})
}
}
99 changes: 82 additions & 17 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,27 @@ func FuzzDivDec(f *testing.F) {
f.Add(c.neg, c.hi, c.lo, c.prec, d.neg, d.hi, d.lo, d.prec)
}
}
for _, seed := range []struct {
ahi, alo uint64
aprec uint8
bhi, blo uint64
bprec uint8
}{
{math.MaxUint64, math.MaxUint64, 0, 9999999999999999999, 0, 0},
{1, 15581492618384294731, 19, 1, 0, 19},
{1, 15581492618384294730, 19, 1, 0, 19},
{5, 7766279631452241920, 0, 14693679385278593849, 11237880364719817873, 19},
} {
for _, aneg := range []bool{false, true} {
for _, bneg := range []bool{false, true} {
f.Add(aneg, seed.ahi, seed.alo, seed.aprec, bneg, seed.bhi, seed.blo, seed.bprec)
}
}
}

f.Fuzz(func(t *testing.T, aneg bool, ahi uint64, alo uint64, aprec uint8, bneg bool, bhi uint64, blo uint64, bprec uint8) {
aprec = aprec % maxPrec
bprec = bprec % maxPrec
aprec = aprec % (maxPrec + 1)
bprec = bprec % (maxPrec + 1)

a, err := NewFromHiLo(aneg, ahi, alo, aprec)
require.NoError(t, err)
Expand All @@ -317,22 +334,70 @@ func FuzzDivDec(f *testing.F) {

require.NoError(t, err)

// compare with shopspring/decimal
aa := ssDecimal(aneg, ahi, alo, aprec)
bb := ssDecimal(bneg, bhi, blo, bprec)

prec := int32(c.Prec())
cc := aa.DivRound(bb, 28).Truncate(prec)

// sometimes shopspring/decimal does rounding differently
// e.g. 0.099999999999999 -> 0.1
// so to check the result, we can check the difference
// between our result and shopspring/decimal result
// valid result should be less than or equal to 1e-19, which is our smallest unit
d := MustParse(cc.String())
e := c.Sub(d)
// Scale positive coefficients and truncate with independent integer arithmetic.
exponent := int(defaultPrec) + int(b.prec) - int(a.prec)
factor := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(exponent)), nil)
want := new(big.Int).Mul(a.coef.GetBig(), factor)
want.Quo(want, b.coef.GetBig())
require.Zero(t, want.Cmp(c.coef.GetBig()))
if want.Sign() == 0 {
require.Equal(t, Zero, c)
} else {
require.Equal(t, defaultPrec, c.prec)
require.Equal(t, a.neg != b.neg, c.neg)
}
})
}

require.LessOrEqual(t, e.Abs().Cmp(oneUnit), 0, "a: %s, b: %s, expected %s, got %s", a, b, cc.String(), c.String())
func FuzzFastQuo(f *testing.F) {
const highBit = uint64(1) << 63
for _, seed := range []struct {
carryHi, carryLo, hi, lo, divisorHi, divisorLo uint64
}{
{0, 0, 0, 15, 0, 4},
{0, 1, 0, 0, 0, 2},
{0, 1, 0, 0, 2, 0},
{0, 1, 0, 0, 1, 0},
{1, 0, 1, 0, highBit, 0},
{highBit, 0, 0, 0, highBit, 1},
{1, 0, 1, 0, highBit, 1},
{1, 1, 1, 0, highBit, highBit + 1},
{1, 0, 0, 0, 1, 0},
} {
f.Add(seed.carryHi, seed.carryLo, seed.hi, seed.lo, seed.divisorHi, seed.divisorLo)
}
f.Fuzz(func(t *testing.T, carryHi, carryLo, hi, lo, divisorHi, divisorLo uint64) {
u := u256{carry: u128{hi: carryHi, lo: carryLo}, hi: hi, lo: lo}
v := u128{hi: divisorHi, lo: divisorLo}
if v.IsZero() {
t.Skip()
}
numerator, divisor := u256BigInt(u), v.ToBigInt()
wantQ, wantR := new(big.Int), new(big.Int)
wantQ.QuoRem(numerator, divisor, wantR)
wantErr := wantQ.BitLen() > 128
if !wantErr && !u.carry.IsZero() && v.hi != 0 {
// The two trial estimates can exceed a word even when the final quotient fits.
shift := uint(128 - divisor.BitLen())
n := new(big.Int).Lsh(numerator, shift)
d := new(big.Int).Lsh(divisor, shift)
dHi := new(big.Int).Rsh(new(big.Int).Set(d), 64)
first := new(big.Int).Quo(new(big.Int).Rsh(new(big.Int).Set(n), 128), dHi)
prefixR := new(big.Int)
new(big.Int).QuoRem(new(big.Int).Rsh(new(big.Int).Set(n), 64), d, prefixR)
second := new(big.Int).Quo(prefixR, dHi)
wantErr = first.BitLen() > 64 || second.BitLen() > 64
}
q, r, err := u.fastQuo(v)
if wantErr {
require.ErrorIs(t, err, errOverflow)
require.Equal(t, u128{}, q)
require.Equal(t, u128{}, r)
return
}
require.NoError(t, err)
require.Zero(t, wantQ.Cmp(q.ToBigInt()))
require.Zero(t, wantR.Cmp(r.ToBigInt()))
})
}

Expand Down
14 changes: 9 additions & 5 deletions u256.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ func (u u256) fastQuo(v u128) (u128, u128, error) {
return u128{}, u128{}, errOverflow
}

q, r := u.div256by128(v)
return q, r, nil
return u.div256by128(v)
}

// div192by64 return q,r which:
Expand All @@ -160,9 +159,10 @@ func (u u256) div192by64(v uint64) (u128, uint64, error) {

// div256by128 performs u256 / u128, which u256.carry < u128
// Returns both quotient and remainder
// Returns errOverflow if a trial quotient cannot fit into uint64.
// This implementation is based on divllu from https://github.com/ridiculousfish/libdivide
// The algorithm is explained in this blog post: https://ridiculousfish.com/blog/posts/labor-of-division-episode-iv.html
func (u u256) div256by128(v u128) (u128, u128) {
func (u u256) div256by128(v u128) (u128, u128, error) {
// normalize v
n := bits.LeadingZeros64(v.hi)

Expand All @@ -178,14 +178,18 @@ func (u u256) div256by128(v u128) (u128, u128) {

// q = a / v
aLen := 3
if a[3] != 0 || (a[3] == 0 && a[2] > v.hi) {
if a[3] != 0 || (a[3] == 0 && a[2] >= v.hi) {
aLen = 4
}

q := [2]uint64{}

for i := aLen - 3; i >= 0; i-- {
u2, u1, u0 := a[i+2], a[i+1], a[i]
if u2 >= v.hi {
// The trial quotient can overflow even when the final quotient fits.
return u128{}, u128{}, errOverflow
}

// trial quotient tq = [u2,u1,u0] / v ~= [u2,u1] / v.hi
// tq <= q + 2
Expand Down Expand Up @@ -237,7 +241,7 @@ func (u u256) div256by128(v u128) (u128, u128) {
//nolint:gosec // 0 <= n <= 63, so it's safe to convert to uint
r := u128{hi: a[1], lo: a[0]}.Rsh(uint(n))

return u128{hi: q[1], lo: q[0]}, r
return u128{hi: q[1], lo: q[0]}, r, nil
}

// subUnsafe returns u - v with u >= v
Expand Down