在我的 64 位计算机上long long
类型有 64 位。
print(sizeof(long long))
# prints 8
我需要使用 128 位整数,幸运的是海湾合作委员会支持这些 https://gcc.gnu.org/onlinedocs/gcc/_005f_005fint128.html。我如何在 Cython 中使用这些?
以下不起作用。编译foo.pyx
只包含
cdef __int128_t x = 0
yields
$ cython foo.pyx
Error compiling Cython file:
------------------------------------------------------------
...
cdef __int128_t x = 0
^
------------------------------------------------------------
foo.pyx:2:5: '__int128_t' is not a type identifier
编辑:这不再是解决方法,这是正确的方法。另请参阅@IanH 的回答。
现在,你遇到的问题是cython
无法识别您的类型,同时gcc
做。所以我们可以尝试欺骗cython
.
File helloworld.pyx
:
cdef extern from "header_int128.h":
# this is WRONG, as this would be a int64. it is here
# just to let cython pass the first step, which is generating
# the .c file.
ctypedef unsigned long long int128
print "hello world"
cpdef int foo():
cdef int128 foo = 4
return 32
File header_int128.h
:
typedef __int128_t int128;
File setup.py
:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("helloworld.pyx"))
现在,在我的机器上,当我运行时python setup.py build_ext --inplace
,第一步通过,文件helloworld.c
生成,然后gcc
编译也通过了。
现在如果你打开文件helloworld.c
,你可以检查你的变量foo
实际上被声明为int128
.
使用此解决方法时要非常小心。特别是,如果您分配一个,则 cython 可能不需要在 C 代码中进行强制转换int128
to a int64
例如,因为在该过程的这一步实际上并没有区分它们。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)