LCOV - code coverage report
Current view: top level - Objects - floatobject.c (source / functions) Hit Total Coverage
Test: CPython 3.12 LCOV report [commit 5e6661bce9] Lines: 516 1073 48.1 %
Date: 2023-03-20 08:15:36 Functions: 44 60 73.3 %
Branches: 271 684 39.6 %

           Branch data     Line data    Source code
       1                 :            : /* Float object implementation */
       2                 :            : 
       3                 :            : /* XXX There should be overflow checks here, but it's hard to check
       4                 :            :    for any kind of float exception without losing portability. */
       5                 :            : 
       6                 :            : #include "Python.h"
       7                 :            : #include "pycore_dtoa.h"          // _Py_dg_dtoa()
       8                 :            : #include "pycore_floatobject.h"   // _PyFloat_FormatAdvancedWriter()
       9                 :            : #include "pycore_initconfig.h"    // _PyStatus_OK()
      10                 :            : #include "pycore_interp.h"        // _PyInterpreterState.float_state
      11                 :            : #include "pycore_long.h"          // _PyLong_GetOne()
      12                 :            : #include "pycore_object.h"        // _PyObject_Init()
      13                 :            : #include "pycore_pymath.h"        // _PY_SHORT_FLOAT_REPR
      14                 :            : #include "pycore_pystate.h"       // _PyInterpreterState_GET()
      15                 :            : #include "pycore_structseq.h"     // _PyStructSequence_FiniType()
      16                 :            : 
      17                 :            : #include <ctype.h>
      18                 :            : #include <float.h>
      19                 :            : #include <stdlib.h>               // strtol()
      20                 :            : 
      21                 :            : /*[clinic input]
      22                 :            : class float "PyObject *" "&PyFloat_Type"
      23                 :            : [clinic start generated code]*/
      24                 :            : /*[clinic end generated code: output=da39a3ee5e6b4b0d input=dd0003f68f144284]*/
      25                 :            : 
      26                 :            : #include "clinic/floatobject.c.h"
      27                 :            : 
      28                 :            : #ifndef PyFloat_MAXFREELIST
      29                 :            : #  define PyFloat_MAXFREELIST   100
      30                 :            : #endif
      31                 :            : 
      32                 :            : 
      33                 :            : #if PyFloat_MAXFREELIST > 0
      34                 :            : static struct _Py_float_state *
      35                 :    6445607 : get_float_state(void)
      36                 :            : {
      37                 :    6445607 :     PyInterpreterState *interp = _PyInterpreterState_GET();
      38                 :    6445607 :     return &interp->float_state;
      39                 :            : }
      40                 :            : #endif
      41                 :            : 
      42                 :            : 
      43                 :            : double
      44                 :          0 : PyFloat_GetMax(void)
      45                 :            : {
      46                 :          0 :     return DBL_MAX;
      47                 :            : }
      48                 :            : 
      49                 :            : double
      50                 :          0 : PyFloat_GetMin(void)
      51                 :            : {
      52                 :          0 :     return DBL_MIN;
      53                 :            : }
      54                 :            : 
      55                 :            : static PyTypeObject FloatInfoType;
      56                 :            : 
      57                 :            : PyDoc_STRVAR(floatinfo__doc__,
      58                 :            : "sys.float_info\n\
      59                 :            : \n\
      60                 :            : A named tuple holding information about the float type. It contains low level\n\
      61                 :            : information about the precision and internal representation. Please study\n\
      62                 :            : your system's :file:`float.h` for more information.");
      63                 :            : 
      64                 :            : static PyStructSequence_Field floatinfo_fields[] = {
      65                 :            :     {"max",             "DBL_MAX -- maximum representable finite float"},
      66                 :            :     {"max_exp",         "DBL_MAX_EXP -- maximum int e such that radix**(e-1) "
      67                 :            :                     "is representable"},
      68                 :            :     {"max_10_exp",      "DBL_MAX_10_EXP -- maximum int e such that 10**e "
      69                 :            :                     "is representable"},
      70                 :            :     {"min",             "DBL_MIN -- Minimum positive normalized float"},
      71                 :            :     {"min_exp",         "DBL_MIN_EXP -- minimum int e such that radix**(e-1) "
      72                 :            :                     "is a normalized float"},
      73                 :            :     {"min_10_exp",      "DBL_MIN_10_EXP -- minimum int e such that 10**e is "
      74                 :            :                     "a normalized float"},
      75                 :            :     {"dig",             "DBL_DIG -- maximum number of decimal digits that "
      76                 :            :                     "can be faithfully represented in a float"},
      77                 :            :     {"mant_dig",        "DBL_MANT_DIG -- mantissa digits"},
      78                 :            :     {"epsilon",         "DBL_EPSILON -- Difference between 1 and the next "
      79                 :            :                     "representable float"},
      80                 :            :     {"radix",           "FLT_RADIX -- radix of exponent"},
      81                 :            :     {"rounds",          "FLT_ROUNDS -- rounding mode used for arithmetic "
      82                 :            :                     "operations"},
      83                 :            :     {0}
      84                 :            : };
      85                 :            : 
      86                 :            : static PyStructSequence_Desc floatinfo_desc = {
      87                 :            :     "sys.float_info",           /* name */
      88                 :            :     floatinfo__doc__,           /* doc */
      89                 :            :     floatinfo_fields,           /* fields */
      90                 :            :     11
      91                 :            : };
      92                 :            : 
      93                 :            : PyObject *
      94                 :         29 : PyFloat_GetInfo(void)
      95                 :            : {
      96                 :            :     PyObject* floatinfo;
      97                 :         29 :     int pos = 0;
      98                 :            : 
      99                 :         29 :     floatinfo = PyStructSequence_New(&FloatInfoType);
     100         [ -  + ]:         29 :     if (floatinfo == NULL) {
     101                 :          0 :         return NULL;
     102                 :            :     }
     103                 :            : 
     104                 :            : #define SetIntFlag(flag) \
     105                 :            :     PyStructSequence_SET_ITEM(floatinfo, pos++, PyLong_FromLong(flag))
     106                 :            : #define SetDblFlag(flag) \
     107                 :            :     PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag))
     108                 :            : 
     109                 :         29 :     SetDblFlag(DBL_MAX);
     110                 :         29 :     SetIntFlag(DBL_MAX_EXP);
     111                 :         29 :     SetIntFlag(DBL_MAX_10_EXP);
     112                 :         29 :     SetDblFlag(DBL_MIN);
     113                 :         29 :     SetIntFlag(DBL_MIN_EXP);
     114                 :         29 :     SetIntFlag(DBL_MIN_10_EXP);
     115                 :         29 :     SetIntFlag(DBL_DIG);
     116                 :         29 :     SetIntFlag(DBL_MANT_DIG);
     117                 :         29 :     SetDblFlag(DBL_EPSILON);
     118                 :         29 :     SetIntFlag(FLT_RADIX);
     119                 :         29 :     SetIntFlag(FLT_ROUNDS);
     120                 :            : #undef SetIntFlag
     121                 :            : #undef SetDblFlag
     122                 :            : 
     123         [ -  + ]:         29 :     if (PyErr_Occurred()) {
     124         [ #  # ]:          0 :         Py_CLEAR(floatinfo);
     125                 :          0 :         return NULL;
     126                 :            :     }
     127                 :         29 :     return floatinfo;
     128                 :            : }
     129                 :            : 
     130                 :            : PyObject *
     131                 :    3222810 : PyFloat_FromDouble(double fval)
     132                 :            : {
     133                 :            :     PyFloatObject *op;
     134                 :            : #if PyFloat_MAXFREELIST > 0
     135                 :    3222810 :     struct _Py_float_state *state = get_float_state();
     136                 :    3222810 :     op = state->free_list;
     137         [ +  + ]:    3222810 :     if (op != NULL) {
     138                 :            : #ifdef Py_DEBUG
     139                 :            :         // PyFloat_FromDouble() must not be called after _PyFloat_Fini()
     140                 :            :         assert(state->numfree != -1);
     141                 :            : #endif
     142                 :    3102247 :         state->free_list = (PyFloatObject *) Py_TYPE(op);
     143                 :    3102247 :         state->numfree--;
     144                 :            :         OBJECT_STAT_INC(from_freelist);
     145                 :            :     }
     146                 :            :     else
     147                 :            : #endif
     148                 :            :     {
     149                 :     120563 :         op = PyObject_Malloc(sizeof(PyFloatObject));
     150         [ -  + ]:     120563 :         if (!op) {
     151                 :          0 :             return PyErr_NoMemory();
     152                 :            :         }
     153                 :            :     }
     154                 :    3222810 :     _PyObject_Init((PyObject*)op, &PyFloat_Type);
     155                 :    3222810 :     op->ob_fval = fval;
     156                 :    3222810 :     return (PyObject *) op;
     157                 :            : }
     158                 :            : 
     159                 :            : static PyObject *
     160                 :       9343 : float_from_string_inner(const char *s, Py_ssize_t len, void *obj)
     161                 :            : {
     162                 :            :     double x;
     163                 :            :     const char *end;
     164                 :       9343 :     const char *last = s + len;
     165                 :            :     /* strip leading whitespace */
     166   [ +  -  -  + ]:       9343 :     while (s < last && Py_ISSPACE(*s)) {
     167                 :          0 :         s++;
     168                 :            :     }
     169         [ -  + ]:       9343 :     if (s == last) {
     170                 :          0 :         PyErr_Format(PyExc_ValueError,
     171                 :            :                      "could not convert string to float: "
     172                 :            :                      "%R", obj);
     173                 :          0 :         return NULL;
     174                 :            :     }
     175                 :            : 
     176                 :            :     /* strip trailing whitespace */
     177   [ +  +  -  + ]:       9343 :     while (s < last - 1 && Py_ISSPACE(last[-1])) {
     178                 :          0 :         last--;
     179                 :            :     }
     180                 :            : 
     181                 :            :     /* We don't care about overflow or underflow.  If the platform
     182                 :            :      * supports them, infinities and signed zeroes (on underflow) are
     183                 :            :      * fine. */
     184                 :       9343 :     x = PyOS_string_to_double(s, (char **)&end, NULL);
     185         [ -  + ]:       9343 :     if (end != last) {
     186                 :          0 :         PyErr_Format(PyExc_ValueError,
     187                 :            :                      "could not convert string to float: "
     188                 :            :                      "%R", obj);
     189                 :          0 :         return NULL;
     190                 :            :     }
     191   [ +  +  -  + ]:       9343 :     else if (x == -1.0 && PyErr_Occurred()) {
     192                 :          0 :         return NULL;
     193                 :            :     }
     194                 :            :     else {
     195                 :       9343 :         return PyFloat_FromDouble(x);
     196                 :            :     }
     197                 :            : }
     198                 :            : 
     199                 :            : PyObject *
     200                 :       9343 : PyFloat_FromString(PyObject *v)
     201                 :            : {
     202                 :            :     const char *s;
     203                 :       9343 :     PyObject *s_buffer = NULL;
     204                 :            :     Py_ssize_t len;
     205                 :       9343 :     Py_buffer view = {NULL, NULL};
     206                 :       9343 :     PyObject *result = NULL;
     207                 :            : 
     208         [ +  - ]:       9343 :     if (PyUnicode_Check(v)) {
     209                 :       9343 :         s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v);
     210         [ -  + ]:       9343 :         if (s_buffer == NULL)
     211                 :          0 :             return NULL;
     212                 :            :         assert(PyUnicode_IS_ASCII(s_buffer));
     213                 :            :         /* Simply get a pointer to existing ASCII characters. */
     214                 :       9343 :         s = PyUnicode_AsUTF8AndSize(s_buffer, &len);
     215                 :            :         assert(s != NULL);
     216                 :            :     }
     217         [ #  # ]:          0 :     else if (PyBytes_Check(v)) {
     218                 :          0 :         s = PyBytes_AS_STRING(v);
     219                 :          0 :         len = PyBytes_GET_SIZE(v);
     220                 :            :     }
     221         [ #  # ]:          0 :     else if (PyByteArray_Check(v)) {
     222                 :          0 :         s = PyByteArray_AS_STRING(v);
     223                 :          0 :         len = PyByteArray_GET_SIZE(v);
     224                 :            :     }
     225         [ #  # ]:          0 :     else if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) == 0) {
     226                 :          0 :         s = (const char *)view.buf;
     227                 :          0 :         len = view.len;
     228                 :            :         /* Copy to NUL-terminated buffer. */
     229                 :          0 :         s_buffer = PyBytes_FromStringAndSize(s, len);
     230         [ #  # ]:          0 :         if (s_buffer == NULL) {
     231                 :          0 :             PyBuffer_Release(&view);
     232                 :          0 :             return NULL;
     233                 :            :         }
     234                 :          0 :         s = PyBytes_AS_STRING(s_buffer);
     235                 :            :     }
     236                 :            :     else {
     237                 :          0 :         PyErr_Format(PyExc_TypeError,
     238                 :            :             "float() argument must be a string or a real number, not '%.200s'",
     239                 :          0 :             Py_TYPE(v)->tp_name);
     240                 :          0 :         return NULL;
     241                 :            :     }
     242                 :       9343 :     result = _Py_string_to_number_with_underscores(s, len, "float", v, v,
     243                 :            :                                                    float_from_string_inner);
     244                 :       9343 :     PyBuffer_Release(&view);
     245                 :       9343 :     Py_XDECREF(s_buffer);
     246                 :       9343 :     return result;
     247                 :            : }
     248                 :            : 
     249                 :            : void
     250                 :    3222797 : _PyFloat_ExactDealloc(PyObject *obj)
     251                 :            : {
     252                 :            :     assert(PyFloat_CheckExact(obj));
     253                 :    3222797 :     PyFloatObject *op = (PyFloatObject *)obj;
     254                 :            : #if PyFloat_MAXFREELIST > 0
     255                 :    3222797 :     struct _Py_float_state *state = get_float_state();
     256                 :            : #ifdef Py_DEBUG
     257                 :            :     // float_dealloc() must not be called after _PyFloat_Fini()
     258                 :            :     assert(state->numfree != -1);
     259                 :            : #endif
     260         [ +  + ]:    3222797 :     if (state->numfree >= PyFloat_MAXFREELIST)  {
     261                 :     119881 :         PyObject_Free(op);
     262                 :     119881 :         return;
     263                 :            :     }
     264                 :    3102916 :     state->numfree++;
     265                 :    3102916 :     Py_SET_TYPE(op, (PyTypeObject *)state->free_list);
     266                 :    3102916 :     state->free_list = op;
     267                 :            :     OBJECT_STAT_INC(to_freelist);
     268                 :            : #else
     269                 :            :     PyObject_Free(op);
     270                 :            : #endif
     271                 :            : }
     272                 :            : 
     273                 :            : static void
     274                 :    3220565 : float_dealloc(PyObject *op)
     275                 :            : {
     276                 :            :     assert(PyFloat_Check(op));
     277                 :            : #if PyFloat_MAXFREELIST > 0
     278         [ +  + ]:    3220565 :     if (PyFloat_CheckExact(op)) {
     279                 :    3220562 :         _PyFloat_ExactDealloc(op);
     280                 :            :     }
     281                 :            :     else
     282                 :            : #endif
     283                 :            :     {
     284                 :          3 :         Py_TYPE(op)->tp_free(op);
     285                 :            :     }
     286                 :    3220565 : }
     287                 :            : 
     288                 :            : double
     289                 :     465772 : PyFloat_AsDouble(PyObject *op)
     290                 :            : {
     291                 :            :     PyNumberMethods *nb;
     292                 :            :     PyObject *res;
     293                 :            :     double val;
     294                 :            : 
     295         [ -  + ]:     465772 :     if (op == NULL) {
     296                 :          0 :         PyErr_BadArgument();
     297                 :          0 :         return -1;
     298                 :            :     }
     299                 :            : 
     300         [ +  + ]:     465772 :     if (PyFloat_Check(op)) {
     301                 :     423454 :         return PyFloat_AS_DOUBLE(op);
     302                 :            :     }
     303                 :            : 
     304                 :      42318 :     nb = Py_TYPE(op)->tp_as_number;
     305   [ +  -  +  + ]:      42318 :     if (nb == NULL || nb->nb_float == NULL) {
     306   [ +  -  -  + ]:         10 :         if (nb && nb->nb_index) {
     307                 :          0 :             PyObject *res = _PyNumber_Index(op);
     308         [ #  # ]:          0 :             if (!res) {
     309                 :          0 :                 return -1;
     310                 :            :             }
     311                 :          0 :             double val = PyLong_AsDouble(res);
     312                 :          0 :             Py_DECREF(res);
     313                 :          0 :             return val;
     314                 :            :         }
     315                 :         10 :         PyErr_Format(PyExc_TypeError, "must be real number, not %.50s",
     316                 :         10 :                      Py_TYPE(op)->tp_name);
     317                 :         10 :         return -1;
     318                 :            :     }
     319                 :            : 
     320                 :      42308 :     res = (*nb->nb_float) (op);
     321         [ -  + ]:      42308 :     if (res == NULL) {
     322                 :          0 :         return -1;
     323                 :            :     }
     324         [ -  + ]:      42308 :     if (!PyFloat_CheckExact(res)) {
     325         [ #  # ]:          0 :         if (!PyFloat_Check(res)) {
     326                 :          0 :             PyErr_Format(PyExc_TypeError,
     327                 :            :                          "%.50s.__float__ returned non-float (type %.50s)",
     328                 :          0 :                          Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name);
     329                 :          0 :             Py_DECREF(res);
     330                 :          0 :             return -1;
     331                 :            :         }
     332         [ #  # ]:          0 :         if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
     333                 :            :                 "%.50s.__float__ returned non-float (type %.50s).  "
     334                 :            :                 "The ability to return an instance of a strict subclass of float "
     335                 :            :                 "is deprecated, and may be removed in a future version of Python.",
     336                 :          0 :                 Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name)) {
     337                 :          0 :             Py_DECREF(res);
     338                 :          0 :             return -1;
     339                 :            :         }
     340                 :            :     }
     341                 :            : 
     342                 :      42308 :     val = PyFloat_AS_DOUBLE(res);
     343                 :      42308 :     Py_DECREF(res);
     344                 :      42308 :     return val;
     345                 :            : }
     346                 :            : 
     347                 :            : /* Macro and helper that convert PyObject obj to a C double and store
     348                 :            :    the value in dbl.  If conversion to double raises an exception, obj is
     349                 :            :    set to NULL, and the function invoking this macro returns NULL.  If
     350                 :            :    obj is not of float or int type, Py_NotImplemented is incref'ed,
     351                 :            :    stored in obj, and returned from the function invoking this macro.
     352                 :            : */
     353                 :            : #define CONVERT_TO_DOUBLE(obj, dbl)                     \
     354                 :            :     if (PyFloat_Check(obj))                             \
     355                 :            :         dbl = PyFloat_AS_DOUBLE(obj);                   \
     356                 :            :     else if (convert_to_double(&(obj), &(dbl)) < 0)     \
     357                 :            :         return obj;
     358                 :            : 
     359                 :            : /* Methods */
     360                 :            : 
     361                 :            : static int
     362                 :     415261 : convert_to_double(PyObject **v, double *dbl)
     363                 :            : {
     364                 :     415261 :     PyObject *obj = *v;
     365                 :            : 
     366         [ +  + ]:     415261 :     if (PyLong_Check(obj)) {
     367                 :     415256 :         *dbl = PyLong_AsDouble(obj);
     368   [ +  +  -  + ]:     415256 :         if (*dbl == -1.0 && PyErr_Occurred()) {
     369                 :          0 :             *v = NULL;
     370                 :          0 :             return -1;
     371                 :            :         }
     372                 :            :     }
     373                 :            :     else {
     374                 :          5 :         *v = Py_NewRef(Py_NotImplemented);
     375                 :          5 :         return -1;
     376                 :            :     }
     377                 :     415256 :     return 0;
     378                 :            : }
     379                 :            : 
     380                 :            : static PyObject *
     381                 :        104 : float_repr(PyFloatObject *v)
     382                 :            : {
     383                 :            :     PyObject *result;
     384                 :            :     char *buf;
     385                 :            : 
     386                 :        104 :     buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
     387                 :            :                                 'r', 0,
     388                 :            :                                 Py_DTSF_ADD_DOT_0,
     389                 :            :                                 NULL);
     390         [ -  + ]:        104 :     if (!buf)
     391                 :          0 :         return PyErr_NoMemory();
     392                 :        104 :     result = _PyUnicode_FromASCII(buf, strlen(buf));
     393                 :        104 :     PyMem_Free(buf);
     394                 :        104 :     return result;
     395                 :            : }
     396                 :            : 
     397                 :            : /* Comparison is pretty much a nightmare.  When comparing float to float,
     398                 :            :  * we do it as straightforwardly (and long-windedly) as conceivable, so
     399                 :            :  * that, e.g., Python x == y delivers the same result as the platform
     400                 :            :  * C x == y when x and/or y is a NaN.
     401                 :            :  * When mixing float with an integer type, there's no good *uniform* approach.
     402                 :            :  * Converting the double to an integer obviously doesn't work, since we
     403                 :            :  * may lose info from fractional bits.  Converting the integer to a double
     404                 :            :  * also has two failure modes:  (1) an int may trigger overflow (too
     405                 :            :  * large to fit in the dynamic range of a C double); (2) even a C long may have
     406                 :            :  * more bits than fit in a C double (e.g., on a 64-bit box long may have
     407                 :            :  * 63 bits of precision, but a C double probably has only 53), and then
     408                 :            :  * we can falsely claim equality when low-order integer bits are lost by
     409                 :            :  * coercion to double.  So this part is painful too.
     410                 :            :  */
     411                 :            : 
     412                 :            : static PyObject*
     413                 :       5424 : float_richcompare(PyObject *v, PyObject *w, int op)
     414                 :            : {
     415                 :            :     double i, j;
     416                 :       5424 :     int r = 0;
     417                 :            : 
     418                 :            :     assert(PyFloat_Check(v));
     419                 :       5424 :     i = PyFloat_AS_DOUBLE(v);
     420                 :            : 
     421                 :            :     /* Switch on the type of w.  Set i and j to doubles to be compared,
     422                 :            :      * and op to the richcomp to use.
     423                 :            :      */
     424         [ +  + ]:       5424 :     if (PyFloat_Check(w))
     425                 :       5256 :         j = PyFloat_AS_DOUBLE(w);
     426                 :            : 
     427         [ +  + ]:        168 :     else if (!Py_IS_FINITE(i)) {
     428         [ +  - ]:          2 :         if (PyLong_Check(w))
     429                 :            :             /* If i is an infinity, its magnitude exceeds any
     430                 :            :              * finite integer, so it doesn't matter which int we
     431                 :            :              * compare i with.  If i is a NaN, similarly.
     432                 :            :              */
     433                 :          2 :             j = 0.0;
     434                 :            :         else
     435                 :          0 :             goto Unimplemented;
     436                 :            :     }
     437                 :            : 
     438         [ +  + ]:        166 :     else if (PyLong_Check(w)) {
     439   [ +  +  +  + ]:        163 :         int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1;
     440                 :        163 :         int wsign = _PyLong_Sign(w);
     441                 :            :         size_t nbits;
     442                 :            :         int exponent;
     443                 :            : 
     444         [ +  + ]:        163 :         if (vsign != wsign) {
     445                 :            :             /* Magnitudes are irrelevant -- the signs alone
     446                 :            :              * determine the outcome.
     447                 :            :              */
     448                 :         89 :             i = (double)vsign;
     449                 :         89 :             j = (double)wsign;
     450                 :        163 :             goto Compare;
     451                 :            :         }
     452                 :            :         /* The signs are the same. */
     453                 :            :         /* Convert w to a double if it fits.  In particular, 0 fits. */
     454                 :         74 :         nbits = _PyLong_NumBits(w);
     455   [ -  +  -  - ]:         74 :         if (nbits == (size_t)-1 && PyErr_Occurred()) {
     456                 :            :             /* This long is so large that size_t isn't big enough
     457                 :            :              * to hold the # of bits.  Replace with little doubles
     458                 :            :              * that give the same outcome -- w is so large that
     459                 :            :              * its magnitude must exceed the magnitude of any
     460                 :            :              * finite float.
     461                 :            :              */
     462                 :          0 :             PyErr_Clear();
     463                 :          0 :             i = (double)vsign;
     464                 :            :             assert(wsign != 0);
     465                 :          0 :             j = wsign * 2.0;
     466                 :          0 :             goto Compare;
     467                 :            :         }
     468         [ +  - ]:         74 :         if (nbits <= 48) {
     469                 :         74 :             j = PyLong_AsDouble(w);
     470                 :            :             /* It's impossible that <= 48 bits overflowed. */
     471                 :            :             assert(j != -1.0 || ! PyErr_Occurred());
     472                 :         74 :             goto Compare;
     473                 :            :         }
     474                 :            :         assert(wsign != 0); /* else nbits was 0 */
     475                 :            :         assert(vsign != 0); /* if vsign were 0, then since wsign is
     476                 :            :                              * not 0, we would have taken the
     477                 :            :                              * vsign != wsign branch at the start */
     478                 :            :         /* We want to work with non-negative numbers. */
     479         [ #  # ]:          0 :         if (vsign < 0) {
     480                 :            :             /* "Multiply both sides" by -1; this also swaps the
     481                 :            :              * comparator.
     482                 :            :              */
     483                 :          0 :             i = -i;
     484                 :          0 :             op = _Py_SwappedOp[op];
     485                 :            :         }
     486                 :            :         assert(i > 0.0);
     487                 :          0 :         (void) frexp(i, &exponent);
     488                 :            :         /* exponent is the # of bits in v before the radix point;
     489                 :            :          * we know that nbits (the # of bits in w) > 48 at this point
     490                 :            :          */
     491   [ #  #  #  # ]:          0 :         if (exponent < 0 || (size_t)exponent < nbits) {
     492                 :          0 :             i = 1.0;
     493                 :          0 :             j = 2.0;
     494                 :          0 :             goto Compare;
     495                 :            :         }
     496         [ #  # ]:          0 :         if ((size_t)exponent > nbits) {
     497                 :          0 :             i = 2.0;
     498                 :          0 :             j = 1.0;
     499                 :          0 :             goto Compare;
     500                 :            :         }
     501                 :            :         /* v and w have the same number of bits before the radix
     502                 :            :          * point.  Construct two ints that have the same comparison
     503                 :            :          * outcome.
     504                 :            :          */
     505                 :            :         {
     506                 :            :             double fracpart;
     507                 :            :             double intpart;
     508                 :          0 :             PyObject *result = NULL;
     509                 :          0 :             PyObject *vv = NULL;
     510                 :          0 :             PyObject *ww = w;
     511                 :            : 
     512         [ #  # ]:          0 :             if (wsign < 0) {
     513                 :          0 :                 ww = PyNumber_Negative(w);
     514         [ #  # ]:          0 :                 if (ww == NULL)
     515                 :          0 :                     goto Error;
     516                 :            :             }
     517                 :            :             else
     518                 :          0 :                 Py_INCREF(ww);
     519                 :            : 
     520                 :          0 :             fracpart = modf(i, &intpart);
     521                 :          0 :             vv = PyLong_FromDouble(intpart);
     522         [ #  # ]:          0 :             if (vv == NULL)
     523                 :          0 :                 goto Error;
     524                 :            : 
     525         [ #  # ]:          0 :             if (fracpart != 0.0) {
     526                 :            :                 /* Shift left, and or a 1 bit into vv
     527                 :            :                  * to represent the lost fraction.
     528                 :            :                  */
     529                 :            :                 PyObject *temp;
     530                 :            : 
     531                 :          0 :                 temp = _PyLong_Lshift(ww, 1);
     532         [ #  # ]:          0 :                 if (temp == NULL)
     533                 :          0 :                     goto Error;
     534                 :          0 :                 Py_SETREF(ww, temp);
     535                 :            : 
     536                 :          0 :                 temp = _PyLong_Lshift(vv, 1);
     537         [ #  # ]:          0 :                 if (temp == NULL)
     538                 :          0 :                     goto Error;
     539                 :          0 :                 Py_SETREF(vv, temp);
     540                 :            : 
     541                 :          0 :                 temp = PyNumber_Or(vv, _PyLong_GetOne());
     542         [ #  # ]:          0 :                 if (temp == NULL)
     543                 :          0 :                     goto Error;
     544                 :          0 :                 Py_SETREF(vv, temp);
     545                 :            :             }
     546                 :            : 
     547                 :          0 :             r = PyObject_RichCompareBool(vv, ww, op);
     548         [ #  # ]:          0 :             if (r < 0)
     549                 :          0 :                 goto Error;
     550                 :          0 :             result = PyBool_FromLong(r);
     551                 :          0 :          Error:
     552                 :          0 :             Py_XDECREF(vv);
     553                 :          0 :             Py_XDECREF(ww);
     554                 :          0 :             return result;
     555                 :            :         }
     556                 :            :     } /* else if (PyLong_Check(w)) */
     557                 :            : 
     558                 :            :     else        /* w isn't float or int */
     559                 :          3 :         goto Unimplemented;
     560                 :            : 
     561                 :       5421 :  Compare:
     562   [ +  +  +  +  :       5421 :     switch (op) {
                +  +  - ]
     563                 :       5273 :     case Py_EQ:
     564                 :       5273 :         r = i == j;
     565                 :       5273 :         break;
     566                 :        118 :     case Py_NE:
     567                 :        118 :         r = i != j;
     568                 :        118 :         break;
     569                 :          3 :     case Py_LE:
     570                 :          3 :         r = i <= j;
     571                 :          3 :         break;
     572                 :          3 :     case Py_GE:
     573                 :          3 :         r = i >= j;
     574                 :          3 :         break;
     575                 :          7 :     case Py_LT:
     576                 :          7 :         r = i < j;
     577                 :          7 :         break;
     578                 :         17 :     case Py_GT:
     579                 :         17 :         r = i > j;
     580                 :         17 :         break;
     581                 :            :     }
     582                 :       5421 :     return PyBool_FromLong(r);
     583                 :            : 
     584                 :          3 :  Unimplemented:
     585                 :          3 :     Py_RETURN_NOTIMPLEMENTED;
     586                 :            : }
     587                 :            : 
     588                 :            : static Py_hash_t
     589                 :      10010 : float_hash(PyFloatObject *v)
     590                 :            : {
     591                 :      10010 :     return _Py_HashDouble((PyObject *)v, v->ob_fval);
     592                 :            : }
     593                 :            : 
     594                 :            : static PyObject *
     595                 :     201473 : float_add(PyObject *v, PyObject *w)
     596                 :            : {
     597                 :            :     double a,b;
     598   [ +  +  -  + ]:     201473 :     CONVERT_TO_DOUBLE(v, a);
     599   [ +  +  +  + ]:     201473 :     CONVERT_TO_DOUBLE(w, b);
     600                 :     201470 :     a = a + b;
     601                 :     201470 :     return PyFloat_FromDouble(a);
     602                 :            : }
     603                 :            : 
     604                 :            : static PyObject *
     605                 :       1032 : float_sub(PyObject *v, PyObject *w)
     606                 :            : {
     607                 :            :     double a,b;
     608   [ +  +  -  + ]:       1032 :     CONVERT_TO_DOUBLE(v, a);
     609   [ +  +  -  + ]:       1032 :     CONVERT_TO_DOUBLE(w, b);
     610                 :       1032 :     a = a - b;
     611                 :       1032 :     return PyFloat_FromDouble(a);
     612                 :            : }
     613                 :            : 
     614                 :            : static PyObject *
     615                 :       5666 : float_mul(PyObject *v, PyObject *w)
     616                 :            : {
     617                 :            :     double a,b;
     618   [ +  +  -  + ]:       5666 :     CONVERT_TO_DOUBLE(v, a);
     619   [ +  +  +  + ]:       5666 :     CONVERT_TO_DOUBLE(w, b);
     620                 :       5664 :     a = a * b;
     621                 :       5664 :     return PyFloat_FromDouble(a);
     622                 :            : }
     623                 :            : 
     624                 :            : static PyObject *
     625                 :       2116 : float_div(PyObject *v, PyObject *w)
     626                 :            : {
     627                 :            :     double a,b;
     628   [ +  +  -  + ]:       2116 :     CONVERT_TO_DOUBLE(v, a);
     629   [ +  +  -  + ]:       2116 :     CONVERT_TO_DOUBLE(w, b);
     630         [ -  + ]:       2116 :     if (b == 0.0) {
     631                 :          0 :         PyErr_SetString(PyExc_ZeroDivisionError,
     632                 :            :                         "float division by zero");
     633                 :          0 :         return NULL;
     634                 :            :     }
     635                 :       2116 :     a = a / b;
     636                 :       2116 :     return PyFloat_FromDouble(a);
     637                 :            : }
     638                 :            : 
     639                 :            : static PyObject *
     640                 :          0 : float_rem(PyObject *v, PyObject *w)
     641                 :            : {
     642                 :            :     double vx, wx;
     643                 :            :     double mod;
     644   [ #  #  #  # ]:          0 :     CONVERT_TO_DOUBLE(v, vx);
     645   [ #  #  #  # ]:          0 :     CONVERT_TO_DOUBLE(w, wx);
     646         [ #  # ]:          0 :     if (wx == 0.0) {
     647                 :          0 :         PyErr_SetString(PyExc_ZeroDivisionError,
     648                 :            :                         "float modulo");
     649                 :          0 :         return NULL;
     650                 :            :     }
     651                 :          0 :     mod = fmod(vx, wx);
     652         [ #  # ]:          0 :     if (mod) {
     653                 :            :         /* ensure the remainder has the same sign as the denominator */
     654         [ #  # ]:          0 :         if ((wx < 0) != (mod < 0)) {
     655                 :          0 :             mod += wx;
     656                 :            :         }
     657                 :            :     }
     658                 :            :     else {
     659                 :            :         /* the remainder is zero, and in the presence of signed zeroes
     660                 :            :            fmod returns different results across platforms; ensure
     661                 :            :            it has the same sign as the denominator. */
     662                 :          0 :         mod = copysign(0.0, wx);
     663                 :            :     }
     664                 :          0 :     return PyFloat_FromDouble(mod);
     665                 :            : }
     666                 :            : 
     667                 :            : static void
     668                 :          0 : _float_div_mod(double vx, double wx, double *floordiv, double *mod)
     669                 :            : {
     670                 :            :     double div;
     671                 :          0 :     *mod = fmod(vx, wx);
     672                 :            :     /* fmod is typically exact, so vx-mod is *mathematically* an
     673                 :            :        exact multiple of wx.  But this is fp arithmetic, and fp
     674                 :            :        vx - mod is an approximation; the result is that div may
     675                 :            :        not be an exact integral value after the division, although
     676                 :            :        it will always be very close to one.
     677                 :            :     */
     678                 :          0 :     div = (vx - *mod) / wx;
     679         [ #  # ]:          0 :     if (*mod) {
     680                 :            :         /* ensure the remainder has the same sign as the denominator */
     681         [ #  # ]:          0 :         if ((wx < 0) != (*mod < 0)) {
     682                 :          0 :             *mod += wx;
     683                 :          0 :             div -= 1.0;
     684                 :            :         }
     685                 :            :     }
     686                 :            :     else {
     687                 :            :         /* the remainder is zero, and in the presence of signed zeroes
     688                 :            :            fmod returns different results across platforms; ensure
     689                 :            :            it has the same sign as the denominator. */
     690                 :          0 :         *mod = copysign(0.0, wx);
     691                 :            :     }
     692                 :            :     /* snap quotient to nearest integral value */
     693         [ #  # ]:          0 :     if (div) {
     694                 :          0 :         *floordiv = floor(div);
     695         [ #  # ]:          0 :         if (div - *floordiv > 0.5) {
     696                 :          0 :             *floordiv += 1.0;
     697                 :            :         }
     698                 :            :     }
     699                 :            :     else {
     700                 :            :         /* div is zero - get the same sign as the true quotient */
     701                 :          0 :         *floordiv = copysign(0.0, vx / wx); /* zero w/ sign of vx/wx */
     702                 :            :     }
     703                 :          0 : }
     704                 :            : 
     705                 :            : static PyObject *
     706                 :          0 : float_divmod(PyObject *v, PyObject *w)
     707                 :            : {
     708                 :            :     double vx, wx;
     709                 :            :     double mod, floordiv;
     710   [ #  #  #  # ]:          0 :     CONVERT_TO_DOUBLE(v, vx);
     711   [ #  #  #  # ]:          0 :     CONVERT_TO_DOUBLE(w, wx);
     712         [ #  # ]:          0 :     if (wx == 0.0) {
     713                 :          0 :         PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
     714                 :          0 :         return NULL;
     715                 :            :     }
     716                 :          0 :     _float_div_mod(vx, wx, &floordiv, &mod);
     717                 :          0 :     return Py_BuildValue("(dd)", floordiv, mod);
     718                 :            : }
     719                 :            : 
     720                 :            : static PyObject *
     721                 :          0 : float_floor_div(PyObject *v, PyObject *w)
     722                 :            : {
     723                 :            :     double vx, wx;
     724                 :            :     double mod, floordiv;
     725   [ #  #  #  # ]:          0 :     CONVERT_TO_DOUBLE(v, vx);
     726   [ #  #  #  # ]:          0 :     CONVERT_TO_DOUBLE(w, wx);
     727         [ #  # ]:          0 :     if (wx == 0.0) {
     728                 :          0 :         PyErr_SetString(PyExc_ZeroDivisionError, "float floor division by zero");
     729                 :          0 :         return NULL;
     730                 :            :     }
     731                 :          0 :     _float_div_mod(vx, wx, &floordiv, &mod);
     732                 :          0 :     return PyFloat_FromDouble(floordiv);
     733                 :            : }
     734                 :            : 
     735                 :            : /* determine whether x is an odd integer or not;  assumes that
     736                 :            :    x is not an infinity or nan. */
     737                 :            : #define DOUBLE_IS_ODD_INTEGER(x) (fmod(fabs(x), 2.0) == 1.0)
     738                 :            : 
     739                 :            : static PyObject *
     740                 :     205360 : float_pow(PyObject *v, PyObject *w, PyObject *z)
     741                 :            : {
     742                 :            :     double iv, iw, ix;
     743                 :     205360 :     int negate_result = 0;
     744                 :            : 
     745         [ -  + ]:     205360 :     if ((PyObject *)z != Py_None) {
     746                 :          0 :         PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
     747                 :            :             "allowed unless all arguments are integers");
     748                 :          0 :         return NULL;
     749                 :            :     }
     750                 :            : 
     751   [ +  +  -  + ]:     205360 :     CONVERT_TO_DOUBLE(v, iv);
     752   [ +  +  -  + ]:     205360 :     CONVERT_TO_DOUBLE(w, iw);
     753                 :            : 
     754                 :            :     /* Sort out special cases here instead of relying on pow() */
     755         [ +  + ]:     205360 :     if (iw == 0) {              /* v**0 is 1, even 0**0 */
     756                 :          7 :         return PyFloat_FromDouble(1.0);
     757                 :            :     }
     758         [ -  + ]:     205353 :     if (Py_IS_NAN(iv)) {        /* nan**w = nan, unless w == 0 */
     759                 :          0 :         return PyFloat_FromDouble(iv);
     760                 :            :     }
     761         [ +  + ]:     205353 :     if (Py_IS_NAN(iw)) {        /* v**nan = nan, unless v == 1; 1**nan = 1 */
     762         [ +  + ]:          2 :         return PyFloat_FromDouble(iv == 1.0 ? 1.0 : iw);
     763                 :            :     }
     764   [ -  +  -  + ]:     205351 :     if (Py_IS_INFINITY(iw)) {
     765                 :            :         /* v**inf is: 0.0 if abs(v) < 1; 1.0 if abs(v) == 1; inf if
     766                 :            :          *     abs(v) > 1 (including case where v infinite)
     767                 :            :          *
     768                 :            :          * v**-inf is: inf if abs(v) < 1; 1.0 if abs(v) == 1; 0.0 if
     769                 :            :          *     abs(v) > 1 (including case where v infinite)
     770                 :            :          */
     771                 :          0 :         iv = fabs(iv);
     772         [ #  # ]:          0 :         if (iv == 1.0)
     773                 :          0 :             return PyFloat_FromDouble(1.0);
     774         [ #  # ]:          0 :         else if ((iw > 0.0) == (iv > 1.0))
     775                 :          0 :             return PyFloat_FromDouble(fabs(iw)); /* return inf */
     776                 :            :         else
     777                 :          0 :             return PyFloat_FromDouble(0.0);
     778                 :            :     }
     779   [ -  +  -  + ]:     205351 :     if (Py_IS_INFINITY(iv)) {
     780                 :            :         /* (+-inf)**w is: inf for w positive, 0 for w negative; in
     781                 :            :          *     both cases, we need to add the appropriate sign if w is
     782                 :            :          *     an odd integer.
     783                 :            :          */
     784                 :          0 :         int iw_is_odd = DOUBLE_IS_ODD_INTEGER(iw);
     785         [ #  # ]:          0 :         if (iw > 0.0)
     786         [ #  # ]:          0 :             return PyFloat_FromDouble(iw_is_odd ? iv : fabs(iv));
     787                 :            :         else
     788         [ #  # ]:          0 :             return PyFloat_FromDouble(iw_is_odd ?
     789                 :          0 :                                       copysign(0.0, iv) : 0.0);
     790                 :            :     }
     791         [ +  + ]:     205351 :     if (iv == 0.0) {  /* 0**w is: 0 for w positive, 1 for w zero
     792                 :            :                          (already dealt with above), and an error
     793                 :            :                          if w is negative. */
     794                 :          2 :         int iw_is_odd = DOUBLE_IS_ODD_INTEGER(iw);
     795         [ +  - ]:          2 :         if (iw < 0.0) {
     796                 :          2 :             PyErr_SetString(PyExc_ZeroDivisionError,
     797                 :            :                             "0.0 cannot be raised to a "
     798                 :            :                             "negative power");
     799                 :          2 :             return NULL;
     800                 :            :         }
     801                 :            :         /* use correct sign if iw is odd */
     802         [ #  # ]:          0 :         return PyFloat_FromDouble(iw_is_odd ? iv : 0.0);
     803                 :            :     }
     804                 :            : 
     805         [ +  + ]:     205349 :     if (iv < 0.0) {
     806                 :            :         /* Whether this is an error is a mess, and bumps into libm
     807                 :            :          * bugs so we have to figure it out ourselves.
     808                 :            :          */
     809         [ -  + ]:     100793 :         if (iw != floor(iw)) {
     810                 :            :             /* Negative numbers raised to fractional powers
     811                 :            :              * become complex.
     812                 :            :              */
     813                 :          0 :             return PyComplex_Type.tp_as_number->nb_power(v, w, z);
     814                 :            :         }
     815                 :            :         /* iw is an exact integer, albeit perhaps a very large
     816                 :            :          * one.  Replace iv by its absolute value and remember
     817                 :            :          * to negate the pow result if iw is odd.
     818                 :            :          */
     819                 :     100793 :         iv = -iv;
     820                 :     100793 :         negate_result = DOUBLE_IS_ODD_INTEGER(iw);
     821                 :            :     }
     822                 :            : 
     823         [ +  + ]:     205349 :     if (iv == 1.0) { /* 1**w is 1, even 1**inf and 1**nan */
     824                 :            :         /* (-1) ** large_integer also ends up here.  Here's an
     825                 :            :          * extract from the comments for the previous
     826                 :            :          * implementation explaining why this special case is
     827                 :            :          * necessary:
     828                 :            :          *
     829                 :            :          * -1 raised to an exact integer should never be exceptional.
     830                 :            :          * Alas, some libms (chiefly glibc as of early 2003) return
     831                 :            :          * NaN and set EDOM on pow(-1, large_int) if the int doesn't
     832                 :            :          * happen to be representable in a *C* integer.  That's a
     833                 :            :          * bug.
     834                 :            :          */
     835         [ +  + ]:       1000 :         return PyFloat_FromDouble(negate_result ? -1.0 : 1.0);
     836                 :            :     }
     837                 :            : 
     838                 :            :     /* Now iv and iw are finite, iw is nonzero, and iv is
     839                 :            :      * positive and not equal to 1.0.  We finally allow
     840                 :            :      * the platform pow to step in and do the rest.
     841                 :            :      */
     842                 :     204349 :     errno = 0;
     843                 :     204349 :     ix = pow(iv, iw);
     844                 :     204349 :     _Py_ADJUST_ERANGE1(ix);
     845         [ +  + ]:     204349 :     if (negate_result)
     846                 :      99709 :         ix = -ix;
     847                 :            : 
     848         [ -  + ]:     204349 :     if (errno != 0) {
     849                 :            :         /* We don't expect any errno value other than ERANGE, but
     850                 :            :          * the range of libm bugs appears unbounded.
     851                 :            :          */
     852         [ #  # ]:          0 :         PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
     853                 :            :                              PyExc_ValueError);
     854                 :          0 :         return NULL;
     855                 :            :     }
     856                 :     204349 :     return PyFloat_FromDouble(ix);
     857                 :            : }
     858                 :            : 
     859                 :            : #undef DOUBLE_IS_ODD_INTEGER
     860                 :            : 
     861                 :            : static PyObject *
     862                 :      10106 : float_neg(PyFloatObject *v)
     863                 :            : {
     864                 :      10106 :     return PyFloat_FromDouble(-v->ob_fval);
     865                 :            : }
     866                 :            : 
     867                 :            : static PyObject *
     868                 :        241 : float_abs(PyFloatObject *v)
     869                 :            : {
     870                 :        241 :     return PyFloat_FromDouble(fabs(v->ob_fval));
     871                 :            : }
     872                 :            : 
     873                 :            : static int
     874                 :          2 : float_bool(PyFloatObject *v)
     875                 :            : {
     876                 :          2 :     return v->ob_fval != 0.0;
     877                 :            : }
     878                 :            : 
     879                 :            : /*[clinic input]
     880                 :            : float.is_integer
     881                 :            : 
     882                 :            : Return True if the float is an integer.
     883                 :            : [clinic start generated code]*/
     884                 :            : 
     885                 :            : static PyObject *
     886                 :          0 : float_is_integer_impl(PyObject *self)
     887                 :            : /*[clinic end generated code: output=7112acf95a4d31ea input=311810d3f777e10d]*/
     888                 :            : {
     889                 :          0 :     double x = PyFloat_AsDouble(self);
     890                 :            :     PyObject *o;
     891                 :            : 
     892   [ #  #  #  # ]:          0 :     if (x == -1.0 && PyErr_Occurred())
     893                 :          0 :         return NULL;
     894         [ #  # ]:          0 :     if (!Py_IS_FINITE(x))
     895                 :          0 :         Py_RETURN_FALSE;
     896                 :          0 :     errno = 0;
     897         [ #  # ]:          0 :     o = (floor(x) == x) ? Py_True : Py_False;
     898         [ #  # ]:          0 :     if (errno != 0) {
     899         [ #  # ]:          0 :         PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
     900                 :            :                              PyExc_ValueError);
     901                 :          0 :         return NULL;
     902                 :            :     }
     903                 :          0 :     return Py_NewRef(o);
     904                 :            : }
     905                 :            : 
     906                 :            : /*[clinic input]
     907                 :            : float.__trunc__
     908                 :            : 
     909                 :            : Return the Integral closest to x between 0 and x.
     910                 :            : [clinic start generated code]*/
     911                 :            : 
     912                 :            : static PyObject *
     913                 :     520554 : float___trunc___impl(PyObject *self)
     914                 :            : /*[clinic end generated code: output=dd3e289dd4c6b538 input=591b9ba0d650fdff]*/
     915                 :            : {
     916                 :     520554 :     return PyLong_FromDouble(PyFloat_AS_DOUBLE(self));
     917                 :            : }
     918                 :            : 
     919                 :            : /*[clinic input]
     920                 :            : float.__floor__
     921                 :            : 
     922                 :            : Return the floor as an Integral.
     923                 :            : [clinic start generated code]*/
     924                 :            : 
     925                 :            : static PyObject *
     926                 :          0 : float___floor___impl(PyObject *self)
     927                 :            : /*[clinic end generated code: output=e0551dbaea8c01d1 input=77bb13eb12e268df]*/
     928                 :            : {
     929                 :          0 :     double x = PyFloat_AS_DOUBLE(self);
     930                 :          0 :     return PyLong_FromDouble(floor(x));
     931                 :            : }
     932                 :            : 
     933                 :            : /*[clinic input]
     934                 :            : float.__ceil__
     935                 :            : 
     936                 :            : Return the ceiling as an Integral.
     937                 :            : [clinic start generated code]*/
     938                 :            : 
     939                 :            : static PyObject *
     940                 :          0 : float___ceil___impl(PyObject *self)
     941                 :            : /*[clinic end generated code: output=a2fd8858f73736f9 input=79e41ae94aa0a516]*/
     942                 :            : {
     943                 :          0 :     double x = PyFloat_AS_DOUBLE(self);
     944                 :          0 :     return PyLong_FromDouble(ceil(x));
     945                 :            : }
     946                 :            : 
     947                 :            : /* double_round: rounds a finite double to the closest multiple of
     948                 :            :    10**-ndigits; here ndigits is within reasonable bounds (typically, -308 <=
     949                 :            :    ndigits <= 323).  Returns a Python float, or sets a Python error and
     950                 :            :    returns NULL on failure (OverflowError and memory errors are possible). */
     951                 :            : 
     952                 :            : #if _PY_SHORT_FLOAT_REPR == 1
     953                 :            : /* version of double_round that uses the correctly-rounded string<->double
     954                 :            :    conversions from Python/dtoa.c */
     955                 :            : 
     956                 :            : static PyObject *
     957                 :          6 : double_round(double x, int ndigits) {
     958                 :            : 
     959                 :            :     double rounded;
     960                 :          6 :     Py_ssize_t buflen, mybuflen=100;
     961                 :          6 :     char *buf, *buf_end, shortbuf[100], *mybuf=shortbuf;
     962                 :            :     int decpt, sign;
     963                 :          6 :     PyObject *result = NULL;
     964                 :            :     _Py_SET_53BIT_PRECISION_HEADER;
     965                 :            : 
     966                 :            :     /* round to a decimal string */
     967         [ +  - ]:          6 :     _Py_SET_53BIT_PRECISION_START;
     968                 :          6 :     buf = _Py_dg_dtoa(x, 3, ndigits, &decpt, &sign, &buf_end);
     969         [ +  - ]:          6 :     _Py_SET_53BIT_PRECISION_END;
     970         [ -  + ]:          6 :     if (buf == NULL) {
     971                 :          0 :         PyErr_NoMemory();
     972                 :          0 :         return NULL;
     973                 :            :     }
     974                 :            : 
     975                 :            :     /* Get new buffer if shortbuf is too small.  Space needed <= buf_end -
     976                 :            :     buf + 8: (1 extra for '0', 1 for sign, 5 for exp, 1 for '\0').  */
     977                 :          6 :     buflen = buf_end - buf;
     978         [ -  + ]:          6 :     if (buflen + 8 > mybuflen) {
     979                 :          0 :         mybuflen = buflen+8;
     980                 :          0 :         mybuf = (char *)PyMem_Malloc(mybuflen);
     981         [ #  # ]:          0 :         if (mybuf == NULL) {
     982                 :          0 :             PyErr_NoMemory();
     983                 :          0 :             goto exit;
     984                 :            :         }
     985                 :            :     }
     986                 :            :     /* copy buf to mybuf, adding exponent, sign and leading 0 */
     987                 :          6 :     PyOS_snprintf(mybuf, mybuflen, "%s0%se%d", (sign ? "-" : ""),
     988         [ -  + ]:          6 :                   buf, decpt - (int)buflen);
     989                 :            : 
     990                 :            :     /* and convert the resulting string back to a double */
     991                 :          6 :     errno = 0;
     992         [ +  - ]:          6 :     _Py_SET_53BIT_PRECISION_START;
     993                 :          6 :     rounded = _Py_dg_strtod(mybuf, NULL);
     994         [ +  - ]:          6 :     _Py_SET_53BIT_PRECISION_END;
     995   [ -  +  -  - ]:          6 :     if (errno == ERANGE && fabs(rounded) >= 1.)
     996                 :          0 :         PyErr_SetString(PyExc_OverflowError,
     997                 :            :                         "rounded value too large to represent");
     998                 :            :     else
     999                 :          6 :         result = PyFloat_FromDouble(rounded);
    1000                 :            : 
    1001                 :            :     /* done computing value;  now clean up */
    1002         [ +  - ]:          6 :     if (mybuf != shortbuf)
    1003                 :          0 :         PyMem_Free(mybuf);
    1004                 :          6 :   exit:
    1005                 :          6 :     _Py_dg_freedtoa(buf);
    1006                 :          6 :     return result;
    1007                 :            : }
    1008                 :            : 
    1009                 :            : #else  // _PY_SHORT_FLOAT_REPR == 0
    1010                 :            : 
    1011                 :            : /* fallback version, to be used when correctly rounded binary<->decimal
    1012                 :            :    conversions aren't available */
    1013                 :            : 
    1014                 :            : static PyObject *
    1015                 :            : double_round(double x, int ndigits) {
    1016                 :            :     double pow1, pow2, y, z;
    1017                 :            :     if (ndigits >= 0) {
    1018                 :            :         if (ndigits > 22) {
    1019                 :            :             /* pow1 and pow2 are each safe from overflow, but
    1020                 :            :                pow1*pow2 ~= pow(10.0, ndigits) might overflow */
    1021                 :            :             pow1 = pow(10.0, (double)(ndigits-22));
    1022                 :            :             pow2 = 1e22;
    1023                 :            :         }
    1024                 :            :         else {
    1025                 :            :             pow1 = pow(10.0, (double)ndigits);
    1026                 :            :             pow2 = 1.0;
    1027                 :            :         }
    1028                 :            :         y = (x*pow1)*pow2;
    1029                 :            :         /* if y overflows, then rounded value is exactly x */
    1030                 :            :         if (!Py_IS_FINITE(y))
    1031                 :            :             return PyFloat_FromDouble(x);
    1032                 :            :     }
    1033                 :            :     else {
    1034                 :            :         pow1 = pow(10.0, (double)-ndigits);
    1035                 :            :         pow2 = 1.0; /* unused; silences a gcc compiler warning */
    1036                 :            :         y = x / pow1;
    1037                 :            :     }
    1038                 :            : 
    1039                 :            :     z = round(y);
    1040                 :            :     if (fabs(y-z) == 0.5)
    1041                 :            :         /* halfway between two integers; use round-half-even */
    1042                 :            :         z = 2.0*round(y/2.0);
    1043                 :            : 
    1044                 :            :     if (ndigits >= 0)
    1045                 :            :         z = (z / pow2) / pow1;
    1046                 :            :     else
    1047                 :            :         z *= pow1;
    1048                 :            : 
    1049                 :            :     /* if computation resulted in overflow, raise OverflowError */
    1050                 :            :     if (!Py_IS_FINITE(z)) {
    1051                 :            :         PyErr_SetString(PyExc_OverflowError,
    1052                 :            :                         "overflow occurred during round");
    1053                 :            :         return NULL;
    1054                 :            :     }
    1055                 :            : 
    1056                 :            :     return PyFloat_FromDouble(z);
    1057                 :            : }
    1058                 :            : 
    1059                 :            : #endif  // _PY_SHORT_FLOAT_REPR == 0
    1060                 :            : 
    1061                 :            : /* round a Python float v to the closest multiple of 10**-ndigits */
    1062                 :            : 
    1063                 :            : /*[clinic input]
    1064                 :            : float.__round__
    1065                 :            : 
    1066                 :            :     ndigits as o_ndigits: object = None
    1067                 :            :     /
    1068                 :            : 
    1069                 :            : Return the Integral closest to x, rounding half toward even.
    1070                 :            : 
    1071                 :            : When an argument is passed, work like built-in round(x, ndigits).
    1072                 :            : [clinic start generated code]*/
    1073                 :            : 
    1074                 :            : static PyObject *
    1075                 :         18 : float___round___impl(PyObject *self, PyObject *o_ndigits)
    1076                 :            : /*[clinic end generated code: output=374c36aaa0f13980 input=fc0fe25924fbc9ed]*/
    1077                 :            : {
    1078                 :            :     double x, rounded;
    1079                 :            :     Py_ssize_t ndigits;
    1080                 :            : 
    1081                 :         18 :     x = PyFloat_AsDouble(self);
    1082         [ +  + ]:         18 :     if (o_ndigits == Py_None) {
    1083                 :            :         /* single-argument round or with None ndigits:
    1084                 :            :          * round to nearest integer */
    1085                 :         12 :         rounded = round(x);
    1086         [ -  + ]:         12 :         if (fabs(x-rounded) == 0.5)
    1087                 :            :             /* halfway case: round to even */
    1088                 :          0 :             rounded = 2.0*round(x/2.0);
    1089                 :         12 :         return PyLong_FromDouble(rounded);
    1090                 :            :     }
    1091                 :            : 
    1092                 :            :     /* interpret second argument as a Py_ssize_t; clips on overflow */
    1093                 :          6 :     ndigits = PyNumber_AsSsize_t(o_ndigits, NULL);
    1094   [ -  +  -  - ]:          6 :     if (ndigits == -1 && PyErr_Occurred())
    1095                 :          0 :         return NULL;
    1096                 :            : 
    1097                 :            :     /* nans and infinities round to themselves */
    1098         [ -  + ]:          6 :     if (!Py_IS_FINITE(x))
    1099                 :          0 :         return PyFloat_FromDouble(x);
    1100                 :            : 
    1101                 :            :     /* Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x
    1102                 :            :        always rounds to itself.  For ndigits < NDIGITS_MIN, x always
    1103                 :            :        rounds to +-0.0.  Here 0.30103 is an upper bound for log10(2). */
    1104                 :            : #define NDIGITS_MAX ((int)((DBL_MANT_DIG-DBL_MIN_EXP) * 0.30103))
    1105                 :            : #define NDIGITS_MIN (-(int)((DBL_MAX_EXP + 1) * 0.30103))
    1106         [ -  + ]:          6 :     if (ndigits > NDIGITS_MAX)
    1107                 :            :         /* return x */
    1108                 :          0 :         return PyFloat_FromDouble(x);
    1109         [ -  + ]:          6 :     else if (ndigits < NDIGITS_MIN)
    1110                 :            :         /* return 0.0, but with sign of x */
    1111                 :          0 :         return PyFloat_FromDouble(0.0*x);
    1112                 :            :     else
    1113                 :            :         /* finite x, and ndigits is not unreasonably large */
    1114                 :          6 :         return double_round(x, (int)ndigits);
    1115                 :            : #undef NDIGITS_MAX
    1116                 :            : #undef NDIGITS_MIN
    1117                 :            : }
    1118                 :            : 
    1119                 :            : static PyObject *
    1120                 :         11 : float_float(PyObject *v)
    1121                 :            : {
    1122         [ +  - ]:         11 :     if (PyFloat_CheckExact(v)) {
    1123                 :         11 :         return Py_NewRef(v);
    1124                 :            :     }
    1125                 :            :     else {
    1126                 :          0 :         return PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);
    1127                 :            :     }
    1128                 :            : }
    1129                 :            : 
    1130                 :            : /*[clinic input]
    1131                 :            : float.conjugate
    1132                 :            : 
    1133                 :            : Return self, the complex conjugate of any float.
    1134                 :            : [clinic start generated code]*/
    1135                 :            : 
    1136                 :            : static PyObject *
    1137                 :          0 : float_conjugate_impl(PyObject *self)
    1138                 :            : /*[clinic end generated code: output=8ca292c2479194af input=82ba6f37a9ff91dd]*/
    1139                 :            : {
    1140                 :          0 :     return float_float(self);
    1141                 :            : }
    1142                 :            : 
    1143                 :            : /* turn ASCII hex characters into integer values and vice versa */
    1144                 :            : 
    1145                 :            : static char
    1146                 :        980 : char_from_hex(int x)
    1147                 :            : {
    1148                 :            :     assert(0 <= x && x < 16);
    1149                 :        980 :     return Py_hexdigits[x];
    1150                 :            : }
    1151                 :            : 
    1152                 :            : static int
    1153                 :       4405 : hex_from_char(char c) {
    1154                 :            :     int x;
    1155   [ +  +  +  +  :       4405 :     switch(c) {
          +  +  +  +  +  
          +  +  +  +  +  
                +  +  + ]
    1156                 :        366 :     case '0':
    1157                 :        366 :         x = 0;
    1158                 :        366 :         break;
    1159                 :        904 :     case '1':
    1160                 :        904 :         x = 1;
    1161                 :        904 :         break;
    1162                 :        304 :     case '2':
    1163                 :        304 :         x = 2;
    1164                 :        304 :         break;
    1165                 :        132 :     case '3':
    1166                 :        132 :         x = 3;
    1167                 :        132 :         break;
    1168                 :        440 :     case '4':
    1169                 :        440 :         x = 4;
    1170                 :        440 :         break;
    1171                 :        230 :     case '5':
    1172                 :        230 :         x = 5;
    1173                 :        230 :         break;
    1174                 :        108 :     case '6':
    1175                 :        108 :         x = 6;
    1176                 :        108 :         break;
    1177                 :        136 :     case '7':
    1178                 :        136 :         x = 7;
    1179                 :        136 :         break;
    1180                 :        202 :     case '8':
    1181                 :        202 :         x = 8;
    1182                 :        202 :         break;
    1183                 :        220 :     case '9':
    1184                 :        220 :         x = 9;
    1185                 :        220 :         break;
    1186                 :        106 :     case 'a':
    1187                 :            :     case 'A':
    1188                 :        106 :         x = 10;
    1189                 :        106 :         break;
    1190                 :        184 :     case 'b':
    1191                 :            :     case 'B':
    1192                 :        184 :         x = 11;
    1193                 :        184 :         break;
    1194                 :        146 :     case 'c':
    1195                 :            :     case 'C':
    1196                 :        146 :         x = 12;
    1197                 :        146 :         break;
    1198                 :        202 :     case 'd':
    1199                 :            :     case 'D':
    1200                 :        202 :         x = 13;
    1201                 :        202 :         break;
    1202                 :        116 :     case 'e':
    1203                 :            :     case 'E':
    1204                 :        116 :         x = 14;
    1205                 :        116 :         break;
    1206                 :        214 :     case 'f':
    1207                 :            :     case 'F':
    1208                 :        214 :         x = 15;
    1209                 :        214 :         break;
    1210                 :        395 :     default:
    1211                 :        395 :         x = -1;
    1212                 :        395 :         break;
    1213                 :            :     }
    1214                 :       4405 :     return x;
    1215                 :            : }
    1216                 :            : 
    1217                 :            : /* convert a float to a hexadecimal string */
    1218                 :            : 
    1219                 :            : /* TOHEX_NBITS is DBL_MANT_DIG rounded up to the next integer
    1220                 :            :    of the form 4k+1. */
    1221                 :            : #define TOHEX_NBITS DBL_MANT_DIG + 3 - (DBL_MANT_DIG+2)%4
    1222                 :            : 
    1223                 :            : /*[clinic input]
    1224                 :            : float.hex
    1225                 :            : 
    1226                 :            : Return a hexadecimal representation of a floating-point number.
    1227                 :            : 
    1228                 :            : >>> (-0.1).hex()
    1229                 :            : '-0x1.999999999999ap-4'
    1230                 :            : >>> 3.14159.hex()
    1231                 :            : '0x1.921f9f01b866ep+1'
    1232                 :            : [clinic start generated code]*/
    1233                 :            : 
    1234                 :            : static PyObject *
    1235                 :         96 : float_hex_impl(PyObject *self)
    1236                 :            : /*[clinic end generated code: output=0ebc9836e4d302d4 input=bec1271a33d47e67]*/
    1237                 :            : {
    1238                 :            :     double x, m;
    1239                 :            :     int e, shift, i, si, esign;
    1240                 :            :     /* Space for 1+(TOHEX_NBITS-1)/4 digits, a decimal point, and the
    1241                 :            :        trailing NUL byte. */
    1242                 :            :     char s[(TOHEX_NBITS-1)/4+3];
    1243                 :            : 
    1244   [ +  -  -  - ]:         96 :     CONVERT_TO_DOUBLE(self, x);
    1245                 :            : 
    1246   [ +  -  -  + ]:         96 :     if (Py_IS_NAN(x) || Py_IS_INFINITY(x))
    1247                 :          0 :         return float_repr((PyFloatObject *)self);
    1248                 :            : 
    1249         [ +  + ]:         96 :     if (x == 0.0) {
    1250         [ +  + ]:         26 :         if (copysign(1.0, x) == -1.0)
    1251                 :         10 :             return PyUnicode_FromString("-0x0.0p+0");
    1252                 :            :         else
    1253                 :         16 :             return PyUnicode_FromString("0x0.0p+0");
    1254                 :            :     }
    1255                 :            : 
    1256                 :         70 :     m = frexp(fabs(x), &e);
    1257                 :         70 :     shift = 1 - Py_MAX(DBL_MIN_EXP - e, 0);
    1258                 :         70 :     m = ldexp(m, shift);
    1259                 :         70 :     e -= shift;
    1260                 :            : 
    1261                 :         70 :     si = 0;
    1262                 :         70 :     s[si] = char_from_hex((int)m);
    1263                 :         70 :     si++;
    1264                 :         70 :     m -= (int)m;
    1265                 :         70 :     s[si] = '.';
    1266                 :         70 :     si++;
    1267         [ +  + ]:        980 :     for (i=0; i < (TOHEX_NBITS-1)/4; i++) {
    1268                 :        910 :         m *= 16.0;
    1269                 :        910 :         s[si] = char_from_hex((int)m);
    1270                 :        910 :         si++;
    1271                 :        910 :         m -= (int)m;
    1272                 :            :     }
    1273                 :         70 :     s[si] = '\0';
    1274                 :            : 
    1275         [ +  + ]:         70 :     if (e < 0) {
    1276                 :         40 :         esign = (int)'-';
    1277                 :         40 :         e = -e;
    1278                 :            :     }
    1279                 :            :     else
    1280                 :         30 :         esign = (int)'+';
    1281                 :            : 
    1282         [ +  + ]:         70 :     if (x < 0.0)
    1283                 :         34 :         return PyUnicode_FromFormat("-0x%sp%c%d", s, esign, e);
    1284                 :            :     else
    1285                 :         36 :         return PyUnicode_FromFormat("0x%sp%c%d", s, esign, e);
    1286                 :            : }
    1287                 :            : 
    1288                 :            : /* Convert a hexadecimal string to a float. */
    1289                 :            : 
    1290                 :            : /*[clinic input]
    1291                 :            : @classmethod
    1292                 :            : float.fromhex
    1293                 :            : 
    1294                 :            :     string: object
    1295                 :            :     /
    1296                 :            : 
    1297                 :            : Create a floating-point number from a hexadecimal string.
    1298                 :            : 
    1299                 :            : >>> float.fromhex('0x1.ffffp10')
    1300                 :            : 2047.984375
    1301                 :            : >>> float.fromhex('-0x1p-1074')
    1302                 :            : -5e-324
    1303                 :            : [clinic start generated code]*/
    1304                 :            : 
    1305                 :            : static PyObject *
    1306                 :        210 : float_fromhex(PyTypeObject *type, PyObject *string)
    1307                 :            : /*[clinic end generated code: output=46c0274d22b78e82 input=0407bebd354bca89]*/
    1308                 :            : {
    1309                 :            :     PyObject *result;
    1310                 :            :     double x;
    1311                 :            :     long exp, top_exp, lsb, key_digit;
    1312                 :            :     const char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end;
    1313                 :        210 :     int half_eps, digit, round_up, negate=0;
    1314                 :            :     Py_ssize_t length, ndigits, fdigits, i;
    1315                 :            : 
    1316                 :            :     /*
    1317                 :            :      * For the sake of simplicity and correctness, we impose an artificial
    1318                 :            :      * limit on ndigits, the total number of hex digits in the coefficient
    1319                 :            :      * The limit is chosen to ensure that, writing exp for the exponent,
    1320                 :            :      *
    1321                 :            :      *   (1) if exp > LONG_MAX/2 then the value of the hex string is
    1322                 :            :      *   guaranteed to overflow (provided it's nonzero)
    1323                 :            :      *
    1324                 :            :      *   (2) if exp < LONG_MIN/2 then the value of the hex string is
    1325                 :            :      *   guaranteed to underflow to 0.
    1326                 :            :      *
    1327                 :            :      *   (3) if LONG_MIN/2 <= exp <= LONG_MAX/2 then there's no danger of
    1328                 :            :      *   overflow in the calculation of exp and top_exp below.
    1329                 :            :      *
    1330                 :            :      * More specifically, ndigits is assumed to satisfy the following
    1331                 :            :      * inequalities:
    1332                 :            :      *
    1333                 :            :      *   4*ndigits <= DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2
    1334                 :            :      *   4*ndigits <= LONG_MAX/2 + 1 - DBL_MAX_EXP
    1335                 :            :      *
    1336                 :            :      * If either of these inequalities is not satisfied, a ValueError is
    1337                 :            :      * raised.  Otherwise, write x for the value of the hex string, and
    1338                 :            :      * assume x is nonzero.  Then
    1339                 :            :      *
    1340                 :            :      *   2**(exp-4*ndigits) <= |x| < 2**(exp+4*ndigits).
    1341                 :            :      *
    1342                 :            :      * Now if exp > LONG_MAX/2 then:
    1343                 :            :      *
    1344                 :            :      *   exp - 4*ndigits >= LONG_MAX/2 + 1 - (LONG_MAX/2 + 1 - DBL_MAX_EXP)
    1345                 :            :      *                    = DBL_MAX_EXP
    1346                 :            :      *
    1347                 :            :      * so |x| >= 2**DBL_MAX_EXP, which is too large to be stored in C
    1348                 :            :      * double, so overflows.  If exp < LONG_MIN/2, then
    1349                 :            :      *
    1350                 :            :      *   exp + 4*ndigits <= LONG_MIN/2 - 1 + (
    1351                 :            :      *                      DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2)
    1352                 :            :      *                    = DBL_MIN_EXP - DBL_MANT_DIG - 1
    1353                 :            :      *
    1354                 :            :      * and so |x| < 2**(DBL_MIN_EXP-DBL_MANT_DIG-1), hence underflows to 0
    1355                 :            :      * when converted to a C double.
    1356                 :            :      *
    1357                 :            :      * It's easy to show that if LONG_MIN/2 <= exp <= LONG_MAX/2 then both
    1358                 :            :      * exp+4*ndigits and exp-4*ndigits are within the range of a long.
    1359                 :            :      */
    1360                 :            : 
    1361                 :        210 :     s = PyUnicode_AsUTF8AndSize(string, &length);
    1362         [ -  + ]:        210 :     if (s == NULL)
    1363                 :          0 :         return NULL;
    1364                 :        210 :     s_end = s + length;
    1365                 :            : 
    1366                 :            :     /********************
    1367                 :            :      * Parse the string *
    1368                 :            :      ********************/
    1369                 :            : 
    1370                 :            :     /* leading whitespace */
    1371         [ -  + ]:        210 :     while (Py_ISSPACE(*s))
    1372                 :          0 :         s++;
    1373                 :            : 
    1374                 :            :     /* infinities and nans */
    1375                 :        210 :     x = _Py_parse_inf_or_nan(s, (char **)&coeff_end);
    1376         [ -  + ]:        210 :     if (coeff_end != s) {
    1377                 :          0 :         s = coeff_end;
    1378                 :          0 :         goto finished;
    1379                 :            :     }
    1380                 :            : 
    1381                 :            :     /* optional sign */
    1382         [ +  + ]:        210 :     if (*s == '-') {
    1383                 :         40 :         s++;
    1384                 :         40 :         negate = 1;
    1385                 :            :     }
    1386         [ -  + ]:        170 :     else if (*s == '+')
    1387                 :          0 :         s++;
    1388                 :            : 
    1389                 :            :     /* [0x] */
    1390                 :        210 :     s_store = s;
    1391         [ +  + ]:        210 :     if (*s == '0') {
    1392                 :        169 :         s++;
    1393   [ +  +  -  + ]:        169 :         if (*s == 'x' || *s == 'X')
    1394                 :        131 :             s++;
    1395                 :            :         else
    1396                 :         38 :             s = s_store;
    1397                 :            :     }
    1398                 :            : 
    1399                 :            :     /* coefficient: <integer> [. <fraction>] */
    1400                 :        210 :     coeff_start = s;
    1401         [ +  + ]:        420 :     while (hex_from_char(*s) >= 0)
    1402                 :        210 :         s++;
    1403                 :        210 :     s_store = s;
    1404         [ +  + ]:        210 :     if (*s == '.') {
    1405                 :        185 :         s++;
    1406         [ +  + ]:       1786 :         while (hex_from_char(*s) >= 0)
    1407                 :       1601 :             s++;
    1408                 :        185 :         coeff_end = s-1;
    1409                 :            :     }
    1410                 :            :     else
    1411                 :         25 :         coeff_end = s;
    1412                 :            : 
    1413                 :            :     /* ndigits = total # of hex digits; fdigits = # after point */
    1414                 :        210 :     ndigits = coeff_end - coeff_start;
    1415                 :        210 :     fdigits = coeff_end - s_store;
    1416         [ -  + ]:        210 :     if (ndigits == 0)
    1417                 :          0 :         goto parse_error;
    1418         [ -  + ]:        210 :     if (ndigits > Py_MIN(DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2,
    1419                 :            :                          LONG_MAX/2 + 1 - DBL_MAX_EXP)/4)
    1420                 :          0 :         goto insane_length_error;
    1421                 :            : 
    1422                 :            :     /* [p <exponent>] */
    1423   [ +  +  -  + ]:        210 :     if (*s == 'p' || *s == 'P') {
    1424                 :        132 :         s++;
    1425                 :        132 :         exp_start = s;
    1426   [ +  +  +  + ]:        132 :         if (*s == '-' || *s == '+')
    1427                 :        130 :             s++;
    1428   [ +  -  -  + ]:        132 :         if (!('0' <= *s && *s <= '9'))
    1429                 :          0 :             goto parse_error;
    1430                 :        132 :         s++;
    1431   [ +  +  +  - ]:        224 :         while ('0' <= *s && *s <= '9')
    1432                 :         92 :             s++;
    1433                 :        132 :         exp = strtol(exp_start, NULL, 10);
    1434                 :            :     }
    1435                 :            :     else
    1436                 :         78 :         exp = 0;
    1437                 :            : 
    1438                 :            : /* for 0 <= j < ndigits, HEX_DIGIT(j) gives the jth most significant digit */
    1439                 :            : #define HEX_DIGIT(j) hex_from_char(*((j) < fdigits ?            \
    1440                 :            :                      coeff_end-(j) :                                    \
    1441                 :            :                      coeff_end-1-(j)))
    1442                 :            : 
    1443                 :            :     /*******************************************
    1444                 :            :      * Compute rounded value of the hex string *
    1445                 :            :      *******************************************/
    1446                 :            : 
    1447                 :            :     /* Discard leading zeros, and catch extreme overflow and underflow */
    1448   [ +  +  +  +  :        321 :     while (ndigits > 0 && HEX_DIGIT(ndigits-1) == 0)
                   +  + ]
    1449                 :        111 :         ndigits--;
    1450   [ +  +  -  + ]:        210 :     if (ndigits == 0 || exp < LONG_MIN/2) {
    1451                 :         16 :         x = 0.0;
    1452                 :         16 :         goto finished;
    1453                 :            :     }
    1454         [ -  + ]:        194 :     if (exp > LONG_MAX/2)
    1455                 :          0 :         goto overflow_error;
    1456                 :            : 
    1457                 :            :     /* Adjust exponent for fractional part. */
    1458                 :        194 :     exp = exp - 4*((long)fdigits);
    1459                 :            : 
    1460                 :            :     /* top_exp = 1 more than exponent of most sig. bit of coefficient */
    1461                 :        194 :     top_exp = exp + 4*((long)ndigits - 1);
    1462   [ +  +  +  + ]:        479 :     for (digit = HEX_DIGIT(ndigits-1); digit != 0; digit /= 2)
    1463                 :        285 :         top_exp++;
    1464                 :            : 
    1465                 :            :     /* catch almost all nonextreme cases of overflow and underflow here */
    1466         [ -  + ]:        194 :     if (top_exp < DBL_MIN_EXP - DBL_MANT_DIG) {
    1467                 :          0 :         x = 0.0;
    1468                 :          0 :         goto finished;
    1469                 :            :     }
    1470         [ -  + ]:        194 :     if (top_exp > DBL_MAX_EXP)
    1471                 :          0 :         goto overflow_error;
    1472                 :            : 
    1473                 :            :     /* lsb = exponent of least significant bit of the *rounded* value.
    1474                 :            :        This is top_exp - DBL_MANT_DIG unless result is subnormal. */
    1475                 :        194 :     lsb = Py_MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG;
    1476                 :            : 
    1477                 :        194 :     x = 0.0;
    1478         [ +  - ]:        194 :     if (exp >= lsb) {
    1479                 :            :         /* no rounding required */
    1480         [ +  + ]:       1894 :         for (i = ndigits-1; i >= 0; i--)
    1481         [ +  + ]:       1700 :             x = 16.0*x + HEX_DIGIT(i);
    1482                 :        194 :         x = ldexp(x, (int)(exp));
    1483                 :        194 :         goto finished;
    1484                 :            :     }
    1485                 :            :     /* rounding required.  key_digit is the index of the hex digit
    1486                 :            :        containing the first bit to be rounded away. */
    1487                 :          0 :     half_eps = 1 << (int)((lsb - exp - 1) % 4);
    1488                 :          0 :     key_digit = (lsb - exp - 1) / 4;
    1489         [ #  # ]:          0 :     for (i = ndigits-1; i > key_digit; i--)
    1490         [ #  # ]:          0 :         x = 16.0*x + HEX_DIGIT(i);
    1491         [ #  # ]:          0 :     digit = HEX_DIGIT(key_digit);
    1492                 :          0 :     x = 16.0*x + (double)(digit & (16-2*half_eps));
    1493                 :            : 
    1494                 :            :     /* round-half-even: round up if bit lsb-1 is 1 and at least one of
    1495                 :            :        bits lsb, lsb-2, lsb-3, lsb-4, ... is 1. */
    1496         [ #  # ]:          0 :     if ((digit & half_eps) != 0) {
    1497                 :          0 :         round_up = 0;
    1498   [ #  #  #  # ]:          0 :         if ((digit & (3*half_eps-1)) != 0 || (half_eps == 8 &&
    1499   [ #  #  #  #  :          0 :                 key_digit+1 < ndigits && (HEX_DIGIT(key_digit+1) & 1) != 0))
                   #  # ]
    1500                 :          0 :             round_up = 1;
    1501                 :            :         else
    1502         [ #  # ]:          0 :             for (i = key_digit-1; i >= 0; i--)
    1503   [ #  #  #  # ]:          0 :                 if (HEX_DIGIT(i) != 0) {
    1504                 :          0 :                     round_up = 1;
    1505                 :          0 :                     break;
    1506                 :            :                 }
    1507         [ #  # ]:          0 :         if (round_up) {
    1508                 :          0 :             x += 2*half_eps;
    1509         [ #  # ]:          0 :             if (top_exp == DBL_MAX_EXP &&
    1510         [ #  # ]:          0 :                 x == ldexp((double)(2*half_eps), DBL_MANT_DIG))
    1511                 :            :                 /* overflow corner case: pre-rounded value <
    1512                 :            :                    2**DBL_MAX_EXP; rounded=2**DBL_MAX_EXP. */
    1513                 :          0 :                 goto overflow_error;
    1514                 :            :         }
    1515                 :            :     }
    1516                 :          0 :     x = ldexp(x, (int)(exp+4*key_digit));
    1517                 :            : 
    1518                 :        210 :   finished:
    1519                 :            :     /* optional trailing whitespace leading to the end of the string */
    1520         [ -  + ]:        210 :     while (Py_ISSPACE(*s))
    1521                 :          0 :         s++;
    1522         [ -  + ]:        210 :     if (s != s_end)
    1523                 :          0 :         goto parse_error;
    1524         [ +  + ]:        210 :     result = PyFloat_FromDouble(negate ? -x : x);
    1525   [ -  +  -  - ]:        210 :     if (type != &PyFloat_Type && result != NULL) {
    1526                 :          0 :         Py_SETREF(result, PyObject_CallOneArg((PyObject *)type, result));
    1527                 :            :     }
    1528                 :        210 :     return result;
    1529                 :            : 
    1530                 :          0 :   overflow_error:
    1531                 :          0 :     PyErr_SetString(PyExc_OverflowError,
    1532                 :            :                     "hexadecimal value too large to represent as a float");
    1533                 :          0 :     return NULL;
    1534                 :            : 
    1535                 :          0 :   parse_error:
    1536                 :          0 :     PyErr_SetString(PyExc_ValueError,
    1537                 :            :                     "invalid hexadecimal floating-point string");
    1538                 :          0 :     return NULL;
    1539                 :            : 
    1540                 :          0 :   insane_length_error:
    1541                 :          0 :     PyErr_SetString(PyExc_ValueError,
    1542                 :            :                     "hexadecimal string too long to convert");
    1543                 :          0 :     return NULL;
    1544                 :            : }
    1545                 :            : 
    1546                 :            : /*[clinic input]
    1547                 :            : float.as_integer_ratio
    1548                 :            : 
    1549                 :            : Return a pair of integers, whose ratio is exactly equal to the original float.
    1550                 :            : 
    1551                 :            : The ratio is in lowest terms and has a positive denominator.  Raise
    1552                 :            : OverflowError on infinities and a ValueError on NaNs.
    1553                 :            : 
    1554                 :            : >>> (10.0).as_integer_ratio()
    1555                 :            : (10, 1)
    1556                 :            : >>> (0.0).as_integer_ratio()
    1557                 :            : (0, 1)
    1558                 :            : >>> (-.25).as_integer_ratio()
    1559                 :            : (-1, 4)
    1560                 :            : [clinic start generated code]*/
    1561                 :            : 
    1562                 :            : static PyObject *
    1563                 :      29615 : float_as_integer_ratio_impl(PyObject *self)
    1564                 :            : /*[clinic end generated code: output=65f25f0d8d30a712 input=d5ba7765655d75bd]*/
    1565                 :            : {
    1566                 :            :     double self_double;
    1567                 :            :     double float_part;
    1568                 :            :     int exponent;
    1569                 :            :     int i;
    1570                 :            : 
    1571                 :      29615 :     PyObject *py_exponent = NULL;
    1572                 :      29615 :     PyObject *numerator = NULL;
    1573                 :      29615 :     PyObject *denominator = NULL;
    1574                 :      29615 :     PyObject *result_pair = NULL;
    1575                 :      29615 :     PyNumberMethods *long_methods = PyLong_Type.tp_as_number;
    1576                 :            : 
    1577   [ +  -  -  - ]:      29615 :     CONVERT_TO_DOUBLE(self, self_double);
    1578                 :            : 
    1579   [ -  +  -  + ]:      29615 :     if (Py_IS_INFINITY(self_double)) {
    1580                 :          0 :         PyErr_SetString(PyExc_OverflowError,
    1581                 :            :                         "cannot convert Infinity to integer ratio");
    1582                 :          0 :         return NULL;
    1583                 :            :     }
    1584         [ -  + ]:      29615 :     if (Py_IS_NAN(self_double)) {
    1585                 :          0 :         PyErr_SetString(PyExc_ValueError,
    1586                 :            :                         "cannot convert NaN to integer ratio");
    1587                 :          0 :         return NULL;
    1588                 :            :     }
    1589                 :            : 
    1590                 :      29615 :     float_part = frexp(self_double, &exponent);        /* self_double == float_part * 2**exponent exactly */
    1591                 :            : 
    1592   [ +  -  +  + ]:     125835 :     for (i=0; i<300 && float_part != floor(float_part) ; i++) {
    1593                 :      96220 :         float_part *= 2.0;
    1594                 :      96220 :         exponent--;
    1595                 :            :     }
    1596                 :            :     /* self == float_part * 2**exponent exactly and float_part is integral.
    1597                 :            :        If FLT_RADIX != 2, the 300 steps may leave a tiny fractional part
    1598                 :            :        to be truncated by PyLong_FromDouble(). */
    1599                 :            : 
    1600                 :      29615 :     numerator = PyLong_FromDouble(float_part);
    1601         [ -  + ]:      29615 :     if (numerator == NULL)
    1602                 :          0 :         goto error;
    1603                 :      29615 :     denominator = PyLong_FromLong(1);
    1604         [ -  + ]:      29615 :     if (denominator == NULL)
    1605                 :          0 :         goto error;
    1606                 :      29615 :     py_exponent = PyLong_FromLong(Py_ABS(exponent));
    1607         [ -  + ]:      29615 :     if (py_exponent == NULL)
    1608                 :          0 :         goto error;
    1609                 :            : 
    1610                 :            :     /* fold in 2**exponent */
    1611         [ +  + ]:      29615 :     if (exponent > 0) {
    1612                 :         16 :         Py_SETREF(numerator,
    1613                 :            :                   long_methods->nb_lshift(numerator, py_exponent));
    1614         [ -  + ]:         16 :         if (numerator == NULL)
    1615                 :          0 :             goto error;
    1616                 :            :     }
    1617                 :            :     else {
    1618                 :      29599 :         Py_SETREF(denominator,
    1619                 :            :                   long_methods->nb_lshift(denominator, py_exponent));
    1620         [ -  + ]:      29599 :         if (denominator == NULL)
    1621                 :          0 :             goto error;
    1622                 :            :     }
    1623                 :            : 
    1624                 :      29615 :     result_pair = PyTuple_Pack(2, numerator, denominator);
    1625                 :            : 
    1626                 :      29615 : error:
    1627                 :      29615 :     Py_XDECREF(py_exponent);
    1628                 :      29615 :     Py_XDECREF(denominator);
    1629                 :      29615 :     Py_XDECREF(numerator);
    1630                 :      29615 :     return result_pair;
    1631                 :            : }
    1632                 :            : 
    1633                 :            : static PyObject *
    1634                 :            : float_subtype_new(PyTypeObject *type, PyObject *x);
    1635                 :            : 
    1636                 :            : /*[clinic input]
    1637                 :            : @classmethod
    1638                 :            : float.__new__ as float_new
    1639                 :            :     x: object(c_default="NULL") = 0
    1640                 :            :     /
    1641                 :            : 
    1642                 :            : Convert a string or number to a floating point number, if possible.
    1643                 :            : [clinic start generated code]*/
    1644                 :            : 
    1645                 :            : static PyObject *
    1646                 :      15444 : float_new_impl(PyTypeObject *type, PyObject *x)
    1647                 :            : /*[clinic end generated code: output=ccf1e8dc460ba6ba input=f43661b7de03e9d8]*/
    1648                 :            : {
    1649         [ +  + ]:      15444 :     if (type != &PyFloat_Type) {
    1650         [ +  - ]:          3 :         if (x == NULL) {
    1651                 :          3 :             x = _PyLong_GetZero();
    1652                 :            :         }
    1653                 :          3 :         return float_subtype_new(type, x); /* Wimp out */
    1654                 :            :     }
    1655                 :            : 
    1656         [ -  + ]:      15441 :     if (x == NULL) {
    1657                 :          0 :         return PyFloat_FromDouble(0.0);
    1658                 :            :     }
    1659                 :            :     /* If it's a string, but not a string subclass, use
    1660                 :            :        PyFloat_FromString. */
    1661         [ +  + ]:      15441 :     if (PyUnicode_CheckExact(x))
    1662                 :       9291 :         return PyFloat_FromString(x);
    1663                 :       6150 :     return PyNumber_Float(x);
    1664                 :            : }
    1665                 :            : 
    1666                 :            : /* Wimpy, slow approach to tp_new calls for subtypes of float:
    1667                 :            :    first create a regular float from whatever arguments we got,
    1668                 :            :    then allocate a subtype instance and initialize its ob_fval
    1669                 :            :    from the regular float.  The regular float is then thrown away.
    1670                 :            : */
    1671                 :            : static PyObject *
    1672                 :          3 : float_subtype_new(PyTypeObject *type, PyObject *x)
    1673                 :            : {
    1674                 :            :     PyObject *tmp, *newobj;
    1675                 :            : 
    1676                 :            :     assert(PyType_IsSubtype(type, &PyFloat_Type));
    1677                 :          3 :     tmp = float_new_impl(&PyFloat_Type, x);
    1678         [ -  + ]:          3 :     if (tmp == NULL)
    1679                 :          0 :         return NULL;
    1680                 :            :     assert(PyFloat_Check(tmp));
    1681                 :          3 :     newobj = type->tp_alloc(type, 0);
    1682         [ -  + ]:          3 :     if (newobj == NULL) {
    1683                 :          0 :         Py_DECREF(tmp);
    1684                 :          0 :         return NULL;
    1685                 :            :     }
    1686                 :          3 :     ((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
    1687                 :          3 :     Py_DECREF(tmp);
    1688                 :          3 :     return newobj;
    1689                 :            : }
    1690                 :            : 
    1691                 :            : static PyObject *
    1692                 :      15438 : float_vectorcall(PyObject *type, PyObject * const*args,
    1693                 :            :                  size_t nargsf, PyObject *kwnames)
    1694                 :            : {
    1695   [ -  +  -  - ]:      15438 :     if (!_PyArg_NoKwnames("float", kwnames)) {
    1696                 :          0 :         return NULL;
    1697                 :            :     }
    1698                 :            : 
    1699                 :      15438 :     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
    1700   [ +  -  -  +  :      15438 :     if (!_PyArg_CheckPositional("float", nargs, 0, 1)) {
                   -  - ]
    1701                 :          0 :         return NULL;
    1702                 :            :     }
    1703                 :            : 
    1704         [ +  - ]:      15438 :     PyObject *x = nargs >= 1 ? args[0] : NULL;
    1705                 :      15438 :     return float_new_impl(_PyType_CAST(type), x);
    1706                 :            : }
    1707                 :            : 
    1708                 :            : 
    1709                 :            : /*[clinic input]
    1710                 :            : float.__getnewargs__
    1711                 :            : [clinic start generated code]*/
    1712                 :            : 
    1713                 :            : static PyObject *
    1714                 :          0 : float___getnewargs___impl(PyObject *self)
    1715                 :            : /*[clinic end generated code: output=873258c9d206b088 input=002279d1d77891e6]*/
    1716                 :            : {
    1717                 :          0 :     return Py_BuildValue("(d)", ((PyFloatObject *)self)->ob_fval);
    1718                 :            : }
    1719                 :            : 
    1720                 :            : /* this is for the benefit of the pack/unpack routines below */
    1721                 :            : typedef enum _py_float_format_type float_format_type;
    1722                 :            : #define unknown_format _py_float_format_unknown
    1723                 :            : #define ieee_big_endian_format _py_float_format_ieee_big_endian
    1724                 :            : #define ieee_little_endian_format _py_float_format_ieee_little_endian
    1725                 :            : 
    1726                 :            : #define float_format (_PyRuntime.float_state.float_format)
    1727                 :            : #define double_format (_PyRuntime.float_state.double_format)
    1728                 :            : 
    1729                 :            : 
    1730                 :            : /*[clinic input]
    1731                 :            : @classmethod
    1732                 :            : float.__getformat__
    1733                 :            : 
    1734                 :            :     typestr: str
    1735                 :            :         Must be 'double' or 'float'.
    1736                 :            :     /
    1737                 :            : 
    1738                 :            : You probably don't want to use this function.
    1739                 :            : 
    1740                 :            : It exists mainly to be used in Python's test suite.
    1741                 :            : 
    1742                 :            : This function returns whichever of 'unknown', 'IEEE, big-endian' or 'IEEE,
    1743                 :            : little-endian' best describes the format of floating point numbers used by the
    1744                 :            : C type named by typestr.
    1745                 :            : [clinic start generated code]*/
    1746                 :            : 
    1747                 :            : static PyObject *
    1748                 :          1 : float___getformat___impl(PyTypeObject *type, const char *typestr)
    1749                 :            : /*[clinic end generated code: output=2bfb987228cc9628 input=d5a52600f835ad67]*/
    1750                 :            : {
    1751                 :            :     float_format_type r;
    1752                 :            : 
    1753         [ +  - ]:          1 :     if (strcmp(typestr, "double") == 0) {
    1754                 :          1 :         r = double_format;
    1755                 :            :     }
    1756         [ #  # ]:          0 :     else if (strcmp(typestr, "float") == 0) {
    1757                 :          0 :         r = float_format;
    1758                 :            :     }
    1759                 :            :     else {
    1760                 :          0 :         PyErr_SetString(PyExc_ValueError,
    1761                 :            :                         "__getformat__() argument 1 must be "
    1762                 :            :                         "'double' or 'float'");
    1763                 :          0 :         return NULL;
    1764                 :            :     }
    1765                 :            : 
    1766   [ -  +  -  - ]:          1 :     switch (r) {
    1767                 :          0 :     case unknown_format:
    1768                 :          0 :         return PyUnicode_FromString("unknown");
    1769                 :          1 :     case ieee_little_endian_format:
    1770                 :          1 :         return PyUnicode_FromString("IEEE, little-endian");
    1771                 :          0 :     case ieee_big_endian_format:
    1772                 :          0 :         return PyUnicode_FromString("IEEE, big-endian");
    1773                 :          0 :     default:
    1774                 :          0 :         PyErr_SetString(PyExc_RuntimeError,
    1775                 :            :                         "insane float_format or double_format");
    1776                 :          0 :         return NULL;
    1777                 :            :     }
    1778                 :            : }
    1779                 :            : 
    1780                 :            : 
    1781                 :            : static PyObject *
    1782                 :          2 : float_getreal(PyObject *v, void *closure)
    1783                 :            : {
    1784                 :          2 :     return float_float(v);
    1785                 :            : }
    1786                 :            : 
    1787                 :            : static PyObject *
    1788                 :          2 : float_getimag(PyObject *v, void *closure)
    1789                 :            : {
    1790                 :          2 :     return PyFloat_FromDouble(0.0);
    1791                 :            : }
    1792                 :            : 
    1793                 :            : /*[clinic input]
    1794                 :            : float.__format__
    1795                 :            : 
    1796                 :            :   format_spec: unicode
    1797                 :            :   /
    1798                 :            : 
    1799                 :            : Formats the float according to format_spec.
    1800                 :            : [clinic start generated code]*/
    1801                 :            : 
    1802                 :            : static PyObject *
    1803                 :          2 : float___format___impl(PyObject *self, PyObject *format_spec)
    1804                 :            : /*[clinic end generated code: output=b260e52a47eade56 input=2ece1052211fd0e6]*/
    1805                 :            : {
    1806                 :            :     _PyUnicodeWriter writer;
    1807                 :            :     int ret;
    1808                 :            : 
    1809                 :          2 :     _PyUnicodeWriter_Init(&writer);
    1810                 :          2 :     ret = _PyFloat_FormatAdvancedWriter(
    1811                 :            :         &writer,
    1812                 :            :         self,
    1813                 :            :         format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
    1814         [ -  + ]:          2 :     if (ret == -1) {
    1815                 :          0 :         _PyUnicodeWriter_Dealloc(&writer);
    1816                 :          0 :         return NULL;
    1817                 :            :     }
    1818                 :          2 :     return _PyUnicodeWriter_Finish(&writer);
    1819                 :            : }
    1820                 :            : 
    1821                 :            : static PyMethodDef float_methods[] = {
    1822                 :            :     FLOAT_CONJUGATE_METHODDEF
    1823                 :            :     FLOAT___TRUNC___METHODDEF
    1824                 :            :     FLOAT___FLOOR___METHODDEF
    1825                 :            :     FLOAT___CEIL___METHODDEF
    1826                 :            :     FLOAT___ROUND___METHODDEF
    1827                 :            :     FLOAT_AS_INTEGER_RATIO_METHODDEF
    1828                 :            :     FLOAT_FROMHEX_METHODDEF
    1829                 :            :     FLOAT_HEX_METHODDEF
    1830                 :            :     FLOAT_IS_INTEGER_METHODDEF
    1831                 :            :     FLOAT___GETNEWARGS___METHODDEF
    1832                 :            :     FLOAT___GETFORMAT___METHODDEF
    1833                 :            :     FLOAT___FORMAT___METHODDEF
    1834                 :            :     {NULL,              NULL}           /* sentinel */
    1835                 :            : };
    1836                 :            : 
    1837                 :            : static PyGetSetDef float_getset[] = {
    1838                 :            :     {"real",
    1839                 :            :      float_getreal, (setter)NULL,
    1840                 :            :      "the real part of a complex number",
    1841                 :            :      NULL},
    1842                 :            :     {"imag",
    1843                 :            :      float_getimag, (setter)NULL,
    1844                 :            :      "the imaginary part of a complex number",
    1845                 :            :      NULL},
    1846                 :            :     {NULL}  /* Sentinel */
    1847                 :            : };
    1848                 :            : 
    1849                 :            : 
    1850                 :            : static PyNumberMethods float_as_number = {
    1851                 :            :     float_add,          /* nb_add */
    1852                 :            :     float_sub,          /* nb_subtract */
    1853                 :            :     float_mul,          /* nb_multiply */
    1854                 :            :     float_rem,          /* nb_remainder */
    1855                 :            :     float_divmod,       /* nb_divmod */
    1856                 :            :     float_pow,          /* nb_power */
    1857                 :            :     (unaryfunc)float_neg, /* nb_negative */
    1858                 :            :     float_float,        /* nb_positive */
    1859                 :            :     (unaryfunc)float_abs, /* nb_absolute */
    1860                 :            :     (inquiry)float_bool, /* nb_bool */
    1861                 :            :     0,                  /* nb_invert */
    1862                 :            :     0,                  /* nb_lshift */
    1863                 :            :     0,                  /* nb_rshift */
    1864                 :            :     0,                  /* nb_and */
    1865                 :            :     0,                  /* nb_xor */
    1866                 :            :     0,                  /* nb_or */
    1867                 :            :     float___trunc___impl, /* nb_int */
    1868                 :            :     0,                  /* nb_reserved */
    1869                 :            :     float_float,        /* nb_float */
    1870                 :            :     0,                  /* nb_inplace_add */
    1871                 :            :     0,                  /* nb_inplace_subtract */
    1872                 :            :     0,                  /* nb_inplace_multiply */
    1873                 :            :     0,                  /* nb_inplace_remainder */
    1874                 :            :     0,                  /* nb_inplace_power */
    1875                 :            :     0,                  /* nb_inplace_lshift */
    1876                 :            :     0,                  /* nb_inplace_rshift */
    1877                 :            :     0,                  /* nb_inplace_and */
    1878                 :            :     0,                  /* nb_inplace_xor */
    1879                 :            :     0,                  /* nb_inplace_or */
    1880                 :            :     float_floor_div,    /* nb_floor_divide */
    1881                 :            :     float_div,          /* nb_true_divide */
    1882                 :            :     0,                  /* nb_inplace_floor_divide */
    1883                 :            :     0,                  /* nb_inplace_true_divide */
    1884                 :            : };
    1885                 :            : 
    1886                 :            : PyTypeObject PyFloat_Type = {
    1887                 :            :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
    1888                 :            :     "float",
    1889                 :            :     sizeof(PyFloatObject),
    1890                 :            :     0,
    1891                 :            :     (destructor)float_dealloc,                  /* tp_dealloc */
    1892                 :            :     0,                                          /* tp_vectorcall_offset */
    1893                 :            :     0,                                          /* tp_getattr */
    1894                 :            :     0,                                          /* tp_setattr */
    1895                 :            :     0,                                          /* tp_as_async */
    1896                 :            :     (reprfunc)float_repr,                       /* tp_repr */
    1897                 :            :     &float_as_number,                           /* tp_as_number */
    1898                 :            :     0,                                          /* tp_as_sequence */
    1899                 :            :     0,                                          /* tp_as_mapping */
    1900                 :            :     (hashfunc)float_hash,                       /* tp_hash */
    1901                 :            :     0,                                          /* tp_call */
    1902                 :            :     0,                                          /* tp_str */
    1903                 :            :     PyObject_GenericGetAttr,                    /* tp_getattro */
    1904                 :            :     0,                                          /* tp_setattro */
    1905                 :            :     0,                                          /* tp_as_buffer */
    1906                 :            :     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
    1907                 :            :         _Py_TPFLAGS_MATCH_SELF,               /* tp_flags */
    1908                 :            :     float_new__doc__,                           /* tp_doc */
    1909                 :            :     0,                                          /* tp_traverse */
    1910                 :            :     0,                                          /* tp_clear */
    1911                 :            :     float_richcompare,                          /* tp_richcompare */
    1912                 :            :     0,                                          /* tp_weaklistoffset */
    1913                 :            :     0,                                          /* tp_iter */
    1914                 :            :     0,                                          /* tp_iternext */
    1915                 :            :     float_methods,                              /* tp_methods */
    1916                 :            :     0,                                          /* tp_members */
    1917                 :            :     float_getset,                               /* tp_getset */
    1918                 :            :     0,                                          /* tp_base */
    1919                 :            :     0,                                          /* tp_dict */
    1920                 :            :     0,                                          /* tp_descr_get */
    1921                 :            :     0,                                          /* tp_descr_set */
    1922                 :            :     0,                                          /* tp_dictoffset */
    1923                 :            :     0,                                          /* tp_init */
    1924                 :            :     0,                                          /* tp_alloc */
    1925                 :            :     float_new,                                  /* tp_new */
    1926                 :            :     .tp_vectorcall = (vectorcallfunc)float_vectorcall,
    1927                 :            : };
    1928                 :            : 
    1929                 :            : static void
    1930                 :         29 : _init_global_state(void)
    1931                 :            : {
    1932                 :            :     float_format_type detected_double_format, detected_float_format;
    1933                 :            : 
    1934                 :            :     /* We attempt to determine if this machine is using IEEE
    1935                 :            :        floating point formats by peering at the bits of some
    1936                 :            :        carefully chosen values.  If it looks like we are on an
    1937                 :            :        IEEE platform, the float packing/unpacking routines can
    1938                 :            :        just copy bits, if not they resort to arithmetic & shifts
    1939                 :            :        and masks.  The shifts & masks approach works on all finite
    1940                 :            :        values, but what happens to infinities, NaNs and signed
    1941                 :            :        zeroes on packing is an accident, and attempting to unpack
    1942                 :            :        a NaN or an infinity will raise an exception.
    1943                 :            : 
    1944                 :            :        Note that if we're on some whacked-out platform which uses
    1945                 :            :        IEEE formats but isn't strictly little-endian or big-
    1946                 :            :        endian, we will fall back to the portable shifts & masks
    1947                 :            :        method. */
    1948                 :            : 
    1949                 :            : #if SIZEOF_DOUBLE == 8
    1950                 :            :     {
    1951                 :         29 :         double x = 9006104071832581.0;
    1952         [ -  + ]:         29 :         if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
    1953                 :          0 :             detected_double_format = ieee_big_endian_format;
    1954         [ +  - ]:         29 :         else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
    1955                 :         29 :             detected_double_format = ieee_little_endian_format;
    1956                 :            :         else
    1957                 :          0 :             detected_double_format = unknown_format;
    1958                 :            :     }
    1959                 :            : #else
    1960                 :            :     detected_double_format = unknown_format;
    1961                 :            : #endif
    1962                 :            : 
    1963                 :            : #if SIZEOF_FLOAT == 4
    1964                 :            :     {
    1965                 :         29 :         float y = 16711938.0;
    1966         [ -  + ]:         29 :         if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
    1967                 :          0 :             detected_float_format = ieee_big_endian_format;
    1968         [ +  - ]:         29 :         else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
    1969                 :         29 :             detected_float_format = ieee_little_endian_format;
    1970                 :            :         else
    1971                 :          0 :             detected_float_format = unknown_format;
    1972                 :            :     }
    1973                 :            : #else
    1974                 :            :     detected_float_format = unknown_format;
    1975                 :            : #endif
    1976                 :            : 
    1977                 :         29 :     double_format = detected_double_format;
    1978                 :         29 :     float_format = detected_float_format;
    1979                 :         29 : }
    1980                 :            : 
    1981                 :            : void
    1982                 :         29 : _PyFloat_InitState(PyInterpreterState *interp)
    1983                 :            : {
    1984         [ -  + ]:         29 :     if (!_Py_IsMainInterpreter(interp)) {
    1985                 :          0 :         return;
    1986                 :            :     }
    1987                 :         29 :     _init_global_state();
    1988                 :            : }
    1989                 :            : 
    1990                 :            : PyStatus
    1991                 :         29 : _PyFloat_InitTypes(PyInterpreterState *interp)
    1992                 :            : {
    1993         [ -  + ]:         29 :     if (!_Py_IsMainInterpreter(interp)) {
    1994                 :          0 :         return _PyStatus_OK();
    1995                 :            :     }
    1996                 :            : 
    1997         [ -  + ]:         29 :     if (PyType_Ready(&PyFloat_Type) < 0) {
    1998                 :          0 :         return _PyStatus_ERR("Can't initialize float type");
    1999                 :            :     }
    2000                 :            : 
    2001                 :            :     /* Init float info */
    2002         [ +  - ]:         29 :     if (FloatInfoType.tp_name == NULL) {
    2003         [ -  + ]:         29 :         if (_PyStructSequence_InitBuiltin(&FloatInfoType,
    2004                 :            :                                           &floatinfo_desc) < 0) {
    2005                 :          0 :             return _PyStatus_ERR("can't init float info type");
    2006                 :            :         }
    2007                 :            :     }
    2008                 :            : 
    2009                 :         29 :     return _PyStatus_OK();
    2010                 :            : }
    2011                 :            : 
    2012                 :            : void
    2013                 :        137 : _PyFloat_ClearFreeList(PyInterpreterState *interp)
    2014                 :            : {
    2015                 :            : #if PyFloat_MAXFREELIST > 0
    2016                 :        137 :     struct _Py_float_state *state = &interp->float_state;
    2017                 :        137 :     PyFloatObject *f = state->free_list;
    2018         [ +  + ]:        806 :     while (f != NULL) {
    2019                 :        669 :         PyFloatObject *next = (PyFloatObject*) Py_TYPE(f);
    2020                 :        669 :         PyObject_Free(f);
    2021                 :        669 :         f = next;
    2022                 :            :     }
    2023                 :        137 :     state->free_list = NULL;
    2024                 :        137 :     state->numfree = 0;
    2025                 :            : #endif
    2026                 :        137 : }
    2027                 :            : 
    2028                 :            : void
    2029                 :         25 : _PyFloat_Fini(PyInterpreterState *interp)
    2030                 :            : {
    2031                 :         25 :     _PyFloat_ClearFreeList(interp);
    2032                 :            : #if defined(Py_DEBUG) && PyFloat_MAXFREELIST > 0
    2033                 :            :     struct _Py_float_state *state = &interp->float_state;
    2034                 :            :     state->numfree = -1;
    2035                 :            : #endif
    2036                 :         25 : }
    2037                 :            : 
    2038                 :            : void
    2039                 :         25 : _PyFloat_FiniType(PyInterpreterState *interp)
    2040                 :            : {
    2041         [ +  - ]:         25 :     if (_Py_IsMainInterpreter(interp)) {
    2042                 :         25 :         _PyStructSequence_FiniType(&FloatInfoType);
    2043                 :            :     }
    2044                 :         25 : }
    2045                 :            : 
    2046                 :            : /* Print summary info about the state of the optimized allocator */
    2047                 :            : void
    2048                 :          0 : _PyFloat_DebugMallocStats(FILE *out)
    2049                 :            : {
    2050                 :            : #if PyFloat_MAXFREELIST > 0
    2051                 :          0 :     struct _Py_float_state *state = get_float_state();
    2052                 :          0 :     _PyDebugAllocatorStats(out,
    2053                 :            :                            "free PyFloatObject",
    2054                 :            :                            state->numfree, sizeof(PyFloatObject));
    2055                 :            : #endif
    2056                 :          0 : }
    2057                 :            : 
    2058                 :            : 
    2059                 :            : /*----------------------------------------------------------------------------
    2060                 :            :  * PyFloat_{Pack,Unpack}{2,4,8}.  See floatobject.h.
    2061                 :            :  * To match the NPY_HALF_ROUND_TIES_TO_EVEN behavior in:
    2062                 :            :  * https://github.com/numpy/numpy/blob/master/numpy/core/src/npymath/halffloat.c
    2063                 :            :  * We use:
    2064                 :            :  *       bits = (unsigned short)f;    Note the truncation
    2065                 :            :  *       if ((f - bits > 0.5) || (f - bits == 0.5 && bits % 2)) {
    2066                 :            :  *           bits++;
    2067                 :            :  *       }
    2068                 :            :  */
    2069                 :            : 
    2070                 :            : int
    2071                 :          0 : PyFloat_Pack2(double x, char *data, int le)
    2072                 :            : {
    2073                 :          0 :     unsigned char *p = (unsigned char *)data;
    2074                 :            :     unsigned char sign;
    2075                 :            :     int e;
    2076                 :            :     double f;
    2077                 :            :     unsigned short bits;
    2078                 :          0 :     int incr = 1;
    2079                 :            : 
    2080         [ #  # ]:          0 :     if (x == 0.0) {
    2081                 :          0 :         sign = (copysign(1.0, x) == -1.0);
    2082                 :          0 :         e = 0;
    2083                 :          0 :         bits = 0;
    2084                 :            :     }
    2085         [ #  # ]:          0 :     else if (Py_IS_INFINITY(x)) {
    2086                 :          0 :         sign = (x < 0.0);
    2087                 :          0 :         e = 0x1f;
    2088                 :          0 :         bits = 0;
    2089                 :            :     }
    2090         [ #  # ]:          0 :     else if (Py_IS_NAN(x)) {
    2091                 :            :         /* There are 2046 distinct half-precision NaNs (1022 signaling and
    2092                 :            :            1024 quiet), but there are only two quiet NaNs that don't arise by
    2093                 :            :            quieting a signaling NaN; we get those by setting the topmost bit
    2094                 :            :            of the fraction field and clearing all other fraction bits. We
    2095                 :            :            choose the one with the appropriate sign. */
    2096                 :          0 :         sign = (copysign(1.0, x) == -1.0);
    2097                 :          0 :         e = 0x1f;
    2098                 :          0 :         bits = 512;
    2099                 :            :     }
    2100                 :            :     else {
    2101                 :          0 :         sign = (x < 0.0);
    2102         [ #  # ]:          0 :         if (sign) {
    2103                 :          0 :             x = -x;
    2104                 :            :         }
    2105                 :            : 
    2106                 :          0 :         f = frexp(x, &e);
    2107   [ #  #  #  # ]:          0 :         if (f < 0.5 || f >= 1.0) {
    2108                 :          0 :             PyErr_SetString(PyExc_SystemError,
    2109                 :            :                             "frexp() result out of range");
    2110                 :          0 :             return -1;
    2111                 :            :         }
    2112                 :            : 
    2113                 :            :         /* Normalize f to be in the range [1.0, 2.0) */
    2114                 :          0 :         f *= 2.0;
    2115                 :          0 :         e--;
    2116                 :            : 
    2117         [ #  # ]:          0 :         if (e >= 16) {
    2118                 :          0 :             goto Overflow;
    2119                 :            :         }
    2120         [ #  # ]:          0 :         else if (e < -25) {
    2121                 :            :             /* |x| < 2**-25. Underflow to zero. */
    2122                 :          0 :             f = 0.0;
    2123                 :          0 :             e = 0;
    2124                 :            :         }
    2125         [ #  # ]:          0 :         else if (e < -14) {
    2126                 :            :             /* |x| < 2**-14. Gradual underflow */
    2127                 :          0 :             f = ldexp(f, 14 + e);
    2128                 :          0 :             e = 0;
    2129                 :            :         }
    2130                 :            :         else /* if (!(e == 0 && f == 0.0)) */ {
    2131                 :          0 :             e += 15;
    2132                 :          0 :             f -= 1.0; /* Get rid of leading 1 */
    2133                 :            :         }
    2134                 :            : 
    2135                 :          0 :         f *= 1024.0; /* 2**10 */
    2136                 :            :         /* Round to even */
    2137                 :          0 :         bits = (unsigned short)f; /* Note the truncation */
    2138                 :            :         assert(bits < 1024);
    2139                 :            :         assert(e < 31);
    2140   [ #  #  #  #  :          0 :         if ((f - bits > 0.5) || ((f - bits == 0.5) && (bits % 2 == 1))) {
                   #  # ]
    2141                 :          0 :             ++bits;
    2142         [ #  # ]:          0 :             if (bits == 1024) {
    2143                 :            :                 /* The carry propagated out of a string of 10 1 bits. */
    2144                 :          0 :                 bits = 0;
    2145                 :          0 :                 ++e;
    2146         [ #  # ]:          0 :                 if (e == 31)
    2147                 :          0 :                     goto Overflow;
    2148                 :            :             }
    2149                 :            :         }
    2150                 :            :     }
    2151                 :            : 
    2152                 :          0 :     bits |= (e << 10) | (sign << 15);
    2153                 :            : 
    2154                 :            :     /* Write out result. */
    2155         [ #  # ]:          0 :     if (le) {
    2156                 :          0 :         p += 1;
    2157                 :          0 :         incr = -1;
    2158                 :            :     }
    2159                 :            : 
    2160                 :            :     /* First byte */
    2161                 :          0 :     *p = (unsigned char)((bits >> 8) & 0xFF);
    2162                 :          0 :     p += incr;
    2163                 :            : 
    2164                 :            :     /* Second byte */
    2165                 :          0 :     *p = (unsigned char)(bits & 0xFF);
    2166                 :            : 
    2167                 :          0 :     return 0;
    2168                 :            : 
    2169                 :          0 :   Overflow:
    2170                 :          0 :     PyErr_SetString(PyExc_OverflowError,
    2171                 :            :                     "float too large to pack with e format");
    2172                 :          0 :     return -1;
    2173                 :            : }
    2174                 :            : 
    2175                 :            : int
    2176                 :          0 : PyFloat_Pack4(double x, char *data, int le)
    2177                 :            : {
    2178                 :          0 :     unsigned char *p = (unsigned char *)data;
    2179         [ #  # ]:          0 :     if (float_format == unknown_format) {
    2180                 :            :         unsigned char sign;
    2181                 :            :         int e;
    2182                 :            :         double f;
    2183                 :            :         unsigned int fbits;
    2184                 :          0 :         int incr = 1;
    2185                 :            : 
    2186         [ #  # ]:          0 :         if (le) {
    2187                 :          0 :             p += 3;
    2188                 :          0 :             incr = -1;
    2189                 :            :         }
    2190                 :            : 
    2191         [ #  # ]:          0 :         if (x < 0) {
    2192                 :          0 :             sign = 1;
    2193                 :          0 :             x = -x;
    2194                 :            :         }
    2195                 :            :         else
    2196                 :          0 :             sign = 0;
    2197                 :            : 
    2198                 :          0 :         f = frexp(x, &e);
    2199                 :            : 
    2200                 :            :         /* Normalize f to be in the range [1.0, 2.0) */
    2201   [ #  #  #  # ]:          0 :         if (0.5 <= f && f < 1.0) {
    2202                 :          0 :             f *= 2.0;
    2203                 :          0 :             e--;
    2204                 :            :         }
    2205         [ #  # ]:          0 :         else if (f == 0.0)
    2206                 :          0 :             e = 0;
    2207                 :            :         else {
    2208                 :          0 :             PyErr_SetString(PyExc_SystemError,
    2209                 :            :                             "frexp() result out of range");
    2210                 :          0 :             return -1;
    2211                 :            :         }
    2212                 :            : 
    2213         [ #  # ]:          0 :         if (e >= 128)
    2214                 :          0 :             goto Overflow;
    2215         [ #  # ]:          0 :         else if (e < -126) {
    2216                 :            :             /* Gradual underflow */
    2217                 :          0 :             f = ldexp(f, 126 + e);
    2218                 :          0 :             e = 0;
    2219                 :            :         }
    2220   [ #  #  #  # ]:          0 :         else if (!(e == 0 && f == 0.0)) {
    2221                 :          0 :             e += 127;
    2222                 :          0 :             f -= 1.0; /* Get rid of leading 1 */
    2223                 :            :         }
    2224                 :            : 
    2225                 :          0 :         f *= 8388608.0; /* 2**23 */
    2226                 :          0 :         fbits = (unsigned int)(f + 0.5); /* Round */
    2227                 :            :         assert(fbits <= 8388608);
    2228         [ #  # ]:          0 :         if (fbits >> 23) {
    2229                 :            :             /* The carry propagated out of a string of 23 1 bits. */
    2230                 :          0 :             fbits = 0;
    2231                 :          0 :             ++e;
    2232         [ #  # ]:          0 :             if (e >= 255)
    2233                 :          0 :                 goto Overflow;
    2234                 :            :         }
    2235                 :            : 
    2236                 :            :         /* First byte */
    2237                 :          0 :         *p = (sign << 7) | (e >> 1);
    2238                 :          0 :         p += incr;
    2239                 :            : 
    2240                 :            :         /* Second byte */
    2241                 :          0 :         *p = (char) (((e & 1) << 7) | (fbits >> 16));
    2242                 :          0 :         p += incr;
    2243                 :            : 
    2244                 :            :         /* Third byte */
    2245                 :          0 :         *p = (fbits >> 8) & 0xFF;
    2246                 :          0 :         p += incr;
    2247                 :            : 
    2248                 :            :         /* Fourth byte */
    2249                 :          0 :         *p = fbits & 0xFF;
    2250                 :            : 
    2251                 :            :         /* Done */
    2252                 :          0 :         return 0;
    2253                 :            : 
    2254                 :            :     }
    2255                 :            :     else {
    2256                 :          0 :         float y = (float)x;
    2257                 :          0 :         int i, incr = 1;
    2258                 :            : 
    2259   [ #  #  #  # ]:          0 :         if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
    2260                 :          0 :             goto Overflow;
    2261                 :            : 
    2262                 :            :         unsigned char s[sizeof(float)];
    2263                 :          0 :         memcpy(s, &y, sizeof(float));
    2264                 :            : 
    2265   [ #  #  #  # ]:          0 :         if ((float_format == ieee_little_endian_format && !le)
    2266   [ #  #  #  # ]:          0 :             || (float_format == ieee_big_endian_format && le)) {
    2267                 :          0 :             p += 3;
    2268                 :          0 :             incr = -1;
    2269                 :            :         }
    2270                 :            : 
    2271         [ #  # ]:          0 :         for (i = 0; i < 4; i++) {
    2272                 :          0 :             *p = s[i];
    2273                 :          0 :             p += incr;
    2274                 :            :         }
    2275                 :          0 :         return 0;
    2276                 :            :     }
    2277                 :          0 :   Overflow:
    2278                 :          0 :     PyErr_SetString(PyExc_OverflowError,
    2279                 :            :                     "float too large to pack with f format");
    2280                 :          0 :     return -1;
    2281                 :            : }
    2282                 :            : 
    2283                 :            : int
    2284                 :        452 : PyFloat_Pack8(double x, char *data, int le)
    2285                 :            : {
    2286                 :        452 :     unsigned char *p = (unsigned char *)data;
    2287         [ -  + ]:        452 :     if (double_format == unknown_format) {
    2288                 :            :         unsigned char sign;
    2289                 :            :         int e;
    2290                 :            :         double f;
    2291                 :            :         unsigned int fhi, flo;
    2292                 :          0 :         int incr = 1;
    2293                 :            : 
    2294         [ #  # ]:          0 :         if (le) {
    2295                 :          0 :             p += 7;
    2296                 :          0 :             incr = -1;
    2297                 :            :         }
    2298                 :            : 
    2299         [ #  # ]:          0 :         if (x < 0) {
    2300                 :          0 :             sign = 1;
    2301                 :          0 :             x = -x;
    2302                 :            :         }
    2303                 :            :         else
    2304                 :          0 :             sign = 0;
    2305                 :            : 
    2306                 :          0 :         f = frexp(x, &e);
    2307                 :            : 
    2308                 :            :         /* Normalize f to be in the range [1.0, 2.0) */
    2309   [ #  #  #  # ]:          0 :         if (0.5 <= f && f < 1.0) {
    2310                 :          0 :             f *= 2.0;
    2311                 :          0 :             e--;
    2312                 :            :         }
    2313         [ #  # ]:          0 :         else if (f == 0.0)
    2314                 :          0 :             e = 0;
    2315                 :            :         else {
    2316                 :          0 :             PyErr_SetString(PyExc_SystemError,
    2317                 :            :                             "frexp() result out of range");
    2318                 :          0 :             return -1;
    2319                 :            :         }
    2320                 :            : 
    2321         [ #  # ]:          0 :         if (e >= 1024)
    2322                 :          0 :             goto Overflow;
    2323         [ #  # ]:          0 :         else if (e < -1022) {
    2324                 :            :             /* Gradual underflow */
    2325                 :          0 :             f = ldexp(f, 1022 + e);
    2326                 :          0 :             e = 0;
    2327                 :            :         }
    2328   [ #  #  #  # ]:          0 :         else if (!(e == 0 && f == 0.0)) {
    2329                 :          0 :             e += 1023;
    2330                 :          0 :             f -= 1.0; /* Get rid of leading 1 */
    2331                 :            :         }
    2332                 :            : 
    2333                 :            :         /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
    2334                 :          0 :         f *= 268435456.0; /* 2**28 */
    2335                 :          0 :         fhi = (unsigned int)f; /* Truncate */
    2336                 :            :         assert(fhi < 268435456);
    2337                 :            : 
    2338                 :          0 :         f -= (double)fhi;
    2339                 :          0 :         f *= 16777216.0; /* 2**24 */
    2340                 :          0 :         flo = (unsigned int)(f + 0.5); /* Round */
    2341                 :            :         assert(flo <= 16777216);
    2342         [ #  # ]:          0 :         if (flo >> 24) {
    2343                 :            :             /* The carry propagated out of a string of 24 1 bits. */
    2344                 :          0 :             flo = 0;
    2345                 :          0 :             ++fhi;
    2346         [ #  # ]:          0 :             if (fhi >> 28) {
    2347                 :            :                 /* And it also propagated out of the next 28 bits. */
    2348                 :          0 :                 fhi = 0;
    2349                 :          0 :                 ++e;
    2350         [ #  # ]:          0 :                 if (e >= 2047)
    2351                 :          0 :                     goto Overflow;
    2352                 :            :             }
    2353                 :            :         }
    2354                 :            : 
    2355                 :            :         /* First byte */
    2356                 :          0 :         *p = (sign << 7) | (e >> 4);
    2357                 :          0 :         p += incr;
    2358                 :            : 
    2359                 :            :         /* Second byte */
    2360                 :          0 :         *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
    2361                 :          0 :         p += incr;
    2362                 :            : 
    2363                 :            :         /* Third byte */
    2364                 :          0 :         *p = (fhi >> 16) & 0xFF;
    2365                 :          0 :         p += incr;
    2366                 :            : 
    2367                 :            :         /* Fourth byte */
    2368                 :          0 :         *p = (fhi >> 8) & 0xFF;
    2369                 :          0 :         p += incr;
    2370                 :            : 
    2371                 :            :         /* Fifth byte */
    2372                 :          0 :         *p = fhi & 0xFF;
    2373                 :          0 :         p += incr;
    2374                 :            : 
    2375                 :            :         /* Sixth byte */
    2376                 :          0 :         *p = (flo >> 16) & 0xFF;
    2377                 :          0 :         p += incr;
    2378                 :            : 
    2379                 :            :         /* Seventh byte */
    2380                 :          0 :         *p = (flo >> 8) & 0xFF;
    2381                 :          0 :         p += incr;
    2382                 :            : 
    2383                 :            :         /* Eighth byte */
    2384                 :          0 :         *p = flo & 0xFF;
    2385                 :            :         /* p += incr; */
    2386                 :            : 
    2387                 :            :         /* Done */
    2388                 :          0 :         return 0;
    2389                 :            : 
    2390                 :          0 :       Overflow:
    2391                 :          0 :         PyErr_SetString(PyExc_OverflowError,
    2392                 :            :                         "float too large to pack with d format");
    2393                 :          0 :         return -1;
    2394                 :            :     }
    2395                 :            :     else {
    2396                 :        452 :         const unsigned char *s = (unsigned char*)&x;
    2397                 :        452 :         int i, incr = 1;
    2398                 :            : 
    2399   [ +  -  +  - ]:        452 :         if ((double_format == ieee_little_endian_format && !le)
    2400   [ -  +  -  - ]:        452 :             || (double_format == ieee_big_endian_format && le)) {
    2401                 :          0 :             p += 7;
    2402                 :          0 :             incr = -1;
    2403                 :            :         }
    2404                 :            : 
    2405         [ +  + ]:       4068 :         for (i = 0; i < 8; i++) {
    2406                 :       3616 :             *p = *s++;
    2407                 :       3616 :             p += incr;
    2408                 :            :         }
    2409                 :        452 :         return 0;
    2410                 :            :     }
    2411                 :            : }
    2412                 :            : 
    2413                 :            : double
    2414                 :          0 : PyFloat_Unpack2(const char *data, int le)
    2415                 :            : {
    2416                 :          0 :     unsigned char *p = (unsigned char *)data;
    2417                 :            :     unsigned char sign;
    2418                 :            :     int e;
    2419                 :            :     unsigned int f;
    2420                 :            :     double x;
    2421                 :          0 :     int incr = 1;
    2422                 :            : 
    2423         [ #  # ]:          0 :     if (le) {
    2424                 :          0 :         p += 1;
    2425                 :          0 :         incr = -1;
    2426                 :            :     }
    2427                 :            : 
    2428                 :            :     /* First byte */
    2429                 :          0 :     sign = (*p >> 7) & 1;
    2430                 :          0 :     e = (*p & 0x7C) >> 2;
    2431                 :          0 :     f = (*p & 0x03) << 8;
    2432                 :          0 :     p += incr;
    2433                 :            : 
    2434                 :            :     /* Second byte */
    2435                 :          0 :     f |= *p;
    2436                 :            : 
    2437         [ #  # ]:          0 :     if (e == 0x1f) {
    2438                 :            : #if _PY_SHORT_FLOAT_REPR == 0
    2439                 :            :         if (f == 0) {
    2440                 :            :             /* Infinity */
    2441                 :            :             return sign ? -Py_HUGE_VAL : Py_HUGE_VAL;
    2442                 :            :         }
    2443                 :            :         else {
    2444                 :            :             /* NaN */
    2445                 :            :             return sign ? -Py_NAN : Py_NAN;
    2446                 :            :         }
    2447                 :            : #else  // _PY_SHORT_FLOAT_REPR == 1
    2448         [ #  # ]:          0 :         if (f == 0) {
    2449                 :            :             /* Infinity */
    2450                 :          0 :             return _Py_dg_infinity(sign);
    2451                 :            :         }
    2452                 :            :         else {
    2453                 :            :             /* NaN */
    2454                 :          0 :             return _Py_dg_stdnan(sign);
    2455                 :            :         }
    2456                 :            : #endif  // _PY_SHORT_FLOAT_REPR == 1
    2457                 :            :     }
    2458                 :            : 
    2459                 :          0 :     x = (double)f / 1024.0;
    2460                 :            : 
    2461         [ #  # ]:          0 :     if (e == 0) {
    2462                 :          0 :         e = -14;
    2463                 :            :     }
    2464                 :            :     else {
    2465                 :          0 :         x += 1.0;
    2466                 :          0 :         e -= 15;
    2467                 :            :     }
    2468                 :          0 :     x = ldexp(x, e);
    2469                 :            : 
    2470         [ #  # ]:          0 :     if (sign)
    2471                 :          0 :         x = -x;
    2472                 :            : 
    2473                 :          0 :     return x;
    2474                 :            : }
    2475                 :            : 
    2476                 :            : double
    2477                 :          0 : PyFloat_Unpack4(const char *data, int le)
    2478                 :            : {
    2479                 :          0 :     unsigned char *p = (unsigned char *)data;
    2480         [ #  # ]:          0 :     if (float_format == unknown_format) {
    2481                 :            :         unsigned char sign;
    2482                 :            :         int e;
    2483                 :            :         unsigned int f;
    2484                 :            :         double x;
    2485                 :          0 :         int incr = 1;
    2486                 :            : 
    2487         [ #  # ]:          0 :         if (le) {
    2488                 :          0 :             p += 3;
    2489                 :          0 :             incr = -1;
    2490                 :            :         }
    2491                 :            : 
    2492                 :            :         /* First byte */
    2493                 :          0 :         sign = (*p >> 7) & 1;
    2494                 :          0 :         e = (*p & 0x7F) << 1;
    2495                 :          0 :         p += incr;
    2496                 :            : 
    2497                 :            :         /* Second byte */
    2498                 :          0 :         e |= (*p >> 7) & 1;
    2499                 :          0 :         f = (*p & 0x7F) << 16;
    2500                 :          0 :         p += incr;
    2501                 :            : 
    2502         [ #  # ]:          0 :         if (e == 255) {
    2503                 :          0 :             PyErr_SetString(
    2504                 :            :                 PyExc_ValueError,
    2505                 :            :                 "can't unpack IEEE 754 special value "
    2506                 :            :                 "on non-IEEE platform");
    2507                 :          0 :             return -1;
    2508                 :            :         }
    2509                 :            : 
    2510                 :            :         /* Third byte */
    2511                 :          0 :         f |= *p << 8;
    2512                 :          0 :         p += incr;
    2513                 :            : 
    2514                 :            :         /* Fourth byte */
    2515                 :          0 :         f |= *p;
    2516                 :            : 
    2517                 :          0 :         x = (double)f / 8388608.0;
    2518                 :            : 
    2519                 :            :         /* XXX This sadly ignores Inf/NaN issues */
    2520         [ #  # ]:          0 :         if (e == 0)
    2521                 :          0 :             e = -126;
    2522                 :            :         else {
    2523                 :          0 :             x += 1.0;
    2524                 :          0 :             e -= 127;
    2525                 :            :         }
    2526                 :          0 :         x = ldexp(x, e);
    2527                 :            : 
    2528         [ #  # ]:          0 :         if (sign)
    2529                 :          0 :             x = -x;
    2530                 :            : 
    2531                 :          0 :         return x;
    2532                 :            :     }
    2533                 :            :     else {
    2534                 :            :         float x;
    2535                 :            : 
    2536   [ #  #  #  # ]:          0 :         if ((float_format == ieee_little_endian_format && !le)
    2537   [ #  #  #  # ]:          0 :             || (float_format == ieee_big_endian_format && le)) {
    2538                 :            :             char buf[4];
    2539                 :          0 :             char *d = &buf[3];
    2540                 :            :             int i;
    2541                 :            : 
    2542         [ #  # ]:          0 :             for (i = 0; i < 4; i++) {
    2543                 :          0 :                 *d-- = *p++;
    2544                 :            :             }
    2545                 :          0 :             memcpy(&x, buf, 4);
    2546                 :            :         }
    2547                 :            :         else {
    2548                 :          0 :             memcpy(&x, p, 4);
    2549                 :            :         }
    2550                 :            : 
    2551                 :          0 :         return x;
    2552                 :            :     }
    2553                 :            : }
    2554                 :            : 
    2555                 :            : double
    2556                 :          4 : PyFloat_Unpack8(const char *data, int le)
    2557                 :            : {
    2558                 :          4 :     unsigned char *p = (unsigned char *)data;
    2559         [ -  + ]:          4 :     if (double_format == unknown_format) {
    2560                 :            :         unsigned char sign;
    2561                 :            :         int e;
    2562                 :            :         unsigned int fhi, flo;
    2563                 :            :         double x;
    2564                 :          0 :         int incr = 1;
    2565                 :            : 
    2566         [ #  # ]:          0 :         if (le) {
    2567                 :          0 :             p += 7;
    2568                 :          0 :             incr = -1;
    2569                 :            :         }
    2570                 :            : 
    2571                 :            :         /* First byte */
    2572                 :          0 :         sign = (*p >> 7) & 1;
    2573                 :          0 :         e = (*p & 0x7F) << 4;
    2574                 :            : 
    2575                 :          0 :         p += incr;
    2576                 :            : 
    2577                 :            :         /* Second byte */
    2578                 :          0 :         e |= (*p >> 4) & 0xF;
    2579                 :          0 :         fhi = (*p & 0xF) << 24;
    2580                 :          0 :         p += incr;
    2581                 :            : 
    2582         [ #  # ]:          0 :         if (e == 2047) {
    2583                 :          0 :             PyErr_SetString(
    2584                 :            :                 PyExc_ValueError,
    2585                 :            :                 "can't unpack IEEE 754 special value "
    2586                 :            :                 "on non-IEEE platform");
    2587                 :          0 :             return -1.0;
    2588                 :            :         }
    2589                 :            : 
    2590                 :            :         /* Third byte */
    2591                 :          0 :         fhi |= *p << 16;
    2592                 :          0 :         p += incr;
    2593                 :            : 
    2594                 :            :         /* Fourth byte */
    2595                 :          0 :         fhi |= *p  << 8;
    2596                 :          0 :         p += incr;
    2597                 :            : 
    2598                 :            :         /* Fifth byte */
    2599                 :          0 :         fhi |= *p;
    2600                 :          0 :         p += incr;
    2601                 :            : 
    2602                 :            :         /* Sixth byte */
    2603                 :          0 :         flo = *p << 16;
    2604                 :          0 :         p += incr;
    2605                 :            : 
    2606                 :            :         /* Seventh byte */
    2607                 :          0 :         flo |= *p << 8;
    2608                 :          0 :         p += incr;
    2609                 :            : 
    2610                 :            :         /* Eighth byte */
    2611                 :          0 :         flo |= *p;
    2612                 :            : 
    2613                 :          0 :         x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
    2614                 :          0 :         x /= 268435456.0; /* 2**28 */
    2615                 :            : 
    2616         [ #  # ]:          0 :         if (e == 0)
    2617                 :          0 :             e = -1022;
    2618                 :            :         else {
    2619                 :          0 :             x += 1.0;
    2620                 :          0 :             e -= 1023;
    2621                 :            :         }
    2622                 :          0 :         x = ldexp(x, e);
    2623                 :            : 
    2624         [ #  # ]:          0 :         if (sign)
    2625                 :          0 :             x = -x;
    2626                 :            : 
    2627                 :          0 :         return x;
    2628                 :            :     }
    2629                 :            :     else {
    2630                 :            :         double x;
    2631                 :            : 
    2632   [ +  -  +  - ]:          4 :         if ((double_format == ieee_little_endian_format && !le)
    2633   [ -  +  -  - ]:          4 :             || (double_format == ieee_big_endian_format && le)) {
    2634                 :            :             char buf[8];
    2635                 :          0 :             char *d = &buf[7];
    2636                 :            :             int i;
    2637                 :            : 
    2638         [ #  # ]:          0 :             for (i = 0; i < 8; i++) {
    2639                 :          0 :                 *d-- = *p++;
    2640                 :            :             }
    2641                 :          0 :             memcpy(&x, buf, 8);
    2642                 :            :         }
    2643                 :            :         else {
    2644                 :          4 :             memcpy(&x, p, 8);
    2645                 :            :         }
    2646                 :            : 
    2647                 :          4 :         return x;
    2648                 :            :     }
    2649                 :            : }

Generated by: LCOV version 1.14