1/* mpfr_gmp -- Limited gmp-impl emulator
2   Modified version of the GMP files.
3
4Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
5Contributed by the AriC and Caramel projects, INRIA.
6
7This file is part of the GNU MPFR Library.
8
9The GNU MPFR Library is free software; you can redistribute it and/or modify
10it under the terms of the GNU Lesser General Public License as published by
11the Free Software Foundation; either version 3 of the License, or (at your
12option) any later version.
13
14The GNU MPFR Library is distributed in the hope that it will be useful, but
15WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17License for more details.
18
19You should have received a copy of the GNU Lesser General Public License
20along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
21http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2251 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23
24#include <stdlib.h> /* For malloc, free, realloc and abort */
25
26#include "mpfr-impl.h"
27
28#ifndef MPFR_HAVE_GMP_IMPL
29
30char             mpfr_rands_initialized = 0;
31gmp_randstate_t  mpfr_rands;
32
33const struct bases mpfr_bases[257] =
34{
35  /*  0 */ {0.0},
36  /*  1 */ {1e37},
37  /*  2 */ {1.0000000000000000},
38  /*  3 */ {0.6309297535714574},
39  /*  4 */ {0.5000000000000000},
40  /*  5 */ {0.4306765580733931},
41  /*  6 */ {0.3868528072345416},
42  /*  7 */ {0.3562071871080222},
43  /*  8 */ {0.3333333333333333},
44  /*  9 */ {0.3154648767857287},
45  /* 10 */ {0.3010299956639812},
46  /* 11 */ {0.2890648263178878},
47  /* 12 */ {0.2789429456511298},
48  /* 13 */ {0.2702381544273197},
49  /* 14 */ {0.2626495350371935},
50  /* 15 */ {0.2559580248098155},
51  /* 16 */ {0.2500000000000000},
52  /* 17 */ {0.2446505421182260},
53  /* 18 */ {0.2398124665681314},
54  /* 19 */ {0.2354089133666382},
55  /* 20 */ {0.2313782131597592},
56  /* 21 */ {0.2276702486969530},
57  /* 22 */ {0.2242438242175754},
58  /* 23 */ {0.2210647294575037},
59  /* 24 */ {0.2181042919855316},
60  /* 25 */ {0.2153382790366965},
61  /* 26 */ {0.2127460535533632},
62  /* 27 */ {0.2103099178571525},
63  /* 28 */ {0.2080145976765095},
64  /* 29 */ {0.2058468324604344},
65  /* 30 */ {0.2037950470905062},
66  /* 31 */ {0.2018490865820999},
67  /* 32 */ {0.2000000000000000},
68  /* 33 */ {0.1982398631705605},
69  /* 34 */ {0.1965616322328226},
70  /* 35 */ {0.1949590218937863},
71  /* 36 */ {0.1934264036172708},
72  /* 37 */ {0.1919587200065601},
73  /* 38 */ {0.1905514124267734},
74  /* 39 */ {0.1892003595168700},
75  /* 40 */ {0.1879018247091076},
76  /* 41 */ {0.1866524112389434},
77  /* 42 */ {0.1854490234153689},
78  /* 43 */ {0.1842888331487062},
79  /* 44 */ {0.1831692509136336},
80  /* 45 */ {0.1820879004699383},
81  /* 46 */ {0.1810425967800402},
82  /* 47 */ {0.1800313266566926},
83  /* 48 */ {0.1790522317510414},
84  /* 49 */ {0.1781035935540111},
85  /* 50 */ {0.1771838201355579},
86  /* 51 */ {0.1762914343888821},
87  /* 52 */ {0.1754250635819545},
88  /* 53 */ {0.1745834300480449},
89  /* 54 */ {0.1737653428714400},
90  /* 55 */ {0.1729696904450771},
91  /* 56 */ {0.1721954337940981},
92  /* 57 */ {0.1714416005739134},
93  /* 58 */ {0.1707072796637201},
94  /* 59 */ {0.1699916162869140},
95  /* 60 */ {0.1692938075987814},
96  /* 61 */ {0.1686130986895011},
97  /* 62 */ {0.1679487789570419},
98  /* 63 */ {0.1673001788101741},
99  /* 64 */ {0.1666666666666667},
100  /* 65 */ {0.1660476462159378},
101  /* 66 */ {0.1654425539190583},
102  /* 67 */ {0.1648508567221603},
103  /* 68 */ {0.1642720499620502},
104  /* 69 */ {0.1637056554452156},
105  /* 70 */ {0.1631512196835108},
106  /* 71 */ {0.1626083122716342},
107  /* 72 */ {0.1620765243931223},
108  /* 73 */ {0.1615554674429964},
109  /* 74 */ {0.1610447717564444},
110  /* 75 */ {0.1605440854340214},
111  /* 76 */ {0.1600530732548213},
112  /* 77 */ {0.1595714156699382},
113  /* 78 */ {0.1590988078692941},
114  /* 79 */ {0.1586349589155960},
115  /* 80 */ {0.1581795909397823},
116  /* 81 */ {0.1577324383928644},
117  /* 82 */ {0.1572932473495469},
118  /* 83 */ {0.1568617748594410},
119  /* 84 */ {0.1564377883420715},
120  /* 85 */ {0.1560210650222250},
121  /* 86 */ {0.1556113914024939},
122  /* 87 */ {0.1552085627701551},
123  /* 88 */ {0.1548123827357682},
124  /* 89 */ {0.1544226628011101},
125  /* 90 */ {0.1540392219542636},
126  /* 91 */ {0.1536618862898642},
127  /* 92 */ {0.1532904886526781},
128  /* 93 */ {0.1529248683028321},
129  /* 94 */ {0.1525648706011593},
130  /* 95 */ {0.1522103467132434},
131  /* 96 */ {0.1518611533308632},
132  /* 97 */ {0.1515171524096389},
133  /* 98 */ {0.1511782109217764},
134  /* 99 */ {0.1508442006228941},
135  /* 100 */ {0.1505149978319906},
136  /* 101 */ {0.1501904832236880},
137  /* 102 */ {0.1498705416319474},
138  /* 103 */ {0.1495550618645152},
139  /* 104 */ {0.1492439365274121},
140  /* 105 */ {0.1489370618588283},
141  /* 106 */ {0.1486343375718350},
142  /* 107 */ {0.1483356667053617},
143  /* 108 */ {0.1480409554829326},
144  /* 109 */ {0.1477501131786861},
145  /* 110 */ {0.1474630519902391},
146  /* 111 */ {0.1471796869179852},
147  /* 112 */ {0.1468999356504447},
148  /* 113 */ {0.1466237184553111},
149  /* 114 */ {0.1463509580758620},
150  /* 115 */ {0.1460815796324244},
151  /* 116 */ {0.1458155105286054},
152  /* 117 */ {0.1455526803620167},
153  /* 118 */ {0.1452930208392429},
154  /* 119 */ {0.1450364656948130},
155  /* 120 */ {0.1447829506139581},
156  /* 121 */ {0.1445324131589439},
157  /* 122 */ {0.1442847926987864},
158  /* 123 */ {0.1440400303421672},
159  /* 124 */ {0.1437980688733776},
160  /* 125 */ {0.1435588526911310},
161  /* 126 */ {0.1433223277500932},
162  /* 127 */ {0.1430884415049874},
163  /* 128 */ {0.1428571428571428},
164  /* 129 */ {0.1426283821033600},
165  /* 130 */ {0.1424021108869747},
166  /* 131 */ {0.1421782821510107},
167  /* 132 */ {0.1419568500933153},
168  /* 133 */ {0.1417377701235801},
169  /* 134 */ {0.1415209988221527},
170  /* 135 */ {0.1413064939005528},
171  /* 136 */ {0.1410942141636095},
172  /* 137 */ {0.1408841194731412},
173  /* 138 */ {0.1406761707131039},
174  /* 139 */ {0.1404703297561400},
175  /* 140 */ {0.1402665594314587},
176  /* 141 */ {0.1400648234939879},
177  /* 142 */ {0.1398650865947379},
178  /* 143 */ {0.1396673142523192},
179  /* 144 */ {0.1394714728255649},
180  /* 145 */ {0.1392775294872041},
181  /* 146 */ {0.1390854521985406},
182  /* 147 */ {0.1388952096850913},
183  /* 148 */ {0.1387067714131417},
184  /* 149 */ {0.1385201075671774},
185  /* 150 */ {0.1383351890281539},
186  /* 151 */ {0.1381519873525671},
187  /* 152 */ {0.1379704747522905},
188  /* 153 */ {0.1377906240751463},
189  /* 154 */ {0.1376124087861776},
190  /* 155 */ {0.1374358029495937},
191  /* 156 */ {0.1372607812113589},
192  /* 157 */ {0.1370873187823978},
193  /* 158 */ {0.1369153914223921},
194  /* 159 */ {0.1367449754241439},
195  /* 160 */ {0.1365760475984821},
196  /* 161 */ {0.1364085852596902},
197  /* 162 */ {0.1362425662114337},
198  /* 163 */ {0.1360779687331669},
199  /* 164 */ {0.1359147715670014},
200  /* 165 */ {0.1357529539050150},
201  /* 166 */ {0.1355924953769864},
202  /* 167 */ {0.1354333760385373},
203  /* 168 */ {0.1352755763596663},
204  /* 169 */ {0.1351190772136599},
205  /* 170 */ {0.1349638598663645},
206  /* 171 */ {0.1348099059658080},
207  /* 172 */ {0.1346571975321549},
208  /* 173 */ {0.1345057169479844},
209  /* 174 */ {0.1343554469488779},
210  /* 175 */ {0.1342063706143054},
211  /* 176 */ {0.1340584713587979},
212  /* 177 */ {0.1339117329233981},
213  /* 178 */ {0.1337661393673756},
214  /* 179 */ {0.1336216750601996},
215  /* 180 */ {0.1334783246737591},
216  /* 181 */ {0.1333360731748201},
217  /* 182 */ {0.1331949058177136},
218  /* 183 */ {0.1330548081372441},
219  /* 184 */ {0.1329157659418126},
220  /* 185 */ {0.1327777653067443},
221  /* 186 */ {0.1326407925678156},
222  /* 187 */ {0.1325048343149731},
223  /* 188 */ {0.1323698773862368},
224  /* 189 */ {0.1322359088617821},
225  /* 190 */ {0.1321029160581950},
226  /* 191 */ {0.1319708865228925},
227  /* 192 */ {0.1318398080287045},
228  /* 193 */ {0.1317096685686114},
229  /* 194 */ {0.1315804563506306},
230  /* 195 */ {0.1314521597928493},
231  /* 196 */ {0.1313247675185968},
232  /* 197 */ {0.1311982683517524},
233  /* 198 */ {0.1310726513121843},
234  /* 199 */ {0.1309479056113158},
235  /* 200 */ {0.1308240206478128},
236  /* 201 */ {0.1307009860033912},
237  /* 202 */ {0.1305787914387386},
238  /* 203 */ {0.1304574268895465},
239  /* 204 */ {0.1303368824626505},
240  /* 205 */ {0.1302171484322746},
241  /* 206 */ {0.1300982152363760},
242  /* 207 */ {0.1299800734730872},
243  /* 208 */ {0.1298627138972530},
244  /* 209 */ {0.1297461274170591},
245  /* 210 */ {0.1296303050907487},
246  /* 211 */ {0.1295152381234257},
247  /* 212 */ {0.1294009178639407},
248  /* 213 */ {0.1292873358018581},
249  /* 214 */ {0.1291744835645007},
250  /* 215 */ {0.1290623529140715},
251  /* 216 */ {0.1289509357448472},
252  /* 217 */ {0.1288402240804449},
253  /* 218 */ {0.1287302100711566},
254  /* 219 */ {0.1286208859913518},
255  /* 220 */ {0.1285122442369443},
256  /* 221 */ {0.1284042773229231},
257  /* 222 */ {0.1282969778809442},
258  /* 223 */ {0.1281903386569819},
259  /* 224 */ {0.1280843525090381},
260  /* 225 */ {0.1279790124049077},
261  /* 226 */ {0.1278743114199984},
262  /* 227 */ {0.1277702427352035},
263  /* 228 */ {0.1276667996348261},
264  /* 229 */ {0.1275639755045533},
265  /* 230 */ {0.1274617638294791},
266  /* 231 */ {0.1273601581921740},
267  /* 232 */ {0.1272591522708010},
268  /* 233 */ {0.1271587398372755},
269  /* 234 */ {0.1270589147554692},
270  /* 235 */ {0.1269596709794558},
271  /* 236 */ {0.1268610025517973},
272  /* 237 */ {0.1267629036018709},
273  /* 238 */ {0.1266653683442337},
274  /* 239 */ {0.1265683910770258},
275  /* 240 */ {0.1264719661804097},
276  /* 241 */ {0.1263760881150453},
277  /* 242 */ {0.1262807514205999},
278  /* 243 */ {0.1261859507142915},
279  /* 244 */ {0.1260916806894653},
280  /* 245 */ {0.1259979361142023},
281  /* 246 */ {0.1259047118299582},
282  /* 247 */ {0.1258120027502338},
283  /* 248 */ {0.1257198038592741},
284  /* 249 */ {0.1256281102107963},
285  /* 250 */ {0.1255369169267456},
286  /* 251 */ {0.1254462191960791},
287  /* 252 */ {0.1253560122735751},
288  /* 253 */ {0.1252662914786691},
289  /* 254 */ {0.1251770521943144},
290  /* 255 */ {0.1250882898658681},
291  /* 256 */ {0.1250000000000000},
292};
293
294void
295mpfr_assert_fail (const char *filename, int linenum,
296                     const char *expr)
297{
298  if (filename != NULL && filename[0] != '\0')
299    {
300      fprintf (stderr, "%s:", filename);
301      if (linenum != -1)
302        fprintf (stderr, "%d: ", linenum);
303    }
304  fprintf (stderr, "MPFR assertion failed: %s\n", expr);
305  abort();
306}
307
308#ifdef mp_get_memory_functions
309
310/* putting 0 as initial values forces those symbols to be fully defined,
311   and always resolved, otherwise they are only tentatively defined, which
312   leads to problems on e.g. MacOS, cf
313   http://lists.gforge.inria.fr/pipermail/mpc-discuss/2008-November/000048.html
314   and http://software.intel.com/en-us/articles/intelr-fortran-compiler-for-mac-os-non_lazy_ptr-unresolved-references-from-linking
315   Note that using ranlib -c or libtool -c is another fix.
316*/
317void * (*mpfr_allocate_func) (size_t) = 0;
318void * (*mpfr_reallocate_func) (void *,size_t, size_t) = 0;
319void   (*mpfr_free_func) (void *, size_t) = 0;
320
321#endif
322
323void *
324mpfr_default_allocate (size_t size)
325{
326  void *ret;
327  ret = malloc (size);
328  if (ret == NULL)
329    {
330      fprintf (stderr, "MPFR: Can't allocate memory (size=%lu)\n",
331               (unsigned long) size);
332      abort ();
333    }
334  return ret;
335}
336
337void *
338mpfr_default_reallocate (void *oldptr, size_t old_size, size_t new_size)
339{
340  void *ret;
341  ret = realloc (oldptr, new_size);
342  if (ret == NULL)
343    {
344      fprintf (stderr,
345               "MPFR: Can't reallocate memory (old_size=%lu new_size=%lu)\n",
346               (unsigned long) old_size, (unsigned long) new_size);
347      abort ();
348    }
349  return ret;
350}
351
352void
353mpfr_default_free (void *blk_ptr, size_t blk_size)
354{
355  free (blk_ptr);
356}
357
358void *
359mpfr_tmp_allocate (struct tmp_marker **tmp_marker, size_t size)
360{
361  struct tmp_marker *head;
362
363  head = (struct tmp_marker *)
364    mpfr_default_allocate (sizeof (struct tmp_marker));
365  head->ptr = mpfr_default_allocate (size);
366  head->size = size;
367  head->next = *tmp_marker;
368  *tmp_marker = head;
369  return head->ptr;
370}
371
372void
373mpfr_tmp_free (struct tmp_marker *tmp_marker)
374{
375  struct tmp_marker *t;
376
377  while (tmp_marker != NULL)
378    {
379      t = tmp_marker;
380      mpfr_default_free (t->ptr, t->size);
381      tmp_marker = t->next;
382      mpfr_default_free (t, sizeof (struct tmp_marker));
383    }
384}
385
386#endif /* Have gmp-impl.h */
387