[英]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_t
或uint64_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:
您可能会:
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.