Emil Jerabek wrote:
during some debugging I noticed that GPC generates very hairy code for the `mod' operator with signed arguments. A closer look revealed that the code contains a lot of branching on conditions which can be determined on compile time, and most surprisingly, the value of X mod Y is recomputed _twice_, if X is negative. (I've attached a commented assembler dump.)
I might be interesting to note that not all Pascal compilers follow the ISO 7185 Pascal standard for the mod operator. The standard states: "Evaluation of a term of the form x mod y is an error if y is less than or equal to zero; otherwise there is an integer k such that x mod y satisfies the following relation: 0 <= x mod y = x - k * y < y." But on page 6-5 of the Delphi 3 Object Pascal Language Guide, we read: "The mod operator returns the remainder obtained by dividing its two operands; that is, I mod J = I - (I div J) * J The sign of the result of mod is the same as the sign of I. A runtime error occurs if J is zero." That is, in Delphi (1) J < 0 is allowed and (2) I mod J < 0 if I < 0. A nasty trap if you are porting code from Delphi (or some other Pascal compilers) ! Adriaan van Os