AND faster than integer modulo operation?

后端 未结 6 1822
自闭症患者
自闭症患者 2021-02-14 09:34

It is possible to re-express:

  • i % m

as:

  • i & (m-1)

where,

  • i is an unsigned integer
  • m is a po
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-14 09:55

    If you are using a decent C compiler with optimizations enabled, it will already optimize this to whatever is faster, a technique called "strength reduction". If you're doing hand-written assembly, only sure way to test is to benchmark it. But beware, even different models of the same processor could give different results.

提交回复
热议问题