问题描述
我试图实现对三角函数的范围缩小操作。但是相反,我认为这可能是最好的传入数据只是执行模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()
使用优化的内联汇编code( /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\n\t" \
"fnstsw %%ax\n\t" \
"sahf\n\t" \
"jp 1b" \
: "=t" (__value) : "0" (__x), "u" (__y) : "ax", "cc"); \
return __value)
#endif
所以它实际上使用专用的CPU指令(F $ P $点)进行计算。
So it actually uses a dedicated CPU instruction (fprem) for the calculation.
这篇关于浮点模操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!