如何在 32 位体系结构中将 64 位 unsigned long long 分配给 32 位结构 [英] how to assign 64 bit unsigned long long to 32 bit structure in 32 bit architecture

查看:49
本文介绍了如何在 32 位体系结构中将 64 位 unsigned long long 分配给 32 位结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个内核,一个是 32 位,另一个是 64 位.

I have 2 cores, one is 32 bit and other is 64 bit.

在 64 位机器上,我支持 unsigned long long,我需要将这个值分配给一个可以在 32 位机器上访问的变量,例如:-

On 64 bit machine, I have support for unsigned long long, and I need to assign this value to a varriable which will be access in 32 bit machine, such as:-

typedef struct {
    unsigned int low;
    unsigned int high;
} myint64_t;

myint64_t app_sc;

以下是 64 位机器的代码片段:

Below is the code snippet for 64 bit machine:

unsigned long long sc;

/* Calculate sc */
...

现在在 64 位机器上,我需要将sc"分配给 app_sc,并将其用于 64 位机器上的一些计算.

Now on 64 bit machine, I need to assign "sc" to app_sc, and use it for some computing on 64 bit machine.

我试图做这样的事情:-

I was trying to do something like this:-

app_sc = sc;

但是编译器给了我编译时错误.我也可以做类似的事情:-

but compiler gives me compile time errors. I can similarly do, something like this:-

 app_sc.low = sc & 0xFFFFFFFF;
 app_sc.high = (sc>>32) & (0xFFFFFFFF);

但这能保证在所有情况下都有效吗?

But does that guarantee, it will work in all cases?

有什么更好的方法吗?

推荐答案

如果编译器并不太老,支持相当新的 C 标准(可能是 C99,甚至更早),你应该有一个 <stdint.h> 标题给你一个像 int64_t<这样的类型/code>(对于有符号的 64 位整数)或 uint64_t(对于无符号的 64 位整数),即使在 32 位机器上也正好是 64 位.

With a not too old compiler supporting a reasonably recent C standard (probably C99, perhaps even earlier) you should have a <stdint.h> header giving you a type like int64_t (for a signed 64 bits integer) or uint64_t (for an unsigned 64 bits integer) which is exactly 64 bits, even on 32 bits machine.

如果你的编译器没有,我强烈建议升级你的编译器.您可能可以使用 GCC(除非您有一个非常奇怪的目标架构,GCC 不支持).最新的 GCC 版本是 4.7.

If your compiler don't have it, I strongly suggest to upgrade your compiler. You could probably use GCC (except if you have a very strange target architecture, unsupported by GCC). Latest GCC version is 4.7.

请注意,32 位机器上的 64 位算术在实践中需要一些编译器支持才能达到合理的效率.(例如,使用带进位指令的加法).它不能仅通过库来完成,即使需要库来提供最复杂的操作(例如 32 位机器上的 64 位除法).换句话说,对于 32 位机器,快速的 64 位算术不能用 C 进行可移植的编码.

Notice that 64 bits arithmetic on 32 bits machines needs in practice some compiler support to be reasonably efficient. (e.g. to use add with carry instructions). It can't be done only thru a library, even if a library is required to supply the most complex operations (e.g. 64 bits divisions on 32 bits machines). In other words, fast 64 bits arithmetic can't be portably coded in C for a 32 bits machine.

对于更大的数字,请考虑使用任意精度的数字(称为bigints),例如通过 GNU gmp 库.此类库使用非平凡的算法(因此即使是数学也很困难,您可以阅读有关它们的整本书并通过发明具有竞争力的 bigint 实现获得博士学位).

For much bigger numbers, consider using arbitrary precision numbers (called bigints), e.g. thru the GNU gmp library. Such libraries uses non-trivial algorithms (so even the math is quite difficult, you can read entire books on them and earn a PhD by inventing a competitive bigint implementation).

这篇关于如何在 32 位体系结构中将 64 位 unsigned long long 分配给 32 位结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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