| 1 |
/* |
| 2 |
* ML GMP - Interface between Objective Caml and GNU MP |
| 3 |
* Copyright (C) 2001 David MONNIAUX |
| 4 |
* |
| 5 |
* This software is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Library General Public |
| 7 |
* License version 2 published by the Free Software Foundation, |
| 8 |
* or any more recent version published by the Free Software |
| 9 |
* Foundation, at your choice. |
| 10 |
* |
| 11 |
* This software is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
* |
| 15 |
* See the GNU Library General Public License version 2 for more details |
| 16 |
* (enclosed in the file LGPL). |
| 17 |
* |
| 18 |
* As a special exception to the GNU Library General Public License, you |
| 19 |
* may link, statically or dynamically, a "work that uses the Library" |
| 20 |
* with a publicly distributed version of the Library to produce an |
| 21 |
* executable file containing portions of the Library, and distribute |
| 22 |
* that executable file under terms of your choice, without any of the |
| 23 |
* additional requirements listed in clause 6 of the GNU Library General |
| 24 |
* Public License. By "a publicly distributed version of the Library", |
| 25 |
* we mean either the unmodified Library as distributed by INRIA, or a |
| 26 |
* modified version of the Library that is distributed under the |
| 27 |
* conditions defined in clause 3 of the GNU Library General Public |
| 28 |
* License. This exception does not however invalidate any other reasons |
| 29 |
* why the executable file might be covered by the GNU Library General |
| 30 |
* Public License. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <assert.h> |
| 34 |
|
| 35 |
struct custom_operations _mlgmp_custom_z; |
| 36 |
|
| 37 |
static inline gmp_randstate_t *randstate_val(value val) |
| 38 |
{ |
| 39 |
return ((gmp_randstate_t *) (Data_custom_val(val))); |
| 40 |
} |
| 41 |
|
| 42 |
static inline int Int_option_val(value val, int default_val) |
| 43 |
{ |
| 44 |
if (val == Val_int(0)) return default_val; |
| 45 |
return Int_val(Field(val, 0)); |
| 46 |
} |
| 47 |
|
| 48 |
static inline mpz_t * mpz_val (value val) |
| 49 |
{ |
| 50 |
return ((mpz_t *) (Data_custom_val(val))); |
| 51 |
} |
| 52 |
|
| 53 |
static inline value alloc_mpz (void) |
| 54 |
{ |
| 55 |
return alloc_custom(&_mlgmp_custom_z, |
| 56 |
sizeof(mpz_t), |
| 57 |
0, |
| 58 |
1); |
| 59 |
} |
| 60 |
|
| 61 |
static inline value alloc_init_mpz (void) |
| 62 |
{ |
| 63 |
value r= alloc_mpz(); |
| 64 |
mpz_init(*mpz_val(r)); |
| 65 |
return r; |
| 66 |
} |
| 67 |
|
| 68 |
#pragma inline(Int_option_val, mpz_val, alloc_mpz, alloc_init_mpz) |
| 69 |
|
| 70 |
struct custom_operations _mlgmp_custom_q; |
| 71 |
|
| 72 |
static inline mpq_t * mpq_val (value val) |
| 73 |
{ |
| 74 |
return ((mpq_t *) (Data_custom_val(val))); |
| 75 |
} |
| 76 |
|
| 77 |
static inline value alloc_mpq (void) |
| 78 |
{ |
| 79 |
return alloc_custom(&_mlgmp_custom_q, |
| 80 |
sizeof(mpq_t), |
| 81 |
0, |
| 82 |
1); |
| 83 |
} |
| 84 |
|
| 85 |
static inline value alloc_init_mpq (void) |
| 86 |
{ |
| 87 |
value r= alloc_mpq(); |
| 88 |
mpq_init(*mpq_val(r)); |
| 89 |
return r; |
| 90 |
} |
| 91 |
|
| 92 |
#pragma inline(mpq_val, alloc_mpq, alloc_init_mpq) |
| 93 |
|
| 94 |
|
| 95 |
struct custom_operations _mlgmp_custom_f; |
| 96 |
|
| 97 |
static inline mpf_t * mpf_val (value val) |
| 98 |
{ |
| 99 |
return ((mpf_t *) (Data_custom_val(val))); |
| 100 |
} |
| 101 |
|
| 102 |
static inline value alloc_mpf (void) |
| 103 |
{ |
| 104 |
return alloc_custom(&_mlgmp_custom_f, |
| 105 |
sizeof(mpf_t), |
| 106 |
0, |
| 107 |
1); |
| 108 |
} |
| 109 |
|
| 110 |
static inline value alloc_init_mpf (value prec) |
| 111 |
{ |
| 112 |
value r= alloc_mpf(); |
| 113 |
mpf_init2(*mpf_val(r), Int_val(prec)); |
| 114 |
return r; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
struct custom_operations _mlgmp_custom_fr; |
| 119 |
|
| 120 |
#ifdef USE_MPFR |
| 121 |
static inline mpfr_t * mpfr_val (value val) |
| 122 |
{ |
| 123 |
return ((mpfr_t *) (Data_custom_val(val))); |
| 124 |
} |
| 125 |
|
| 126 |
static inline mp_rnd_t Mode_val (value val) |
| 127 |
{ |
| 128 |
return (mp_rnd_t) (Int_val(val)); |
| 129 |
} |
| 130 |
|
| 131 |
static inline value alloc_mpfr (void) |
| 132 |
{ |
| 133 |
return alloc_custom(&_mlgmp_custom_fr, |
| 134 |
sizeof(mpfr_t), |
| 135 |
0, |
| 136 |
1); |
| 137 |
} |
| 138 |
|
| 139 |
static inline value alloc_init_mpfr (value prec) |
| 140 |
{ |
| 141 |
value r= alloc_mpfr(); |
| 142 |
mpfr_init2(*mpfr_val(r), Int_val(prec)); |
| 143 |
return r; |
| 144 |
} |
| 145 |
#endif |