Commit 1abd8d8f authored by Shenghou Ma's avatar Shenghou Ma

misc/cgo/gmp: update for Go 1

1. make the program go buildable
2. update os.EINVAL and runtime.Cgocalls()
3. wrap mpz_div_2exp() and mpz_mul_2exp to support both
   pre-5.0 and post-5.0 gmp (we really have no reason to
   restrict ourselves to gmp 5.0+)

R=golang-dev, remyoudompheng, iant
CC=golang-dev
https://golang.org/cl/5847061
parent 4b1933df
...@@ -98,8 +98,20 @@ Go to hang on to a reference to the pointer until C is done with it. ...@@ -98,8 +98,20 @@ Go to hang on to a reference to the pointer until C is done with it.
*/ */
package gmp 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 "C"
import ( import (
...@@ -182,12 +194,12 @@ func (z *Int) SetInt64(x int64) *Int { ...@@ -182,12 +194,12 @@ func (z *Int) SetInt64(x int64) *Int {
func (z *Int) SetString(s string, base int) error { func (z *Int) SetString(s string, base int) error {
z.doinit() z.doinit()
if base < 2 || base > 36 { if base < 2 || base > 36 {
return os.EINVAL return os.ErrInvalid
} }
p := C.CString(s) p := C.CString(s)
defer C.free(unsafe.Pointer(p)) defer C.free(unsafe.Pointer(p))
if C.mpz_set_str(&z.i[0], p, C.int(base)) < 0 { if C.mpz_set_str(&z.i[0], p, C.int(base)) < 0 {
return os.EINVAL return os.ErrInvalid
} }
return nil return nil
} }
...@@ -265,7 +277,7 @@ func (z *Int) Mod(x, y *Int) *Int { ...@@ -265,7 +277,7 @@ func (z *Int) Mod(x, y *Int) *Int {
func (z *Int) Lsh(x *Int, s uint) *Int { func (z *Int) Lsh(x *Int, s uint) *Int {
x.doinit() x.doinit()
z.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 return z
} }
...@@ -273,7 +285,7 @@ func (z *Int) Lsh(x *Int, s uint) *Int { ...@@ -273,7 +285,7 @@ func (z *Int) Lsh(x *Int, s uint) *Int {
func (z *Int) Rsh(x *Int, s uint) *Int { func (z *Int) Rsh(x *Int, s uint) *Int {
x.doinit() x.doinit()
z.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 return z
} }
......
...@@ -102,5 +102,5 @@ func main() { ...@@ -102,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())
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment