仅通过加法计算指数

发布于 2024-08-14 18:55:35 字数 182 浏览 6 评论 0原文

我们正在编写一个非常简单的程序,在我们为类构建的处理器上执行。它没有乘法或除法的能力。不过,我们确实支持加法、减法、与、或以及循环控制分支(如果您熟悉 MIPS,则支持等于分支)。我们认为在其上运行的简洁程序应该是某种 x^n 程序。当然,这些数字必须进行硬编码,但考虑到我们处理器的限制,这现实吗?

是否有指数的仅加法计算? 谢谢。

We're writing a very simple program to execute on a processor we've built for a class. It doesn't have the capability to multiply or divide. We do however, had support for addition, subtraction, and, or, and branching for loop control (like branch on equal if you are familiar with MIPS). We were thinking a neat program to run on it would be some sort of x^n program. Of course, those numbers would have to be hardcoded, but given the limitations of our processor, is it realistic?

Is there an addition only calculation for exponents?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

忆梦 2024-08-21 18:55:35

对于小整数,为什么不呢?

首先,使用重复加法实现乘法。然后,使用重复乘法实现 pow()。它会很慢,但会很好地工作。

有一种更快的求幂算法,称为平方求幂。但是,鉴于您没有快速乘法,我不确定它是否值得 - 您可能想首先实现快速乘法算法。

For small integers, why not?

First, implement multiply using repeated addition. Then, implement pow() using repeated multiplication. It will be slow, but it will work fine.

There is a faster algorithm for exponentiation, called Exponentiation by Squaring. However, given that you don't have a fast multiply, I'm not sure it's worth it - you might want to first work on implementing a fast multiplication algorithm.

眼眸印温柔 2024-08-21 18:55:35

与 dmazzoni 的 c 风格语法响应一致:

int mulitply(int x, int y)
{
    int product;

    for (int i = 0; i<y; i++)
       product += x;

    return product;
}

int power(int x, int exponent)
{
    int result = 1;

    for (int i = 0; i < exponent; i++)
        result = multiply(result, x);

    return result;
}

In line with dmazzoni's respone in c style syntax:

int mulitply(int x, int y)
{
    int product;

    for (int i = 0; i<y; i++)
       product += x;

    return product;
}

int power(int x, int exponent)
{
    int result = 1;

    for (int i = 0; i < exponent; i++)
        result = multiply(result, x);

    return result;
}
灰色世界里的红玫瑰 2024-08-21 18:55:35

与 Aequitarum 的解决方案类似,但对幂使用重复平方,对乘法使用重复加倍。对于较大的 x,y 应该更快:

int multiply(int x, int y) {
  int product = 0;
  int bitmask = 1;

  while (y >= bitmask) {
    if (y & bitmask) product += x;
    x += x;
    bitmask += bitmask;
  }
  return product;
}

int power(int x, int exponent)
{
  int result = 1;
  int bitmask = 1;

  while (exponent >= bitmask) {
    if (exponent & bitmask) result = multiply(result, x);
    x = multiply(x, x);
    bitmask += bitmask;
  }
  return result;
}

Like Aequitarum's solution, but uses repeated squaring for powers and repeated doubling for multiplies. Should be faster for large x,y:

int multiply(int x, int y) {
  int product = 0;
  int bitmask = 1;

  while (y >= bitmask) {
    if (y & bitmask) product += x;
    x += x;
    bitmask += bitmask;
  }
  return product;
}

int power(int x, int exponent)
{
  int result = 1;
  int bitmask = 1;

  while (exponent >= bitmask) {
    if (exponent & bitmask) result = multiply(result, x);
    x = multiply(x, x);
    bitmask += bitmask;
  }
  return result;
}
微凉徒眸意 2024-08-21 18:55:35

这是非常现实的。几年前,处理器还没有可以执行乘法和除法等高级运算的 ALU。

乘法通常使用移位和加法来完成。这是一些伪汇编:(

; multiply registers a and b

; use c as high b
mov c,#0
; use d as low result
mov d,#0
; use e as high result
mov e,#0
.nextbit:
  ; shift low bit out of a
  shr a
  ; skip if zero
  bcc .noadd
    ; add b to result
    add d,b
    adc e,c
  .noadd:
  ; double b
  shl b
  rcl c
  ; test a
  cmp a,#0
bne .nextbit

请注意,两个字节值相乘的结果是一个两个字节值。)

一旦有了乘法,您就可以循环计算幂。

使用说明:

mov x,y = move y into x
shr x = shift right one bit
shl x = shift left one bit
rcl x = rotate left one bit with carry inserted
add x,y = add y to x
adc x,y = add y to x with carry
cmp x,y = compare x to y
bcc = branch on carry clear
bne = branch on not equal
#0 = literal number zero (as opposed to 0, which would be the address zero)

It's very realistic. It's not so many years back that processors didn't have an ALU that could do any advanced operations like multiplication and division.

Multiplication is usually done using shifting and adding. Here's some pseudo-assembly:

; multiply registers a and b

; use c as high b
mov c,#0
; use d as low result
mov d,#0
; use e as high result
mov e,#0
.nextbit:
  ; shift low bit out of a
  shr a
  ; skip if zero
  bcc .noadd
    ; add b to result
    add d,b
    adc e,c
  .noadd:
  ; double b
  shl b
  rcl c
  ; test a
  cmp a,#0
bne .nextbit

(Note that the result of multiplying two byte values is a two byte value.)

Once you have a multiplication, you can just loop to calculate power.

instructions used:

mov x,y = move y into x
shr x = shift right one bit
shl x = shift left one bit
rcl x = rotate left one bit with carry inserted
add x,y = add y to x
adc x,y = add y to x with carry
cmp x,y = compare x to y
bcc = branch on carry clear
bne = branch on not equal
#0 = literal number zero (as opposed to 0, which would be the address zero)
儭儭莪哋寶赑 2024-08-21 18:55:35

您可能会发现有关 乘法 ALU 的相关维基百科文章。通过加法和按位运算(与和或),您可以按一位一步实现乘法,而不必按照较小运算符的大小进行多次加法。

You may find the Wikipedia article on Multiplication ALU relevant. With addition and bitwise operations (and and or), you can implement multiplication in one step per bit, rather than having to add as many times as the magnitude of the smaller operator.

所有深爱都是秘密 2024-08-21 18:55:35

指数 n 的 k 次方:

exponent(n,k) {
   for(x=1..n)
      x = x + exponent(x,k-1)
}

exponent n to power of k:

exponent(n,k) {
   for(x=1..n)
      x = x + exponent(x,k-1)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文