在C字和双字整数 [英] Word and Double Word integers in C

查看:283
本文介绍了在C字和双字整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个简单,高效的适度库BIGNUM在C.我想用来存储使用它的(presumably 32位或64位整数)编译系统的全部寄存器大小的数字。我的理解是,我可以通过使用intptr_t做到这一点。它是否正确?有没有更合适的语义类型,即像intword_t?

I am trying to implement a simple, moderately efficient bignum library in C. I would like to store digits using the full register size of the system it's compiled on (presumably 32 or 64-bit ints). My understanding is that I can accomplish this using intptr_t. Is this correct? Is there a more semantically appropriate type, i.e. something like intword_t?

我也知道,与GCC我可以上溯造型两个参数为64位整数,这将占用两个寄存器,并采取像IA31 ADC指令(带进位加法)的优势很容易做到在32位机器上溢出检测。我可以做一个64位计算机上类似的东西?有128位型我可以上溯造型到编译,如果他们提供使用这些指令?更妙的是,有没有重新presents两倍的寄存器大小(如intdoubleptr_t),所以这可能在一台机器无关的方式来完成?标准型

I also know that with GCC I can easily do overflow detection on a 32-bit machine by upcasting both arguments to 64-bit ints, which will occupy two registers and take advantage of instructions like IA31 ADC (add with carry). Can I do something similar on a 64-bit machine? Is there a 128-bit type I can upcast to which will compile to use these instructions if they're available? Better yet, is there a standard type that represents twice the register size (like intdoubleptr_t) so this could be done in a machine independent fashion?

谢谢!

推荐答案

我强烈建议使用C99 < stdint.h> 头。它声明 int32_t 的int64_t uint32_t的 uint64_t中,它是什么样子,你真的想用。

I'd strongly recommend using the C99 <stdint.h> header. It declares int32_t, int64_t, uint32_t, and uint64_t, which look like what you really want to use.

编辑:阿洛克指出, int_fast32_t int_fast64_t 等,可能要使用的东西。指定应该需要用于数学最小工作,即用于计算到不是翻身比特的数目。

As Alok points out, int_fast32_t, int_fast64_t, etc. are probably what you want to use. The number of bits you specify should be the minimum you need for the math to work, i.e. for the calculation to not "roll over".

优化来自一个事实,即CPU没有浪费周期重新调整数据,在读填充领先比特,并做在写一个读 - 修改 - 写。事实是,很多处理器(如最近的x86s)有硬件以及优化这些接入pretty在CPU中(至少填充和读 - 修改 - 写的部分),因为它们是如此普遍,通常只涉及处理器和缓存之间的转移。

The optimization comes from the fact that the CPU doesn't have to waste cycles realigning data, padding the leading bits on a read, and doing a read-modify-write on a write. Truth is, a lot of processors (such as recent x86s) have hardware in the CPU that optimizes these access pretty well (at least the padding and read-modify-write parts), since they're so common and usually only involve transfers between the processor and cache.

所以,留给你唯一要做的就是确保访问均是对齐:取的sizeof(int_fast32_t)或什么,并用它来确保你的缓冲区指针对准该

So the only thing left for you to do is make sure the accesses are aligned: take sizeof(int_fast32_t) or whatever and use it to make sure your buffer pointers are aligned to that.

事实是,这可能不会达到那么多的改善(由于硬件优化运行​​时转移无论如何),所以写的东西,时间可能是可以肯定的唯一途径。另外,如果你真的很疯狂的表现,你可能需要看看SSE或AltiVec技术或任何量化技术的处理器,因为这将胜过任何东西做向量运算时,你可以写是便携式的。

Truth is, this may not amount to that much improvement (due to the hardware optimizing transfers at runtime anyway), so writing something and timing it may be the only way to be sure. Also, if you're really crazy about performance, you may need to look at SSE or AltiVec or whatever vectorization tech your processor has, since that will outperform anything you can write that is portable when doing vectored math.

这篇关于在C字和双字整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