Skip to content

Commit

Permalink
dec: code cleanup
Browse files Browse the repository at this point in the history
Mostly removed linter complaints. Also moved some consts and functions
from math/big to stdlib.go.
  • Loading branch information
db47h committed May 15, 2020
1 parent caf8e97 commit 709091c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 30 deletions.
3 changes: 3 additions & 0 deletions arith_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
package decimal

var support_adx = false

// keep golint quiet
var _ = support_adx
8 changes: 0 additions & 8 deletions dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type dec []Word

var (
decOne = dec{1}
decTwo = dec{2}
decTen = dec{10}
)

Expand Down Expand Up @@ -315,8 +314,6 @@ func putDec(x *dec) {

var decPool sync.Pool

const divRecursiveThreshold = 100

// q = (uIn-r)/vIn, with 0 <= r < vIn
// Uses z as storage for q, and u as storage for r if possible.
// See Knuth, Volume 2, section 4.3.1, Algorithm D.
Expand Down Expand Up @@ -429,11 +426,6 @@ func (q dec) divBasic(u, v dec) {
putDec(qhatvp)
}

// greaterThan reports whether (x1*_BD + x2) > (y1*_BD + y2)
func greaterThan(x1, x2, y1, y2 Word) bool {
return x1 > y1 || x1 == y1 && x2 > y2
}

// modW returns x % d.
func (x dec) modW(d Word) (r Word) {
for i := len(x) - 1; i >= 0; i-- {
Expand Down
4 changes: 0 additions & 4 deletions dec_arith_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ var mul10WWTests = []struct {
q, r Word
}{
{_DMax, _DMax, _DMax - 1, 1},
// 32 bit only: {0xc47dfa8c, 50911, 0x98a4, 0x998587f4},
}

func TestDecMul10WW(t *testing.T) {
Expand All @@ -467,9 +466,6 @@ var mulAdd10WWWTests = []struct {
x, y, c Word
q, r Word
}{
// TODO(db47h): These will only work on 64-bit platforms.
// {15064310297182388543, 0xe7df04d2d35d5d80, 13537600649892366549, 13644450054494335067, 10832252001440893781},
// {15064310297182388543, 0xdab2f18048baa68d, 13644450054494335067, 12869334219691522700, 14233854684711418382},
{_DMax, _DMax, 0, _DMax - 1, 1},
{_DMax, _DMax, _DMax, _DMax, 0},
}
Expand Down
6 changes: 4 additions & 2 deletions dec_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ func (x dec) itoa(neg bool, base int) []byte {
return s[i:]
}

type divisor struct{}

// TODO(db47h): implement recursive algorithm
func (q dec) convertWords(s []byte, b Word, ndigits int, bb Word, table []decDivisor) {
// split larger blocks recursively
Expand Down Expand Up @@ -277,5 +275,9 @@ func (z dec) expWW(x, y Word) dec {
}

func divisors(m int, b Word, ndigits int, bb Word) []decDivisor {
// keep golint quiet
if decLeafSize > 100 {
_ = decDivisor{bbb: dec(nil), nbits: 0, ndigits: 0}
}
return nil
}
13 changes: 10 additions & 3 deletions dec_conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,13 @@ func TestDecScanPiParallel(t *testing.T) {
}

func BenchmarkDecScanPi(b *testing.B) {
var err error
for i := 0; i < b.N; i++ {
var x dec
x.scan(strings.NewReader(pi), 10, false)
_, _, _, err = x.scan(strings.NewReader(pi), 10, false)
}
if err != nil {
b.Fatalf("scan failed%s", err)
}
}

Expand Down Expand Up @@ -353,9 +357,12 @@ func BenchmarkDecScan(b *testing.B) {
b.Fatalf("scanning: got %s; want %s", s, t)
}
b.StartTimer()

var err error
for i := 0; i < b.N; i++ {
z.scan(bytes.NewReader(s), base, false)
_, _, _, err = z.scan(bytes.NewReader(s), base, false)
}
if err != nil {
b.Fatalf("scan failed%s", err)
}
})
}
Expand Down
6 changes: 1 addition & 5 deletions dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,7 @@ func benchmarkDecimalDiv(b *testing.B, aSize, bSize int) {
}

func BenchmarkDecimalDiv(b *testing.B) {
sizes := []int{
10, 20, 50, 100, 200, 500, 1000,
1e4, 1e5, 1e6, 1e7,
// TODO(db47h): enable these after optimizing
}
sizes := []int{10, 20, 50, 100, 200, 500, 1000, 1e4}
for _, i := range sizes {
j := 2 * i
b.Run(fmt.Sprintf("%d/%d", j, i), func(b *testing.B) {
Expand Down
23 changes: 15 additions & 8 deletions stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func scanSign(r io.ByteScanner) (neg bool, err error) {
case '+':
// nothing to do
default:
r.UnreadByte()
_ = r.UnreadByte()
}
return
}
Expand All @@ -157,7 +157,7 @@ func scanExponent(r io.ByteScanner, base2ok, sepOk bool) (exp int64, base int, e
}
fallthrough // binary exponent not permitted
default:
r.UnreadByte() // ch does not belong to exponent anymore
_ = r.UnreadByte() // ch does not belong to exponent anymore
return 0, 10, nil
}

Expand Down Expand Up @@ -190,7 +190,7 @@ func scanExponent(r io.ByteScanner, base2ok, sepOk bool) (exp int64, base int, e
}
prev = '_'
} else {
r.UnreadByte() // ch does not belong to number anymore
_ = r.UnreadByte() // ch does not belong to number anymore
break
}
ch, err = r.ReadByte()
Expand Down Expand Up @@ -241,11 +241,19 @@ func (err ErrNaN) Error() string {

type nat []Word

const divRecursiveThreshold = 100

// Operands that are shorter than karatsubaThreshold are multiplied using
// "grade school" multiplication; for longer operands the Karatsuba algorithm
// is used.
const karatsubaThreshold = 40 // computed by calibrate_test.go

// Operands that are shorter than basicSqrThreshold are squared using
// "grade school" multiplication; for operands longer than karatsubaSqrThreshold
// we use the Karatsuba algorithm optimized for x == y.
var basicSqrThreshold = 20 // computed by calibrate_test.go
var karatsubaSqrThreshold = 260 // computed by calibrate_test.go

// karatsubaLen computes an approximation to the maximum k <= n such that
// k = p/10**i for a number p <= threshold and an i >= 0. Thus, the
// result is the largest number that can be divided repeatedly by 10 before
Expand All @@ -266,8 +274,7 @@ func max(x, y int) int {
return y
}

// Operands that are shorter than basicSqrThreshold are squared using
// "grade school" multiplication; for operands longer than karatsubaSqrThreshold
// we use the Karatsuba algorithm optimized for x == y.
var basicSqrThreshold = 20 // computed by calibrate_test.go
var karatsubaSqrThreshold = 260 // computed by calibrate_test.go
// greaterThan reports whether (x1*_BD + x2) > (y1*_BD + y2)
func greaterThan(x1, x2, y1, y2 Word) bool {
return x1 > y1 || x1 == y1 && x2 > y2
}

0 comments on commit 709091c

Please sign in to comment.