2

I have a code segment that requires the remainder of two ints. I am using modulus operator as usual but I am hearing that it takes more time. So I am asking that is there any way to get the remainder more efficiently than mod operation... The following will be my code

int rem=gid%bpp

gid being any int and bpp being 2,4,8,16,32.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Piyush Kumar
  • 157
  • 1
  • 2
  • 14

1 Answers1

3

It appears from your question that bpp is always a power of 2, in which case you can use instead:

int rem = gid & (bpp - 1);

However you should not optimise prematurely - unless you have profiled and know for certain that this mod operation is a bottleneck then you should just leave it in its original, more readable form.

Paul R
  • 208,748
  • 37
  • 389
  • 560