Skip to content

Commit 93cef03

Browse files
committed
to cython
1 parent cb0aa2f commit 93cef03

File tree

2 files changed

+39
-48
lines changed

2 files changed

+39
-48
lines changed

numcodecs/_fletcher.c

Lines changed: 0 additions & 43 deletions
This file was deleted.

numcodecs/fletcher32.pyx

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
1+
# cython: language_level=3
2+
# cython: overflowcheck=False
3+
# cython: cdivision=True
14
import struct
25

36
from numcodecs.abc import Codec
47
from numcodecs.compat import ensure_contiguous_ndarray
58

69
from libc.stdint cimport uint8_t, uint16_t, uint32_t
710

8-
cdef extern from "_fletcher.c":
9-
uint32_t H5_checksum_fletcher32(const void *_data, size_t _len)
11+
12+
cdef uint32_t _fletcher32(const uint8_t[::1] _data):
13+
cdef:
14+
const uint8_t *data = &_data[0]
15+
size_t _len = _data.shape[0]
16+
size_t len = _len / 2
17+
size_t tlen
18+
uint32_t sum1 = 0, sum2 = 0;
19+
20+
21+
while len:
22+
tlen = 360 if len > 360 else len
23+
len -= tlen
24+
while True:
25+
sum1 += <uint32_t>((<uint16_t>data[0]) << 8) | (<uint16_t>data[1])
26+
data += 2
27+
sum2 += sum1
28+
tlen -= 1
29+
if tlen < 1:
30+
break
31+
sum1 = (sum1 & 0xffff) + (sum1 >> 16)
32+
sum2 = (sum2 & 0xffff) + (sum2 >> 16)
33+
34+
if _len % 2:
35+
sum1 += <uint32_t>((<uint16_t>(data[0])) << 8)
36+
sum2 += sum1
37+
sum1 = (sum1 & 0xffff) + (sum1 >> 16)
38+
sum2 = (sum2 & 0xffff) + (sum2 >> 16)
39+
40+
sum1 = (sum1 & 0xffff) + (sum1 >> 16)
41+
sum2 = (sum2 & 0xffff) + (sum2 >> 16)
42+
43+
return (sum2 << 16) | sum1
1044

1145

1246
class Fletcher32(Codec):
@@ -24,14 +58,14 @@ class Fletcher32(Codec):
2458
"""Return buffer plus 4-byte fletcher checksum"""
2559
buf = ensure_contiguous_ndarray(buf).ravel().view('uint8')
2660
cdef const uint8_t[::1] b_ptr = buf
27-
val = H5_checksum_fletcher32(&b_ptr[0], buf.nbytes)
61+
val = _fletcher32(b_ptr)
2862
return buf.tobytes() + struct.pack("<I", val)
2963

3064
def decode(self, buf, out=None):
3165
"""Check fletcher checksum, and return buffer without it"""
3266
b = ensure_contiguous_ndarray(buf).view('uint8')
33-
cdef const uint8_t[::1] b_ptr = b
34-
val = H5_checksum_fletcher32(&b_ptr[0], b.nbytes - 4)
67+
cdef const uint8_t[::1] b_ptr = b[:-4]
68+
val = _fletcher32(b_ptr)
3569
found = b[-4:].view("<u4")[0]
3670
if val != found:
3771
raise RuntimeError(

0 commit comments

Comments
 (0)