1 /*
2     Copyright (C) 2019 Julian Rüth
3 
4     This file is part of Arb.
5 
6     Arb is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
10 */
11 
12 #include <string.h>
13 #include <assert.h>
14 
15 #include "mag.h"
16 #include "arf.h"
17 
18 static void
mag_set_arf_dump(mag_t x,const arf_t y)19 mag_set_arf_dump(mag_t x, const arf_t y)
20 {
21     if (arf_is_special(y))
22     {
23         if (arf_is_zero(y))
24         {
25             mag_zero(x);
26         }
27         else if (arf_is_pos_inf(y))
28         {
29             mag_inf(x);
30         }
31         else
32         {
33             /* a mag cannot be negative infinity or NaN */
34             flint_abort();
35         }
36     }
37     else
38     {
39         fmpz_t mantissa, exponent;
40         fmpz_init(mantissa);
41         fmpz_init(exponent);
42 
43         arf_get_fmpz_2exp(mantissa, exponent, y);
44 
45         if(fmpz_cmp_ui(mantissa, 1 << MAG_BITS) >= 0) flint_abort(); /* assert */
46 
47         mag_set_ui(x, fmpz_get_ui(mantissa));
48 
49         mag_mul_2exp_fmpz(x, x, exponent);
50 
51         fmpz_clear(exponent);
52         fmpz_clear(mantissa);
53     }
54 }
55 
56 int
mag_load_str(mag_t x,const char * data)57 mag_load_str(mag_t x, const char* data)
58 {
59     int err = 0;
60     arf_t y;
61 
62     arf_init(y);
63 
64     err = arf_load_str(y, data);
65     if (err)
66     {
67         arf_clear(y);
68         return err;
69     }
70 
71     mag_set_arf_dump(x, y);
72 
73     arf_clear(y);
74     return err;
75 }
76 
77 int
mag_load_file(mag_t x,FILE * stream)78 mag_load_file(mag_t x, FILE* stream)
79 {
80     int err = 0;
81     arf_t y;
82 
83     arf_init(y);
84 
85     err = arf_load_file(y, stream);
86 
87     if (err)
88     {
89         arf_clear(y);
90         return err;
91     }
92 
93     mag_set_arf_dump(x, y);
94 
95     arf_clear(y);
96     return err;
97 }
98