Skip to content

Commit 6de675c

Browse files
alternative forms of black magic
1 parent 8b10b50 commit 6de675c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Objects/floatobject.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,8 +2618,11 @@ PyFloat_Unpack8(const char *data, int le)
26182618
PyObject*
26192619
_PyFloat_From64Bits(int64_t val)
26202620
{
2621+
union caster {
2622+
int64_t from;
2623+
double to;
2624+
};
26212625
assert(sizeof(double) == sizeof(int64_t));
2622-
double dst;
2623-
memcpy(&dst, &val, sizeof(int64_t));
2624-
return PyFloat_FromDouble(dst);
2626+
union caster temp = {.from = val};
2627+
return PyFloat_FromDouble(temp.to);
26252628
}

Python/optimizer_analysis.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,13 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
286286

287287
static int64_t
288288
double_as_int64_t(double in) {
289+
union caster {
290+
double from;
291+
int64_t to;
292+
};
289293
assert(sizeof(double) == sizeof(int64_t));
290-
double temp = in;
291-
int64_t result;
292-
memcpy(&result, &temp, sizeof(double));
293-
return result;
294+
union caster temp = {.from = in};
295+
return temp.to;
294296
}
295297

296298
#define STACK_LEVEL() ((int)(stack_pointer - ctx->frame->stack))

0 commit comments

Comments
 (0)