使用ctypes处理128位整数

Fil*_*Fil 8 python ctypes int128

使用Python ctypes支持128位整数(目前为__uint128_t)的最佳方法是什么?

可能是两个uint64_t的用户定义结构,但这会在需要时产生对齐问题.

有关为什么ctypes没有扩展到支持128位整数的任何想法?

Ros*_*dge 2

如果您确实想使用 128 位整数,则无需担心对齐问题。当前没有任何架构、没有任何运行 Python 的机器支持 128 位本机整数运算。因此,没有机器需要或受益于 128 位整数 16 字节对齐。只需使用用户定义的结构就可以了。

如果您真正要求的是支持 128 位向量类型,那么您可能需要将它们对齐。也就是说,如果您在 Python 代码中创建它们并通过引用 C/C++ 代码传递它们,则需要将它们对齐。您无法可靠地按值传递它们,无法让 ctypes 在堆栈上正确对齐它们(如果架构 ABI 需要的话)。从 C/C++ 传递到 Python 的向量可能已经正确对齐。因此,如果您可以安排它,以便所有向量都在 C/C++ 代码中分配,您也应该可以使用用户定义的结构。

假设您确实需要在 Python 代码中创建对齐向量,那么我已经包含了对齐 ctypes 数组的代码。我还有代码来对齐其他 ctypes 类型,但我没有将其包含到合理的代码大小中。数组对于大多数用途来说应该足够了。这些对齐的阵列有一些限制。如果将它们按值传递给 C/C++ 函数或者将它们作为成员包含在结构体或联合中,它们将无法正确对齐。您可以使用运算符创建对齐数组的对齐数组*

用于创建新的对齐数组类型。用于创建现有数组类型的对齐版本。aligned_array_type(ctypes-type, length, alignment)aligned_type(ctypes-type, alignment)

import ctypes

ArrayType = type(ctypes.Array)

class _aligned_array_type(ArrayType):
    def __mul__(self, length):
        return aligned_array_type(self._type_ * self._length_,
                      length, self._alignment_)

    def __init__(self, name, bases, d):
        self._alignment_ = max(getattr(self, "_alignment_", 1), 
                       ctypes.alignment(self))

def _aligned__new__(cls):
    a = cls._baseclass_.__new__(cls)
    align = cls._alignment_
    if ctypes.addressof(a) % align == 0:
        return a
    cls._baseclass_.__init__(a) # dunno if necessary
    ctypes.resize(a, ctypes.sizeof(a) + align - 1)
    addr = ctypes.addressof(a)
    aligned = (addr + align - 1) // align * align
    return cls.from_buffer(a, aligned - addr)

class aligned_base(object):
    @classmethod
    def from_address(cls, addr):
        if addr % cls._alignment_ != 0:
            raise ValueError, ("address must be %d byte aligned"
                       % cls._alignment_)
        return cls._baseclass_.from_address(cls, addr)

    @classmethod
    def from_param(cls, addr):
        raise ValueError, ("%s objects may not be passed by value"
                   % cls.__name__)

class aligned_array(ctypes.Array, aligned_base):
    _baseclass_ = ctypes.Array
    _type_ = ctypes.c_byte
    _length_ = 1
    __new__ = _aligned__new__

_aligned_type_cache = {}

def aligned_array_type(typ, length, alignment = None):
    """Create a ctypes array type with an alignment greater than natural"""

    natural = ctypes.alignment(typ)
    if alignment == None:
        alignment = typ._alignment_
    else:
        alignment = max(alignment, getattr(typ, "_alignment_", 1))

    if natural % alignment == 0:
        return typ * length
    eltsize = ctypes.sizeof(typ)
    eltalign = getattr(typ, "_alignment_", 1)
    if eltsize % eltalign != 0:
        raise TypeError("type %s can't have element alignment %d"
                " in an array" % (typ.__name__, alignment))
    key = (_aligned_array_type, (typ, length), alignment)
    ret = _aligned_type_cache.get(key)
    if ret == None:
        name = "%s_array_%d_aligned_%d" % (typ.__name__, length,
                           alignment)
        d = {"_type_": typ,
             "_length_": length,
             "_alignment_": alignment}
        ret = _aligned_array_type(name, (aligned_array,), d)
        _aligned_type_cache[key] = ret
    return ret

def aligned_type(typ, alignment):
    """Create a ctypes type with an alignment greater than natural"""

    if ctypes.alignment(typ) % alignment == 0:
        return typ
    if issubclass(typ, ctypes.Array):
        return aligned_array_type(typ._type_, typ._length_,
                      alignment)
    else:
        raise TypeError("unsupported type %s" % typ)
Run Code Online (Sandbox Code Playgroud)