diff options
Diffstat (limited to 'misc/cgo/gmp')
-rw-r--r-- | misc/cgo/gmp/Makefile | 38 | ||||
-rw-r--r-- | misc/cgo/gmp/fib.go | 4 | ||||
-rw-r--r-- | misc/cgo/gmp/gmp.go | 24 | ||||
-rw-r--r-- | misc/cgo/gmp/pi.go | 6 |
4 files changed, 25 insertions, 47 deletions
diff --git a/misc/cgo/gmp/Makefile b/misc/cgo/gmp/Makefile deleted file mode 100644 index d9390c146..000000000 --- a/misc/cgo/gmp/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright 2009 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. - -include ../../../src/Make.inc - -TARG=gmp - -# Can have plain GOFILES too, but this example doesn't. - -CGOFILES=\ - gmp.go - -CGO_LDFLAGS=-lgmp - -# To add flags necessary for locating the library or its include files, -# set CGO_CFLAGS or CGO_LDFLAGS. For example, to use an -# alternate installation of the library: -# CGO_CFLAGS=-I/home/rsc/gmp32/include -# CGO_LDFLAGS+=-L/home/rsc/gmp32/lib -# Note the += on the second line. - -CLEANFILES+=pi fib - -include ../../../src/Make.pkg - -# Simple test programs - -# Computes 1000 digits of pi; single-threaded. -pi: install pi.go - $(GC) $(GCFLAGS) $(GCIMPORTS) pi.go - $(LD) -o $@ pi.$O - -# Computes 200 Fibonacci numbers; multi-threaded. -fib: install fib.go - $(GC) $(GCFLAGS) $(GCIMPORTS) fib.go - $(LD) -o $@ fib.$O - diff --git a/misc/cgo/gmp/fib.go b/misc/cgo/gmp/fib.go index 3eda39e17..18434beaf 100644 --- a/misc/cgo/gmp/fib.go +++ b/misc/cgo/gmp/fib.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build ignore + // Compute Fibonacci numbers with two goroutines // that pass integers back and forth. No actual // concurrency, just threads and synchronization @@ -10,7 +12,7 @@ package main import ( - big "gmp" + big "." "runtime" ) diff --git a/misc/cgo/gmp/gmp.go b/misc/cgo/gmp/gmp.go index 9325d8bfd..3bcf99151 100644 --- a/misc/cgo/gmp/gmp.go +++ b/misc/cgo/gmp/gmp.go @@ -98,8 +98,20 @@ Go to hang on to a reference to the pointer until C is done with it. */ package gmp -// #include <gmp.h> -// #include <stdlib.h> +/* +#cgo LDFLAGS: -lgmp +#include <gmp.h> +#include <stdlib.h> + +// gmp 5.0.0+ changed the type of the 3rd argument to mp_bitcnt_t, +// so, to support older versions, we wrap these two functions. +void _mpz_mul_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) { + mpz_mul_2exp(a, b, n); +} +void _mpz_div_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) { + mpz_div_2exp(a, b, n); +} +*/ import "C" import ( @@ -182,12 +194,12 @@ func (z *Int) SetInt64(x int64) *Int { func (z *Int) SetString(s string, base int) error { z.doinit() if base < 2 || base > 36 { - return os.EINVAL + return os.ErrInvalid } 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 os.ErrInvalid } return nil } @@ -265,7 +277,7 @@ func (z *Int) Mod(x, y *Int) *Int { func (z *Int) Lsh(x *Int, s uint) *Int { x.doinit() z.doinit() - C.mpz_mul_2exp(&z.i[0], &x.i[0], C.mp_bitcnt_t(s)) + C._mpz_mul_2exp(&z.i[0], &x.i[0], C.ulong(s)) return z } @@ -273,7 +285,7 @@ func (z *Int) Lsh(x *Int, s uint) *Int { func (z *Int) Rsh(x *Int, s uint) *Int { x.doinit() z.doinit() - C.mpz_div_2exp(&z.i[0], &x.i[0], C.mp_bitcnt_t(s)) + C._mpz_div_2exp(&z.i[0], &x.i[0], C.ulong(s)) return z } diff --git a/misc/cgo/gmp/pi.go b/misc/cgo/gmp/pi.go index 3e40624cf..1914cf214 100644 --- a/misc/cgo/gmp/pi.go +++ b/misc/cgo/gmp/pi.go @@ -1,3 +1,5 @@ +// +build ignore + /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -38,8 +40,8 @@ POSSIBILITY OF SUCH DAMAGE. package main import ( + big "." "fmt" - big "gmp" "runtime" ) @@ -100,5 +102,5 @@ func main() { } } - 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.NumCgoCall(), numer.Len(), accum.Len(), denom.Len()) } |