Most optimized way to calculate modulus in C

后端 未结 8 1291
逝去的感伤
逝去的感伤 2020-12-08 15:39

I have minimize cost of calculating modulus in C. say I have a number x and n is the number which will divide x

when n == 65536 (which happens to be 2^16):

m

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 16:04

    If the constant with which you want to take the modulo is known at compile time and you have a decent compiler (e.g. gcc), tis usually best to let the compiler work its magic. Just declare the modulo const.

    If you don't know the constant at compile time, but you are going to take - say - a billion modulos with the same number, then use this http://libdivide.com/

提交回复
热议问题