[fpc-devel] bug: Inc(v,i)/Dec(v,i)

Gerhard Scholz gs at g--s.de
Fri Jul 8 11:42:45 CEST 2005


----- Original Message -----
From: "Vinzent Hoefler" <JeLlyFish.software at gmx.net>
To: "FPC developers' list" <fpc-devel at lists.freepascal.org>
Sent: Thursday, July 07, 2005 9:47 AM
Subject: Re: [fpc-devel] bug: Inc(v,i)/Dec(v,i)


> On Thursday 07 July 2005 05:39, Peter Vreman wrote:
>
> > > shouldn't it implement inv(v,-1) in exactly the same way it
> > > implements v:=v-1?
> >
> > The problem is what type do you give to -1. In the old situation the
> > -1 was converted to the same type as v -> longword.
>
> Which should trigger a range check already, because -1 can't be
> represented as valid longword.
>
> Personal note: Maybe there was a reason why Borland Pascal didn't do
> range checks on Inc/Dec. The only time I ever used it was a checksum
> calculation which was modulo anyway. Any other time I use the usual
> 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.
>
>
> Vinzent.

I assume that was exactly the reason why they were introduced: as a
shorthand and as a hint for optimization.
The original Pascal only had the forms inc(v) and dec(v) so the operation
could be done directly on the memory location of the variable (if the
machine code had a possibility for that).

inc(v,d) and dec(v,d) came later; I think Borland introduced them.

The problem is not really a constant param2 (that can be avoided by myself),
but a variable param2 (and both
can occur: positive and negative deltas!):

    inc ( v, delta ) ;

Solution 1 of course would be:

    if delta < 0
        then dec ( v, -delta )
        else inc ( v, delta ) ;

but that looks like a joke and not very optimal.

Solution 2 would be the safe way (as I'm forced to do it now):

    v := v + delta ;

I think the easiest solution to the whole problem would be to internally
translate a "inc(v,d)" to "v := v + d", at least in {$r+,q+} checked code.

Gerhard






More information about the fpc-devel mailing list