In Java, we have two different modulo (and remiander) operations, % and Math.floorMod(). The difference is in the area of the mapping destination, which can either depend on the sign of the first or the sign of the second operand. This page explains the difference nicely.
Is there an equivalent (fast) operation in C++ with the same effect as Math.floorMod()?
I currently go with the following (and for performance reasons it's not even fully equivalent to Math.floorMod()):
inline int floor_mod(int x, int y) {
x %= y;
if (x < 0) {
x += y;
}
return x;
}
I think there might be an intrinsic or something similar which compiles to only 1 instruction on some CPUs.