在MIPS多precision算术 [英] Multi-Precision Arithmetic on MIPS

查看:153
本文介绍了在MIPS多precision算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想实现对本地MIPS多precision算术。假设
一个64位的整数寄存器$ 12和$ 13和另一种是在寄存器$ 14和$ 15
总和是被放置在寄存器$ 10和$ 11 64位整数的最显著字偶数寄存器中发现,最低显著词在奇数寄存器中。在互联网上,它说,这是最短的执行情况。

 阿杜$ 11 $ 13 $ 15#添加至少显著字
sltu $ 10,$ 11 $ 15#集运​​入位
阿杜$ 10,$ 10,$ 12#在添加第一个最显著字
阿杜$ 10,$ 10,$ 14#在添加第二个最显著字

我只是想仔细检查我理解正确。该sltu检查
的两个最低显著字的总和是小于或大于一个等于
操作数。如果是这样的话,不是没有进位发生,这是正确的?

要检查是否添加了两个最显著当发生进位
字和存储在$ 9 I必须做的结果:

  sltu $ 9 $ 10,$ 12#集运入位

这是否有什么意义?


解决方案

  

的sltu检查的两个最低显著话之和大于一个操作数小于或等于


不太:它集 $ 10个为1,如果两个最显著字之和的严格的考虑操作数的不到一(为32位的无符号的值);和0,如果总和等于或大于,该操作数。


  

如果是这种情况,比没有进位发生,是这样吧?


考虑到一些特定的增值中的的各种可能的值时,会发生什么情况的的(这里的一切是一个无符号的32位值):


  • 如果没有发生溢出,必须要有的的< = SUM< = 0xFFFFFFFF的,所以0℃=的的< =(0xFFFFFFFF的 - 的)。

  • 其余病例为的的导致溢出;在这些情况下,实际的总和必须0x100000000&下; = SUM&下; = + 0xFFFFFFFF的,其中,当截断为32位给出0℃; = SUM&下; = 的 - 1


  

要检查是否添加了两个最显著的话时,有一个发生携带和存放在$ 9
  我要做的:


  
  

sltu $ 9 $ 10,$ 12#集进位位


不完全是。

这里的问题是,你增加了两个32位值的可能是从最不显著的话总和矣。例如,考虑那里有一个随身携带的情况下两者最显著的话是0xFFFFFFFF的:和是1+ 0xFFFFFFFF的+ 0xFFFFFFFF的= 0xFFFFFFFF的,所以进位将不会置(但应)

要对付这将是加入港币$ 96 $ 10个后,检查随身携带,并再次检查的一种方式之后加入 $ 11个来的总和。只有那些资金能产生一个进位中的一个( $ 12 + $ 10个溢时港币$ 96 是0xFFFFFFFF,因为 $ 10个为0或1;在此情况下的总和为0,所以第二总和不能溢出以及)

所以这可能(免责声明:这是晚了,这是未经测试)做的伎俩:

 阿杜$ 11 $ 13 $ 15个
sltu $ 10,$ 11 $ 15#从低位字进行
阿杜$ 10,$ 10,$ 12个
sltu $ 9 $ 10,$ 12#可能携带高字(1)
阿杜$ 10,$ 10,$ 14个
sltu $ 8 $ 10,$ 14#可能携带高字(2)
或者$ 9 $ 8 $ 9#携带结果如果(1)或(2)是真实的(不能同时是真实的一次)

I am just trying to implement multi-precision arithmetic on native MIPS. Assume that one 64-bit integer is in register $12 and $13 and another is in registers $14 and $15. The sum is to be placed in registers $10 and $11. The most significant word of the 64-bit integer is found in the even-numbered registers, and the least significant word is found in the odd-numbered registers. On the internet, it said, this is the shortest possible implementation.

addu  $11, $13, $15    # add least significant word
sltu  $10, $11, $15    # set carry-in bit 
addu  $10, $10, $12    # add in first most significant word
addu  $10, $10, $14    # add in second most significant word

I just wanna double check that I understand correctly. The sltu checks if the sum of the two least significant words is smaller or equal than one of the operands. If this is the case, than did a carry occur, is this right?

To check if there occured a carry when adding the two most significant words and store the result in $9 I have to do:

sltu  $9, $10, $12    # set carry-in bit 

Does this make any sense?

解决方案

The sltu checks if the sum of the two least significant words is smaller or equal than one of the operands.

Not quite: it sets $10 to 1 if the sum of the two least significant words is strictly less than one of the operands (considered as 32-bit unsigned values); and 0 if the sum is equal to, or greater than, that operand.

If this is the case, than did a carry occur, is this right?

Yes.

Consider what can happen when adding various possible values of b to some particular value a (where everything is an unsigned 32-bit value):

  • If overflow has not occurred, we must have a <= sum <= 0xFFFFFFFF, so 0 <= b <= (0xFFFFFFFF - a).
  • The remaining cases for b cause an overflow; the actual sum in these cases must be 0x100000000 <= sum <= a + 0xFFFFFFFF, which when truncated to 32 bits gives 0 <= sum <= a - 1.

To check if there occured a carry when adding the two most significant words and store the result in $9 I have to do:

sltu $9, $10, $12 # set carry-in bit

Not quite.

The problem here is that you're adding two 32-bit values and possibly a carry from the sum of the least significant words. For example, consider the case where there is a carry and both most significant words are 0xFFFFFFFF: the sum will be 1+ 0xFFFFFFFF + 0xFFFFFFFF = 0xFFFFFFFF, and so the carry will not be set (but it should be).

One way to deal with this would be to check for carry after adding $12 to $10, and check again after adding $11 to that sum. Only one of those sums can produce a carry ($12 + $10 only overflows when $12 is 0xFFFFFFFF, because $10 is either 0 or 1; and in that case the sum is 0, so the second sum can't overflow as well).

So this might (disclaimer: it's late, and this is untested) do the trick:

addu $11, $13, $15
sltu $10, $11, $15  # carry from low word
addu $10, $10, $12
sltu  $9, $10, $12  # possible carry from high word (1)
addu $10, $10, $14
sltu  $8, $10, $14  # possible carry from high word (2)
or    $9,  $8,  $9  # carry in result if either (1) or (2) were true (can't both be true at once)

这篇关于在MIPS多precision算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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