浮点模运算 [英] Floating Point Modulo Operation
问题描述
我正在尝试实现三角函数的范围缩减操作.但相反,我认为对传入数据执行模 pi/2 运算可能会更好.我想知道存在哪些算法并且对于 32 位 IEEE 754 浮点的此操作有效?
I am trying to implement the range reduction operation for trigonometry. But instead I think it might be better to just perform a modulo pi/2 operation on incoming data. I was wondering what algorithms exist and are efficient for this operation for 32-bit IEEE 754 floating-point?
我必须在汇编中实现它,所以 fmod、除法、乘法等仅靠一条指令对我来说是不可用的.我的处理器使用 16 位字,我已经实现了 32 位浮点加法、减法、乘法、除法、平方根、余弦和正弦.我只需要范围缩小(模数)来输入余弦和正弦值.
I have to implement this in assembly, so fmod, division, multiplication, etc. aren't available to me with just one instruction. My processor uses 16-bit words and I have implemented 32-bit floating point addition, subtraction, multiplication, division, square root, cosine, and sine. I just need range reduction (modulus) for inputting values to cosine and sine.
推荐答案
我认为标准库的 fmod()
在大多数情况下会是最好的选择.这是一个链接,讨论了几种简单的算法.
I think standard library's fmod()
will be the best choice in most cases. Here's a link to a discussion of several simple algorithms.
在我的机器上,fmod()
使用优化的内联汇编代码(/usr/include/bits/mathinline.h
):
On my machine, fmod()
uses optimized inline assembly code (/usr/include/bits/mathinline.h
):
#if defined __FAST_MATH__ && !__GNUC_PREREQ (3, 5)
__inline_mathcodeNP2 (fmod, __x, __y,
register long double __value;
__asm __volatile__
("1: fprem
"
"fnstsw %%ax
"
"sahf
"
"jp 1b"
: "=t" (__value) : "0" (__x), "u" (__y) : "ax", "cc");
return __value)
#endif
所以它实际上使用专用的 CPU 指令 (fprem) 进行计算.
So it actually uses a dedicated CPU instruction (fprem) for the calculation.
这篇关于浮点模运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!