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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
/* mpfr_fpif -- Binary export & import of MPFR numbers
(floating-point interchange format)
Copyright 2012-2014 Free Software Foundation, Inc.
Contributed by Olivier Demengeon.
This file is part of the GNU MPFR Library.
The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
The GNU MPFR Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
#include "mpfr-impl.h"
#if !defined (HAVE_BIG_ENDIAN) && !defined (HAVE_LITTLE_ENDIAN)
#error "Endianness is unknown. Not supported yet."
#endif
#define MPFR_KIND_ZERO 119
#define MPFR_KIND_INF 120
#define MPFR_KIND_NAN 121
#define MPFR_MAX_EMBEDDED_PRECISION 248
#define MPFR_MAX_EMBEDDED_EXPONENT 47
#define MPFR_EXTERNAL_EXPONENT 94
/* Begin: Low level helper functions */
#define COUNT_NB_BYTE(storage, size) \
do \
{ \
(storage) >>= 8; \
(size)++; \
} \
while ((storage) != 0)
#define ALLOC_RESULT(buffer, buffer_size, wanted_size) \
do \
{ \
if ((buffer) == NULL || *(buffer_size) < (wanted_size)) \
{ \
(buffer) = (unsigned char *) (*__gmp_reallocate_func) \
((buffer), *(buffer_size), (wanted_size)); \
if ((buffer) == NULL) \
{ \
*(buffer_size) = 0; \
return NULL; \
} \
} \
*(buffer_size) = (wanted_size); \
} \
while (0)
/*
* size in byte of a MPFR number in a binary object of a variable size
*/
#define MAX_VARIABLE_STORAGE(exponent_size, precision) \
((size_t)(((precision) >> 3) + (exponent_size) + \
((precision) > 248 ? sizeof(mpfr_prec_t) : 0) + 3))
/* copy in result[] the values in data[] with a different endianness,
where data_size might be smaller than data_max_size, so that we only
copy data_size bytes from the end of data[]. */
static void
#if defined (HAVE_BIG_ENDIAN)
putLittleEndianData (unsigned char *result, unsigned char *data,
size_t data_max_size, size_t data_size)
#elif defined (HAVE_LITTLE_ENDIAN)
putBigEndianData (unsigned char *result, unsigned char *data,
size_t data_max_size, size_t data_size)
#endif
{
size_t j;
MPFR_ASSERTD (data_size <= data_max_size);
for (j = 0; j < data_size; j++)
result[j] = data[data_max_size - j - 1];
}
/* copy in result[] the values in data[] with the same endianness */
static void
#if defined (HAVE_BIG_ENDIAN)
putBigEndianData (unsigned char *result, unsigned char *data,
size_t data_max_size, size_t data_size)
#elif defined (HAVE_LITTLE_ENDIAN)
putLittleEndianData (unsigned char *result, unsigned char *data,
size_t data_max_size, size_t data_size)
#endif
{
MPFR_ASSERTD (data_size <= data_max_size);
memcpy (result, data, data_size);
}
/* copy in result[] the values in data[] with a different endianness;
the data are written at the end of the result[] buffer (if
data_size < data_max_size, the first bytes of result[] are
left untouched). */
static void
#if defined (HAVE_BIG_ENDIAN)
getLittleEndianData (unsigned char *result, unsigned char *data,
size_t data_max_size, size_t data_size)
#elif defined (HAVE_LITTLE_ENDIAN)
getBigEndianData (unsigned char *result, unsigned char *data,
size_t data_max_size, size_t data_size)
#endif
{
size_t j;
MPFR_ASSERTD (data_size <= data_max_size);
for (j = 0; j < data_size; j++)
result[data_max_size - j - 1] = data[j];
}
/* copy in result[] the values in data[] with the same endianness */
static void
#if defined (HAVE_BIG_ENDIAN)
getBigEndianData (unsigned char *result, unsigned char *data,
size_t data_max_size, size_t data_size)
#elif defined (HAVE_LITTLE_ENDIAN)
getLittleEndianData (unsigned char *result, unsigned char *data,
size_t data_max_size, size_t data_size)
#endif
{
MPFR_ASSERTD (data_size <= data_max_size);
memcpy (result, data, data_size);
}
/* End: Low level helper functions */
/* Internal Function */
/*
* buffer : OUT : store the precision in binary format, can be null
* (may be reallocated if too small)
* buffer_size : IN/OUT : size of the buffer => size used in the buffer
* precision : IN : precision to store
* return pointer to a buffer storing the precision in binary format
*/
static unsigned char *
mpfr_fpif_store_precision (unsigned char *buffer, size_t *buffer_size,
mpfr_prec_t precision)
{
unsigned char *result;
size_t size_precision;
size_precision = 0;
if (precision > MPFR_MAX_EMBEDDED_PRECISION)
{
mpfr_prec_t copy_precision;
copy_precision = precision - (MPFR_MAX_EMBEDDED_PRECISION + 1);
COUNT_NB_BYTE(copy_precision, size_precision);
}
result = buffer;
ALLOC_RESULT(result, buffer_size, size_precision + 1);
if (precision > MPFR_MAX_EMBEDDED_PRECISION)
{
result[0] = size_precision - 1;
precision -= (MPFR_MAX_EMBEDDED_PRECISION + 1);
putLittleEndianData (result + 1, (unsigned char *) &precision,
sizeof(mpfr_prec_t), size_precision);
}
else
result[0] = precision + 7;
return result;
}
/*
* fh : IN : file handler
* return the precision stored in the binary buffer, 0 in case of error
*/
static mpfr_prec_t
mpfr_fpif_read_precision_from_file (FILE *fh)
{
mpfr_prec_t precision;
size_t precision_size;
unsigned char buffer[8];
if (fh == NULL)
return 0;
if (fread (buffer, 1, 1, fh) != 1)
return 0;
precision_size = buffer[0];
if (precision_size >= 8)
return precision_size - 7;
precision_size++;
/* Read the precision in little-endian format. */
if (fread (buffer, precision_size, 1, fh) != 1)
return 0;
while (precision_size > sizeof(mpfr_prec_t))
{
if (buffer[precision_size-1] != 0)
return 0; /* the read precision doesn't fit in a mpfr_prec_t */
precision_size--;
}
if (precision_size == sizeof(mpfr_prec_t) &&
buffer[precision_size-1] >= 0x80)
return 0; /* the read precision doesn't fit in a mpfr_prec_t */
precision = 0; /* to pad with 0's if data_size < data_max_size */
/* On big-endian machines, the data must be copied at the end of the
precision object in the memory; thus data_max_size (3rd argument)
must be sizeof(mpfr_prec_t). */
getLittleEndianData ((unsigned char *) &precision, buffer,
sizeof(mpfr_prec_t), precision_size);
return precision + (MPFR_MAX_EMBEDDED_PRECISION + 1);
}
/*
* buffer : OUT : store the kind of the MPFR number x, its sign, the size of
* its exponent and its exponent value in a binary format,
* can be null (may be reallocated if too small)
* buffer_size : IN/OUT : size of the buffer => size used in the buffer
* x : IN : MPFR number
* return pointer to a buffer storing the kind of the MPFR number x, its sign,
* the size of its exponent and its exponent value in a binary format,
*/
/* TODO
* exponents that use more than 16 bytes are not managed
*/
static unsigned char*
mpfr_fpif_store_exponent (unsigned char *buffer, size_t *buffer_size, mpfr_t x)
{
unsigned char *result;
mpfr_exp_t exponent;
mpfr_uexp_t uexp;
size_t exponent_size;
exponent = mpfr_get_exp (x);
exponent_size = 0;
if (mpfr_regular_p (x))
{
if (exponent > MPFR_MAX_EMBEDDED_EXPONENT ||
exponent < -MPFR_MAX_EMBEDDED_EXPONENT)
{
mpfr_exp_t copy_exponent;
uexp = SAFE_ABS (mpfr_uexp_t, exponent)
- MPFR_MAX_EMBEDDED_EXPONENT;
copy_exponent = uexp << 1;
COUNT_NB_BYTE(copy_exponent, exponent_size);
if (exponent < 0)
uexp |= (mpfr_uexp_t) 1 << (8 * exponent_size - 1);
}
else
uexp = exponent + MPFR_MAX_EMBEDDED_EXPONENT;
}
result = buffer;
ALLOC_RESULT(result, buffer_size, exponent_size + 1);
if (mpfr_regular_p (x))
{
if (exponent_size == 0)
result[0] = uexp;
else
{
result[0] = MPFR_EXTERNAL_EXPONENT + exponent_size;
putLittleEndianData (result + 1, (unsigned char *) &uexp,
sizeof(mpfr_exp_t), exponent_size);
}
}
else if (mpfr_zero_p (x))
result[0] = MPFR_KIND_ZERO;
else if (mpfr_inf_p (x))
result[0] = MPFR_KIND_INF;
else
{
MPFR_ASSERTD (mpfr_nan_p (x));
result[0] = MPFR_KIND_NAN;
}
if (MPFR_IS_NEG (x))
result[0] |= 0x80;
return result;
}
/*
* x : OUT : MPFR number extracted from the binary buffer
* fh : IN : file handler
* return 0 if successful
*/
/* TODO
* exponents that use more than 16 bytes are not managed
*/
static int
mpfr_fpif_read_exponent_from_file (mpfr_t x, FILE * fh)
{
mpfr_exp_t exponent;
mpfr_uexp_t uexp;
size_t exponent_size;
int sign;
unsigned char buffer[sizeof(mpfr_exp_t)];
if (fh == NULL)
return 1;
if (fread (buffer, 1, 1, fh) != 1)
return 1;
sign = (buffer[0] & 0x80) ? -1 : 1;
exponent = buffer[0] & 0x7F;
exponent_size = 1;
if (exponent > MPFR_EXTERNAL_EXPONENT && exponent < MPFR_KIND_ZERO)
{
mpfr_uexp_t exponent_sign;
exponent_size = exponent - MPFR_EXTERNAL_EXPONENT;
if (exponent_size > sizeof(mpfr_exp_t))
return 1;
if (fread (buffer, exponent_size, 1, fh) != 1)
return 1;
uexp = 0;
getLittleEndianData ((unsigned char *) &uexp, buffer,
sizeof(mpfr_exp_t), exponent_size);
exponent_sign = uexp & ((mpfr_uexp_t) 1 << (8 * exponent_size - 1));
uexp &= ~exponent_sign;
uexp += MPFR_MAX_EMBEDDED_EXPONENT;
exponent = exponent_sign ? - (mpfr_exp_t) uexp : (mpfr_exp_t) uexp;
if (exponent < __gmpfr_emin || exponent > __gmpfr_emax)
return 1;
MPFR_EXP (x) = exponent;
MPFR_SET_SIGN (x, sign);
exponent_size++;
}
else if (exponent == MPFR_KIND_ZERO)
mpfr_set_zero (x, sign);
else if (exponent == MPFR_KIND_INF)
mpfr_set_inf (x, sign);
else if (exponent == MPFR_KIND_NAN)
mpfr_set_nan (x);
else if (exponent < 95)
{
/* FIXME: no sign set. Is this normal? Check with a testcase. */
exponent -= MPFR_MAX_EMBEDDED_EXPONENT;
if (exponent < __gmpfr_emin || exponent > __gmpfr_emax)
return 1;
MPFR_EXP (x) = exponent;
}
else
return 1;
return 0;
}
/*
* buffer : OUT : store the limb of the MPFR number x in a binary format,
* can be null (may be reallocated if too small)
* buffer_size : IN/OUT : size of the buffer => size used in the buffer
* x : IN : MPFR number
* return pointer to a buffer storing the limb of the MPFR number x in a binary
* format
*/
static unsigned char*
mpfr_fpif_store_limbs (unsigned char *buffer, size_t *buffer_size, mpfr_t x)
{
unsigned char *result;
mpfr_prec_t precision;
size_t nb_byte;
size_t nb_limb, mp_bytes_per_limb;
size_t nb_partial_byte;
size_t i, j;
precision = mpfr_get_prec (x);
nb_byte = (precision + 7) >> 3;
mp_bytes_per_limb = mp_bits_per_limb >> 3;
nb_partial_byte = nb_byte % mp_bytes_per_limb;
nb_limb = (nb_byte + mp_bytes_per_limb - 1) / mp_bytes_per_limb;
result = buffer;
ALLOC_RESULT(result, buffer_size, nb_byte);
putBigEndianData (result, (unsigned char*) MPFR_MANT(x),
sizeof(mp_limb_t), nb_partial_byte);
for (i = nb_partial_byte, j = (nb_partial_byte == 0) ? 0 : 1; j < nb_limb;
i += mp_bytes_per_limb, j++)
putLittleEndianData (result + i, (unsigned char*) (MPFR_MANT(x) + j),
sizeof(mp_limb_t), sizeof(mp_limb_t));
return result;
}
/*
* x : OUT : MPFR number extracted from the binary buffer, should have the same
* precision than the number in the binary format
* buffer : IN : limb of the MPFR number x in a binary format,
* buffer_size : IN/OUT : size of the buffer => size used in the buffer
* return 0 if successful
*/
static int
mpfr_fpif_read_limbs (mpfr_t x, unsigned char *buffer, size_t *buffer_size)
{
mpfr_prec_t precision;
size_t nb_byte;
size_t mp_bytes_per_limb;
size_t nb_partial_byte;
size_t i, j;
precision = mpfr_get_prec (x);
nb_byte = (precision + 7) >> 3;
mp_bytes_per_limb = mp_bits_per_limb >> 3;
nb_partial_byte = nb_byte % mp_bytes_per_limb;
if ((buffer == NULL) || (*buffer_size < nb_byte))
{
*buffer_size = 0;
return 1;
}
*buffer_size = nb_byte;
if (nb_partial_byte > 0)
{
memset (MPFR_MANT(x), 0, sizeof(mp_limb_t));
getBigEndianData ((unsigned char*) MPFR_MANT(x), buffer,
sizeof(mp_limb_t), nb_partial_byte);
}
for (i = nb_partial_byte, j = (nb_partial_byte == 0) ? 0 : 1; i < nb_byte;
i += mp_bytes_per_limb, j++)
getLittleEndianData ((unsigned char*) (MPFR_MANT(x) + j), buffer + i,
sizeof(mp_limb_t), sizeof(mp_limb_t));
return 0;
}
/* External Function */
/*
* fh : IN : file hander
* x : IN : MPFR number to put in the file
* return 0 if successful
*/
int
mpfr_fpif_export (FILE *fh, mpfr_t x)
{
int status;
unsigned char *buf;
unsigned char *bufResult;
size_t used_size, buf_size;
if (fh == NULL)
return -1;
buf_size = MAX_VARIABLE_STORAGE(sizeof(mpfr_exp_t), mpfr_get_prec (x));
buf = (unsigned char*) (*__gmp_allocate_func) (buf_size);
if (buf == NULL)
return -1;
used_size = buf_size;
buf = mpfr_fpif_store_precision (buf, &used_size, mpfr_get_prec (x));
used_size > buf_size ? buf_size = used_size : 0;
status = fwrite (buf, used_size, 1, fh);
if (status != 1)
{
(*__gmp_free_func) (buf, buf_size);
return -1;
}
used_size = buf_size;
bufResult = mpfr_fpif_store_exponent (buf, &used_size, x);
if (bufResult == NULL)
{
(*__gmp_free_func) (buf, buf_size);
return -1;
}
buf = bufResult;
used_size > buf_size ? buf_size = used_size : 0;
status = fwrite (buf, used_size, 1, fh);
if (status != 1)
{
(*__gmp_free_func) (buf, buf_size);
return -1;
}
if (mpfr_regular_p (x))
{
used_size = buf_size;
buf = mpfr_fpif_store_limbs (buf, &used_size, x);
used_size > buf_size ? buf_size = used_size : 0;
status = fwrite (buf, used_size, 1, fh);
if (status != 1)
{
(*__gmp_free_func) (buf, buf_size);
return -1;
}
}
(*__gmp_free_func) (buf, buf_size);
return 0;
}
/*
* x : IN/OUT : MPFR number extracted from the file, its precision is reset to
* be able to hold the number
* fh : IN : file hander
* Return 0 if the import was successful.
*/
int
mpfr_fpif_import (mpfr_t x, FILE *fh)
{
int status;
mpfr_prec_t precision;
unsigned char *buffer;
size_t used_size;
precision = mpfr_fpif_read_precision_from_file (fh);
if (precision == 0) /* precision = 0 means an error */
return -1;
if (precision > MPFR_PREC_MAX)
return -1;
MPFR_STAT_STATIC_ASSERT (MPFR_PREC_MIN <= 8);
if (precision < MPFR_PREC_MIN)
precision = MPFR_PREC_MIN;
mpfr_set_prec (x, precision);
status = mpfr_fpif_read_exponent_from_file (x, fh);
if (status != 0)
return -1;
if (mpfr_regular_p (x))
{
used_size = (precision + 7) >> 3; /* ceil(precision/8) */
buffer = (unsigned char*) (*__gmp_allocate_func) (used_size);
if (buffer == NULL)
{
return -1;
}
status = fread (buffer, used_size, 1, fh);
if (status != 1)
{
(*__gmp_free_func) (buffer, used_size);
return -1;
}
status = mpfr_fpif_read_limbs (x, buffer, &used_size);
(*__gmp_free_func) (buffer, used_size);
if (status != 0)
return -1;
}
return 0;
}
|