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
|
// Copyright (c) 2018 The mathutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mathutil
import (
"fmt"
"math"
"math/big"
)
var (
// MaxInt128 represents the maximun Int128 value.
MaxInt128 *big.Int
// MinInt128 represents the minimun Int128 value.
MinInt128 *big.Int
)
func init() {
MaxInt128 = big.NewInt(0)
MaxInt128.SetBit(MaxInt128, 127, 1)
MaxInt128.Sub(MaxInt128, _1)
MinInt128 = big.NewInt(0)
MinInt128.Set(MaxInt128)
MinInt128.Add(MinInt128, _1)
MinInt128.Neg(MinInt128)
}
// Int128 is an 128 bit integer.
type Int128 struct {
Lo int64 // Bits 63..0.
Hi int64 // Bits 127..64.
}
// Add returns the sum of x and y and a carry indication.
func (x Int128) Add(y Int128) (r Int128, cy bool) {
r.Lo = x.Lo + y.Lo
r.Hi = x.Hi + y.Hi
if uint64(r.Lo) < uint64(x.Lo) {
r.Hi++
}
return r, (r.Cmp(x) < 0) == (y.Sign() >= 0)
}
// BigInt returns x in the form of a big.Int.
func (x Int128) BigInt() *big.Int {
r := big.NewInt(x.Hi)
r.Lsh(r, 64)
lo := big.NewInt(0)
lo.SetUint64(uint64(x.Lo))
return r.Add(r, lo)
}
// Cmp compares x and y and returns:
//
// -1 if x < y
// 0 if x == y
// +1 if x > y
func (x Int128) Cmp(y Int128) int {
if x.Hi > y.Hi {
return 1
}
if x.Hi < y.Hi {
return -1
}
if uint64(x.Lo) > uint64(y.Lo) {
return 1
}
if uint64(x.Lo) < uint64(y.Lo) {
return -1
}
return 0
}
// Neg returns -x and an indication that x was not equal to MinInt128.
func (x Int128) Neg() (r Int128, ok bool) {
if x == (Int128{Hi: math.MinInt64}) {
return x, false
}
x.Lo = ^x.Lo
x.Hi = ^x.Hi
r, _ = x.Add(Int128{Lo: 1})
return r, true
}
// SetBigInt sets x to y, returns x and an error, if any.
func (x *Int128) SetBigInt(y *big.Int) (r Int128, err error) {
if y.Cmp(MaxInt128) > 0 {
return *x, fmt.Errorf("%T.SetInt: overflow", x)
}
if y.Cmp(MinInt128) < 0 {
return *x, fmt.Errorf("%T.SetInt: underflow", x)
}
neg := y.Sign() < 0
var z big.Int
z.Set(y)
if neg {
z.Neg(&z)
}
r.Lo = z.Int64()
z.Rsh(&z, 64)
r.Hi = z.Int64()
if neg {
r, _ = r.Neg()
}
*x = r
return r, nil
}
// SetInt64 sets x to y and returns x.
func (x *Int128) SetInt64(y int64) (r Int128) {
r.Lo = y
if y >= 0 {
r.Hi = 0
*x = r
return r
}
r.Hi = -1
*x = r
return r
}
// SetUint64 sets x to y and returns x.
func (x *Int128) SetUint64(y uint64) (r Int128) {
r = Int128{Lo: int64(y)}
*x = r
return r
}
// Sign returns:
//
// -1 if x < 0
// 0 if x == 0
// +1 if x > 0
func (x Int128) Sign() int {
if x.Hi < 0 {
return -1
}
if x.Hi != 0 || x.Lo != 0 {
return 1
}
return 0
}
// String implements fmt.Stringer()
func (x Int128) String() string { return x.BigInt().String() }
|