32位和64位计算机的算法 [英] Arithmetic for 32-bit and 64-bit machines

查看:160
本文介绍了32位和64位计算机的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道CPU寄存器决定了机器的大小。

但是编码算术运算的正确方法是什么,以避免

环绕时代码是在32位和64位

机器上运行的?


示例:


uint32_t x = SOME_LARGE_NO;

uint64_t y;

....


y =(uint64_t)(10000 * x); / *在演员阵容之前会发生溢出吗? * /

y =(int64_t)(x)* 10000; / *或者,最好先进行演员表演吗?

* /


请评论。


/为什么茶

解决方案

为什么Tea< yt **** @ gmail.comwrites:


>我明白CPU寄存器决定了机器的大小。
但是,在运行代码时,编写算术运算的正确方法是什么,以避免回绕?在32位和64位
机器上?


>示例:


> uint32_t x = SOME_LARGE_NO ;
uint64_t y;
...


> y =(uint64_t)(10000 * x); / *在演员阵容之前会发生溢出吗? * /
y =(int64_t)(x)* 10000; / *或者,最好先进行演员表吗?
* /



为什么不用64位执行所有算术?

uint64_t x = SOME_LARGE_NO;

uint64_t y;


y = 10000 * x;

y = x * 10000 ;


-

克里斯。


1月9日下午3:43,克里斯McDonald< ch ... @ csse.uwa.edu.auwrote:


为什么Tea< ytl ... @ gmail.comwrites:
< blockquote class =post_quotes>
我知道CPU寄存器决定了机器的大小。

但编码算术运算的正确方法是什么,以避免
$当代码要在32位和64位

机器上运行时b $ b环绕?

示例:

uint32_t x = SOME_LARGE_NO;

uint64_t y;

...

y =(uint64_t)(10000 * x); / *在演员阵容之前会发生溢出吗? * /

y =(int64_t)(x)* 10000; / *或者,最好先进行演员表吗?

* /



为什么不在64位中执行所有算术?


uint64_t x = SOME_LARGE_NO;

uint64_t y;


y = 10000 * x;

y = x * 10000;



谢谢Chris。但假设x为x。是一个32位变量在其他地方声明了

,没有简单的方法可以将它改为uint64_t;并且

它也可以作为函数参数传递,例如

some_func(1000 * x,...),所以你提到的不起作用。


让我重新解释一下这个问题:


void somefunc(uint64_t y,....);

... 。


uint32_t x;

uint64_t y;

....

y =(uint64_t )(10000 * x); / *在演员阵容之前会发生溢出吗? * /

y =(int64_t)(x)* 10000; / *或者,最好先进行演员表演吗?

* /

....

somefunc(1000 * x,。 ...); / *在32位机器上可能出现溢出?

* /

...


为何选择茶叶< yt **** @ gmail.comwrites:


>谢谢Chris。但假设x为x。是在其他地方声明的32位变量,并没有简单的方法将其更改为uint64_t;并且它也可以作为函数参数传递,例如
some_func(1000 * x,...),所以你提到的不起作用。


>让我重新解释一下这个问题:


> void somefunc(uint64_t y,....);
...


> uint32_t x;
uint64_t y ;
...
y =(uint64_t)(10000 * x); / *在演员阵容之前会发生溢出吗? * /
y =(int64_t)(x)* 10000; / *或者,最好先进行演员表演吗?
* /
...
somefunc(1000 * x,....); / *在32位机器上可能发生溢出?
* /
..



然后快速使用:int64_t x64 = x;


如果somefunc()在调用

时正确原型并且可见,则10000 * x应该被提升为64位


(10000LL *(int64_t)x)。


-

克里斯。

I understood that the CPU registers determine the size of the machine.
But what is the correct way to code an arithmetic operation to avoid
wrap-around when the code is to be run on both 32-bit and 64-bit
machines?

Example:

uint32_t x = SOME_LARGE_NO;
uint64_t y;
....

y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/

Please comment.

/Why Tea

解决方案

Why Tea <yt****@gmail.comwrites:

>I understood that the CPU registers determine the size of the machine.
But what is the correct way to code an arithmetic operation to avoid
wrap-around when the code is to be run on both 32-bit and 64-bit
machines?

>Example:

>uint32_t x = SOME_LARGE_NO;
uint64_t y;
...

>y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/

Why not perform all arithmetic in 64bits?

uint64_t x = SOME_LARGE_NO;
uint64_t y;

y = 10000 * x;
y = x * 10000;

--
Chris.


On Jan 9, 3:43 pm, Chris McDonald <ch...@csse.uwa.edu.auwrote:

Why Tea <ytl...@gmail.comwrites:

I understood that the CPU registers determine the size of the machine.
But what is the correct way to code an arithmetic operation to avoid
wrap-around when the code is to be run on both 32-bit and 64-bit
machines?
Example:
uint32_t x = SOME_LARGE_NO;
uint64_t y;
...
y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/


Why not perform all arithmetic in 64bits?

uint64_t x = SOME_LARGE_NO;
uint64_t y;

y = 10000 * x;
y = x * 10000;

Thanks Chris. But assuming the "x" is a 32-bit variable declared
somewhere else, and there is no easy way to change it to uint64_t; and
also it can be passed as a function parameter such as
some_func(1000*x,...), so what you mentioned would not work.

Let me rephrase the question:

void somefunc(uint64_t y, ....);
....

uint32_t x;
uint64_t y;
....
y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/
....
somefunc(1000*x,....); /* Overflow can occur here on 32-bit machine?
*/
...


Why Tea <yt****@gmail.comwrites:

>Thanks Chris. But assuming the "x" is a 32-bit variable declared
somewhere else, and there is no easy way to change it to uint64_t; and
also it can be passed as a function parameter such as
some_func(1000*x,...), so what you mentioned would not work.

>Let me rephrase the question:

>void somefunc(uint64_t y, ....);
...

>uint32_t x;
uint64_t y;
...
y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/
...
somefunc(1000*x,....); /* Overflow can occur here on 32-bit machine?
*/
..


Then quickly use: int64_t x64 = x;

If somefunc() is correctly prototyped and visible at the point of calling
it, 10000*x should be promoted to 64bits as

(10000LL * (int64_t)x).

--
Chris.


这篇关于32位和64位计算机的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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