如何在 Visual Studio 上启用 __int128?
当我在 Visual Studio 的 C++ 项目中键入 __int128 时,编辑器将 __int128 的颜色更改为蓝色(类似于关键字)。
但是当我编译源代码时,出现以下错误:
error C4235:
nonstandard extension used : '__int128' keyword not supported on this architecture
How can I enable __int128
on Visual Studio?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
经过长时间的争论,微软员工秘密地在他们的标准库中添加了对 int128 的支持。然而,考虑到一些程序员认为 128 位整数是不必要的,我不能保证它们在未来的代码中可用。
__MSVC_Int128.hpp
实现了一个类似于__Int128
的类,并且使用运算符对其进行了重载。但是,由于它是一个类,因此可能需要在某些地方进行强制转换它的名称是
std::_Signed128
或std::_Unsigned128
,您需要检查 Header使用前先将其归档。MSVC STL
辩论
After a long debate, Microsoft employees secretly added support for int128 in their Standard library. However, given that some programmers believe that 128 bit integers are unnecessary, I cannot guarantee that they will be available in future code.
The
__MSVC_Int128.hpp
implements a class similar to__Int128
and they overloaded it with operators. However, as it is a class, it may need to be cast in some placesIts name is
std::_Signed128
orstd::_Unsigned128
, you need to check the Header file before using it.MSVC STL
debate
另一种方法是使用 boost:
https://www.boost.org/doc/libs/1_62_0/libs/multi precision/doc/html/boost_multi precision/tut/ints/cpp_int.html
An alternative is to use boost:
https://www.boost.org/doc/libs/1_62_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/cpp_int.html
您与字符串的转换可能需要一些改进。
为了安全起见,转换为字符串的接口应该包括用户提供的字符串的分配长度,因此如果它们没有提供足够的内存,您可以返回错误。
另外,尝试以块的形式处理字符串:例如,假设用户想要将 128 位数字转换为基数 10。您可以执行模
1000000000ul
操作,而不是重复执行模 10,并使用 <代码>sprintf(s, "%09u", c).从字符串转换也可以进行类似的优化。
包含返回类型为
std::pair<_uint128, _uint128>
的divrem
方法并不是一个坏主意。如果您有一个整数类,其中用于
hi
和lo
的类型是模板参数,那就太棒了。然后,使用少量 typedef,您可以创建 int256、int512 等。Your conversions to/from string could use some improvement.
For saftey, the interface for converting to a string should include the allocated length of the user provided string, so you can return an error if they didn't provide enough memory.
Also, try to process strings in chunks: for example, suppose the user wants to convert a 128 bit number into base 10. Instead of repeatedly doing modulo 10, you could be doing modulo
1000000000ul
, and usingsprintf(s, "%09u", c)
.Converting from a string could be similarly optimized.
It would not be a bad idea to include a
divrem
method, with a return type ofstd::pair<_uint128, _uint128>
.It would be absolutely awesome if you had a integer class where the type used for
hi
andlo
was a template parameter. Then, with a small handful of typedefs, you could create an int256, an int512, etc..MSDN 未将其列为可用,并且最近的响应 同意,所以正式地,不,没有名为
__int128
的类型,并且无法启用它。此外,永远不要相信语法荧光笔;它是用户可编辑的,因此可能包含虚假或“未来”类型。 (但是,由于错误,它可能是一个保留字,因此您应该避免命名任何类型
__int128
,这遵循以下约定:任何带有双下划线前缀的内容都应保留供编译器使用)。人们可能会认为
__int128
可能通过寄存器跨越在 x64/IPF 机器上可用,就像__in64
在 32 位目标上一样,但目前仅 128 位类型源于 SIMD 类型(__m128
及其各种类型形式)。MSDN doesn't list it as being available, and this recent response agrees, so officially, no, there is no type called
__int128
and it can't be enabled.Additionally, never trust the syntax hilighter; it is user editable, and thus likely to either have bogus or 'future' types in it. (it is probably a reserved word however, due to the error, thus you should avoid naming any types
__int128
, this follows the convention that anything prefixed with a double underscore should reserved for compiler use).One would think the
__int128
might be available on x64/IPF machines via register spanning, like__in64
is on 32bit targets, but right now the only 128 bit types stem from SIMD types (__m128
and its various typed forms).我在 1996 年的旧 Visual Studio 6.0 C++(32 位)中发现了一个宝藏,它利用 MS 自己的汇编程序例程在 32 位 CPU 上启用 64 位数学(__int64)。遗憾的是源代码不可用)。
但是,执行调用这些函数的调试会话、复制/粘贴反汇编程序列表、搜索替换“dword ptr”-> “qword ptr”,eax,ebx,...-> rax,rbx,...以及对用于参数传递的寄存器的一些调整(和很多咖啡),我成功地编写了这段代码,这使得在 x64 模式下执行 _int128-math 成为可能可以用 32 位进行 __int64-math。重要的是相同的代码,并且位/周期加倍。
关于版权问题,我在反汇编程序列表中没有看到任何许可证,也许微软是时候将其集成到他们的 x64 C++ 编译器中了(2015 年版本)
代码放在这里
还有 3 个文件。这里空间不够...
I've found a treasure in my old Visual Studio 6.0 C++ from 1996 (32-bit) making use of MS's own assembler routines that enabled 64-bit math on a 32-bit CPU(__int64). Source-code unfortunately not available).
However, doing a debug-session that calls these functions, copy/paste the disassembler-list, search-replace "dword ptr" -> "qword ptr", eax,ebx,... -> rax,rbx,... and a Little adjustment of registers used for parameter-passing (and a lot of coffee), I succeeded to make this code, that makes it possible to do _int128-math in x64-mode just as it is possible to do __int64-math with 32-bit. It's essential the same code, with a doubleup in bits/cycle.
For the matter of copyrights, I've seen no licenses in the disassembler-list, and perhaps it's time for Microsoft to integrate this into their x64 C++ compiler (vers. 2015)
The code goes here
There will be 3 more files. not enough Space here...
_int128 的新版本解决了提到的一些问题。它包含一个 natvis 插件,因此您可以在调试器中查看 int128。为此,需要编写 x86 版本的 int128,因为 natvis-dll 需要是 win32。
为成员 lo,hi 使用 af 模板的想法是可以的,但我认为这有点乐观,因为真正完成工作的例程必须使用
CPU 的寄存器至少目前只有 64 位。但是当 Intel 发布 128 位 CPU 时就可以了。
添加了 c++ std 流中的输入/输出
还添加了许多内联运算符,因此编译器将
不会产生歧义。
代码太大,无法适应这个答案,因此它被放在 github 中,并包含下面的文件列表的链接
新标头 Int128.h
Int128x64.asm x64 汇编代码
<一个href="https://github.com/JesperMikkelsen/Big-Numbers/blob/master/Lib/Src/Math/Int128x86.cpp" rel="nofollow noreferrer">Int128x86.cpp
Int128Str.cpp 对于 x86 和 x64 通用
Int128IO.cpp x86 和 x64 通用
由调试器调用的 AddIn-dll 将 _int128/_uint128 转换为字符*(十进制/十六进制)
所有 natvis addin dll 的标头
There is a new version of _int128 which solves some of the problems mentioned. It includes a natvis addin, so you can view int128 in the debugger. To do this it was nessecary to write an x86 version of int128, because natvis-dll's need to be win32.
The idea of using af template for the members lo,hi is ok, but I think it's a little to optimistic, because the routines that do the real job have to use the
CPU's registers which, at least at the moment, are only 64 bit. But ok when Intel releases a 128-bit CPU.
in/out in c++ std stream are added
A lot of inline operators have been added too, so the compiler will do
without ambiguities.
The code is too big to fit in this answer so it's put in github with links to the files listet below
New header Int128.h
Int128x64.asm Assembler code for x64
Int128x86.cpp
Int128Str.cpp Common for x86 and x64
Int128IO.cpp Common for x86 and x64
AddIn-dll called by debugger to convert _int128/_uint128 to char*(decimal/hex)
Header for all natvis addin dll's
其余的都在这里。 (与字符串的转换函数)
And the rest is here. (conversion functions to/from string)