This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add 128-bit integer support to struct
Type: enhancement Stage: needs patch
Components: ctypes Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: alex, fil, francismb, gregory.p.smith, mark.dickinson, meador.inge, nanjekyejoannah, pitrou, tim.peters
Priority: normal Keywords:

Created on 2013-12-06 02:04 by fil, last changed 2022-04-11 14:57 by admin.

Messages (15)
msg205344 - (view) Author: Fil Mackay (fil) Date: 2013-12-06 02:04
I've been looking at adding 128-bit support to the struct module. Currently only named integer types are supported, which vary in implementation. These include:

short
int
long
long long

Depending on the platform, none may translate to 128-bit integer (the case with all platforms today?).

One approach would be to make a new type that relates specifically to 128-bit integer, side-stepping the naming approaches to integer in C.

The other, would be to make new types for all integer sizes that relate to specific sizes, instead of relying on C namings. Much bigger implications?

I propose creating new types:

"o": __int128_t
"O": __uint128_t
"t": __int256_t (why not?)
"T": __uint256_t
"v": __int512_t (what, too far?)
"V": __int512_t

What implications are there here in killing the connection between a C named type and a specific size?
msg205357 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-12-06 08:26
Do you have a particular application / use-case in mind?  Would int.from_bytes and int.to_bytes fill the need?
msg205399 - (view) Author: Fil Mackay (fil) Date: 2013-12-06 20:45
The use case is interacting with C structures that have 128/256/512 bit integers in them. These require correct memory alignment, and can't reliably be hacked with multiple int64's.

These size ints come from the fact that CPU's now have registers of these sizes, and can't be serviced with int.from/to_bytes for performance reasons. The same reason int64 being supported with this approach would be very inconvenient and terrible for performance since they are an intrinsic type in modern hardware, not a software construction.

Two key use cases I can think of are:

- any form of integer SIMD operation (vectors)
- hosting and maintaining hash values which are routinely 128-bit and greater
msg205400 - (view) Author: Fil Mackay (fil) Date: 2013-12-06 20:46
I noticed that python 2.7 has been removed - why would this not be appropriate for this version?

Given the current level of use of this version, I see it's inclusion as very important - without having to be relegated to 3.x only.
msg205403 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2013-12-06 21:17
Fil, here's the release schedule for Python 2.8:

    http://www.python.org/dev/peps/pep-0404/

In short, 2.8 will never be released (well, never by us), and only bugfixes can be applied to the 2.7 line.  That's why 2.7 was removed.  Regardless of its merits, this is a new feature request, not a bugfix.

A future Python 3 release is the only possibility for new features.
msg205431 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-12-07 01:37
> any form of integer SIMD operation (vectors)

Wouldn't these be appropriately represented by a tuple of integers (or floats)? For example, a SIMD vector of four 32-bit integers could be represented as four Python ints. Or would that be "terrible for performance"?

Note that the struct module may include support for int128_t, but it certainly won't have native support for every SIMD vector format under the sun (if they have different alignment requirements).

> hosting and maintaining hash values which are routinely 128-bit and greater

That sounds like a job for a bytes object (as returned by e.g. hashlib.sha1(...).digest()).
msg205447 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2013-12-07 10:47
If performance is the reason for the feature: My impression is that
the goal of the struct module is not necessarily top performance.

E.g. in memoryview the custom unpackers for comparisons are 30-60 times
faster than using the struct module.
msg205574 - (view) Author: Francis MB (francismb) * Date: 2013-12-08 16:10
> If performance is the reason for the feature: My impression is that
> the goal of the struct module is not necessarily top performance.

I'm not sure if it applies but on #19905 (message 205345 [1])
is said its a dependency for that issue

----
[1] http://bugs.python.org/issue19905#msg205345
msg205626 - (view) Author: Fil Mackay (fil) Date: 2013-12-09 00:34
Stefan, performance is not the principle motivator here: the intention is that it is just sensible to support this integer type, just like supporting int64 since it is an intrinsic type supported by CPU's.

Of course any integer length could be handled by memoryview unpacker, but this would not really make sense for int64. My view is that any intrinsic type (ie. register type) is the key point at which it should be supported by struct, and not a custom unpacker. 128/256/512 bit integers are in this category..

