mirror of
https://github.com/golang/go
synced 2024-10-04 12:31:21 -06:00
75a38963ca
better FFI demo: compute fibonacci numbers using FFI'ed libgmp. R=r DELTA=281 (255 added, 19 deleted, 7 changed) OCL=33815 CL=33820
55 lines
739 B
C
55 lines
739 B
C
// 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 <stdint.h>
|
|
#include <gmp.h>
|
|
#include <string.h>
|
|
|
|
typedef int32_t int32;
|
|
typedef uint64_t uint64;
|
|
|
|
typedef struct Int Int;
|
|
struct Int
|
|
{
|
|
mpz_t *mp;
|
|
};
|
|
|
|
void
|
|
gmp_newInt(void *v)
|
|
{
|
|
struct {
|
|
uint64 x;
|
|
Int *z;
|
|
} *a = v;
|
|
|
|
a->z->mp = malloc(sizeof *a->z->mp);
|
|
mpz_init_set_ui(*a->z->mp, a->x);
|
|
}
|
|
|
|
void
|
|
gmp_addInt(void *v)
|
|
{
|
|
struct {
|
|
Int *z;
|
|
Int *x;
|
|
Int *y;
|
|
Int *ret;
|
|
} *a = v;
|
|
|
|
a->ret = a->z;
|
|
mpz_add(*a->z->mp, *a->x->mp, *a->y->mp);
|
|
}
|
|
|
|
void
|
|
gmp_stringInt(void *v)
|
|
{
|
|
struct {
|
|
Int *z;
|
|
char *p;
|
|
} *a = v;
|
|
|
|
a->p = mpz_get_str(NULL, 10, *a->z->mp);
|
|
}
|
|
|