The functions you specify are not directly available in any Chapel modules, but you can write extern procs and extern types for direct access to GMP functions.
First, we should be able to work with C files, so we declare some procedures and types for them:
extern type FILE; extern type FILEptr = c_ptr(FILE); extern proc fopen(filename: c_string, mode: c_string): FILEptr; extern proc fclose(fp: FILEptr);
Then we can declare the GMP functions that we need:
extern proc mpz_out_raw(stream: FILEptr, const op: mpz_t): size_t; extern proc mpz_inp_raw(ref rop: mpz_t, stream: FILEptr): size_t;
Now we can use them to write the bigint value:
use BigInteger; var res: bigint; res.fac(100);
And read it back from the file:
var readIt: bigint; f = fopen("gmp_outfile", "r"); mpz_inp_raw(readIt.mpz, f); fclose(f); writeln("Read the number:", readIt);
For arrays of bigint values bigint just bigint over them to write or read them:
// initialize the array var A: [1..10] bigint; for i in 1..10 do A[i].fac(i); // write the array to a file f = fopen("gmp_outfile", "w"); for i in 1..10 do mpz_out_raw(f, A[i].mpz); fclose(f); // read the array back in from the file var B: [1..10] bigint; f = fopen("gmp_outfile", "r"); for i in 1..10 do mpz_inp_raw(B[i].mpz, f); fclose(f);
source share