program project1;
{$mode objfpc}{$H+}
{.$FPUTYPE X87} //<---- 1 - using x87
{$FPUTYPE SSE2}
uses
Classes, SysUtils, StreamIO, windows;
const
ixmax = 2500;
iymax = 2000;
cxmin = -2.5;
cxmax = 1.5;
cymin = -2.0;
cymax = 2.0;
maxcolorcomponentvalue = 255;
maxiteration = 200;
escaperadius = 2;
type
colortype = record
red : byte;
green : byte;
blue : byte;
end;
real = double;
var
ix, iy : integer;
cx, cy : real;
pixelwidth : real = (cxmax - cxmin) / ixmax;
pixelheight : real = (cymax - cymin) / iymax;
filename : string = 'new1.ppm';
comment : string = '# ';
outfile : textfile;
color : colortype;
zx, zy : real;
zx2, zy2 : real;
iteration : integer;
er2 : real = (escaperadius * escaperadius);
tm : longword;
AFileStream : TFileStream;
AMemoryStream : TMemoryStream;
APChar : PByte;
begin
AFileStream := TFileStream.Create(filename, fmCreate);
AMemoryStream := TMemoryStream.Create;
AMemoryStream.Size := ixmax * iymax * 3;
AMemoryStream.Position := 0;
APChar := AMemoryStream.Memory;
(* {$I-}
assign(outfile, filename);
rewrite(outfile);
if ioresult <> 0 then
begin
writeln(stderr, 'unable to open output file: ', filename);
exit;
end;*)
AssignStream(outfile, AFileStream);
rewrite(outfile);
writeln(outfile, 'P6');
writeln(outfile, ' ', comment);
writeln(outfile, ' ', ixmax);
writeln(outfile, ' ', iymax);
writeln(outfile, ' ', maxcolorcomponentvalue);
tm := GetTickCount();
for iy := 1 to iymax do
begin
cy := cymin + (iy - 1)*pixelheight;
cx := cxmin - pixelwidth;
if abs(cy) < pixelheight / 2 then cy := 0.0;
for ix := 1 to ixmax do
begin
cx := cx + pixelwidth;
zx := 0.0;
zy := 0.0;
zx2 := zx*zx;
zy2 := zy*zy;
iteration := 0;
while (iteration < maxiteration) and (zx2 + zy2 < er2) do
begin
zy := 2*zx*zy + cy;
zx := zx2 - zy2 + cx;
zx2 := zx*zx;
zy2 := zy*zy;
iteration := iteration + 1;
end;
if iteration = maxiteration then
begin
color.red := 0;
color.green := 0;
color.blue := 0;
end
else
begin
color.red := 255;
color.green := 255;
color.blue := 255;
end;
//write(outfile, chr(color.red), chr(color.green), chr(color.blue));
{ AMemoryStream.Write(color.red, SizeOf(color.red));
AMemoryStream.Write(color.green, SizeOf(color.green));
AMemoryStream.Write(color.blue, SizeOf(color.blue)); }
APChar^ := color.red;
Inc(APChar);
APChar^ := color.green;
Inc(APChar);
APChar^ := color.blue;
Inc(APChar);
end;
end;
writeln(GetTickCount() - tm,'ms');
close(outfile);
AFileStream.Seek(0, soEnd);
AFileStream.CopyFrom(AMemoryStream, 0);
FreeAndNil(AFileStream);
FreeAndNil(AMemoryStream);
readln;
end.
With this code and compiler option -O4, I can get between 998ms and 1029ms execution times in real hardware.
EDIT : Same code with FPC 2.6.5 (fixes) with -O3 option, gives between 2511ms and 1592ms.