Principally this is so that it can be used by ctypes (see #19905).
msg205627 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-12-09 00:38
> Of course any integer length could be handled by memoryview unpacker,
> but this would not really make sense for int64. My view is that any
> intrinsic type (ie. register type) is the key point at which it should
> be supported by struct, and not a custom unpacker. 128/256/512 bit
> integers are in this category..

I don't think "register types" matter here. The struct module provides
interoperability with low-level *data types* (in C and other common
languages), not CPU *registers*.

So IMHO the question is whether there is an use case for 128-bit
integers; *not* for arrays of four 32-bit integers packed in a 128-bit
register.
msg205628 - (view) Author: Fil Mackay (fil) Date: 2013-12-09 00:39
Antoine,

No, the SIMD vector should be treated as a int128 (for eg.), not as 4 python ints. It is the unpacking process that unpacks into 4 python ints. The two shouldn't be confused - you definitely want to be able to treat the vector in both states (packed and unpacked).

I'm not saying that Python should necessarily make use of SIMD instructions (don't think performance is critical there in py), but that it should at least be access to a raw 128/256/512 bit integer within a C structure. Part of this is interacting with SIMD'able structures - at least in my use cases :)
msg205629 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-12-09 00:44
> The two shouldn't be confused - you definitely want to be able to
> treat the vector in both states (packed and unpacked).

Why do you need the packed state as a 128-bit int, rather than as a
bytes object? You are not doing arithmetic on it, AFAIU.
msg205634 - (view) Author: Fil Mackay (fil) Date: 2013-12-09 01:04
Antoine,

> I don't think "register types" matter here. The struct module provides
> interoperability with low-level *data types* (in C and other common
> languages), not CPU *registers*.

OK, see where you're coming from. I guess my thinking was such that struct (and ctypes) support is required for data types that are common within C structures. The fact that 128-bit ints are now appearing within these structures is due to the fact that CPU support has dramatically improved, and are supported natively in register types. So it was kind of an indirect connection.

So the question I guess is whether you think these types are "common enough" in C structs, which for me they certainly are for my data processing applications.. (pretty please :)

> So IMHO the question is whether there is an use case for 128-bit
> integers; *not* for arrays of four 32-bit integers packed in a 128-bit
> register.

Agree completely - just stick to thinking about atomic 128-bit integers. A pack/unpack function should do the conversion, and have nothing to do with this struct support.
msg205763 - (view) Author: Fil Mackay (fil) Date: 2013-12-10 01:14
So where do we go from here - is there enough support for this to proceed, or vote the idea off the island :)
msg205765 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2013-12-10 01:43
Pick two suitable letters for the int128 and uint128 cases and create a patch for an implementation with tests (targeting Python 3.5).  The implementation needs to compile and work even when the platform C compiler Python is compiled with does not have a 128-bit integer type (so the struct module always supports this rather than depending upon the platform native support in C).

I wouldn't bother with 256 and 512 bit integer support because I've never seen those used.  If that changes before 3.5 is released (a couple years), they can be added.
History
Date User Action Args
2022-04-11 14:57:55adminsetgithub: 64103
2019-05-27 19:23:17nanjekyejoannahsetnosy: + nanjekyejoannah
2015-08-27 19:20:48zach.warelinkissue19905 dependencies
2015-08-27 19:20:28zach.waresetstage: needs patch
versions: + Python 3.6, - Python 3.5
2014-10-14 16:26:40skrahsetnosy: - skrah
2013-12-10 01:43:26gregory.p.smithsetnosy: + gregory.p.smith
messages: + msg205765
2013-12-10 01:14:29filsetmessages: + msg205763
2013-12-09 01:04:52filsetmessages: + msg205634
2013-12-09 00:44:45pitrousetmessages: + msg205629
2013-12-09 00:39:25filsetmessages: + msg205628
2013-12-09 00:38:55pitrousetmessages: + msg205627
2013-12-09 00:34:26filsetmessages: + msg205626
2013-12-09 00:15:01meador.ingesetnosy: + meador.inge
2013-12-08 16:10:39francismbsetnosy: + francismb
messages: + msg205574
2013-12-07 10:47:49skrahsetnosy: + skrah
messages: + msg205447
2013-12-07 01:37:29pitrousetnosy: + pitrou
messages: + msg205431
2013-12-06 21:17:46tim.peterssetnosy: + tim.peters
messages: + msg205403
2013-12-06 20:47:18alexsetnosy: + alex
2013-12-06 20:46:37filsetmessages: + msg205400
2013-12-06 20:45:06filsetmessages: + msg205399
2013-12-06 19:38:55rhettingersetversions: - Python 2.7, Python 3.4
2013-12-06 08:26:38mark.dickinsonsetnosy: + mark.dickinson
messages: + msg205357
2013-12-06 02:04:23filcreate