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