在不使用 long long 的系统中将 23 位数据类型相乘

发布于 2024-07-14 20:32:12 字数 705 浏览 6 评论 0原文

我正在尝试在微控制器中实现浮点运算,到目前为止我已经取得了巨大的成功。

问题在于我在计算机中进行乘法的方式,并且它工作正常:

unsigned long long gig,mm1,mm2;
unsigned long m,m1,m2;
mm1 = f1.float_parts.mantissa;
mm2 = f2.float_parts.mantissa;

m1 = f1.float_parts.mantissa;
m2 = f2.float_parts.mantissa;



gig = mm1*mm2; //this works fine I get all the bits I need since they are all long long, but won't work in the mcu

gig = m1*m2//this does not work, to be precise it gives only the 32 least significant bits , but works on the mcu

所以你可以看到我的问题是,如果我尝试 gig,微控制器将抛出对 __muldi3 的未定义引用= mm1*mm2 那里。

如果我尝试使用较小的数据类型,它只会保留最低有效位,这是我不希望的。 我需要该产品的 23 msb 位。

有人对我如何做到这一点有任何想法吗?

I am trying to implement floating point operations in a microcontroller and so far I have had ample success.

The problem lies in the way I do multiplication in my computer and it works fine:

unsigned long long gig,mm1,mm2;
unsigned long m,m1,m2;
mm1 = f1.float_parts.mantissa;
mm2 = f2.float_parts.mantissa;

m1 = f1.float_parts.mantissa;
m2 = f2.float_parts.mantissa;



gig = mm1*mm2; //this works fine I get all the bits I need since they are all long long, but won't work in the mcu

gig = m1*m2//this does not work, to be precise it gives only the 32 least significant bits , but works on the mcu

So you can see that my problem is that the microcontroller will throw an undefined refence to __muldi3 if I try the gig = mm1*mm2 there.

And If I try with the smaller data types, it only keeps the least significant bits, which I don't want it to. I need the 23 msb bits of the product.

Does anyone have any ideas as to how I can do this?

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

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

发布评论

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

评论(3

再见回来 2024-07-21 20:32:12

对简短的回答表示歉意,我希望其他人能花时间写一个更完整的解释,但基本上你所做的与在纸上手工乘以两个大数一样! 只是您不是使用基数 10,而是使用基数 256。也就是说,将您的数字视为字节向量,并对每个字节执行“手动乘法”时对数字执行的操作。

Apologizes for the short answer, I hope that someone else will take the time to write a fuller explanation, but basically you do exactly as when you multiply two big numbers by hand on a paper! It's just that instead of working with base 10, you work in base 256. That is, treat your numbers as a byte vectors, and do with each byte what you do to a digit when you "hand multiply".

雅心素梦 2024-07-21 20:32:12

FreeBSD 实现 __muldi3() 中的注释对所需过程有很好的解释,请参阅 muldi3.c。 如果您想直接访问源代码(总是一个好主意!),根据评论,此代码基于 Knuth 的 计算机编程的艺术卷。 2(第 2 版),第 4.3.3 节,第 14 页。 278.(注意该链接适用于第三版。)

The comments in the FreeBSD implementation of __muldi3() have a good explanation of the required procedure, see muldi3.c. If you want to go straight to the source (always a good idea!), according to the comments this code was based on an algorithm described in Knuth's The Art of Computer Programming vol. 2 (2nd ed), section 4.3.3, p. 278. (N.B. the link is for the 3rd edition.)

山田美奈子 2024-07-21 20:32:12

回到 Intel 8088(最初的 PC CPU 和我为其编写汇编代码的最后一个 CPU),当您将两个 16 位数字(32 位?哇哦)相乘时,CPU 将在两个不同的寄存器中返回 2 个 16 位数字 - 一个带有 16 MSB 和LSB 一起。

您应该检查微控制器的硬件功能,也许它有类似的设置(显然,如果有的话,您将需要在汇编中使用此代码)。

否则你将不得不自己实现乘法。

Back on the Intel 8088 (the original PC CPU and the last CPU I wrote assembly code for) when you multiplied two 16 bit numbers (32 bits? whoow) the CPU would return 2 16 bit numbers in two different registers - one with the 16 msb and one with the lsb.

You should check the hardware capabilities of your micro-controller, maybe it has a similar setup (obviously you'll need the code this in assembly if it does).

Otherwise you'll have to implement multiplication on your own.

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