| 18 | namespace nb = nanobind; |
| 19 | |
| 20 | std::string buffer_format(const mx::array& a) { |
| 21 | // https://docs.python.org/3.10/library/struct.html#format-characters |
| 22 | switch (a.dtype()) { |
| 23 | case mx::bool_: |
| 24 | return "?"; |
| 25 | case mx::uint8: |
| 26 | return "B"; |
| 27 | case mx::uint16: |
| 28 | return "H"; |
| 29 | case mx::uint32: |
| 30 | return "I"; |
| 31 | case mx::uint64: |
| 32 | return "Q"; |
| 33 | case mx::int8: |
| 34 | return "b"; |
| 35 | case mx::int16: |
| 36 | return "h"; |
| 37 | case mx::int32: |
| 38 | return "i"; |
| 39 | case mx::int64: |
| 40 | return "q"; |
| 41 | case mx::float16: |
| 42 | return "e"; |
| 43 | case mx::float32: |
| 44 | return "f"; |
| 45 | case mx::bfloat16: |
| 46 | return "B"; |
| 47 | case mx::float64: |
| 48 | return "d"; |
| 49 | case mx::complex64: |
| 50 | return "Zf\0"; |
| 51 | default: { |
| 52 | std::ostringstream os; |
| 53 | os << "bad dtype: " << a.dtype(); |
| 54 | throw std::runtime_error(os.str()); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | struct buffer_info { |
| 60 | std::string format; |