简体   繁体   English

32 位 x86 上的 64 位 int 算术(来自 c )

[英]64 bit int arthimetic on 32bit x86 (from c )

Is there a fast way of using 64bit ints on 32 x86 machines (in c (added: and c++) language compilers)?有没有一种在 32 x86 机器上使用 64 位整数的快速方法(在 c(添加:和 c++)语言编译器中)?

32 bit x86 support 64 bit operations in some extent (there is some movq instruction in old mmx and some other commands probably), but how to use it from c? 32 位 x86 在某种程度上支持 64 位操作(旧 mmx 中可能有一些 movq 指令和其他一些命令),但是如何从 c 中使用它?

What if someone want to use a 64bit ints arithmetic in c on 32 bit x86 machines - how to do it most easy and efficient?如果有人想在 32 位 x86 机器上使用 c 中的 64 位整数算法怎么办 - 如何最简单有效地做到这一点?

//EdiT //编辑

do by now I found some of candidates for it现在我找到了一些候选人

    uint64_t A;
    long long a;
    int64 a;
    __int64 a;

what should be used ?应该使用什么? is there a chance that some implementation of above arthimetic is better/faster than other?上述算术的某些实现是否有可能比其他实现更好/更快?

To perform 64 bit operations you can use either int64_t or uint64_t . 要执行64位操作,可以使用int64_tuint64_t

They are defined in C99 by the header file stdint.h . 它们在C99 stdint.h文件stdint.h定义。

Is there a fast way of using 64bit ints on 32 x86 machines (in c language compilers)? 是否有在32种x86机器上(在C语言编译器中)使用64位整数的快速方法?

int isn't guaranteed to be 64 bits wide; 不能保证int为64位宽; It's guaranteed to be at least 16 bits wide. 保证至少16位宽。 If you want a type that's guaranteed to be at least 64 bits wide, use long long instead. 如果您想要保证至少64位宽的类型,请改用long long Talking about optimisation at this level is quite fruitless. 在这个级别上谈论优化是徒劳的。 You're better off coming up with a complete solution, profiling it to determine what the slowest part of the solution is and targeting that part of your code for optimisation or choosing a different algorithm that performs that slow operation more quickly. 您最好提供一个完整的解决方案,对其进行概要分析以确定解决方案中最慢的部分,然后针对代码的那部分进行优化,或者选择一种可以更快地执行该缓慢操作的算法。 Note: By solution, I mean "a program that solves an actual problem". 注意:解决方案是指“解决实际问题的程序”。

32 bit x86 support 64 bit operations in some extent (there is some movq instruction in old mmx and some other commands probably), but how to use it from c? 32位x86在某种程度上支持64位操作(旧的mmx中可能有一些movq指令以及一些其他命令),但是如何从c中使用它呢?

Whether or not your compiler performs the movq and/or mmx optimisations you mentioned automatically is questionable, as you haven't told us which compiler you're using. 编译器是否自动执行您提到的movq和/或mmx优化是有问题的,因为您还没有告诉我们您使用的是哪个编译器。 However, given the simplicity of this sort of optimisation compared to others (eg. dead code optimisation, tail call optimisation, even loop unrolling), I'd guess that your compiler does it automatically. 但是,鉴于这种优化相对于其他优化的简单性(例如,优化死代码,优化尾部调用,甚至循环展开),我猜您的编译器会自动执行。 This is another reason talking about optimisation at this level is fruitless; 这是在此级别谈论优化是没有结果的另一个原因。 Those who write compilers are usually very good programmers, with a keen understanding of algorithms who can write automaton to perform simple optimisations easily. 那些编写编译器的人通常是非常优秀的程序员,对算法的敏锐理解可以编写自动机以轻松执行简单优化。

What if someone want to use a 64bit ints arithmetic in c on 32 bit x86 machines - how to do it most easy and efficient? 如果有人想在32位x86机器上的c语言中使用64位整数算法,该怎么办?如何最轻松,最有效地做到这一点?

