arb/arf/set_d.c

72 lines
1.4 KiB
C
Raw Normal View History

/*
2014-06-04 12:55:05 +02:00
Copyright (C) 2014 Fredrik Johansson
This file is part of Arb.
Arb is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version. See <http://www.gnu.org/licenses/>.
*/
2014-06-04 12:55:05 +02:00
2016-03-03 15:42:23 +01:00
#include "flint/double_extras.h"
2014-06-04 12:55:05 +02:00
#include "arf.h"
void
arf_set_d(arf_t x, double v)
{
#if FLINT_BITS == 64
mp_limb_t h, sign, exp, frac;
2015-11-05 18:00:39 +00:00
slong real_exp, real_man;
2014-06-04 12:55:05 +02:00
union { double uf; mp_limb_t ul; } u;
u.uf = v;
h = u.ul;
sign = h >> 63;
exp = (h << 1) >> 53;
frac = (h << 12) >> 12;
if (exp == 0 && frac == 0)
{
arf_zero(x);
}
else if (exp == 0x7ff)
{
if (frac == 0)
{
if (sign)
arf_neg_inf(x);
else
arf_pos_inf(x);
}
else
{
arf_nan(x);
}
}
else
{
real_exp = exp - 1023 - 52;
2015-11-06 15:34:19 +00:00
frac |= (UWORD(1) << 52);
2014-06-04 12:55:05 +02:00
real_man = sign ? (-frac) : frac;
arf_set_si_2exp_si(x, real_man, real_exp);
}
#else
mpfr_t t;
mp_limb_t tmp[2];
t->_mpfr_prec = 53;
t->_mpfr_sign = 1;
t->_mpfr_exp = 0;
t->_mpfr_d = tmp;
mpfr_set_d(t, v, MPFR_RNDD);
arf_set_mpfr(x, t);
#endif
}