@@ -10,13 +10,14 @@ from libc.stdint cimport (
10
10
uint32_t,
11
11
uint64_t,
12
12
)
13
+ from libc.string cimport memcpy
13
14
14
15
15
16
def read_float_with_byteswap (bytes data , Py_ssize_t offset , bint byteswap ):
16
17
assert offset + 4 < len (data)
17
18
cdef:
18
19
const char * data_ptr = data
19
- float res = (< float * > (data_ptr + offset))[0 ]
20
+ float res = (< float * > (data_ptr + offset))[0 ]
20
21
if byteswap:
21
22
res = _byteswap_float(res)
22
23
return res
@@ -26,7 +27,7 @@ def read_double_with_byteswap(bytes data, Py_ssize_t offset, bint byteswap):
26
27
assert offset + 8 < len (data)
27
28
cdef:
28
29
const char * data_ptr = data
29
- double res = (< double * > (data_ptr + offset))[0 ]
30
+ double res = (< double * > (data_ptr + offset))[0 ]
30
31
if byteswap:
31
32
res = _byteswap_double(res)
32
33
return res
@@ -82,12 +83,16 @@ cdef extern from *:
82
83
83
84
84
85
cdef float _byteswap_float(float num):
85
- cdef uint32_t * intptr = < uint32_t * > & num
86
- intptr[0 ] = _byteswap4(intptr[0 ])
86
+ cdef uint32_t num_uint
87
+ memcpy(& num_uint, & num, sizeof(float ))
88
+ num_uint = _byteswap4(num_uint)
89
+ memcpy(& num, & num_uint, sizeof(uint32_t))
87
90
return num
88
91
89
92
90
93
cdef double _byteswap_double(double num):
91
- cdef uint64_t * intptr = < uint64_t * > & num
92
- intptr[0 ] = _byteswap8(intptr[0 ])
94
+ cdef uint64_t num_uint
95
+ memcpy(& num_uint, & num, sizeof(double ))
96
+ num_uint = _byteswap8(num_uint)
97
+ memcpy(& num, & num_uint, sizeof(uint64_t))
93
98
return num
0 commit comments