Have you tried compiling a fully-optimised testcase and looking for movq operations in it's machine code? 您是否尝试过编译经过完全优化的测试用例,并在其机器代码中寻找movq操作? If I haven't convinced you that you should profile your code to determine whether or not this is actually worth targetting, then do this: Compile your solution (something that solves a problem, remember... and compile as "fully optimised"), benchmark it so that you have something to measure your optimisations against, convert the machine code to assembly, perform any manual optimisations in assembly, recompile and benchmark again. 如果我没有说服您应该对代码进行概要分析以确定该代码是否真正值得针对,请执行以下操作:编译您的解决方案(解决问题的内容,请记住...并编译为“完全优化”) ,对它进行基准测试,以便您可以衡量优化,将机器代码转换为汇编,在汇编中执行任何手动优化,然后重新编译和基准测试。 You might: 您可能会:

  1. spend a week looking optimising and benchmarking to find that you've shaved a few microseconds and give up... This would be a good indication that your compiler spends a few seconds producing code that is as good as the code you'd take weeks to write. 花一周的时间进行优化和基准测试,以发现您节省了几微秒的时间并放弃了...这将很好地表明您的编译器花了几秒钟来生成与您花费数周的代码一样好的代码来写。 Keep the compiler, but give up on the micro-optimisation. 保留编译器,但放弃微优化。
  2. find a significant optimisation that your compiler doesn't perform (fairly unlikely). 找到编译器无法执行的重大优化(极不可能)。 Either choose a more optimal compiler, or get in contact with the people who wrote your compiler, and explain it to them... 选择更理想的编译器,或者与编写您的编译器的人联系,然后向他们解释...

Either way, progress! 无论哪种方式,进步!

据我所知,在32位系统上的Assemby中有些复杂的过程,因此可能无法在32位系统上进行加速的64位乘法或除法(使用了软件例程)(这将是对它的部分回答),但是我仍然不确定100%是否正确,因此,如果我错了,请更正此错误。

As others have said, the compilers do a lot of optimizations automatically.正如其他人所说,编译器会自动进行很多优化。 To mess with those optimizations, you'd have to go into the compiler settings or add commands, but in your case, people have had 50 years to solve this problem so it's best to leave it be unless you want to write your own compiler.要搞乱这些优化,您必须进入编译器设置或添加命令,但就您而言,人们已经有 50 年的时间来解决这个问题,因此除非您想编写自己的编译器,否则最好将其搁置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在64位x86环境中编译32位x86应用程序? - How to compile 32bit x86 application in 64bit x86 environment? 在x86_64芯片(64位服务器)上运行的C ++ unix 32位到Red Hat Linux - C++ unix 32bit to Red Hat Linux running on an x86_64 chip (64-bit server) 在x86(32位)程序集中实例化c ++类(通过复制将c ++类传递给程序集中的方法) - Instantiating a c++ class in x86 (32bit) assembly (Passing a c++ class by copy to a method in assembly) 如何从32位WOW进程获取Program Files文件夹路径(不是Program Files(x86))? - How to get Program Files folder path (not Program Files (x86)) from 32bit WOW process? 32bit int * 32bit int = 64 bit int? - 32bit int * 32bit int = 64 bit int? 在 Windows 上使用 cmake 构建 x86(32 位)项目时使用 64 位 python3 解释器 - Use 64 bit python3 interpreter when building x86 (32 bit) project with cmake on Windows 什么是C ++中的32Bit和64Bit窗口中的兼容“int”类型? - What is Compatible “int” type in both 32Bit & 64Bit windows in C++? 在Fedora 20 x86_64上编译32位linux应用程序时使用sndfile库 - using sndfile library when compiling 32bit linux application on Fedora 20 x86_64 从32位应用启动64位应用? - Start a 64bit app from a 32bit app? 将应用程序从 32 位移植到 64 位的问题 - Issue in porting application from 32bit to 64bit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM