Python extension - construct and inspect large integers efficiently
I have a native library for which a natural interface would involve
passing potentially large numbers. I anticipate about half being < 32
bits; another quarter < 64 bits; the next eighth < 128 bits - and so on,
without a fixed length limit.
PyLong_FromUnsignedLongLong() and PyLong_AsUnsignedLongLong() would be
suitable if I could constrain values to fit in a single register.
PyLong_FromString() overcomes this - but at the undesirable expense of
requiring an intermediate representation. _PyLong_FromByteArray() and
_PyLong_AsByteArray() mitigate this cost (by making this intermediate
representation simple) but the leading underscore makes me wonder if this
may lead to portability problems.
In longintrepr.h, I've found struct _longobject... which hints that it
might be a way to interact directly with the internal representation...
though an absence of detailed documentation about this structure remains a
hurdle.
What approach will result in optimal throughput between Python and the
library? Is there documentation I've overlooked?
No comments:
Post a Comment