Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 418346/* __gmp_extract_double -- convert from double to array of mp_limb_t.12Copyright 1996, 1999-2002, 2006, 2012 Free Software Foundation, Inc.34This file is part of the GNU MP Library.56The GNU MP Library is free software; you can redistribute it and/or modify7it under the terms of either:89* the GNU Lesser General Public License as published by the Free10Software Foundation; either version 3 of the License, or (at your11option) any later version.1213or1415* the GNU General Public License as published by the Free Software16Foundation; either version 2 of the License, or (at your option) any17later version.1819or both in parallel, as here.2021The GNU MP Library is distributed in the hope that it will be useful, but22WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY23or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License24for more details.2526You should have received copies of the GNU General Public License and the27GNU Lesser General Public License along with the GNU MP Library. If not,28see https://www.gnu.org/licenses/. */2930#include "gmp.h"31#include "gmp-impl.h"3233#ifdef XDEBUG34#undef _GMP_IEEE_FLOATS35#endif3637#ifndef _GMP_IEEE_FLOATS38#define _GMP_IEEE_FLOATS 039#endif4041/* Extract a non-negative double in d. */4243int44__gmp_extract_double (mp_ptr rp, double d)45{46long exp;47unsigned sc;48#ifdef _LONG_LONG_LIMB49#define BITS_PER_PART 64 /* somewhat bogus */50unsigned long long int manl;51#else52#define BITS_PER_PART GMP_LIMB_BITS53unsigned long int manh, manl;54#endif5556/* BUGS57581. Should handle Inf and NaN in IEEE specific code.592. Handle Inf and NaN also in default code, to avoid hangs.603. Generalize to handle all GMP_LIMB_BITS >= 32.614. This lits is incomplete and misspelled.62*/6364ASSERT (d >= 0.0);6566if (d == 0.0)67{68MPN_ZERO (rp, LIMBS_PER_DOUBLE);69return 0;70}7172#if _GMP_IEEE_FLOATS73{74#if defined (__alpha) && __GNUC__ == 2 && __GNUC_MINOR__ == 875/* Work around alpha-specific bug in GCC 2.8.x. */76volatile77#endif78union ieee_double_extract x;79x.d = d;80exp = x.s.exp;81#if BITS_PER_PART == 64 /* generalize this to BITS_PER_PART > BITS_IN_MANTISSA */82manl = (((mp_limb_t) 1 << 63)83| ((mp_limb_t) x.s.manh << 43) | ((mp_limb_t) x.s.manl << 11));84if (exp == 0)85{86/* Denormalized number. Don't try to be clever about this,87since it is not an important case to make fast. */88exp = 1;89do90{91manl = manl << 1;92exp--;93}94while ((manl & GMP_LIMB_HIGHBIT) == 0);95}96#endif97#if BITS_PER_PART == 3298manh = ((mp_limb_t) 1 << 31) | (x.s.manh << 11) | (x.s.manl >> 21);99manl = x.s.manl << 11;100if (exp == 0)101{102/* Denormalized number. Don't try to be clever about this,103since it is not an important case to make fast. */104exp = 1;105do106{107manh = (manh << 1) | (manl >> 31);108manl = manl << 1;109exp--;110}111while ((manh & GMP_LIMB_HIGHBIT) == 0);112}113#endif114#if BITS_PER_PART != 32 && BITS_PER_PART != 64115You need to generalize the code above to handle this.116#endif117exp -= 1022; /* Remove IEEE bias. */118}119#else120{121/* Unknown (or known to be non-IEEE) double format. */122exp = 0;123if (d >= 1.0)124{125ASSERT_ALWAYS (d * 0.5 != d);126127while (d >= 32768.0)128{129d *= (1.0 / 65536.0);130exp += 16;131}132while (d >= 1.0)133{134d *= 0.5;135exp += 1;136}137}138else if (d < 0.5)139{140while (d < (1.0 / 65536.0))141{142d *= 65536.0;143exp -= 16;144}145while (d < 0.5)146{147d *= 2.0;148exp -= 1;149}150}151152d *= (4.0 * ((unsigned long int) 1 << (BITS_PER_PART - 2)));153#if BITS_PER_PART == 64154manl = d;155#endif156#if BITS_PER_PART == 32157manh = d;158manl = (d - manh) * (4.0 * ((unsigned long int) 1 << (BITS_PER_PART - 2)));159#endif160}161#endif /* IEEE */162163sc = (unsigned) (exp + 64 * GMP_NUMB_BITS) % GMP_NUMB_BITS;164165/* We add something here to get rounding right. */166exp = (exp + 64 * GMP_NUMB_BITS) / GMP_NUMB_BITS - 64 * GMP_NUMB_BITS / GMP_NUMB_BITS + 1;167168#if BITS_PER_PART == 64 && LIMBS_PER_DOUBLE == 2169#if GMP_NAIL_BITS == 0170if (sc != 0)171{172rp[1] = manl >> (GMP_LIMB_BITS - sc);173rp[0] = manl << sc;174}175else176{177rp[1] = manl;178rp[0] = 0;179exp--;180}181#else182if (sc > GMP_NAIL_BITS)183{184rp[1] = manl >> (GMP_LIMB_BITS - sc);185rp[0] = (manl << (sc - GMP_NAIL_BITS)) & GMP_NUMB_MASK;186}187else188{189if (sc == 0)190{191rp[1] = manl >> GMP_NAIL_BITS;192rp[0] = (manl << GMP_NUMB_BITS - GMP_NAIL_BITS) & GMP_NUMB_MASK;193exp--;194}195else196{197rp[1] = manl >> (GMP_LIMB_BITS - sc);198rp[0] = (manl >> (GMP_NAIL_BITS - sc)) & GMP_NUMB_MASK;199}200}201#endif202#endif203204#if BITS_PER_PART == 64 && LIMBS_PER_DOUBLE == 3205if (sc > GMP_NAIL_BITS)206{207rp[2] = manl >> (GMP_LIMB_BITS - sc);208rp[1] = (manl << sc - GMP_NAIL_BITS) & GMP_NUMB_MASK;209if (sc >= 2 * GMP_NAIL_BITS)210rp[0] = 0;211else212rp[0] = (manl << GMP_NUMB_BITS - GMP_NAIL_BITS + sc) & GMP_NUMB_MASK;213}214else215{216if (sc == 0)217{218rp[2] = manl >> GMP_NAIL_BITS;219rp[1] = (manl << GMP_NUMB_BITS - GMP_NAIL_BITS) & GMP_NUMB_MASK;220rp[0] = 0;221exp--;222}223else224{225rp[2] = manl >> (GMP_LIMB_BITS - sc);226rp[1] = (manl >> GMP_NAIL_BITS - sc) & GMP_NUMB_MASK;227rp[0] = (manl << GMP_NUMB_BITS - GMP_NAIL_BITS + sc) & GMP_NUMB_MASK;228}229}230#endif231232#if BITS_PER_PART == 32 && LIMBS_PER_DOUBLE == 3233#if GMP_NAIL_BITS == 0234if (sc != 0)235{236rp[2] = manh >> (GMP_LIMB_BITS - sc);237rp[1] = (manh << sc) | (manl >> (GMP_LIMB_BITS - sc));238rp[0] = manl << sc;239}240else241{242rp[2] = manh;243rp[1] = manl;244rp[0] = 0;245exp--;246}247#else248if (sc > GMP_NAIL_BITS)249{250rp[2] = (manh >> (GMP_LIMB_BITS - sc));251rp[1] = ((manh << (sc - GMP_NAIL_BITS)) |252(manl >> (GMP_LIMB_BITS - sc + GMP_NAIL_BITS))) & GMP_NUMB_MASK;253if (sc >= 2 * GMP_NAIL_BITS)254rp[0] = (manl << sc - 2 * GMP_NAIL_BITS) & GMP_NUMB_MASK;255else256rp[0] = manl >> (2 * GMP_NAIL_BITS - sc) & GMP_NUMB_MASK;257}258else259{260if (sc == 0)261{262rp[2] = manh >> GMP_NAIL_BITS;263rp[1] = ((manh << GMP_NUMB_BITS - GMP_NAIL_BITS) | (manl >> 2 * GMP_NAIL_BITS)) & GMP_NUMB_MASK;264rp[0] = (manl << GMP_NUMB_BITS - 2 * GMP_NAIL_BITS) & GMP_NUMB_MASK;265exp--;266}267else268{269rp[2] = (manh >> (GMP_LIMB_BITS - sc));270rp[1] = (manh >> (GMP_NAIL_BITS - sc)) & GMP_NUMB_MASK;271rp[0] = ((manh << (GMP_NUMB_BITS - GMP_NAIL_BITS + sc))272| (manl >> (GMP_LIMB_BITS - (GMP_NUMB_BITS - GMP_NAIL_BITS + sc)))) & GMP_NUMB_MASK;273}274}275#endif276#endif277278#if BITS_PER_PART == 32 && LIMBS_PER_DOUBLE > 3279if (sc == 0)280{281int i;282283for (i = LIMBS_PER_DOUBLE - 1; i >= 0; i--)284{285rp[i] = manh >> (BITS_PER_ULONG - GMP_NUMB_BITS);286manh = ((manh << GMP_NUMB_BITS)287| (manl >> (BITS_PER_ULONG - GMP_NUMB_BITS)));288manl = manl << GMP_NUMB_BITS;289}290exp--;291}292else293{294int i;295296rp[LIMBS_PER_DOUBLE - 1] = (manh >> (GMP_LIMB_BITS - sc));297manh = (manh << sc) | (manl >> (GMP_LIMB_BITS - sc));298manl = (manl << sc);299for (i = LIMBS_PER_DOUBLE - 2; i >= 0; i--)300{301rp[i] = manh >> (BITS_PER_ULONG - GMP_NUMB_BITS);302manh = ((manh << GMP_NUMB_BITS)303| (manl >> (BITS_PER_ULONG - GMP_NUMB_BITS)));304manl = manl << GMP_NUMB_BITS;305}306}307#endif308309return exp;310}311312313