Aussie AI
Avoid Remainder Operations
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Avoid Remainder Operations
One common use of the remainder operator is the use of modulo arithmetic, such as the
wraparound array implementation of a queue abstract data type,
where the value of a variable is cyclically counted from 0
up to N-1
, and then back to 0.
The most common idiom for coding this is:
x = (x + 1) % N;
However, the %
operator is expensive, and in this case it is not really needed. The following code sequence performs the same task more efficiently:
if (x == N - 1) x = 0; else x++;
This can also be written more concisely, but not necessarily more efficiently, as an expression with the “?:
” ternary operator:
(x == N - 1) ? (x = 0) : (x++);
Another example of a clever avoidance of %
is when the operand is similar to the usual
byte or word size. For example, consider this remainder:
x % 256
This can be more efficiently coded with bitwise-and using:
x & 255
But this can be even more efficiently coded as a type cast:
(unsigned char) x
The conversion to this “unsigned char
” type will be efficiently implemented by grabbing a byte out
of a word. Unfortunately, this method is not portable to all obscure systems, as it relies on
an “overflow” being handled harmlessly, and on “unsigned char
” always containing 8 bits.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |