summaryrefslogtreecommitdiff
path: root/misc/cgo
diff options
context:
space:
mode:
Diffstat (limited to 'misc/cgo')
-rw-r--r--misc/cgo/gmp/fib.go24
-rw-r--r--misc/cgo/gmp/gmp.go188
-rw-r--r--misc/cgo/gmp/pi.go62
-rw-r--r--misc/cgo/stdio/chain.go30
-rw-r--r--misc/cgo/stdio/fib.go34
-rw-r--r--misc/cgo/stdio/file.go8
6 files changed, 173 insertions, 173 deletions
diff --git a/misc/cgo/gmp/fib.go b/misc/cgo/gmp/fib.go
index 1ff156ef2..3eda39e17 100644
--- a/misc/cgo/gmp/fib.go
+++ b/misc/cgo/gmp/fib.go
@@ -10,33 +10,33 @@
package main
import (
- big "gmp";
- "runtime";
+ big "gmp"
+ "runtime"
)
func fibber(c chan *big.Int, out chan string, n int64) {
// Keep the fibbers in dedicated operating system
// threads, so that this program tests coordination
// between pthreads and not just goroutines.
- runtime.LockOSThread();
+ runtime.LockOSThread()
- i := big.NewInt(n);
+ i := big.NewInt(n)
if n == 0 {
c <- i
}
for {
- j := <-c;
- out <- j.String();
- i.Add(i, j);
- c <- i;
+ j := <-c
+ out <- j.String()
+ i.Add(i, j)
+ c <- i
}
}
func main() {
- c := make(chan *big.Int);
- out := make(chan string);
- go fibber(c, out, 0);
- go fibber(c, out, 1);
+ c := make(chan *big.Int)
+ out := make(chan string)
+ go fibber(c, out, 0)
+ go fibber(c, out, 1)
for i := 0; i < 200; i++ {
println(<-out)
}
diff --git a/misc/cgo/gmp/gmp.go b/misc/cgo/gmp/gmp.go
index e199543c7..33c16de77 100644
--- a/misc/cgo/gmp/gmp.go
+++ b/misc/cgo/gmp/gmp.go
@@ -104,8 +104,8 @@ package gmp
import "C"
import (
- "os";
- "unsafe";
+ "os"
+ "unsafe"
)
/*
@@ -115,12 +115,12 @@ import (
// An Int represents a signed multi-precision integer.
// The zero value for an Int represents the value 0.
type Int struct {
- i C.mpz_t;
- init bool;
+ i C.mpz_t
+ init bool
}
// NewInt returns a new Int initialized to x.
-func NewInt(x int64) *Int { return new(Int).SetInt64(x) }
+func NewInt(x int64) *Int { return new(Int).SetInt64(x) }
// Int promises that the zero value is a 0, but in gmp
// the zero value is a crash. To bridge the gap, the
@@ -132,65 +132,65 @@ func (z *Int) doinit() {
if z.init {
return
}
- z.init = true;
- C.mpz_init(&z.i[0]);
+ z.init = true
+ C.mpz_init(&z.i[0])
}
// Bytes returns z's representation as a big-endian byte array.
func (z *Int) Bytes() []byte {
- b := make([]byte, (z.Len()+7)/8);
- n := C.size_t(len(b));
- C.mpz_export(unsafe.Pointer(&b[0]), &n, 1, 1, 1, 0, &z.i[0]);
- return b[0:n];
+ b := make([]byte, (z.Len()+7)/8)
+ n := C.size_t(len(b))
+ C.mpz_export(unsafe.Pointer(&b[0]), &n, 1, 1, 1, 0, &z.i[0])
+ return b[0:n]
}
// Len returns the length of z in bits. 0 is considered to have length 1.
func (z *Int) Len() int {
- z.doinit();
- return int(C.mpz_sizeinbase(&z.i[0], 2));
+ z.doinit()
+ return int(C.mpz_sizeinbase(&z.i[0], 2))
}
// Set sets z = x and returns z.
func (z *Int) Set(x *Int) *Int {
- z.doinit();
- C.mpz_set(&z.i[0], &x.i[0]);
- return z;
+ z.doinit()
+ C.mpz_set(&z.i[0], &x.i[0])
+ return z
}
// SetBytes interprets b as the bytes of a big-endian integer
// and sets z to that value.
func (z *Int) SetBytes(b []byte) *Int {
- z.doinit();
+ z.doinit()
if len(b) == 0 {
z.SetInt64(0)
} else {
C.mpz_import(&z.i[0], C.size_t(len(b)), 1, 1, 1, 0, unsafe.Pointer(&b[0]))
}
- return z;
+ return z
}
// SetInt64 sets z = x and returns z.
func (z *Int) SetInt64(x int64) *Int {
- z.doinit();
+ z.doinit()
// TODO(rsc): more work on 32-bit platforms
- C.mpz_set_si(&z.i[0], C.long(x));
- return z;
+ C.mpz_set_si(&z.i[0], C.long(x))
+ return z
}
// SetString interprets s as a number in the given base
// and sets z to that value. The base must be in the range [2,36].
// SetString returns an error if s cannot be parsed or the base is invalid.
func (z *Int) SetString(s string, base int) os.Error {
- z.doinit();
+ z.doinit()
if base < 2 || base > 36 {
return os.EINVAL
}
- p := C.CString(s);
- defer C.free(unsafe.Pointer(p));
+ p := C.CString(s)
+ defer C.free(unsafe.Pointer(p))
if C.mpz_set_str(&z.i[0], p, C.int(base)) < 0 {
return os.EINVAL
}
- return z;
+ return z
}
// String returns the decimal representation of z.
@@ -198,18 +198,18 @@ func (z *Int) String() string {
if z == nil {
return "nil"
}
- z.doinit();
- p := C.mpz_get_str(nil, 10, &z.i[0]);
- s := C.GoString(p);
- C.free(unsafe.Pointer(p));
- return s;
+ z.doinit()
+ p := C.mpz_get_str(nil, 10, &z.i[0])
+ s := C.GoString(p)
+ C.free(unsafe.Pointer(p))
+ return s
}
func (z *Int) destroy() {
if z.init {
C.mpz_clear(&z.i[0])
}
- z.init = false;
+ z.init = false
}
@@ -219,103 +219,103 @@ func (z *Int) destroy() {
// Add sets z = x + y and returns z.
func (z *Int) Add(x, y *Int) *Int {
- x.doinit();
- y.doinit();
- z.doinit();
- C.mpz_add(&z.i[0], &x.i[0], &y.i[0]);
- return z;
+ x.doinit()
+ y.doinit()
+ z.doinit()
+ C.mpz_add(&z.i[0], &x.i[0], &y.i[0])
+ return z
}
// Sub sets z = x - y and returns z.
func (z *Int) Sub(x, y *Int) *Int {
- x.doinit();
- y.doinit();
- z.doinit();
- C.mpz_sub(&z.i[0], &x.i[0], &y.i[0]);
- return z;
+ x.doinit()
+ y.doinit()
+ z.doinit()
+ C.mpz_sub(&z.i[0], &x.i[0], &y.i[0])
+ return z
}
// Mul sets z = x * y and returns z.
func (z *Int) Mul(x, y *Int) *Int {
- x.doinit();
- y.doinit();
- z.doinit();
- C.mpz_mul(&z.i[0], &x.i[0], &y.i[0]);
- return z;
+ x.doinit()
+ y.doinit()
+ z.doinit()
+ C.mpz_mul(&z.i[0], &x.i[0], &y.i[0])
+ return z
}
// Div sets z = x / y, rounding toward zero, and returns z.
func (z *Int) Div(x, y *Int) *Int {
- x.doinit();
- y.doinit();
- z.doinit();
- C.mpz_tdiv_q(&z.i[0], &x.i[0], &y.i[0]);
- return z;
+ x.doinit()
+ y.doinit()
+ z.doinit()
+ C.mpz_tdiv_q(&z.i[0], &x.i[0], &y.i[0])
+ return z
}
// Mod sets z = x % y and returns z.
// Like the result of the Go % operator, z has the same sign as x.
func (z *Int) Mod(x, y *Int) *Int {
- x.doinit();
- y.doinit();
- z.doinit();
- C.mpz_tdiv_r(&z.i[0], &x.i[0], &y.i[0]);
- return z;
+ x.doinit()
+ y.doinit()
+ z.doinit()
+ C.mpz_tdiv_r(&z.i[0], &x.i[0], &y.i[0])
+ return z
}
// Lsh sets z = x << s and returns z.
func (z *Int) Lsh(x *Int, s uint) *Int {
- x.doinit();
- z.doinit();
- C.mpz_mul_2exp(&z.i[0], &x.i[0], C.ulong(s));
- return z;
+ x.doinit()
+ z.doinit()
+ C.mpz_mul_2exp(&z.i[0], &x.i[0], C.ulong(s))
+ return z
}
// Rsh sets z = x >> s and returns z.
func (z *Int) Rsh(x *Int, s uint) *Int {
- x.doinit();
- z.doinit();
- C.mpz_div_2exp(&z.i[0], &x.i[0], C.ulong(s));
- return z;
+ x.doinit()
+ z.doinit()
+ C.mpz_div_2exp(&z.i[0], &x.i[0], C.ulong(s))
+ return z
}
// Exp sets z = x^y % m and returns z.
// If m == nil, Exp sets z = x^y.
func (z *Int) Exp(x, y, m *Int) *Int {
- m.doinit();
- x.doinit();
- y.doinit();
- z.doinit();
+ m.doinit()
+ x.doinit()
+ y.doinit()
+ z.doinit()
if m == nil {
C.mpz_pow_ui(&z.i[0], &x.i[0], C.mpz_get_ui(&y.i[0]))
} else {
C.mpz_powm(&z.i[0], &x.i[0], &y.i[0], &m.i[0])
}
- return z;
+ return z
}
func (z *Int) Int64() int64 {
if !z.init {
return 0
}
- return int64(C.mpz_get_si(&z.i[0]));
+ return int64(C.mpz_get_si(&z.i[0]))
}
// Neg sets z = -x and returns z.
func (z *Int) Neg(x *Int) *Int {
- x.doinit();
- z.doinit();
- C.mpz_neg(&z.i[0], &x.i[0]);
- return z;
+ x.doinit()
+ z.doinit()
+ C.mpz_neg(&z.i[0], &x.i[0])
+ return z
}
// Abs sets z to the absolute value of x and returns z.
func (z *Int) Abs(x *Int) *Int {
- x.doinit();
- z.doinit();
- C.mpz_abs(&z.i[0], &x.i[0]);
- return z;
+ x.doinit()
+ z.doinit()
+ C.mpz_abs(&z.i[0], &x.i[0])
+ return z
}
@@ -330,24 +330,24 @@ func (z *Int) Abs(x *Int) *Int {
// +1 if x > y
//
func CmpInt(x, y *Int) int {
- x.doinit();
- y.doinit();
+ x.doinit()
+ y.doinit()
switch cmp := C.mpz_cmp(&x.i[0], &y.i[0]); {
case cmp < 0:
return -1
case cmp == 0:
return 0
}
- return +1;
+ return +1
}
// DivModInt sets q = x / y and r = x % y.
func DivModInt(q, r, x, y *Int) {
- q.doinit();
- r.doinit();
- x.doinit();
- y.doinit();
- C.mpz_tdiv_qr(&q.i[0], &r.i[0], &x.i[0], &y.i[0]);
+ q.doinit()
+ r.doinit()
+ x.doinit()
+ y.doinit()
+ C.mpz_tdiv_qr(&q.i[0], &r.i[0], &x.i[0], &y.i[0])
}
// GcdInt sets d to the greatest common divisor of a and b,
@@ -355,18 +355,18 @@ func DivModInt(q, r, x, y *Int) {
// If x and y are not nil, GcdInt sets x and y such that d = a*x + b*y.
// If either a or b is not positive, GcdInt sets d = x = y = 0.
func GcdInt(d, x, y, a, b *Int) {
- d.doinit();
- x.doinit();
- y.doinit();
- a.doinit();
- b.doinit();
- C.mpz_gcdext(&d.i[0], &x.i[0], &y.i[0], &a.i[0], &b.i[0]);
+ d.doinit()
+ x.doinit()
+ y.doinit()
+ a.doinit()
+ b.doinit()
+ C.mpz_gcdext(&d.i[0], &x.i[0], &y.i[0], &a.i[0], &b.i[0])
}
// ProbablyPrime performs n Miller-Rabin tests to check whether z is prime.
// If it returns true, z is prime with probability 1 - 1/4^n.
// If it returns false, z is not prime.
func (z *Int) ProbablyPrime(n int) bool {
- z.doinit();
- return int(C.mpz_probab_prime_p(&z.i[0], C.int(n))) > 0;
+ z.doinit()
+ return int(C.mpz_probab_prime_p(&z.i[0], C.int(n))) > 0
}
diff --git a/misc/cgo/gmp/pi.go b/misc/cgo/gmp/pi.go
index 61b88a417..45f61abbd 100644
--- a/misc/cgo/gmp/pi.go
+++ b/misc/cgo/gmp/pi.go
@@ -38,67 +38,67 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- big "gmp";
- "fmt";
- "runtime";
+ big "gmp"
+ "fmt"
+ "runtime"
)
var (
- tmp1 = big.NewInt(0);
- tmp2 = big.NewInt(0);
- numer = big.NewInt(1);
- accum = big.NewInt(0);
- denom = big.NewInt(1);
- ten = big.NewInt(10);
+ tmp1 = big.NewInt(0)
+ tmp2 = big.NewInt(0)
+ numer = big.NewInt(1)
+ accum = big.NewInt(0)
+ denom = big.NewInt(1)
+ ten = big.NewInt(10)
)
func extractDigit() int64 {
if big.CmpInt(numer, accum) > 0 {
return -1
}
- tmp1.Lsh(numer, 1).Add(tmp1, numer).Add(tmp1, accum);
- big.DivModInt(tmp1, tmp2, tmp1, denom);
- tmp2.Add(tmp2, numer);
+ tmp1.Lsh(numer, 1).Add(tmp1, numer).Add(tmp1, accum)
+ big.DivModInt(tmp1, tmp2, tmp1, denom)
+ tmp2.Add(tmp2, numer)
if big.CmpInt(tmp2, denom) >= 0 {
return -1
}
- return tmp1.Int64();
+ return tmp1.Int64()
}
func nextTerm(k int64) {
- y2 := k*2 + 1;
- accum.Add(accum, tmp1.Lsh(numer, 1));
- accum.Mul(accum, tmp1.SetInt64(y2));
- numer.Mul(numer, tmp1.SetInt64(k));
- denom.Mul(denom, tmp1.SetInt64(y2));
+ y2 := k*2 + 1
+ accum.Add(accum, tmp1.Lsh(numer, 1))
+ accum.Mul(accum, tmp1.SetInt64(y2))
+ numer.Mul(numer, tmp1.SetInt64(k))
+ denom.Mul(denom, tmp1.SetInt64(y2))
}
func eliminateDigit(d int64) {
- accum.Sub(accum, tmp1.Mul(denom, tmp1.SetInt64(d)));
- accum.Mul(accum, ten);
- numer.Mul(numer, ten);
+ accum.Sub(accum, tmp1.Mul(denom, tmp1.SetInt64(d)))
+ accum.Mul(accum, ten)
+ numer.Mul(numer, ten)
}
func main() {
- i := 0;
- k := int64(0);
+ i := 0
+ k := int64(0)
for {
- d := int64(-1);
+ d := int64(-1)
for d < 0 {
- k++;
- nextTerm(k);
- d = extractDigit();
+ k++
+ nextTerm(k)
+ d = extractDigit()
}
- eliminateDigit(d);
- fmt.Printf("%c", d+'0');
+ eliminateDigit(d)
+ fmt.Printf("%c", d+'0')
if i++; i%50 == 0 {
- fmt.Printf("\n");
+ fmt.Printf("\n")
if i >= 1000 {
break
}
}
}
- fmt.Printf("\n%d calls; bit sizes: %d %d %d\n", runtime.Cgocalls(), numer.Len(), accum.Len(), denom.Len());
+ fmt.Printf("\n%d calls; bit sizes: %d %d %d\n", runtime.Cgocalls(), numer.Len(), accum.Len(), denom.Len())
}
diff --git a/misc/cgo/stdio/chain.go b/misc/cgo/stdio/chain.go
index 18c598d4d..dd5e01542 100644
--- a/misc/cgo/stdio/chain.go
+++ b/misc/cgo/stdio/chain.go
@@ -7,9 +7,9 @@
package main
import (
- "runtime";
- "stdio";
- "strconv";
+ "runtime"
+ "stdio"
+ "strconv"
)
const N = 10
@@ -19,25 +19,25 @@ func link(left chan<- int, right <-chan int) {
// Keep the links in dedicated operating system
// threads, so that this program tests coordination
// between pthreads and not just goroutines.
- runtime.LockOSThread();
+ runtime.LockOSThread()
for {
- v := <-right;
- stdio.Puts(strconv.Itoa(v));
- left <- 1+v;
+ v := <-right
+ stdio.Puts(strconv.Itoa(v))
+ left <- 1+v
}
}
func main() {
- leftmost := make(chan int);
- var left chan int;
- right := leftmost;
+ leftmost := make(chan int)
+ var left chan int
+ right := leftmost
for i := 0; i < N; i++ {
- left, right = right, make(chan int);
- go link(left, right);
+ left, right = right, make(chan int)
+ go link(left, right)
}
for i := 0; i < R; i++ {
- right <- 0;
- x := <-leftmost;
- stdio.Puts(strconv.Itoa(x));
+ right <- 0
+ x := <-leftmost
+ stdio.Puts(strconv.Itoa(x))
}
}
diff --git a/misc/cgo/stdio/fib.go b/misc/cgo/stdio/fib.go
index 1e2336d5b..63ae04988 100644
--- a/misc/cgo/stdio/fib.go
+++ b/misc/cgo/stdio/fib.go
@@ -10,38 +10,38 @@
package main
import (
- "runtime";
- "stdio";
- "strconv";
+ "runtime"
+ "stdio"
+ "strconv"
)
func fibber(c, out chan int64, i int64) {
// Keep the fibbers in dedicated operating system
// threads, so that this program tests coordination
// between pthreads and not just goroutines.
- runtime.LockOSThread();
+ runtime.LockOSThread()
if i == 0 {
c <- i
}
for {
- j := <-c;
- stdio.Puts(strconv.Itoa64(j));
- out <- j;
- <-out;
- i += j;
- c <- i;
+ j := <-c
+ stdio.Puts(strconv.Itoa64(j))
+ out <- j
+ <-out
+ i += j
+ c <- i
}
}
func main() {
- c := make(chan int64);
- out := make(chan int64);
- go fibber(c, out, 0);
- go fibber(c, out, 1);
- <-out;
+ c := make(chan int64)
+ out := make(chan int64)
+ go fibber(c, out, 0)
+ go fibber(c, out, 1)
+ <-out
for i := 0; i < 90; i++ {
- out <- 1;
- <-out;
+ out <- 1
+ <-out
}
}
diff --git a/misc/cgo/stdio/file.go b/misc/cgo/stdio/file.go
index c8493a0e3..7d1f22280 100644
--- a/misc/cgo/stdio/file.go
+++ b/misc/cgo/stdio/file.go
@@ -35,8 +35,8 @@ func (f *File) WriteString(s string) {
*/
func Puts(s string) {
- p := C.CString(s);
- C.puts(p);
- C.free(unsafe.Pointer(p));
- C.fflushstdout();
+ p := C.CString(s)
+ C.puts(p)
+ C.free(unsafe.Pointer(p))
+ C.fflushstdout()
}