32x32和64x64有符号整数乘法 [英] 32x32 and 64x64 signed integer multiplication

查看:147
本文介绍了32x32和64x64有符号整数乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨组,


我正在尝试实现两个例程来处理32x32位和

64x64位有符号整数乘法C中的位机器。

很容易找到非签名乘法的描述,但是,我没有找到任何关于签名对应方的良好描述。


你们有没有对这个主题有很好的参考?


提前致谢,

Chris。


-

电子邮件地址位于 http://www.ifi.uio.no/~erikd/index.cgi

解决方案

< blockquote> Christopher Dyken< fo **** @ uio.no>写道:

我正在尝试在C中的32位机器上实现两个例程来处理32x32位和64x64位有符号整数乘法。它/>很容易找到非签名乘法的描述,但是,我没有找到任何关于签名对应的好描述。




怎么样

- 对于每个已签名的号码,检查它是否已签名,检查这些

标志是否相等,并记住这一事实。

- 将两个符号位设置为+。

- 乘以好像它们是无符号的一样。

- 检查结果是否太大对于已签名的号码,如果是这样的话,

做你在溢出时做的决定。

- 如果符号位相等,请保留数字作为它是。如果他们没有b $ b,请设置它的符号位。


最难的部分可能是决定如何处理溢出。崩溃是惨烈的b $ b brsh。在许多情况下,忽略可能就足够了。设置

错误标志可能是最好的。


Richard


rl *@hoekstra-uitgeverij.nl (Richard Bos)写道:

| Christopher Dyken< fo **** @ uio.no>写道:

我正在尝试实现两个例程来处理32位机器上的32x32位和64x64位有符号整数乘法它很容易找到非签名乘法的描述,但是,我没有找到任何关于签名对应的好描述。



|那么

| - 对于每个已签名的号码,检查是否已签名,检查这些是否为
|标志是平等的,记住这个事实。

| - 将两个符号位设置为+。

| - 好像他们没有签名一样繁殖。

| - 检查结果对于签名号码是否太大,如果是,

|做你在溢出时做的决定。

| - 如果符号位相等,则保留数字。如果他们

|没有,设置它的标志位。



|最难的部分可能是决定如何处理溢出。崩溃是

|苛刻。在许多情况下,忽略可能就足够了。设置

|错误标志可能是最好的。


理查德,


限制输入值没有问题(我定义了间隔

我自己),因此,我可以确定没有溢出。所以这是

不是问题。


我认为,这个策略涉及一些if / else-tests,这可能会伤害

管道吞吐量(或者我错了?)。我有一种直觉感觉

应该可以在没有任何条件的情况下做到这一点。


问候,

Chris 。


-

电子邮件地址位于> http://www.ifi.uio.no/~erikd/index.cgi


Christopher Dyken写道:


rl *@hoekstra-uitgeverij.nl (Richard Bos)写道:
| Christopher Dyken< fo **** @ uio.no>写道:

我正在尝试实现两个例程来处理32位机器上的32x32位和64x64位有符号整数乘法它很容易找到非符号乘法的描述,但是,我没有找到任何关于签名对应的好描述。


|那么
| - 对于每个签名号码,检查是否已签名,检查是否这些
|标志是平等的,记住这个事实。
| - 将两个符号位设置为+。
| - 好像他们没有签名一样繁殖。
| - 检查签名号码的结果是否过大,如果是,
|做你在溢出时决定做的事情。
| - 如果符号位相等,则保留数字。如果他们
|没有,设置它的标志位。


|最难的部分可能是决定如何处理溢出。崩溃是
|苛刻。在许多情况下,忽略可能就足够了。设置
|错误标志可能是最好的。

你好理查德,

我没有问题限制输入值(我自己定义间隔
),因此,我可以确保没有溢出。所以这不是问题。

正如我所看到的,这种策略涉及一些if / else-tests,这可能会损害管道吞吐量(或者我错了? )。我有一种直觉的感觉,没有条件就可以做到这一点。

问候,
克里斯。

- <电子邮件地址可在 http://www.ifi。 uio.no/~erikd/index.cgi




管道档位(因为分支机构)取决于机器。有些cpu''sh

有两个管道,每个分支一个,所以这不是问题。我自己

