Skip to content

Commit 2941f6d

Browse files
committed
address review: replace float_state by macros
1 parent f37db78 commit 2941f6d

4 files changed

Lines changed: 16 additions & 62 deletions

File tree

Include/internal/pycore_floatobject.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ extern double _Py_parse_inf_or_nan(const char *p, char **endptr);
4242

4343
extern int _Py_convert_int_to_double(PyObject **v, double *dbl);
4444

45+
/* Should match endianness of the platform in most (all?) cases. */
46+
47+
#ifdef DOUBLE_IS_BIG_ENDIAN_IEEE754
48+
# define _PY_FLOAT_BIG_ENDIAN 1
49+
# define _PY_FLOAT_LITTLE_ENDIAN 0
50+
#else
51+
# define _PY_FLOAT_BIG_ENDIAN 0
52+
# define _PY_FLOAT_LITTLE_ENDIAN 1
53+
#endif
4554

4655
#ifdef __cplusplus
4756
}

Include/internal/pycore_runtime_init.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ extern PyTypeObject _PyExc_MemoryError;
8484
.stoptheworld = { \
8585
.is_global = 1, \
8686
}, \
87-
.float_state = { \
88-
.float_format = _py_float_format_ieee_little_endian, \
89-
.double_format = _py_float_format_ieee_little_endian, \
90-
}, \
9187
.types = { \
9288
.next_version_tag = _Py_TYPE_VERSION_NEXT, \
9389
}, \

Include/internal/pycore_runtime_structs.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ struct _pymem_allocators {
3535
PyObjectArenaAllocator obj_arena;
3636
};
3737

38-
enum _py_float_format_type {
39-
_py_float_format_ieee_big_endian,
40-
_py_float_format_ieee_little_endian,
41-
};
42-
43-
struct _Py_float_runtime_state {
44-
enum _py_float_format_type float_format;
45-
enum _py_float_format_type double_format;
46-
};
47-
4838
struct pyhash_runtime_state {
4939
struct {
5040
#ifndef MS_WINDOWS
@@ -269,7 +259,6 @@ struct pyruntimestate {
269259
} audit_hooks;
270260

271261
struct _py_object_runtime_state object_state;
272-
struct _Py_float_runtime_state float_state;
273262
struct _Py_unicode_runtime_state unicode_state;
274263
struct _types_runtime_state types;
275264
struct _Py_time_runtime_state time;

Objects/floatobject.c

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,29 +1699,14 @@ float___getformat___impl(PyTypeObject *type, const char *typestr)
16991699
{
17001700
float_format_type r;
17011701

1702-
if (strcmp(typestr, "double") == 0) {
1703-
r = double_format;
1704-
}
1705-
else if (strcmp(typestr, "float") == 0) {
1706-
r = float_format;
1707-
}
1708-
else {
1702+
if (strcmp(typestr, "double") != 0 || strcmp(typestr, "float") != 0) {
17091703
PyErr_SetString(PyExc_ValueError,
17101704
"__getformat__() argument 1 must be "
17111705
"'double' or 'float'");
17121706
return NULL;
17131707
}
1714-
1715-
switch (r) {
1716-
case ieee_little_endian_format:
1717-
return PyUnicode_FromString("IEEE, little-endian");
1718-
case ieee_big_endian_format:
1719-
return PyUnicode_FromString("IEEE, big-endian");
1720-
default:
1721-
PyErr_SetString(PyExc_RuntimeError,
1722-
"insane float_format or double_format");
1723-
return NULL;
1724-
}
1708+
return PyUnicode_FromString(_PY_FLOAT_LITTLE_ENDIAN ?
1709+
"IEEE, little-endian" : "IEEE, big-endian");
17251710
}
17261711

17271712
static PyObject *
@@ -1874,27 +1859,6 @@ PyTypeObject PyFloat_Type = {
18741859
.tp_version_tag = _Py_TYPE_VERSION_FLOAT,
18751860
};
18761861

1877-
static void
1878-
_init_global_state(void)
1879-
{
1880-
#ifdef DOUBLE_IS_BIG_ENDIAN_IEEE754
1881-
double_format = ieee_big_endian_format;
1882-
float_format = ieee_big_endian_format;
1883-
#else
1884-
double_format = ieee_little_endian_format;
1885-
float_format = ieee_little_endian_format;
1886-
#endif
1887-
}
1888-
1889-
void
1890-
_PyFloat_InitState(PyInterpreterState *interp)
1891-
{
1892-
if (!_Py_IsMainInterpreter(interp)) {
1893-
return;
1894-
}
1895-
_init_global_state();
1896-
}
1897-
18981862
PyStatus
18991863
_PyFloat_InitTypes(PyInterpreterState *interp)
19001864
{
@@ -2098,8 +2062,7 @@ PyFloat_Pack4(double x, char *data, int le)
20982062
unsigned char s[sizeof(float)];
20992063
memcpy(s, &y, sizeof(float));
21002064

2101-
if ((float_format == ieee_little_endian_format && !le)
2102-
|| (float_format == ieee_big_endian_format && le)) {
2065+
if ((_PY_FLOAT_LITTLE_ENDIAN && !le) || (_PY_FLOAT_BIG_ENDIAN && le)) {
21032066
p += 3;
21042067
incr = -1;
21052068
}
@@ -2120,8 +2083,7 @@ PyFloat_Pack8(double x, char *data, int le)
21202083
const unsigned char *s = as_bytes;
21212084
int i, incr = 1;
21222085

2123-
if ((double_format == ieee_little_endian_format && !le)
2124-
|| (double_format == ieee_big_endian_format && le)) {
2086+
if ((_PY_FLOAT_LITTLE_ENDIAN && !le) || (_PY_FLOAT_BIG_ENDIAN && le)) {
21252087
p += 7;
21262088
incr = -1;
21272089
}
@@ -2195,8 +2157,7 @@ PyFloat_Unpack4(const char *data, int le)
21952157
unsigned char *p = (unsigned char *)data;
21962158
float x;
21972159

2198-
if ((float_format == ieee_little_endian_format && !le)
2199-
|| (float_format == ieee_big_endian_format && le)) {
2160+
if ((_PY_FLOAT_LITTLE_ENDIAN && !le) || (_PY_FLOAT_BIG_ENDIAN && le)) {
22002161
char buf[4];
22012162
char *d = &buf[3];
22022163
int i;
@@ -2254,8 +2215,7 @@ PyFloat_Unpack8(const char *data, int le)
22542215
unsigned char *p = (unsigned char *)data;
22552216
double x;
22562217

2257-
if ((double_format == ieee_little_endian_format && !le)
2258-
|| (double_format == ieee_big_endian_format && le)) {
2218+
if ((_PY_FLOAT_LITTLE_ENDIAN && !le) || (_PY_FLOAT_BIG_ENDIAN && le)) {
22592219
char buf[8];
22602220
char *d = &buf[7];
22612221
int i;

0 commit comments

Comments
 (0)