Floating Point Modulo Operation

后端 未结 4 848
梦谈多话
梦谈多话 2020-12-18 18:28

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

4条回答
  •  有刺的猬
    2020-12-18 19:23

    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.

    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
    

    So it actually uses a dedicated CPU instruction (fprem) for the calculation.

提交回复
热议问题