Commit 6d156a18 authored by Travis Hance's avatar Travis Hance

get floatobject.c to compile, add float.hex and float.fromhex

parent 8a051d9b
......@@ -377,6 +377,7 @@ STDOBJECT_SRCS := \
capsule.c \
stringobject.c \
exceptions.c \
floatobject.c \
unicodeobject.c \
unicodectype.c \
bytearrayobject.c \
......
......@@ -79,6 +79,7 @@ file(GLOB_RECURSE STDOBJECT_SRCS Objects
capsule.c
cobject.c
exceptions.c
floatobject.c
iterobject.c
memoryobject.c
stringobject.c
......
......@@ -12,16 +12,10 @@ PyFloatObject represents a (double precision) floating point number.
extern "C" {
#endif
// Pyston change: this is not the format we're using
// - actually I think it is but there's no reason to have multiple definitions.
#if 0
typedef struct {
PyObject_HEAD
double ob_fval;
} PyFloatObject;
#endif
struct _PyFloatObject;
typedef struct _PyFloatObject PyFloatObject;
// Pyston change: this is no longer a static object
PyAPI_DATA(PyTypeObject*) float_cls;
......
......@@ -424,3 +424,37 @@ typedef PY_LONG_LONG Py_intptr_t;
#endif /* Py_PYPORT_H */
/* Py_ADJUST_ERANGE1(x)
* Py_ADJUST_ERANGE2(x, y)
* Set errno to 0 before calling a libm function, and invoke one of these
* macros after, passing the function result(s) (Py_ADJUST_ERANGE2 is useful
* for functions returning complex results). This makes two kinds of
* adjustments to errno: (A) If it looks like the platform libm set
* errno=ERANGE due to underflow, clear errno. (B) If it looks like the
* platform libm overflowed but didn't set errno, force errno to ERANGE. In
* effect, we're trying to force a useful implementation of C89 errno
* behavior.
* Caution:
* This isn't reliable. See Py_OVERFLOWED comments.
* X and Y may be evaluated more than once.
*/
#define Py_ADJUST_ERANGE1(X) \
do { \
if (errno == 0) { \
if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
errno = ERANGE; \
} \
else if (errno == ERANGE && (X) == 0.0) \
errno = 0; \
} while(0)
#define Py_ADJUST_ERANGE2(X, Y) \
do { \
if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL || \
(Y) == Py_HUGE_VAL || (Y) == -Py_HUGE_VAL) { \
if (errno == 0) \
errno = ERANGE; \
} \
else if (errno == ERANGE) \
errno = 0; \
} while(0)
......@@ -138,6 +138,8 @@ PyFloat_GetInfo(void)
return floatinfo;
}
// pyston change: comment this out
#if 0
PyObject *
PyFloat_FromDouble(double fval)
{
......@@ -153,6 +155,7 @@ PyFloat_FromDouble(double fval)
op->ob_fval = fval;
return (PyObject *) op;
}
#endif
/**************************************************************************
RED_FLAG 22-Sep-2000 tim
......@@ -170,6 +173,8 @@ Since we can't change the interface of a public API function, pend is
still supported but now *officially* useless: if pend is not NULL,
*pend is set to NULL.
**************************************************************************/
// pyston change: comment this out
#if 0
PyObject *
PyFloat_FromString(PyObject *v, char **pend)
{
......@@ -235,6 +240,7 @@ PyFloat_FromString(PyObject *v, char **pend)
#endif
return result;
}
#endif
static void
float_dealloc(PyFloatObject *op)
......@@ -247,6 +253,8 @@ float_dealloc(PyFloatObject *op)
Py_TYPE(op)->tp_free((PyObject *)op);
}
// pyston change: comment this out
#if 0
double
PyFloat_AsDouble(PyObject *op)
{
......@@ -281,6 +289,7 @@ PyFloat_AsDouble(PyObject *op)
return val;
}
#endif
/* Methods */
......@@ -1078,6 +1087,8 @@ float_long(PyObject *v)
#error "C doubles do not appear to be IEEE 754 binary64 format"
#endif
// pyston change: comment this out
#if 0
PyObject *
_Py_double_round(double x, int ndigits) {
......@@ -1214,6 +1225,7 @@ _Py_double_round(double x, int ndigits) {
_Py_dg_freedtoa(buf);
return result;
}
#endif
#undef FIVE_POW_LIMIT
......@@ -1359,7 +1371,8 @@ hex_from_char(char c) {
of the form 4k+1. */
#define TOHEX_NBITS DBL_MANT_DIG + 3 - (DBL_MANT_DIG+2)%4
static PyObject *
// pyston change: make this not static
PyObject *
float_hex(PyObject *v)
{
double x, m;
......@@ -1437,7 +1450,8 @@ case_insensitive_match(const char *s, const char *t)
/* Convert a hexadecimal string to a float. */
static PyObject *
// pyston change: make this not static
PyObject *
float_fromhex(PyObject *cls, PyObject *arg)
{
PyObject *result_as_float, *result;
......@@ -2105,6 +2119,8 @@ static PyNumberMethods float_as_number = {
0, /* nb_inplace_true_divide */
};
// pyston change: don't need this
#if 0
PyTypeObject PyFloat_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"float",
......@@ -2295,7 +2311,10 @@ PyFloat_Fini(void)
}
}
}
#endif
// pyston change: comment this out
#if 0
/*----------------------------------------------------------------------------
* _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h.
*/
......@@ -2705,3 +2724,4 @@ _PyFloat_Unpack8(const unsigned char *p, int le)
return x;
}
}
#endif
......@@ -17,6 +17,7 @@
#include <cstring>
#include <gmp.h>
#include "capi/types.h"
#include "core/types.h"
#include "runtime/inline/boxing.h"
#include "runtime/long.h"
......@@ -24,6 +25,9 @@
#include "runtime/types.h"
#include "runtime/util.h"
extern "C" PyObject* float_hex(PyObject* v) noexcept;
extern "C" PyObject* float_fromhex(PyObject* cls, PyObject* arg) noexcept;
namespace pyston {
extern "C" PyObject* PyFloat_FromDouble(double d) noexcept {
......@@ -1424,6 +1428,9 @@ exit:
return result;
}
static PyMethodDef float_methods[] = { { "hex", (PyCFunction)float_hex, METH_NOARGS, NULL },
{ "fromhex", (PyCFunction)float_fromhex, METH_O | METH_CLASS, NULL } };
void setupFloat() {
_addFunc("__add__", BOXED_FLOAT, (void*)floatAddFloat, (void*)floatAddInt, (void*)floatAdd);
float_cls->giveAttr("__radd__", float_cls->getattr("__add__"));
......@@ -1472,6 +1479,10 @@ void setupFloat() {
new BoxedClassmethod(new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)floatGetFormat, STR, 2), "__getformat__", floatGetFormatDoc)));
for (auto& md : float_methods) {
float_cls->giveAttr(md.ml_name, new BoxedMethodDescriptor(&md, float_cls));
}
float_cls->freeze();
floatFormatInit();
......
......@@ -407,6 +407,8 @@ public:
DEFAULT_CLASS_SIMPLE(float_cls);
};
static_assert(sizeof(BoxedFloat) == sizeof(PyFloatObject), "");
static_assert(offsetof(BoxedFloat, d) == offsetof(PyFloatObject, ob_fval), "");
class BoxedComplex : public Box {
public:
......
......@@ -58,3 +58,6 @@ try:
float.__getformat__('oooga booga boooga')
except Exception as e:
print e.message
print float.fromhex("f0.04a")
print (5.0).hex()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment