无符号和有符号乘法有多少个最低有效位相同? [英] How many least-significant bits are the same for both an unsigned and a signed multiplication?

查看:225
本文介绍了无符号和有符号乘法有多少个最低有效位相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ARM处理器具有 指令,该指令执行32位x 32位乘法,并返回结果的最低有效32位.他们还具有 umullsmull指令,它们再次执行32位x 32位乘法,但返回完整的64位结果. umull进行无符号乘法,而smull进行有符号乘法.

ARM processors, for example, have a mul instruction, which performs a 32-bit x 32-bit multiplication and returns the least-significant 32-bits of the result. They also have umull and smull instructions, which again do a 32-bit x 32-bit multiplication, but return a full 64-bit result. umull does an unsigned multiplication, and smull does a signed multiplication.

为什么不必具有单独的未签名版本和已签名版本的mul?在32位x 32位乘法的情况下,在这两种情况下,结果的最低有效32位大概是相同的吗?完全相同的32位还是大于32位?

Why is it not necessary to have separate unsigned and signed versions of mul? In the case of a 32-bit x 32-bit multiplication, presumably the least-significant 32-bits of the result are the same in both cases? Are exactly 32 bits the same, or more than 32?

更一般而言,在m位x n位乘法(产生(m + n)位结果)的情况下,无符号和有符号乘法有多少个最低有效位相同?

More generally, in the case of an m-bit x n-bit multiplication (producing an (m+n)-bit result), how many least-significant bits are the same for both an unsigned and a signed multiplication?

推荐答案

您可以用铅笔和纸做的...小学风格

You can do this with pencil and paper...Elementary school style

-1 * 3 = -3,7 * 3 = 21 smull vs umull

-1 * 3 = -3, 7 * 3 = 21 smull vs umull

0b111 * 0b011

0b111 * 0b011

   111111
*  000011
==========
   111111
  111111
 000000
 ...
+
==========
 11111101

(从技术上讲,符号扩展到内部alu输入的宽度)

(technically the signs are extended as wide as the internal alu inputs)

是-3

采用相同的3位数字,但使用umull

take the same 3 bit numbers but use umull

0b111 * 0b011

0b111 * 0b011

      000111
*     000011
=============
      000111
     000111
    000000
+  ...
==============
     0010101 

结果是21.

二进制补码的优点在于加法和减法使用相同的逻辑,但是乘除法必须对符号进行扩展才能得到正确的答案,这就是谎言.无符号符号扩展零.有符号符号扩展符号.更糟糕的是,乘以存储结果所需位数的两倍,因此需要使用32位操作数完成16位乘16的乘数,而您填充到高16位中的内容在有符号乘法和无符号之间变化.一旦签署了扩展,那么请确保您可以输入相同的乘法逻辑,因为那里没有区别.我猜可能有人会争辩说,加法和减法也是如何工作的,馈入相同加法器逻辑的内容也随加法和减法而变化,同样,您最终提取的内容也会有所不同(如果您将进位位反转为借位)

The beauty of twos complement is that add and subtract use the same logic, but multiply and divide you have to sign extend to get the right answer and there in lies the rub. Unsigned sign extends zeros signed sign extends the sign. Multiplies at worse double the number of bits required to store the result so a 16 bit by 16 multiply needs to be done with 32 bit operands and what you fill into those upper 16 bits varies between a signed multiply and unsigned. Once you sign extend then sure you can feed the same multiply logic because there is no difference there. I guess one could argue that is how add and subtract works too, what you feed the same adder logic varies depending add vs subtract likewise what you pull out at the end may vary (if you invert the carry bit to call it a borrow)

现在每个问题,是的,如果您将3比特乘以3比特并仅查看输出的3个低比特,这几乎总是错误的答案,但无论如何还是小学数学

Now per your question, yes if you take a 3 bit by 3 bit in and only look at the 3 lower bits of the output which is almost always the wrong answer, but anyway same elementary school math

   AAAabc
   DDDdef
=========
   AAAabc
  AAAabc
 AAAabc
AAAabc
...
===========

3x3位输入的低3位严格由原始的3位输入确定,在umull和smull之间变化的符号扩展不起作用.这是一个有趣的练习,但在现实世界中并没有太多使用,大多数操作数组合都会溢出,但不是全部,而是很高的百分比.

The lower 3 bits for a 3x3 bit input are strictly determined by the original 3 bit inputs, the sign extension which is what varies between umull and smull do not come into play. It is an interesting exercise but doesnt have too much real world use, most operand combinations will overflow, not all but a high percentage.

如果对M * N位重复此练习,则它应该是不受符号扩展影响的M或N位中的较小者.这个简单的练习留给读者.

If you repeat this exercise for M * N bits then it should be the smaller of M or N bits that are unaffected by the sign extension. That simple exercise is left to the reader.

这篇关于无符号和有符号乘法有多少个最低有效位相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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