Title: Conditionally support native half-precision IEEE types in struct/array/ctypes modules and memoryview · Issue #153740 · python/cpython · GitHub
Open Graph Title: Conditionally support native half-precision IEEE types in struct/array/ctypes modules and memoryview · Issue #153740 · python/cpython
X Title: Conditionally support native half-precision IEEE types in struct/array/ctypes modules and memoryview · Issue #153740 · python/cpython
Description: Feature or enhancement Proposal: Both gcc (since v12) and clang (since v14) have support for the _Float16 (i.e. partial support for Annex H of the C23). Complex half-precision type also supported by mentioned compilers. I think, we could...
Open Graph Description: Feature or enhancement Proposal: Both gcc (since v12) and clang (since v14) have support for the _Float16 (i.e. partial support for Annex H of the C23). Complex half-precision type also supported b...
X Description: Feature or enhancement Proposal: Both gcc (since v12) and clang (since v14) have support for the _Float16 (i.e. partial support for Annex H of the C23). Complex half-precision type also supported b...
Opengraph URL: https://github.com/python/cpython/issues/153740
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Conditionally support native half-precision IEEE types in struct/array/ctypes modules and memoryview","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nBoth gcc (since v12) and clang (since v14) have support for the _Float16 (i.e. partial support for Annex H of the C23). Complex half-precision type also supported by mentioned compilers. I think, we could utilize that and enable using native half-precision types conditionally in the ctypes module, like we do for complex types. For the rest (struct/array/memoryview) - half-precision types will be enabled unconditionally.\n\nToDo:\n- [x] modify PyFloat_Pack2/PyFloat_Unpack2 to use native 16-bit type, if available // https://github.com/python/cpython/pull/154796\n- [ ] ~~support `'Ze'` type in the struct module // https://github.com/python/cpython/pull/156805~~\n- [ ] ~~suppprt `'Ze'` type in the array module // https://github.com/python/cpython/pull/156805~~\n- [ ] ~~support `'Ze'` type in the memoryview~~\n- [ ] ~~add support for `'e'` (`_Float16`) to the ctypes module~~\n- [ ] ~~add support for `'Ze'` (`_Float16 _Complex`) to the ctypes module~~\n\nLast two tasks looks less trivial, as the libffi has no support for `_Float16` type. Does this prevents us from adding such type or we can include workarounds for the libffi to the CPython itself?\n\n\u003cdetails>\n\nFollowing works on x86:\n```\n$ cat a.c\n#define __STDC_WANT_IEC_60559_TYPES_EXT__\n#include \u003cmath.h>\n#include \u003cstdio.h>\n#include \u003cstring.h>\n\n#include \u003cffi.h>\n\n_Float16 myexp(_Float16 x)\n{\n return 1 + x;\n}\n\nint main(void)\n{\n _Float16 f16 = 1.23e3;\n void *values[1] = {&f16};\n float res;\n _Float16 res2;\n ffi_type *args[1] = {&ffi_type_float};\n ffi_cif cif;\n\n if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,\n &ffi_type_float, args) != FFI_OK)\n {\n return 1;\n }\n ffi_call(&cif, FFI_FN(myexp), &res, values);\n memcpy(&res2, &res, 2); /* fixup, assuming LE */\n printf(\"%e\\n\", (double)res2);\n return 0;\n}\n$ gcc a.c -Wall -lm -lffi && ./a.out\n1.231000e+03\n```\n\n\u003c/details>\n\n\n\n### Has this already been discussed elsewhere?\n\nI have already discussed this feature proposal on Discourse\n\n### Links to previous discussion of this feature:\n\nhttps://discuss.python.org/t/106723\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154796\n* gh-156805\n* gh-156815\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/skirpichev","@type":"Person","name":"skirpichev"},"datePublished":"2026-07-15T05:01:28Z","url":"https://github.com/python/cpython/issues/153740"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:18b204d4-431b-6301-aad1-f19b8af877b7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF86:4279A:31B8E94:42DD580:6B020780 |
| html-safe-nonce | 4a02c8bebe201e41f0ca88891f5a4c33a0f5ef16e545490848e3920279206982 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjg2OjQyNzlBOjMxQjhFOTQ6NDJERDU4MDo2QjAyMDc4MCIsInZpc2l0b3JfaWQiOiI4MDgxMDA5NDMxOTY3NTY1Njk2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c2b032678a76bd84d315cb894860d6534dc20f714f3f52c21055f618c6c8ff0d |
| hovercard-subject-tag | issue:4889438016 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/153740/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5a3dd447fa258a89972953e8630a13358c6dc597f67fb58015a2cc8176996015/python/cpython/issues/153740 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5a3dd447fa258a89972953e8630a13358c6dc597f67fb58015a2cc8176996015/python/cpython/issues/153740 |
| og:image:alt | Feature or enhancement Proposal: Both gcc (since v12) and clang (since v14) have support for the _Float16 (i.e. partial support for Annex H of the C23). Complex half-precision type also supported b... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | skirpichev |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width