-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
int_extra.go
208 lines (187 loc) · 4.47 KB
/
int_extra.go
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
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Implement extra functionality not in big.Int.
//
// Because the underlying C compiler may use 32-bit longs we
// use 32-bit integers here for maximum portability and speed.
package gmp
/*
#cgo LDFLAGS: -lgmp
#include <gmp.h>
#include <stdlib.h>
*/
import "C"
// Sqrt sets x to the truncated integer part of the square root of x
//
// NB This is not part of big.Int
func (z *Int) Sqrt(x *Int) *Int {
x.doinit()
z.doinit()
C.mpz_sqrt(&z.i[0], &x.i[0])
return z
}
// Swap exchanges the values of z and x and returns the new z.
//
// NB This is not part of big.Int
func (z *Int) Swap(x *Int) *Int {
x.doinit()
z.doinit()
C.mpz_swap(&z.i[0], &x.i[0])
return z
}
// AddUint32 sets z to the sum x+y and returns z.
//
// NB This is not part of big.Int
func (z *Int) AddUint32(x *Int, y uint32) *Int {
x.doinit()
z.doinit()
C.mpz_add_ui(&z.i[0], &x.i[0], C.ulong(y))
return z
}
// SubUint32 sets z to the difference x-y and returns z.
//
// NB This is not part of big.Int
func (z *Int) SubUint32(x *Int, y uint32) *Int {
x.doinit()
z.doinit()
C.mpz_sub_ui(&z.i[0], &x.i[0], C.ulong(y))
return z
}
// Uint32Sub sets z to the difference x-y and returns z.
//
// NB This is not part of big.Int
func (z *Int) Uint32Sub(x uint32, y *Int) *Int {
y.doinit()
z.doinit()
C.mpz_ui_sub(&z.i[0], C.ulong(x), &y.i[0])
return z
}
// MulUint32 sets z to the product x*y and returns z.
//
// NB This is not part of big.Int
func (z *Int) MulUint32(x *Int, y uint32) *Int {
x.doinit()
z.doinit()
C.mpz_mul_ui(&z.i[0], &x.i[0], C.ulong(y))
return z
}
// MulInt32 sets z to the product x*y and returns z.
//
// NB This is not part of big.Int
func (z *Int) MulInt32(x *Int, y int32) *Int {
x.doinit()
z.doinit()
C.mpz_mul_si(&z.i[0], &x.i[0], C.long(y))
return z
}
// AddMul sets z to z + x*y and returns z.
//
// NB This is not part of big.Int
func (z *Int) AddMul(x, y *Int) *Int {
x.doinit()
y.doinit()
z.doinit()
C.mpz_addmul(&z.i[0], &x.i[0], &y.i[0])
return z
}
// AddMulUint32 sets z to z + x*y and returns z.
//
// NB This is not part of big.Int
func (z *Int) AddMulUint32(x *Int, y uint32) *Int {
x.doinit()
z.doinit()
C.mpz_addmul_ui(&z.i[0], &x.i[0], C.ulong(y))
return z
}
// SubMul sets z to z - x*y and returns z.
//
// NB This is not part of big.Int
func (z *Int) SubMul(x, y *Int) *Int {
x.doinit()
y.doinit()
z.doinit()
C.mpz_submul(&z.i[0], &x.i[0], &y.i[0])
return z
}
// SubMulUint32 sets z to z - x*y and returns z.
//
// NB This is not part of big.Int
func (z *Int) SubMulUint32(x *Int, y uint32) *Int {
x.doinit()
z.doinit()
C.mpz_submul_ui(&z.i[0], &x.i[0], C.ulong(y))
return z
}
// compared reduces the result of a GMP comparison to one of {-1, 0, 1}.
func compared(i C.int) int {
if i > 0 {
return 1
} else if i < 0 {
return -1
} else {
return 0
}
}
// CmpUint32 compares z and x and returns:
//
// -1 if z < x
// 0 if z == x
// +1 if z > x
//
// NB This is not part of big.Int
func (z *Int) CmpUint32(x uint32) int {
z.doinit()
return compared(C._mpz_cmp_ui(&z.i[0], C.ulong(x)))
}
// CmpInt32 compares z and x and returns:
//
// -1 if z < x
// 0 if z == x
// +1 if z > x
//
// NB This is not part of big.Int
func (z *Int) CmpInt32(x int32) int {
z.doinit()
return compared(C._mpz_cmp_si(&z.i[0], C.long(x)))
}
// CmpAbs compares |z| and |x| and returns:
//
// -1 if |z| < |x|
// 0 if |z| == |x|
// +1 if |z| > |x|
//
// NB This is not part of big.Int
func (z *Int) CmpAbs(x *Int) int {
x.doinit()
z.doinit()
return compared(C.mpz_cmpabs(&z.i[0], &x.i[0]))
}
// CmpAbsUint32 compares |z| and |x| and returns:
//
// -1 if |z| < |x|
// 0 if |z| == |x|
// +1 if |z| > |x|
//
// NB This is not part of big.Int
func (z *Int) CmpAbsUint32(x uint32) int {
z.doinit()
return compared(C.mpz_cmpabs_ui(&z.i[0], C.ulong(x)))
}
// Uint32 returns the uint32 representation of z, if z fits into a uint32.
// If z is too big then the least significant bits that do fit are returned.
// The sign of z is ignored, only the absolute value is used.
//
// NB This is not part of big.Int
func (z *Int) Uint32() uint32 {
z.doinit()
return uint32(C.mpz_get_ui(&z.i[0]))
}
// Int32 returns the int32 representation of z, if z fits into a signed int32.
// If z is too big to fit in a int32 then the result is undefined.
//
// NB This is not part of big.Int
func (z *Int) Int32() int32 {
z.doinit()
return int32(C.mpz_get_si(&z.i[0]))
}