#include "Python.h"
#include "pycore_dtoa.h"
#include "pycore_floatobject.h"
#include "pycore_initconfig.h"
#include "pycore_interp.h"
#include "pycore_long.h"
#include "pycore_object.h"
#include "pycore_pymath.h"
#include "pycore_pystate.h"
#include "pycore_structseq.h"
#include <ctype.h>
#include <float.h>
#include <stdlib.h>
#include "clinic/floatobject.c.h"
#ifndef PyFloat_MAXFREELIST
# define PyFloat_MAXFREELIST 100
#endif
#if PyFloat_MAXFREELIST > 0
static struct _Py_float_state *
get_float_state(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
return &interp->float_state;
}
#endif
double
PyFloat_GetMax(void)
{
return DBL_MAX;
}
double
PyFloat_GetMin(void)
{
return DBL_MIN;
}
static PyTypeObject FloatInfoType;
PyDoc_STRVAR(floatinfo__doc__,
"sys.float_info\n\
\n\
A named tuple holding information about the float type. It contains low level\n\
information about the precision and internal representation. Please study\n\
your system's :file:`float.h` for more information.");
static PyStructSequence_Field floatinfo_fields[] = {
{"max", "DBL_MAX -- maximum representable finite float"},
{"max_exp", "DBL_MAX_EXP -- maximum int e such that radix**(e-1) "
"is representable"},
{"max_10_exp", "DBL_MAX_10_EXP -- maximum int e such that 10**e "
"is representable"},
{"min", "DBL_MIN -- Minimum positive normalized float"},
{"min_exp", "DBL_MIN_EXP -- minimum int e such that radix**(e-1) "
"is a normalized float"},
{"min_10_exp", "DBL_MIN_10_EXP -- minimum int e such that 10**e is "
"a normalized float"},
{"dig", "DBL_DIG -- maximum number of decimal digits that "
"can be faithfully represented in a float"},
{"mant_dig", "DBL_MANT_DIG -- mantissa digits"},
{"epsilon", "DBL_EPSILON -- Difference between 1 and the next "
"representable float"},
{"radix", "FLT_RADIX -- radix of exponent"},
{"rounds", "FLT_ROUNDS -- rounding mode used for arithmetic "
"operations"},
{0}
};
static PyStructSequence_Desc floatinfo_desc = {
"sys.float_info",
floatinfo__doc__,
floatinfo_fields,
11
};
PyObject *
PyFloat_GetInfo(void)
{
PyObject* floatinfo;
int pos = 0;
floatinfo = PyStructSequence_New(&FloatInfoType);
if (floatinfo == NULL) {
return NULL;
}
#define SetIntFlag(flag) \
PyStructSequence_SET_ITEM(floatinfo, pos++, PyLong_FromLong(flag))
#define SetDblFlag(flag) \
PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag))
SetDblFlag(DBL_MAX);
SetIntFlag(DBL_MAX_EXP);
SetIntFlag(DBL_MAX_10_EXP);
SetDblFlag(DBL_MIN);
SetIntFlag(DBL_MIN_EXP);
SetIntFlag(DBL_MIN_10_EXP);
SetIntFlag(DBL_DIG);
SetIntFlag(DBL_MANT_DIG);
SetDblFlag(DBL_EPSILON);
SetIntFlag(FLT_RADIX);
SetIntFlag(FLT_ROUNDS);
#undef SetIntFlag
#undef SetDblFlag
if (PyErr_Occurred()) {
Py_CLEAR(floatinfo);
return NULL;
}
return floatinfo;
}
PyObject *
PyFloat_FromDouble(double fval)
{
PyFloatObject *op;
#if PyFloat_MAXFREELIST > 0
struct _Py_float_state *state = get_float_state();
op = state->free_list;
if (op != NULL) {
#ifdef Py_DEBUG
assert(state->numfree != -1);
#endif
state->free_list = (PyFloatObject *) Py_TYPE(op);
state->numfree--;
OBJECT_STAT_INC(from_freelist);
}
else
#endif
{
op = PyObject_Malloc(sizeof(PyFloatObject));
if (!op) {
return PyErr_NoMemory();
}
}
_PyObject_Init((PyObject*)op, &PyFloat_Type);
op->ob_fval = fval;
return (PyObject *) op;
}
static PyObject *
float_from_string_inner(const char *s, Py_ssize_t len, void *obj)
{
double x;
const char *end;
const char *last = s + len;
while (s < last && Py_ISSPACE(*s)) {
s++;
}
if (s == last) {
PyErr_Format(PyExc_ValueError,
"could not convert string to float: "
"%R", obj);
return NULL;
}
while (s < last - 1 && Py_ISSPACE(last[-1])) {
last--;
}
x = PyOS_string_to_double(s, (char **)&end, NULL);
if (end != last) {
PyErr_Format(PyExc_ValueError,
"could not convert string to float: "
"%R", obj);
return NULL;
}
else if (x == -1.0 && PyErr_Occurred()) {
return NULL;
}
else {
return PyFloat_FromDouble(x);
}
}
PyObject *
PyFloat_FromString(PyObject *v)
{
const char *s;
PyObject *s_buffer = NULL;
Py_ssize_t len;
Py_buffer view = {NULL, NULL};
PyObject *result = NULL;
if (PyUnicode_Check(v)) {
s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v);
if (s_buffer == NULL)
return NULL;
assert(PyUnicode_IS_ASCII(s_buffer));
s = PyUnicode_AsUTF8AndSize(s_buffer, &len);
assert(s != NULL);
}
else if (PyBytes_Check(v)) {
s = PyBytes_AS_STRING(v);
len = PyBytes_GET_SIZE(v);
}
else if (PyByteArray_Check(v)) {
s = PyByteArray_AS_STRING(v);
len = PyByteArray_GET_SIZE(v);
}
else if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) == 0) {
s = (const char *)view.buf;
len = view.len;
s_buffer = PyBytes_FromStringAndSize(s, len);
if (s_buffer == NULL) {
PyBuffer_Release(&view);
return NULL;
}
s = PyBytes_AS_STRING(s_buffer);
}
else {
PyErr_Format(PyExc_TypeError,
"float() argument must be a string or a real number, not '%.200s'",
Py_TYPE(v)->tp_name);
return NULL;
}
result = _Py_string_to_number_with_underscores(s, len, "float", v, v,
float_from_string_inner);
PyBuffer_Release(&view);
Py_XDECREF(s_buffer);
return result;
}
void
_PyFloat_ExactDealloc(PyObject *obj)
{
assert(PyFloat_CheckExact(obj));
PyFloatObject *op = (PyFloatObject *)obj;
#if PyFloat_MAXFREELIST > 0
struct _Py_float_state *state = get_float_state();
#ifdef Py_DEBUG
assert(state->numfree != -1);
#endif
if (state->numfree >= PyFloat_MAXFREELIST) {
PyObject_Free(op);
return;
}
state->numfree++;
Py_SET_TYPE(op, (PyTypeObject *)state->free_list);
state->free_list = op;
OBJECT_STAT_INC(to_freelist);
#else
PyObject_Free(op);
#endif
}
static void
float_dealloc(PyObject *op)
{
assert(PyFloat_Check(op));
#if PyFloat_MAXFREELIST > 0
if (PyFloat_CheckExact(op)) {
_PyFloat_ExactDealloc(op);
}
else
#endif
{
Py_TYPE(op)->tp_free(op);
}
}
double
PyFloat_AsDouble(PyObject *op)
{
PyNumberMethods *nb;
PyObject *res;
double val;
if (op == NULL) {
PyErr_BadArgument();
return -1;
}
if (PyFloat_Check(op)) {
return PyFloat_AS_DOUBLE(op);
}
nb = Py_TYPE(op)->tp_as_number;
if (nb == NULL || nb->nb_float == NULL) {
if (nb && nb->nb_index) {
PyObject *res = _PyNumber_Index(op);
if (!res) {
return -1;
}
double val = PyLong_AsDouble(res);
Py_DECREF(res);
return val;
}
PyErr_Format(PyExc_TypeError, "must be real number, not %.50s",
Py_TYPE(op)->tp_name);
return -1;
}
res = (*nb->nb_float) (op);
if (res == NULL) {
return -1;
}
if (!PyFloat_CheckExact(res)) {
if (!PyFloat_Check(res)) {
PyErr_Format(PyExc_TypeError,
"%.50s.__float__ returned non-float (type %.50s)",
Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name);
Py_DECREF(res);
return -1;
}
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"%.50s.__float__ returned non-float (type %.50s). "
"The ability to return an instance of a strict subclass of float "
"is deprecated, and may be removed in a future version of Python.",
Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name)) {
Py_DECREF(res);
return -1;
}
}
val = PyFloat_AS_DOUBLE(res);
Py_DECREF(res);
return val;
}
#define CONVERT_TO_DOUBLE(obj, dbl) \
if (PyFloat_Check(obj)) \
dbl = PyFloat_AS_DOUBLE(obj); \
else if (convert_to_double(&(obj), &(dbl)) < 0) \
return obj;
static int
convert_to_double(PyObject **v, double *dbl)
{
PyObject *obj = *v;
if (PyLong_Check(obj)) {
*dbl = PyLong_AsDouble(obj);
if (*dbl == -1.0 && PyErr_Occurred()) {
*v = NULL;
return -1;
}
}
else {
*v = Py_NewRef(Py_NotImplemented);
return -1;
}
return 0;
}
static PyObject *
float_repr(PyFloatObject *v)
{
PyObject *result;
char *buf;
buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
'r', 0,
Py_DTSF_ADD_DOT_0,
NULL);
if (!buf)
return PyErr_NoMemory();
result = _PyUnicode_FromASCII(buf, strlen(buf));
PyMem_Free(buf);
return result;
}
static PyObject*
float_richcompare(PyObject *v, PyObject *w, int op)
{
double i, j;
int r = 0;
assert(PyFloat_Check(v));
i = PyFloat_AS_DOUBLE(v);
if (PyFloat_Check(w))
j = PyFloat_AS_DOUBLE(w);
else if (!Py_IS_FINITE(i)) {
if (PyLong_Check(w))
j = 0.0;
else
goto Unimplemented;
}
else if (PyLong_Check(w)) {
int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1;
int wsign = _PyLong_Sign(w);
size_t nbits;
int exponent;
if (vsign != wsign) {
i = (double)vsign;
j = (double)wsign;
goto Compare;
}
nbits = _PyLong_NumBits(w);
if (nbits == (size_t)-1 && PyErr_Occurred()) {
PyErr_Clear();
i = (double)vsign;
assert(wsign != 0);
j = wsign * 2.0;
goto Compare;
}
if (nbits <= 48) {
j = PyLong_AsDouble(w);
assert(j != -1.0 || ! PyErr_Occurred());
goto Compare;
}
assert(wsign != 0);
assert(vsign != 0);
if (vsign < 0) {
i = -i;
op = _Py_SwappedOp[op];
}
assert(i > 0.0);
(void) frexp(i, &exponent);
if (exponent < 0 || (size_t)exponent < nbits) {
i = 1.0;
j = 2.0;
goto Compare;
}
if ((size_t)exponent > nbits) {
i = 2.0;
j = 1.0;
goto Compare;
}
{
double fracpart;
double intpart;
PyObject *result = NULL;
PyObject *vv = NULL;
PyObject *ww = w;
if (wsign < 0) {
ww = PyNumber_Negative(w);
if (ww == NULL)
goto Error;
}
else
Py_INCREF(ww);
fracpart = modf(i, &intpart);
vv = PyLong_FromDouble(intpart);
if (vv == NULL)
goto Error;
if (fracpart != 0.0) {
PyObject *temp;
temp = _PyLong_Lshift(ww, 1);
if (temp == NULL)
goto Error;
Py_SETREF(ww, temp);
temp = _PyLong_Lshift(vv, 1);
if (temp == NULL)
goto Error;
Py_SETREF(vv, temp);
temp = PyNumber_Or(vv, _PyLong_GetOne());
if (temp == NULL)
goto Error;
Py_SETREF(vv, temp);
}
r = PyObject_RichCompareBool(vv, ww, op);
if (r < 0)
goto Error;
result = PyBool_FromLong(r);
Error:
Py_XDECREF(vv);
Py_XDECREF(ww);
return result;
}
}
else
goto Unimplemented;
Compare:
switch (op) {
case Py_EQ:
r = i == j;
break;
case Py_NE:
r = i != j;
break;
case Py_LE:
r = i <= j;
break;
case Py_GE:
r = i >= j;
break;
case Py_LT:
r = i < j;
break;
case Py_GT:
r = i > j;
break;
}
return PyBool_FromLong(r);
Unimplemented:
Py_RETURN_NOTIMPLEMENTED;
}
static Py_hash_t
float_hash(PyFloatObject *v)
{
return _Py_HashDouble((PyObject *)v, v->ob_fval);
}
static PyObject *
float_add(PyObject *v, PyObject *w)
{
double a,b;
CONVERT_TO_DOUBLE(v, a);
CONVERT_TO_DOUBLE(w, b);
a = a + b;
return PyFloat_FromDouble(a);
}
static PyObject *
float_sub(PyObject *v, PyObject *w)
{
double a,b;
CONVERT_TO_DOUBLE(v, a);
CONVERT_TO_DOUBLE(w, b);
a = a - b;
return PyFloat_FromDouble(a);
}
static PyObject *
float_mul(PyObject *v, PyObject *w)
{
double a,b;
CONVERT_TO_DOUBLE(v, a);
CONVERT_TO_DOUBLE(w, b);
a = a * b;
return PyFloat_FromDouble(a);
}
static PyObject *
float_div(PyObject *v, PyObject *w)
{
double a,b;
CONVERT_TO_DOUBLE(v, a);
CONVERT_TO_DOUBLE(w, b);
if (b == 0.0) {
PyErr_SetString(PyExc_ZeroDivisionError,
"float division by zero");
return NULL;
}
a = a / b;
return PyFloat_FromDouble(a);
}
static PyObject *
float_rem(PyObject *v, PyObject *w)
{
double vx, wx;
double mod;
CONVERT_TO_DOUBLE(v, vx);
CONVERT_TO_DOUBLE(w, wx);
if (wx == 0.0) {
PyErr_SetString(PyExc_ZeroDivisionError,
"float modulo");
return NULL;
}
mod = fmod(vx, wx);
if (mod) {
if ((wx < 0) != (mod < 0)) {
mod += wx;
}
}
else {
mod = copysign(0.0, wx);
}
return PyFloat_FromDouble(mod);
}
static void
_float_div_mod(double vx, double wx, double *floordiv, double *mod)
{
double div;
*mod = fmod(vx, wx);
div = (vx - *mod) / wx;
if (*mod) {
if ((wx < 0) != (*mod < 0)) {
*mod += wx;
div -= 1.0;
}
}
else {
*mod = copysign(0.0, wx);
}
if (div) {
*floordiv = floor(div);
if (div - *floordiv > 0.5) {
*floordiv += 1.0;
}
}
else {
*floordiv = copysign(0.0, vx / wx);
}
}
static PyObject *
float_divmod(PyObject *v, PyObject *w)
{
double vx, wx;
double mod, floordiv;
CONVERT_TO_DOUBLE(v, vx);
CONVERT_TO_DOUBLE(w, wx);
if (wx == 0.0) {
PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
return NULL;
}
_float_div_mod(vx, wx, &floordiv, &mod);
return Py_BuildValue("(dd)", floordiv, mod);
}
static PyObject *
float_floor_div(PyObject *v, PyObject *w)
{
double vx, wx;
double mod, floordiv;
CONVERT_TO_DOUBLE(v, vx);
CONVERT_TO_DOUBLE(w, wx);
if (wx == 0.0) {
PyErr_SetString(PyExc_ZeroDivisionError, "float floor division by zero");
return NULL;
}
_float_div_mod(vx, wx, &floordiv, &mod);
return PyFloat_FromDouble(floordiv);
}
#define DOUBLE_IS_ODD_INTEGER(x) (fmod(fabs(x), 2.0) == 1.0)
static PyObject *
float_pow(PyObject *v, PyObject *w, PyObject *z)
{
double iv, iw, ix;
int negate_result = 0;
if ((PyObject *)z != Py_None) {
PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
"allowed unless all arguments are integers");
return NULL;
}
CONVERT_TO_DOUBLE(v, iv);
CONVERT_TO_DOUBLE(w, iw);
if (iw == 0) {
return PyFloat_FromDouble(1.0);
}
if (Py_IS_NAN(iv)) {
return PyFloat_FromDouble(iv);
}
if (Py_IS_NAN(iw)) {
return PyFloat_FromDouble(iv == 1.0 ? 1.0 : iw);
}
if (Py_IS_INFINITY(iw)) {
iv = fabs(iv);
if (iv == 1.0)
return PyFloat_FromDouble(1.0);
else if ((iw > 0.0) == (iv > 1.0))
return PyFloat_FromDouble(fabs(iw));
else
return PyFloat_FromDouble(0.0);
}
if (Py_IS_INFINITY(iv)) {
int iw_is_odd = DOUBLE_IS_ODD_INTEGER(iw);
if (iw > 0.0)
return PyFloat_FromDouble(iw_is_odd ? iv : fabs(iv));
else
return PyFloat_FromDouble(iw_is_odd ?
copysign(0.0, iv) : 0.0);
}
if (iv == 0.0) {
int iw_is_odd = DOUBLE_IS_ODD_INTEGER(iw);
if (iw < 0.0) {
PyErr_SetString(PyExc_ZeroDivisionError,
"0.0 cannot be raised to a "
"negative power");
return NULL;
}
return PyFloat_FromDouble(iw_is_odd ? iv : 0.0);
}
if (iv < 0.0) {
if (iw != floor(iw)) {
return PyComplex_Type.tp_as_number->nb_power(v, w, z);
}
iv = -iv;
negate_result = DOUBLE_IS_ODD_INTEGER(iw);
}
if (iv == 1.0) {
return PyFloat_FromDouble(negate_result ? -1.0 : 1.0);
}
errno = 0;
ix = pow(iv, iw);
_Py_ADJUST_ERANGE1(ix);
if (negate_result)
ix = -ix;
if (errno != 0) {
PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
PyExc_ValueError);
return NULL;
}
return PyFloat_FromDouble(ix);
}
#undef DOUBLE_IS_ODD_INTEGER
static PyObject *
float_neg(PyFloatObject *v)
{
return PyFloat_FromDouble(-v->ob_fval);
}
static PyObject *
float_abs(PyFloatObject *v)
{
return PyFloat_FromDouble(fabs(v->ob_fval));
}
static int
float_bool(PyFloatObject *v)
{
return v->ob_fval != 0.0;
}
static PyObject *
float_is_integer_impl(PyObject *self)
{
double x = PyFloat_AsDouble(self);
PyObject *o;
if (x == -1.0 && PyErr_Occurred())
return NULL;
if (!Py_IS_FINITE(x))
Py_RETURN_FALSE;
errno = 0;
o = (floor(x) == x) ? Py_True : Py_False;
if (errno != 0) {
PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
PyExc_ValueError);
return NULL;
}
return Py_NewRef(o);
}
static PyObject *
float___trunc___impl(PyObject *self)
{
return PyLong_FromDouble(PyFloat_AS_DOUBLE(self));
}
static PyObject *
float___floor___impl(PyObject *self)
{
double x = PyFloat_AS_DOUBLE(self);
return PyLong_FromDouble(floor(x));
}
static PyObject *
float___ceil___impl(PyObject *self)
{
double x = PyFloat_AS_DOUBLE(self);
return PyLong_FromDouble(ceil(x));
}
#if _PY_SHORT_FLOAT_REPR == 1
static PyObject *
double_round(double x, int ndigits) {
double rounded;
Py_ssize_t buflen, mybuflen=100;
char *buf, *buf_end, shortbuf[100], *mybuf=shortbuf;
int decpt, sign;
PyObject *result = NULL;
_Py_SET_53BIT_PRECISION_HEADER;
_Py_SET_53BIT_PRECISION_START;
buf = _Py_dg_dtoa(x, 3, ndigits, &decpt, &sign, &buf_end);
_Py_SET_53BIT_PRECISION_END;
if (buf == NULL) {
PyErr_NoMemory();
return NULL;
}
buflen = buf_end - buf;
if (buflen + 8 > mybuflen) {
mybuflen = buflen+8;
mybuf = (char *)PyMem_Malloc(mybuflen);
if (mybuf == NULL) {
PyErr_NoMemory();
goto exit;
}
}
PyOS_snprintf(mybuf, mybuflen, "%s0%se%d", (sign ? "-" : ""),
buf, decpt - (int)buflen);
errno = 0;
_Py_SET_53BIT_PRECISION_START;
rounded = _Py_dg_strtod(mybuf, NULL);
_Py_SET_53BIT_PRECISION_END;
if (errno == ERANGE && fabs(rounded) >= 1.)
PyErr_SetString(PyExc_OverflowError,
"rounded value too large to represent");
else
result = PyFloat_FromDouble(rounded);
if (mybuf != shortbuf)
PyMem_Free(mybuf);
exit:
_Py_dg_freedtoa(buf);
return result;
}
#else
static PyObject *
double_round(double x, int ndigits) {
double pow1, pow2, y, z;
if (ndigits >= 0) {
if (ndigits > 22) {
pow1 = pow(10.0, (double)(ndigits-22));
pow2 = 1e22;
}
else {
pow1 = pow(10.0, (double)ndigits);
pow2 = 1.0;
}
y = (x*pow1)*pow2;
if (!Py_IS_FINITE(y))
return PyFloat_FromDouble(x);
}
else {
pow1 = pow(10.0, (double)-ndigits);
pow2 = 1.0;
y = x / pow1;
}
z = round(y);
if (fabs(y-z) == 0.5)
z = 2.0*round(y/2.0);
if (ndigits >= 0)
z = (z / pow2) / pow1;
else
z *= pow1;
if (!Py_IS_FINITE(z)) {
PyErr_SetString(PyExc_OverflowError,
"overflow occurred during round");
return NULL;
}
return PyFloat_FromDouble(z);
}
#endif
static PyObject *
float___round___impl(PyObject *self, PyObject *o_ndigits)
{
double x, rounded;
Py_ssize_t ndigits;
x = PyFloat_AsDouble(self);
if (o_ndigits == Py_None) {
rounded = round(x);
if (fabs(x-rounded) == 0.5)
rounded = 2.0*round(x/2.0);
return PyLong_FromDouble(rounded);
}
ndigits = PyNumber_AsSsize_t(o_ndigits, NULL);
if (ndigits == -1 && PyErr_Occurred())
return NULL;
if (!Py_IS_FINITE(x))
return PyFloat_FromDouble(x);
#define NDIGITS_MAX ((int)((DBL_MANT_DIG-DBL_MIN_EXP) * 0.30103))
#define NDIGITS_MIN (-(int)((DBL_MAX_EXP + 1) * 0.30103))
if (ndigits > NDIGITS_MAX)
return PyFloat_FromDouble(x);
else if (ndigits < NDIGITS_MIN)
return PyFloat_FromDouble(0.0*x);
else
return double_round(x, (int)ndigits);
#undef NDIGITS_MAX
#undef NDIGITS_MIN
}
static PyObject *
float_float(PyObject *v)
{
if (PyFloat_CheckExact(v)) {
return Py_NewRef(v);
}
else {
return PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);
}
}
static PyObject *
float_conjugate_impl(PyObject *self)
{
return float_float(self);
}
static char
char_from_hex(int x)
{
assert(0 <= x && x < 16);
return Py_hexdigits[x];
}
static int
hex_from_char(char c) {
int x;
switch(c) {
case '0':
x = 0;
break;
case '1':
x = 1;
break;
case '2':
x = 2;
break;
case '3':
x = 3;
break;
case '4':
x = 4;
break;
case '5':
x = 5;
break;
case '6':
x = 6;
break;
case '7':
x = 7;
break;
case '8':
x = 8;
break;
case '9':
x = 9;
break;
case 'a':
case 'A':
x = 10;
break;
case 'b':
case 'B':
x = 11;
break;
case 'c':
case 'C':
x = 12;
break;
case 'd':
case 'D':
x = 13;
break;
case 'e':
case 'E':
x = 14;
break;
case 'f':
case 'F':
x = 15;
break;
default:
x = -1;
break;
}
return x;
}
#define TOHEX_NBITS DBL_MANT_DIG + 3 - (DBL_MANT_DIG+2)%4
static PyObject *
float_hex_impl(PyObject *self)
{
double x, m;
int e, shift, i, si, esign;
char s[(TOHEX_NBITS-1)/4+3];
CONVERT_TO_DOUBLE(self, x);
if (Py_IS_NAN(x) || Py_IS_INFINITY(x))
return float_repr((PyFloatObject *)self);
if (x == 0.0) {
if (copysign(1.0, x) == -1.0)
return PyUnicode_FromString("-0x0.0p+0");
else
return PyUnicode_FromString("0x0.0p+0");
}
m = frexp(fabs(x), &e);
shift = 1 - Py_MAX(DBL_MIN_EXP - e, 0);
m = ldexp(m, shift);
e -= shift;
si = 0;
s[si] = char_from_hex((int)m);
si++;
m -= (int)m;
s[si] = '.';
si++;
for (i=0; i < (TOHEX_NBITS-1)/4; i++) {
m *= 16.0;
s[si] = char_from_hex((int)m);
si++;
m -= (int)m;
}
s[si] = '\0';
if (e < 0) {
esign = (int)'-';
e = -e;
}
else
esign = (int)'+';
if (x < 0.0)
return PyUnicode_FromFormat("-0x%sp%c%d", s, esign, e);
else
return PyUnicode_FromFormat("0x%sp%c%d", s, esign, e);
}
static PyObject *
float_fromhex(PyTypeObject *type, PyObject *string)
{
PyObject *result;
double x;
long exp, top_exp, lsb, key_digit;
const char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end;
int half_eps, digit, round_up, negate=0;
Py_ssize_t length, ndigits, fdigits, i;
s = PyUnicode_AsUTF8AndSize(string, &length);
if (s == NULL)
return NULL;
s_end = s + length;
while (Py_ISSPACE(*s))
s++;
x = _Py_parse_inf_or_nan(s, (char **)&coeff_end);
if (coeff_end != s) {
s = coeff_end;
goto finished;
}
if (*s == '-') {
s++;
negate = 1;
}
else if (*s == '+')
s++;
s_store = s;
if (*s == '0') {
s++;
if (*s == 'x' || *s == 'X')
s++;
else
s = s_store;
}
coeff_start = s;
while (hex_from_char(*s) >= 0)
s++;
s_store = s;
if (*s == '.') {
s++;
while (hex_from_char(*s) >= 0)
s++;
coeff_end = s-1;
}
else
coeff_end = s;
ndigits = coeff_end - coeff_start;
fdigits = coeff_end - s_store;
if (ndigits == 0)
goto parse_error;
if (ndigits > Py_MIN(DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2,
LONG_MAX/2 + 1 - DBL_MAX_EXP)/4)
goto insane_length_error;
if (*s == 'p' || *s == 'P') {
s++;
exp_start = s;
if (*s == '-' || *s == '+')
s++;
if (!('0' <= *s && *s <= '9'))
goto parse_error;
s++;
while ('0' <= *s && *s <= '9')
s++;
exp = strtol(exp_start, NULL, 10);
}
else
exp = 0;
#define HEX_DIGIT(j) hex_from_char(*((j) < fdigits ? \
coeff_end-(j) : \
coeff_end-1-(j)))
while (ndigits > 0 && HEX_DIGIT(ndigits-1) == 0)
ndigits--;
if (ndigits == 0 || exp < LONG_MIN/2) {
x = 0.0;
goto finished;
}
if (exp > LONG_MAX/2)
goto overflow_error;
exp = exp - 4*((long)fdigits);
top_exp = exp + 4*((long)ndigits - 1);
for (digit = HEX_DIGIT(ndigits-1); digit != 0; digit /= 2)
top_exp++;
if (top_exp < DBL_MIN_EXP - DBL_MANT_DIG) {
x = 0.0;
goto finished;
}
if (top_exp > DBL_MAX_EXP)
goto overflow_error;
lsb = Py_MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG;
x = 0.0;
if (exp >= lsb) {
for (i = ndigits-1; i >= 0; i--)
x = 16.0*x + HEX_DIGIT(i);
x = ldexp(x, (int)(exp));
goto finished;
}
half_eps = 1 << (int)((lsb - exp - 1) % 4);
key_digit = (lsb - exp - 1) / 4;
for (i = ndigits-1; i > key_digit; i--)
x = 16.0*x + HEX_DIGIT(i);
digit = HEX_DIGIT(key_digit);
x = 16.0*x + (double)(digit & (16-2*half_eps));
if ((digit & half_eps) != 0) {
round_up = 0;
if ((digit & (3*half_eps-1)) != 0 || (half_eps == 8 &&
key_digit+1 < ndigits && (HEX_DIGIT(key_digit+1) & 1) != 0))
round_up = 1;
else
for (i = key_digit-1; i >= 0; i--)
if (HEX_DIGIT(i) != 0) {
round_up = 1;
break;
}
if (round_up) {
x += 2*half_eps;
if (top_exp == DBL_MAX_EXP &&
x == ldexp((double)(2*half_eps), DBL_MANT_DIG))
goto overflow_error;
}
}
x = ldexp(x, (int)(exp+4*key_digit));
finished:
while (Py_ISSPACE(*s))
s++;
if (s != s_end)
goto parse_error;
result = PyFloat_FromDouble(negate ? -x : x);
if (type != &PyFloat_Type && result != NULL) {
Py_SETREF(result, PyObject_CallOneArg((PyObject *)type, result));
}
return result;
overflow_error:
PyErr_SetString(PyExc_OverflowError,
"hexadecimal value too large to represent as a float");
return NULL;
parse_error:
PyErr_SetString(PyExc_ValueError,
"invalid hexadecimal floating-point string");
return NULL;
insane_length_error:
PyErr_SetString(PyExc_ValueError,
"hexadecimal string too long to convert");
return NULL;
}
static PyObject *
float_as_integer_ratio_impl(PyObject *self)
{
double self_double;
double float_part;
int exponent;
int i;
PyObject *py_exponent = NULL;
PyObject *numerator = NULL;
PyObject *denominator = NULL;
PyObject *result_pair = NULL;
PyNumberMethods *long_methods = PyLong_Type.tp_as_number;
CONVERT_TO_DOUBLE(self, self_double);
if (Py_IS_INFINITY(self_double)) {
PyErr_SetString(PyExc_OverflowError,
"cannot convert Infinity to integer ratio");
return NULL;
}
if (Py_IS_NAN(self_double)) {
PyErr_SetString(PyExc_ValueError,
"cannot convert NaN to integer ratio");
return NULL;
}
float_part = frexp(self_double, &exponent);
for (i=0; i<300 && float_part != floor(float_part) ; i++) {
float_part *= 2.0;
exponent--;
}
numerator = PyLong_FromDouble(float_part);
if (numerator == NULL)
goto error;
denominator = PyLong_FromLong(1);
if (denominator == NULL)
goto error;
py_exponent = PyLong_FromLong(Py_ABS(exponent));
if (py_exponent == NULL)
goto error;
if (exponent > 0) {
Py_SETREF(numerator,
long_methods->nb_lshift(numerator, py_exponent));
if (numerator == NULL)
goto error;
}
else {
Py_SETREF(denominator,
long_methods->nb_lshift(denominator, py_exponent));
if (denominator == NULL)
goto error;
}
result_pair = PyTuple_Pack(2, numerator, denominator);
error:
Py_XDECREF(py_exponent);
Py_XDECREF(denominator);
Py_XDECREF(numerator);
return result_pair;
}
static PyObject *
float_subtype_new(PyTypeObject *type, PyObject *x);
static PyObject *
float_new_impl(PyTypeObject *type, PyObject *x)
{
if (type != &PyFloat_Type) {
if (x == NULL) {
x = _PyLong_GetZero();
}
return float_subtype_new(type, x);
}
if (x == NULL) {
return PyFloat_FromDouble(0.0);
}
if (PyUnicode_CheckExact(x))
return PyFloat_FromString(x);
return PyNumber_Float(x);
}
static PyObject *
float_subtype_new(PyTypeObject *type, PyObject *x)
{
PyObject *tmp, *newobj;
assert(PyType_IsSubtype(type, &PyFloat_Type));
tmp = float_new_impl(&PyFloat_Type, x);
if (tmp == NULL)
return NULL;
assert(PyFloat_Check(tmp));
newobj = type->tp_alloc(type, 0);
if (newobj == NULL) {
Py_DECREF(tmp);
return NULL;
}
((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
Py_DECREF(tmp);
return newobj;
}
static PyObject *
float_vectorcall(PyObject *type, PyObject * const*args,
size_t nargsf, PyObject *kwnames)
{
if (!_PyArg_NoKwnames("float", kwnames)) {
return NULL;
}
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (!_PyArg_CheckPositional("float", nargs, 0, 1)) {
return NULL;
}
PyObject *x = nargs >= 1 ? args[0] : NULL;
return float_new_impl(_PyType_CAST(type), x);
}
static PyObject *
float___getnewargs___impl(PyObject *self)
{
return Py_BuildValue("(d)", ((PyFloatObject *)self)->ob_fval);
}
typedef enum _py_float_format_type float_format_type;
#define unknown_format _py_float_format_unknown
#define ieee_big_endian_format _py_float_format_ieee_big_endian
#define ieee_little_endian_format _py_float_format_ieee_little_endian
#define float_format (_PyRuntime.float_state.float_format)
#define double_format (_PyRuntime.float_state.double_format)
static PyObject *
float___getformat___impl(PyTypeObject *type, const char *typestr)
{
float_format_type r;
if (strcmp(typestr, "double") == 0) {
r = double_format;
}
else if (strcmp(typestr, "float") == 0) {
r = float_format;
}
else {
PyErr_SetString(PyExc_ValueError,
"__getformat__() argument 1 must be "
"'double' or 'float'");
return NULL;
}
switch (r) {
case unknown_format:
return PyUnicode_FromString("unknown");
case ieee_little_endian_format:
return PyUnicode_FromString("IEEE, little-endian");
case ieee_big_endian_format:
return PyUnicode_FromString("IEEE, big-endian");
default:
PyErr_SetString(PyExc_RuntimeError,
"insane float_format or double_format");
return NULL;
}
}
static PyObject *
float_getreal(PyObject *v, void *closure)
{
return float_float(v);
}
static PyObject *
float_getimag(PyObject *v, void *closure)
{
return PyFloat_FromDouble(0.0);
}
static PyObject *
float___format___impl(PyObject *self, PyObject *format_spec)
{
_PyUnicodeWriter writer;
int ret;
_PyUnicodeWriter_Init(&writer);
ret = _PyFloat_FormatAdvancedWriter(
&writer,
self,
format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
if (ret == -1) {
_PyUnicodeWriter_Dealloc(&writer);
return NULL;
}
return _PyUnicodeWriter_Finish(&writer);
}
static PyMethodDef float_methods[] = {
FLOAT_CONJUGATE_METHODDEF
FLOAT___TRUNC___METHODDEF
FLOAT___FLOOR___METHODDEF
FLOAT___CEIL___METHODDEF
FLOAT___ROUND___METHODDEF
FLOAT_AS_INTEGER_RATIO_METHODDEF
FLOAT_FROMHEX_METHODDEF
FLOAT_HEX_METHODDEF
FLOAT_IS_INTEGER_METHODDEF
FLOAT___GETNEWARGS___METHODDEF
FLOAT___GETFORMAT___METHODDEF
FLOAT___FORMAT___METHODDEF
{NULL, NULL}
};
static PyGetSetDef float_getset[] = {
{"real",
float_getreal, (setter)NULL,
"the real part of a complex number",
NULL},
{"imag",
float_getimag, (setter)NULL,
"the imaginary part of a complex number",
NULL},
{NULL}
};
static PyNumberMethods float_as_number = {
float_add,
float_sub,
float_mul,
float_rem,
float_divmod,
float_pow,
(unaryfunc)float_neg,
float_float,
(unaryfunc)float_abs,
(inquiry)float_bool,
0,
0,
0,
0,
0,
0,
float___trunc___impl,
0,
float_float,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
float_floor_div,
float_div,
0,
0,
};
PyTypeObject PyFloat_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"float",
sizeof(PyFloatObject),
0,
(destructor)float_dealloc,
0,
0,
0,
0,
(reprfunc)float_repr,
&float_as_number,
0,
0,
(hashfunc)float_hash,
0,
0,
PyObject_GenericGetAttr,
0,
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
_Py_TPFLAGS_MATCH_SELF,
float_new__doc__,
0,
0,
float_richcompare,
0,
0,
0,
float_methods,
0,
float_getset,
0,
0,
0,
0,
0,
0,
0,
float_new,
.tp_vectorcall = (vectorcallfunc)float_vectorcall,
};
static void
_init_global_state(void)
{
float_format_type detected_double_format, detected_float_format;
#if SIZEOF_DOUBLE == 8
{
double x = 9006104071832581.0;
if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
detected_double_format = ieee_big_endian_format;
else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
detected_double_format = ieee_little_endian_format;
else
detected_double_format = unknown_format;
}
#else
detected_double_format = unknown_format;
#endif
#if SIZEOF_FLOAT == 4
{
float y = 16711938.0;
if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
detected_float_format = ieee_big_endian_format;
else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
detected_float_format = ieee_little_endian_format;
else
detected_float_format = unknown_format;
}
#else
detected_float_format = unknown_format;
#endif
double_format = detected_double_format;
float_format = detected_float_format;
}
void
_PyFloat_InitState(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return;
}
_init_global_state();
}
PyStatus
_PyFloat_InitTypes(PyInterpreterState *interp)
{
if (_PyStructSequence_InitBuiltin(interp, &FloatInfoType,
&floatinfo_desc) < 0)
{
return _PyStatus_ERR("can't init float info type");
}
return _PyStatus_OK();
}
void
_PyFloat_ClearFreeList(PyInterpreterState *interp)
{
#if PyFloat_MAXFREELIST > 0
struct _Py_float_state *state = &interp->float_state;
PyFloatObject *f = state->free_list;
while (f != NULL) {
PyFloatObject *next = (PyFloatObject*) Py_TYPE(f);
PyObject_Free(f);
f = next;
}
state->free_list = NULL;
state->numfree = 0;
#endif
}
void
_PyFloat_Fini(PyInterpreterState *interp)
{
_PyFloat_ClearFreeList(interp);
#if defined(Py_DEBUG) && PyFloat_MAXFREELIST > 0
struct _Py_float_state *state = &interp->float_state;
state->numfree = -1;
#endif
}
void
_PyFloat_FiniType(PyInterpreterState *interp)
{
_PyStructSequence_FiniBuiltin(interp, &FloatInfoType);
}
void
_PyFloat_DebugMallocStats(FILE *out)
{
#if PyFloat_MAXFREELIST > 0
struct _Py_float_state *state = get_float_state();
_PyDebugAllocatorStats(out,
"free PyFloatObject",
state->numfree, sizeof(PyFloatObject));
#endif
}
int
PyFloat_Pack2(double x, char *data, int le)
{
unsigned char *p = (unsigned char *)data;
unsigned char sign;
int e;
double f;
unsigned short bits;
int incr = 1;
if (x == 0.0) {
sign = (copysign(1.0, x) == -1.0);
e = 0;
bits = 0;
}
else if (Py_IS_INFINITY(x)) {
sign = (x < 0.0);
e = 0x1f;
bits = 0;
}
else if (Py_IS_NAN(x)) {
sign = (copysign(1.0, x) == -1.0);
e = 0x1f;
bits = 512;
}
else {
sign = (x < 0.0);
if (sign) {
x = -x;
}
f = frexp(x, &e);
if (f < 0.5 || f >= 1.0) {
PyErr_SetString(PyExc_SystemError,
"frexp() result out of range");
return -1;
}
f *= 2.0;
e--;
if (e >= 16) {
goto Overflow;
}
else if (e < -25) {
f = 0.0;
e = 0;
}
else if (e < -14) {
f = ldexp(f, 14 + e);
e = 0;
}
else {
e += 15;
f -= 1.0;
}
f *= 1024.0;
bits = (unsigned short)f;
assert(bits < 1024);
assert(e < 31);
if ((f - bits > 0.5) || ((f - bits == 0.5) && (bits % 2 == 1))) {
++bits;
if (bits == 1024) {
bits = 0;
++e;
if (e == 31)
goto Overflow;
}
}
}
bits |= (e << 10) | (sign << 15);
if (le) {
p += 1;
incr = -1;
}
*p = (unsigned char)((bits >> 8) & 0xFF);
p += incr;
*p = (unsigned char)(bits & 0xFF);
return 0;
Overflow:
PyErr_SetString(PyExc_OverflowError,
"float too large to pack with e format");
return -1;
}
int
PyFloat_Pack4(double x, char *data, int le)
{
unsigned char *p = (unsigned char *)data;
if (float_format == unknown_format) {
unsigned char sign;
int e;
double f;
unsigned int fbits;
int incr = 1;
if (le) {
p += 3;
incr = -1;
}
if (x < 0) {
sign = 1;
x = -x;
}
else
sign = 0;
f = frexp(x, &e);
if (0.5 <= f && f < 1.0) {
f *= 2.0;
e--;
}
else if (f == 0.0)
e = 0;
else {
PyErr_SetString(PyExc_SystemError,
"frexp() result out of range");
return -1;
}
if (e >= 128)
goto Overflow;
else if (e < -126) {
f = ldexp(f, 126 + e);
e = 0;
}
else if (!(e == 0 && f == 0.0)) {
e += 127;
f -= 1.0;
}
f *= 8388608.0;
fbits = (unsigned int)(f + 0.5);
assert(fbits <= 8388608);
if (fbits >> 23) {
fbits = 0;
++e;
if (e >= 255)
goto Overflow;
}
*p = (sign << 7) | (e >> 1);
p += incr;
*p = (char) (((e & 1) << 7) | (fbits >> 16));
p += incr;
*p = (fbits >> 8) & 0xFF;
p += incr;
*p = fbits & 0xFF;
return 0;
}
else {
float y = (float)x;
int i, incr = 1;
if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
goto Overflow;
unsigned char s[sizeof(float)];
memcpy(s, &y, sizeof(float));
if ((float_format == ieee_little_endian_format && !le)
|| (float_format == ieee_big_endian_format && le)) {
p += 3;
incr = -1;
}
for (i = 0; i < 4; i++) {
*p = s[i];
p += incr;
}
return 0;
}
Overflow:
PyErr_SetString(PyExc_OverflowError,
"float too large to pack with f format");
return -1;
}
int
PyFloat_Pack8(double x, char *data, int le)
{
unsigned char *p = (unsigned char *)data;
if (double_format == unknown_format) {
unsigned char sign;
int e;
double f;
unsigned int fhi, flo;
int incr = 1;
if (le) {
p += 7;
incr = -1;
}
if (x < 0) {
sign = 1;
x = -x;
}
else
sign = 0;
f = frexp(x, &e);
if (0.5 <= f && f < 1.0) {
f *= 2.0;
e--;
}
else if (f == 0.0)
e = 0;
else {
PyErr_SetString(PyExc_SystemError,
"frexp() result out of range");
return -1;
}
if (e >= 1024)
goto Overflow;
else if (e < -1022) {
f = ldexp(f, 1022 + e);
e = 0;
}
else if (!(e == 0 && f == 0.0)) {
e += 1023;
f -= 1.0;
}
f *= 268435456.0;
fhi = (unsigned int)f;
assert(fhi < 268435456);
f -= (double)fhi;
f *= 16777216.0;
flo = (unsigned int)(f + 0.5);
assert(flo <= 16777216);
if (flo >> 24) {
flo = 0;
++fhi;
if (fhi >> 28) {
fhi = 0;
++e;
if (e >= 2047)
goto Overflow;
}
}
*p = (sign << 7) | (e >> 4);
p += incr;
*p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
p += incr;
*p = (fhi >> 16) & 0xFF;
p += incr;
*p = (fhi >> 8) & 0xFF;
p += incr;
*p = fhi & 0xFF;
p += incr;
*p = (flo >> 16) & 0xFF;
p += incr;
*p = (flo >> 8) & 0xFF;
p += incr;
*p = flo & 0xFF;
return 0;
Overflow:
PyErr_SetString(PyExc_OverflowError,
"float too large to pack with d format");
return -1;
}
else {
const unsigned char *s = (unsigned char*)&x;
int i, incr = 1;
if ((double_format == ieee_little_endian_format && !le)
|| (double_format == ieee_big_endian_format && le)) {
p += 7;
incr = -1;
}
for (i = 0; i < 8; i++) {
*p = *s++;
p += incr;
}
return 0;
}
}
double
PyFloat_Unpack2(const char *data, int le)
{
unsigned char *p = (unsigned char *)data;
unsigned char sign;
int e;
unsigned int f;
double x;
int incr = 1;
if (le) {
p += 1;
incr = -1;
}
sign = (*p >> 7) & 1;
e = (*p & 0x7C) >> 2;
f = (*p & 0x03) << 8;
p += incr;
f |= *p;
if (e == 0x1f) {
if (f == 0) {
return sign ? -Py_HUGE_VAL : Py_HUGE_VAL;
}
else {
return sign ? -fabs(Py_NAN) : fabs(Py_NAN);
}
}
x = (double)f / 1024.0;
if (e == 0) {
e = -14;
}
else {
x += 1.0;
e -= 15;
}
x = ldexp(x, e);
if (sign)
x = -x;
return x;
}
double
PyFloat_Unpack4(const char *data, int le)
{
unsigned char *p = (unsigned char *)data;
if (float_format == unknown_format) {
unsigned char sign;
int e;
unsigned int f;
double x;
int incr = 1;
if (le) {
p += 3;
incr = -1;
}
sign = (*p >> 7) & 1;
e = (*p & 0x7F) << 1;
p += incr;
e |= (*p >> 7) & 1;
f = (*p & 0x7F) << 16;
p += incr;
if (e == 255) {
PyErr_SetString(
PyExc_ValueError,
"can't unpack IEEE 754 special value "
"on non-IEEE platform");
return -1;
}
f |= *p << 8;
p += incr;
f |= *p;
x = (double)f / 8388608.0;
if (e == 0)
e = -126;
else {
x += 1.0;
e -= 127;
}
x = ldexp(x, e);
if (sign)
x = -x;
return x;
}
else {
float x;
if ((float_format == ieee_little_endian_format && !le)
|| (float_format == ieee_big_endian_format && le)) {
char buf[4];
char *d = &buf[3];
int i;
for (i = 0; i < 4; i++) {
*d-- = *p++;
}
memcpy(&x, buf, 4);
}
else {
memcpy(&x, p, 4);
}
return x;
}
}
double
PyFloat_Unpack8(const char *data, int le)
{
unsigned char *p = (unsigned char *)data;
if (double_format == unknown_format) {
unsigned char sign;
int e;
unsigned int fhi, flo;
double x;
int incr = 1;
if (le) {
p += 7;
incr = -1;
}
sign = (*p >> 7) & 1;
e = (*p & 0x7F) << 4;
p += incr;
e |= (*p >> 4) & 0xF;
fhi = (*p & 0xF) << 24;
p += incr;
if (e == 2047) {
PyErr_SetString(
PyExc_ValueError,
"can't unpack IEEE 754 special value "
"on non-IEEE platform");
return -1.0;
}
fhi |= *p << 16;
p += incr;
fhi |= *p << 8;
p += incr;
fhi |= *p;
p += incr;
flo = *p << 16;
p += incr;
flo |= *p << 8;
p += incr;
flo |= *p;
x = (double)fhi + (double)flo / 16777216.0;
x /= 268435456.0;
if (e == 0)
e = -1022;
else {
x += 1.0;
e -= 1023;
}
x = ldexp(x, e);
if (sign)
x = -x;
return x;
}
else {
double x;
if ((double_format == ieee_little_endian_format && !le)
|| (double_format == ieee_big_endian_format && le)) {
char buf[8];
char *d = &buf[7];
int i;
for (i = 0; i < 8; i++) {
*d-- = *p++;
}
memcpy(&x, buf, 8);
}
else {
memcpy(&x, p, 8);
}
return x;
}
}