更喜欢使用无判断代码,如果可能的话。您可能需要

才能看到我的文章避免决定!在物理学的计算机ca. 1990年

或1991,讨论了一般的想法。如果你在

汇编中进行它当然会变得更容易,但是可以避免在高级别C中使用

(某些)分支。


-

Julian V. Noble

物理荣誉教授
jv *@lessspamformother.virginia.edu

^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/


上帝不愿意做任何事情,从而带走

我们的自由意志以及合法属于的荣耀份额

给我们。 - N. Machiavelli,The Prince。


Hi group,

I''m trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven''t found any good descriptions for the signed counterpart.

Does any of you have a good reference for this topic?

Thanks in advance,
Chris.

--
email address available at http://www.ifi.uio.no/~erikd/index.cgi

解决方案

Christopher Dyken <fo****@uio.no> wrote:

I''m trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven''t found any good descriptions for the signed counterpart.



What about:
- for each signed number, check whether it''s signed, check whether these
signs are equal, and remember this fact.
- set both sign bits to +.
- multiply as if they''re unsigned.
- check whether the result is too large for a signed number, and if so,
do what you''ve decided to do on overflow.
- if the sign bits were equal, leave the number as it is. If they
weren''t, set its sign bit.

Hardest part is probably deciding what to do on overflow. Crashing is
harsh. Ignoring is perhaps sufficient in many circumstances. Setting an
error flag is probably best.

Richard


rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
| Christopher Dyken <fo****@uio.no> wrote:

I''m trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven''t found any good descriptions for the signed counterpart.


| What about:
| - for each signed number, check whether it''s signed, check whether these
| signs are equal, and remember this fact.
| - set both sign bits to +.
| - multiply as if they''re unsigned.
| - check whether the result is too large for a signed number, and if so,
| do what you''ve decided to do on overflow.
| - if the sign bits were equal, leave the number as it is. If they
| weren''t, set its sign bit.


| Hardest part is probably deciding what to do on overflow. Crashing is
| harsh. Ignoring is perhaps sufficient in many circumstances. Setting an
| error flag is probably best.

Hi Richard,

I have no problem limiting the input values (I define the interval
myself), and thus, I can be sure that no overflows occour. So this is
not an issue.

As I see it, this strategy involves some if/else-tests, which can harm
the pipeline throughput (or am I wrong?) . I''ve an intuitive feeling
that it should be possible to do this without any conditionals.

Regards,
Chris.

--
email address available at http://www.ifi.uio.no/~erikd/index.cgi


Christopher Dyken wrote:


rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
| Christopher Dyken <fo****@uio.no> wrote:

I''m trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven''t found any good descriptions for the signed counterpart.


| What about:
| - for each signed number, check whether it''s signed, check whether these
| signs are equal, and remember this fact.
| - set both sign bits to +.
| - multiply as if they''re unsigned.
| - check whether the result is too large for a signed number, and if so,
| do what you''ve decided to do on overflow.
| - if the sign bits were equal, leave the number as it is. If they
| weren''t, set its sign bit.


| Hardest part is probably deciding what to do on overflow. Crashing is
| harsh. Ignoring is perhaps sufficient in many circumstances. Setting an
| error flag is probably best.

Hi Richard,

I have no problem limiting the input values (I define the interval
myself), and thus, I can be sure that no overflows occour. So this is
not an issue.

As I see it, this strategy involves some if/else-tests, which can harm
the pipeline throughput (or am I wrong?) . I''ve an intuitive feeling
that it should be possible to do this without any conditionals.

Regards,
Chris.

--
email address available at http://www.ifi.uio.no/~erikd/index.cgi



Pipeline stalls (because of branches) are machine dependent. Some cpu''sh
ave two pipelines, one for each branch, so it isn''t an issue. I myself
prefer to use decisionless code, if at all possible. You might want
to see my article "Avoid Decisions!" in Computers in Physics ca. 1990
or 1991, which discusses the general idea. If you are doing it in
assembly it becomes easier, of course, but it is possible to avoid
(some) branches in high-level C.

--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"God is not willing to do everything and thereby take away
our free will and that share of glory that rightfully belongs
to us." -- N. Machiavelli, "The Prince".


这篇关于32x32和64x64有符号整数乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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