Skip to content

Instantly share code, notes, and snippets.

@martindurant
Created April 30, 2021 18:45
Show Gist options
  • Select an option

  • Save martindurant/f652f3214e48c1e0db3ba5618615bfc1 to your computer and use it in GitHub Desktop.

Select an option

Save martindurant/f652f3214e48c1e0db3ba5618615bfc1 to your computer and use it in GitHub Desktop.
cpdef void read_bitpacked8(NumpyIO file_obj, int count, NumpyIO o):
# same as np.frombuffer(input, count, dtype=np.uint8)).astype(np.int32)
cdef int i
cdef int* outptr
cdef char* inptr
outptr = <int*>o.get_pointer()
inptr = file_obj.get_pointer()
for i in range(count):
outptr[0] = <int>inptr[0] & 0xff
outptr += 1
inptr += 1
file_obj.loc += count
o.loc += count * 4
cpdef void read_bitpacked8_simple(NumpyIO file_obj, int count, NumpyIO o):
# same as np.frombuffer(input, count, dtype=np.uint8)).astype(np.int32)
cdef int i
for i in range(count):
o.write_int(<int>file_obj.read_byte() & 0xff)
cpdef void read_bitpacked8_inline (NumpyIO file_obj, int count, NumpyIO o):
# same as np.frombuffer(input, count, dtype=np.uint8)).astype(np.int32)
cdef int i
for i in range(count):
write_int(o, <int>read_byte(file_obj) & 0xff)
cdef inline void write_int(NumpyIO self, int i):
(<int*> (self.ptr + self.loc))[0] = i
self.loc += 4
cdef inline char read_byte(NumpyIO self):
self.loc += 1
return self.ptr[self.loc - 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment