Skip to content

Commit 89bb000

Browse files
committed
Also use _Py_bswap16() at the end of PyFloat_Pack2()
1 parent aa273f8 commit 89bb000

1 file changed

Lines changed: 4 additions & 9 deletions

File tree

‎Objects/floatobject.c‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,7 +1921,7 @@ PyFloat_Pack2(double x, char *data, int le)
19211921
unsigned char sign;
19221922
int e;
19231923
double f;
1924-
unsigned short bits;
1924+
uint16_t bits;
19251925

19261926
if (x == 0.0) {
19271927
sign = (copysign(1.0, x) == -1.0);
@@ -2001,15 +2001,10 @@ PyFloat_Pack2(double x, char *data, int le)
20012001
bits |= (e << 10) | (sign << 15);
20022002

20032003
/* Write out result. */
2004-
unsigned char *p = (unsigned char *)data;
2005-
if (le) {
2006-
p[0] = (unsigned char)(bits & 0xFF);
2007-
p[1] = (unsigned char)((bits >> 8) & 0xFF);
2008-
}
2009-
else {
2010-
p[0] = (unsigned char)((bits >> 8) & 0xFF);
2011-
p[1] = (unsigned char)(bits & 0xFF);
2004+
if ((_PY_FLOAT_LITTLE_ENDIAN && !le) || (_PY_FLOAT_BIG_ENDIAN && le)) {
2005+
bits = _Py_bswap16(bits); // Swap bytes
20122006
}
2007+
memcpy(data, &bits, 2);
20132008
return 0;
20142009

20152010
Overflow:

0 commit comments

Comments
 (0)