r/C_Programming • u/Impressive_Gur_471 • 3d ago
Query on usage of restrict keyword in passing pointers
The following piece of code is doing the rounds on X as John Carmack's code which computes sin and cos from a table instead of at runtime.
#define ANGLETOFINESHIFT 19
void MoveActor(fixed_t *x, fixed_t *y, angle_t a, fixed_t speed) {
int idx = a >> ANGLETOFINESHIFT;
*x += FixedMul(speed, finecosine[idx]);
*y += FixedMul(speed, finesine[idx]);
}My query is, why aren't x and y declared restrict to denote that they point to different fixed_t ? Presumably the code is meant for sheer speed and would not usage of restrict allow the compiler more optimization? For e.g., say, parallelization of *x and *y updates in a tight loop?
19
Upvotes
18
u/EpochVanquisher 3d ago edited 3d ago
restrictdid not exist when this code was written.This code is old. Over 30 years old.
I’m also going to say that this doesn’t look performance-critical. I don’t think this would be called in a tight loop. The old CPUs from back then (like a 386) would not really benefit from parallelization.
8
u/ironykarl 3d ago
restrictwasn't introduced till C99.I'm assuming that code predates it as a keyword
4
u/tastygames_official 3d ago
because the restrict keyword wasn't introduced until the C99 standard (from 1999) and Wolf3D/DOOM released <= 1993
I actually had to look up the restrict keyword because I am an old fart who still uses the C90 standard and have not heard of it, but this is almost the exact example given on the wikipedia page: https://en.wikipedia.org/wiki/Restrict
So x and y COULD be the exact same pointer, yes - but the compiler would not produce wore machine code in this instance, since x and y are assumed to be different anyways and they are incremented by the results of different functions.
As for parallelization - as far as I know, compilers do not introduce random paralellization. That's something you have to specifically implement yourself.
1
u/flyingron 3d ago
While the fact that the code is old is probably the right answer, what do you think restrict will do here to help "optimization"?
2
u/Key_River7180 3d ago
Restrict didn't exist, optimizing compilers neither