[fpc-devel] Advantages of/alternatives for Inc (was: bug: Inc(v, i)/Dec(v, i))

Tom Verhoeff T.Verhoeff at tue.nl
Fri Jul 8 14:39:45 CEST 2005

On Thu, Jul 07, 2005 at 07:47:03AM +0000, Vinzent Hoefler wrote:
> operators. So especially with the "added" overflow check I think, Inc 
> and Dec are pretty useless functions, because the don't do anything you 
> can't accomplish by using "+" or "-" unless you have 
> very_long_and_complex_variable_names and want to save some typing work.

You suggest that it saves only typing, but it also simplifies optimization
in cases where the variable is compositite, because the address needs
to be calculated only once.  E.g. in one piece of code, I have
a 13-dimensional array of integers, and I do

  Inc ( A[e1, e2, ..., e13] )

I don't know if modern compilers will properly optimize

  A[e1, e2, ..., e13] := Succ ( A[e1, e2, ..., e13] )

Recalculation of the index expressions e1 through e13 can be avoided
by introducing auxiliary variables, but it is less straightforward
to avoid the recalculation of the address given the index values.

In fact, it would be nice to have a Self for arrays as well (as for
objects), where you could write

  with A[e1, e2, ..., e13] do
    Self := Succ ( Self )

because that would allow more general expressions to benefit from the
one-time address calculation.  (Of course, this is just a let/where
clause from functional programming).  For example,

  with A[e1, e2, ..., e13] do
    Self := Self * ( Self * ( Self + 1 ) + 2 ) + 3

What I sometimes resort to is

  var A: array [ ... ] of record Self: Integer end;

  with A[e1, e2, ..., e13] do
    Self := Self * ( Self * ( Self + 1 ) + 2 ) + 3

But it is a pain to introduce such wrapping records explicitly,
because you need to change not only the variable's type, but also
all using occurrences of the variable.

E-MAIL: T.Verhoeff @ TUE.NL     | Fac. of Math. & Computing Science
PHONE:  +31 40 247 41 25        | Eindhoven University of Technology
FAX:    +31 40 247 54 04        | PO Box 513, NL-5600 MB Eindhoven
http://www.win.tue.nl/~wstomv/  | The Netherlands

More information about the fpc-devel mailing list