zmc
2023-10-12 ed135d79df12a2466b52dae1a82326941211dcc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
The following are faster versions of struct.unpack that avoid the overhead of Python
function calls.
 
In the SAS7BDAT parser, they may be called up to (n_rows * n_cols) times.
"""
from cython cimport Py_ssize_t
from libc.stdint cimport (
    uint16_t,
    uint32_t,
    uint64_t,
)
 
 
def read_float_with_byteswap(bytes data, Py_ssize_t offset, bint byteswap):
    assert offset + 4 < len(data)
    cdef:
        const char *data_ptr = data
        float res = (<float*>(data_ptr + offset))[0]
    if byteswap:
        res = _byteswap_float(res)
    return res
 
 
def read_double_with_byteswap(bytes data, Py_ssize_t offset, bint byteswap):
    assert offset + 8 < len(data)
    cdef:
        const char *data_ptr = data
        double res = (<double*>(data_ptr + offset))[0]
    if byteswap:
        res = _byteswap_double(res)
    return res
 
 
def read_uint16_with_byteswap(bytes data, Py_ssize_t offset, bint byteswap):
    assert offset + 2 < len(data)
    cdef:
        const char *data_ptr = data
        uint16_t res = (<uint16_t *>(data_ptr + offset))[0]
    if byteswap:
        res = _byteswap2(res)
    return res
 
 
def read_uint32_with_byteswap(bytes data, Py_ssize_t offset, bint byteswap):
    assert offset + 4 < len(data)
    cdef:
        const char *data_ptr = data
        uint32_t res = (<uint32_t *>(data_ptr + offset))[0]
    if byteswap:
        res = _byteswap4(res)
    return res
 
 
def read_uint64_with_byteswap(bytes data, Py_ssize_t offset, bint byteswap):
    assert offset + 8 < len(data)
    cdef:
        const char *data_ptr = data
        uint64_t res = (<uint64_t *>(data_ptr + offset))[0]
    if byteswap:
        res = _byteswap8(res)
    return res
 
 
# Byteswapping
 
cdef extern from *:
    """
    #ifdef _MSC_VER
        #define _byteswap2 _byteswap_ushort
        #define _byteswap4 _byteswap_ulong
        #define _byteswap8 _byteswap_uint64
    #else
        #define _byteswap2 __builtin_bswap16
        #define _byteswap4 __builtin_bswap32
        #define _byteswap8 __builtin_bswap64
    #endif
    """
    uint16_t _byteswap2(uint16_t)
    uint32_t _byteswap4(uint32_t)
    uint64_t _byteswap8(uint64_t)
 
 
cdef float _byteswap_float(float num):
    cdef uint32_t *intptr = <uint32_t *>&num
    intptr[0] = _byteswap4(intptr[0])
    return num
 
 
cdef double _byteswap_double(double num):
    cdef uint64_t *intptr = <uint64_t *>&num
    intptr[0] = _byteswap8(intptr[0])
    return num