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

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

问题描述

ARM 处理器,例如,有一个 mul 指令,执行 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 与 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天全站免登陆