File: conversions.c

package info (click to toggle)
mlgmp 20021123-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 304 kB
  • ctags: 616
  • sloc: ml: 1,769; ansic: 1,750; sh: 144; makefile: 120
file content (145 lines) | stat: -rw-r--r-- 3,623 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * ML GMP - Interface between Objective Caml and GNU MP
 * Copyright (C) 2001 David MONNIAUX
 * 
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License version 2 published by the Free Software Foundation,
 * or any more recent version published by the Free Software
 * Foundation, at your choice.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * 
 * See the GNU Library General Public License version 2 for more details
 * (enclosed in the file LGPL).
 *
 * As a special exception to the GNU Library General Public License, you
 * may link, statically or dynamically, a "work that uses the Library"
 * with a publicly distributed version of the Library to produce an
 * executable file containing portions of the Library, and distribute
 * that executable file under terms of your choice, without any of the
 * additional requirements listed in clause 6 of the GNU Library General
 * Public License.  By "a publicly distributed version of the Library",
 * we mean either the unmodified Library as distributed by INRIA, or a
 * modified version of the Library that is distributed under the
 * conditions defined in clause 3 of the GNU Library General Public
 * License.  This exception does not however invalidate any other reasons
 * why the executable file might be covered by the GNU Library General
 * Public License.
 */

#include <assert.h>

struct custom_operations _mlgmp_custom_z;

static inline gmp_randstate_t *randstate_val(value val)
{
  return ((gmp_randstate_t *) (Data_custom_val(val)));
}

static inline int Int_option_val(value val, int default_val)
{
  if (val == Val_int(0)) return default_val;
  return Int_val(Field(val, 0));
}

static inline mpz_t * mpz_val (value val)
{
  return ((mpz_t *) (Data_custom_val(val)));
}

static inline value alloc_mpz (void)
{
  return alloc_custom(&_mlgmp_custom_z,
		       sizeof(mpz_t),
		       0,
		       1);
}

static inline value alloc_init_mpz (void)
{
  value r= alloc_mpz();
  mpz_init(*mpz_val(r));
  return r;
}

#pragma inline(Int_option_val, mpz_val, alloc_mpz, alloc_init_mpz)

struct custom_operations _mlgmp_custom_q;

static inline mpq_t * mpq_val (value val)
{
  return ((mpq_t *) (Data_custom_val(val)));
}

static inline value alloc_mpq (void)
{
  return alloc_custom(&_mlgmp_custom_q,
		       sizeof(mpq_t),
		       0,
		       1);
}

static inline value alloc_init_mpq (void)
{
  value r= alloc_mpq();
  mpq_init(*mpq_val(r));
  return r;
}

#pragma inline(mpq_val, alloc_mpq, alloc_init_mpq)


struct custom_operations _mlgmp_custom_f;

static inline mpf_t * mpf_val (value val)
{
  return ((mpf_t *) (Data_custom_val(val)));
}

static inline value alloc_mpf (void)
{
  return alloc_custom(&_mlgmp_custom_f,
		       sizeof(mpf_t),
		       0,
		       1);
}

static inline value alloc_init_mpf (value prec)
{
  value r= alloc_mpf();
  mpf_init2(*mpf_val(r), Int_val(prec));
  return r;
}


struct custom_operations _mlgmp_custom_fr;

#ifdef USE_MPFR
static inline mpfr_t * mpfr_val (value val)
{
  return ((mpfr_t *) (Data_custom_val(val)));
}

static inline mp_rnd_t Mode_val (value val)
{
  return (mp_rnd_t) (Int_val(val));
}

static inline value alloc_mpfr (void)
{
  return alloc_custom(&_mlgmp_custom_fr,
		       sizeof(mpfr_t),
		       0,
		       1);
}

static inline value alloc_init_mpfr (value prec)
{
  value r= alloc_mpfr();
  mpfr_init2(*mpfr_val(r), Int_val(prec));
  return r;
}
#endif