Skip to content

Commit 31ae944

Browse files
authored
fix constant div in d64 package (#90)
Same as #89
1 parent 514601a commit 31ae944

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

d64/uint128.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (a *uint128T) sub(x, y *uint128T) *uint128T {
6060

6161
func (a *uint128T) divrem64(x *uint128T, d uint64) uint64 {
6262
var r uint64
63-
if a.hi == 0 {
63+
if x.hi == 0 {
6464
a.lo, r = x.lo/d, x.lo%d
6565
} else {
6666
a.hi, r = bits.Div64(0, x.hi, d)
@@ -70,16 +70,15 @@ func (a *uint128T) divrem64(x *uint128T, d uint64) uint64 {
7070
}
7171

7272
// divbase divides a by [decimalBase].
73-
func (a *uint128T) divbase(x *uint128T) *uint128T { return a.divc(x, 113, 0x901d7cf73ab0acd9) }
73+
func (a *uint128T) divbase(x *uint128T) *uint128T {
74+
a.divrem64(x, decimalBase)
75+
return a
76+
}
7477

7578
// div10base divides a by 10*[decimalBase].
76-
func (a *uint128T) div10base(x *uint128T) *uint128T { return a.divc(x, 117, 0xe69594bec44de15b) }
77-
78-
// divc divides a by n where 1<<po2/rdenom ~ n and po2 is chosen such that rdenom is the largest possible value < 1<<64.
79-
func (a *uint128T) divc(x *uint128T, po2 int, rdenom uint64) *uint128T {
80-
m := x.hi<<(64-43) + x.lo>>43 // (hi, lo) >> 43
81-
q, _ := bits.Mul64(m, rdenom)
82-
return a.set(0, q>>(po2-(43+64))+1)
79+
func (a *uint128T) div10base(x *uint128T) *uint128T {
80+
a.divrem64(x, 10*decimalBase)
81+
return a
8382
}
8483

8584
func (a *uint128T) lt(b *uint128T) bool {

uint128.go

-7
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ func (a *uint128T) div10base(x *uint128T) *uint128T {
8181
return a
8282
}
8383

84-
// divc divides a by n where 1<<po2/rdenom ~ n and po2 is chosen such that rdenom is the largest possible value < 1<<64.
85-
func (a *uint128T) divc(x *uint128T, po2 int, rdenom uint64) *uint128T {
86-
m := x.hi<<(64-43) + x.lo>>43 // (hi, lo) >> 43
87-
q, _ := bits.Mul64(m, rdenom)
88-
return a.set(0, q>>(po2-(43+64))+1)
89-
}
90-
9184
func (a *uint128T) lt(b *uint128T) bool {
9285
if a.hi != b.hi {
9386
return a.hi < b.hi

0 commit comments

Comments
 (0)