Beispiel #1
0
Datei: int.go Projekt: locusf/gmp
// GobEncode implements the gob.GobEncoder interface.
func (x *Int) GobEncode() ([]byte, error) {
	buf := make([]byte, 2+(x.BitLen()+7)/8)
	n := C.size_t(len(buf) - 1)
	C.mpz_export(unsafe.Pointer(&buf[1]), &n, 1, 1, 1, 0, &x.i[0])
	b := intGobVersion << 1 // make space for sign bit
	if x.Sign() < 0 {
		b |= 1
	}
	buf[0] = b
	return buf[:n+1], nil
}
Beispiel #2
0
Datei: int.go Projekt: locusf/gmp
// Uint64 returns the uint64 representation of x.
// If x cannot be represented in a uint64, the result is undefined.
func (x *Int) Uint64() (y uint64) {
	if !x.init {
		return
	}
	if C.mpz_fits_ulong_p(&x.i[0]) != 0 {
		return uint64(C.mpz_get_ui(&x.i[0]))
	}
	// Undefined result if > 64 bits
	if x.BitLen() > 64 {
		return
	}
	C.mpz_export(unsafe.Pointer(&y), nil, -1, 8, 0, 0, &x.i[0])
	return
}
Beispiel #3
0
Datei: int.go Projekt: locusf/gmp
// Int64 returns the int64 representation of x.
// If x cannot be represented in an int64, the result is undefined.
func (x *Int) Int64() (y int64) {
	if !x.init {
		return
	}
	if C.mpz_fits_slong_p(&x.i[0]) != 0 {
		return int64(C.mpz_get_si(&x.i[0]))
	}
	// Undefined result if > 64 bits
	if x.BitLen() > 64 {
		return
	}
	C.mpz_export(unsafe.Pointer(&y), nil, -1, 8, 0, 0, &x.i[0])
	if x.Sign() < 0 {
		y = -y
	}
	return
}
Beispiel #4
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]
}