about summary refs log tree commit diff
path: root/vendor/modernc.org/mathutil/int.go
blob: 7c3578764d4be36664880ecfbf666c346f587275 (plain)
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
// 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 "modernc.org/mathutil"

import (
	"fmt"
	"math"
	"math/big"
)

var (
	// MaxInt128 represents the maximun Int128 value as big.Int
	MaxInt128 *big.Int
	// MinInt128 represents the minimun Int128 value as big.Int
	MinInt128 *big.Int
	// MaxUint128 represents the maximun Uint128 value as big.Int
	MaxUint128 *big.Int
)

func init() {
	var ok bool
	MaxInt128, ok = big.NewInt(0).SetString("0x7fffffff_ffffffff_ffffffff_ffffffff", 0)
	if !ok {
		panic("internal error")
	}

	MinInt128 = big.NewInt(0).Set(MaxInt128)
	MinInt128.Add(MinInt128, _1)
	MinInt128.Neg(MinInt128)

	MaxUint128, ok = big.NewInt(0).SetString("0xffffffff_ffffffff_ffffffff_ffffffff", 0)
	if !ok {
		panic("internal error")
	}
}

const (
	maxInt128  = 1<<127 - 1
	maxUint128 = 1<<128 - 1
	minInt128  = -maxInt128 - 1
)

// Int128 is an 128 bit signed 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() }

// NewInt128FromInt64 return a new Int128 value initialized to n.
func NewInt128FromInt64(n int64) (r Int128) {
	r.Lo = n
	if n < 0 {
		r.Hi = -1
	}
	return r
}

// NewInt128FromUint64 return a new Int128 value initialized to n.
func NewInt128FromUint64(n uint64) (r Int128) { return Int128{Lo: int64(n)} }

// NewInt128FromFloat32 returns a new Int128 value initialized to n. Result is
// not specified in n does not represent a number within the range of Int128
// values.
func NewInt128FromFloat32(n float32) (r Int128) {
	if n >= minInt128 && n <= maxInt128 {
		if n >= math.MinInt64 && n <= math.MaxInt64 {
			return NewInt128FromInt64(int64(n))
		}

		f := big.NewFloat(float64(n))
		bi, _ := f.Int(nil)
		r.SetBigInt(bi)
	}
	return r
}

// NewInt128FromFloat64 returns a new Int128 value initialized to n. Result is
// not specified in n does not represent a number within the range of Int128
// values.
func NewInt128FromFloat64(n float64) (r Int128) {
	if n >= minInt128 && n <= maxInt128 {
		if n >= math.MinInt64 && n <= math.MaxInt64 {
			return NewInt128FromInt64(int64(n))
		}

		f := big.NewFloat(n)
		bi, _ := f.Int(nil)
		r.SetBigInt(bi)
	}
	return r
}

// Uint128 is an 128 bit unsigned integer.
type Uint128 struct {
	Lo uint64 // Bits 63..0.
	Hi uint64 // Bits 127..64.
}

// NewUint128FromInt64 return a new Uint128 value initialized to n.
func NewUint128FromInt64(n int64) (r Uint128) {
	r.Lo = uint64(n)
	if n < 0 {
		r.Hi = ^uint64(0)
	}
	return r
}

// NewUint128FromUint64 return a new Uint128 value initialized to n.
func NewUint128FromUint64(n uint64) (r Uint128) { return Uint128{Lo: n} }

// NewUint128FromFloat32 returns a new Uint128 value initialized to n. Result is
// not specified in n does not represent a number within the range of Uint128
// values.
func NewUint128FromFloat32(n float32) (r Uint128) {
	if n >= 0 {
		if n <= math.MaxUint64 {
			return NewUint128FromUint64(uint64(n))
		}

		f := big.NewFloat(float64(n))
		bi, _ := f.Int(nil)
		r.SetBigInt(bi)
	}
	return r
}

// NewUint128FromFloat64 returns a new Uint128 value initialized to n. Result is
// not specified in n does not represent a number within the range of Uint128
// values.
func NewUint128FromFloat64(n float64) (r Uint128) {
	if n >= 0 && n <= maxUint128 {
		if n <= math.MaxUint64 {
			return NewUint128FromUint64(uint64(n))
		}

		f := big.NewFloat(n)
		bi, _ := f.Int(nil)
		r.SetBigInt(bi)
	}
	return r
}

// SetBigInt sets x to y, returns x and an error, if any.
func (x *Uint128) SetBigInt(y *big.Int) (r Uint128, err error) {
	if y.Sign() < 0 || y.Cmp(MaxUint128) > 0 {
		return *x, fmt.Errorf("%T.SetInt: overflow", x)
	}

	var z big.Int
	z.Set(y)
	r.Lo = z.Uint64()
	z.Rsh(&z, 64)
	r.Hi = z.Uint64()
	*x = r
	return r, nil
}