Portability questions (Arrays)_(re)
Sat, 31 May 1997 22:02:19 +0200
Peter Gerwinski wrote:
> > (Is it generally allowed to cast a pointer into an integer,
> > in the first place?)
>
> I just found out that on the DEC Alpha a pointer has 8 bytes, while
> an integer has 4.
Good that I didn't use this "feature" yet.
I'm briefly describing the situation I have. It's about speed optimization.
I have the following (simplified):
type t=array[0..max] of integer;
function f(var a:t):integer;
var
i:integer;
begin
i:=0;
{some loop}
if {...} then
begin
a[i]:=...;
inc(i)
end;
{...}
f:=i
end.
I thought about optimizing it with a pointer like this:
function f(var a:t):integer;
var
p:^integer;
begin
p:=@a[0];
{some loop}
if {...} then
begin
p^:=...;
inc(p) {is this ok, BTW?}
end;
{...}
f:="offset of p in a"
end.
So "f:=(integer(p)-integer(@a)) div sizeof(a[0])" doesn't seem to work then.
Or are other means of pointer arithmetic available -- or completely other
ways to do it?
I know that all of this is not very "Pascalish", but this routine requires
the utmost speed possible, so I tried to elimitate the need for an array
lookup, and to save one register (only p instead of i and the address of a)
-- this really does make a difference since the rest of the loops uses some
more variables. I hope I don't have to convert the routine into C to get
the best speed.
--
Frank Heckenbach, Erlangen, Germany
heckenb@mi.uni-erlangen.de
Turbo Pascal: http://www.mi.uni-erlangen.de/~heckenb/programs.htm
Internet links: http://www.mi.uni-erlangen.de/~heckenb/links.htm
Frank Heckenbach (heckenb@mi.uni-erlangen.de)
HTML conversion by Lluís de Yzaguirre i Maura
Institut de Lingüística Aplicada -
Universitat "Pompeu Fabra"
e-mail: de_yza@upf.es