Skip to content

Substitution rules not respecting operator precedence

Loopy Code:

e := (i / (ncomp * elemsize)) 
d := ((i / elemsize) % ncomp)
s := (i % elemsize)
v[i] = u[ncomp * indices[(s) + elemsize*(e)] + (d)]

Result:

v[128 * gid(0) + lid(0)] = u[ncomp * indices[((lid(0) + gid(0) * 128) % elemsize) + elemsize * (lid(0) + gid(0) * 128) / (ncomp * elemsize)] + ((lid(0) + gid(0) * 128) / elemsize % ncomp)];

The portion of interest is elemsize * (e) and the corresponding generated code elemsize * (lid(0) + gid(0) * 128) / (ncomp * elemsize). The result has the form a * b / c. The parentheses in the Loopy code intuitively should enforce a * (b / c) but the parentheses are stripped out of the final code leaving the default C precedence of (a * b) / c which often produces a different result.

Edited by Nicholas Christensen