Add bfloat16 support to struct

You do not need the bfloat16 support in struct to use bfloat16. You can unpack bfloat16 as 2-byte bytes object or 16-bit integer and then convert it to float using bit manipulations. You can do this in pure Python, you can write an extension containing the conversion functions and put it on PyPI.

What support of bfloat16 in struct gives you – it is convenience (and a little performance). But you only could benefit from it several years after release of Python version containing this feature. In meantime, you will need to use conversion functions, written by you or by third party.

With such long time perspective, I think it is time to design a way to register custom types. If this mechanism was in place, it would be easy to use third-party providers with all benefits of builtin support. We can finally add builtin support for bfloat16, bfloat8, float24 (needed for WAV files, I guess?), float80 (really was needed for AIFC files), float128, int128, etc, you could use these types long time before this.

For perfomance, we need the C API for registering custom types, but for convenience, we need also the Python API. The registered item should provide four element:

  • size
  • alignment
  • packing function
  • unpacking function

I do not have good ideas about supporting variable-sized types, like Python or C strings, so I think we can do without this.

Global registry should be enough, like for encodings and error handlers.

struct, memoryview and array should support types with long names, the syntax should not conflict with possible future support of nested structures and arrays (see PEP 3118).

5 Likes