[fpc-pascal] Re: Turbo Pascal/legacy data file issue.
Jan Ruzicka
jan.ruzicka at comcast.net
Wed Sep 21 05:59:55 CEST 2005
I think basic reference information you may be looking for is on page
http://www.merlyn.demon.co.uk/pas-type.htm
------------------------------------------------------------------
real2double from rtl/inc/genmath.inc
------------------------------------------------------------------
function real2double(r : real48) : double;
var
res : array[0..7] of byte;
exponent : word;
begin
{ copy mantissa }
res[0]:=0;
res[1]:=r[1] shl 5;
res[2]:=(r[1] shr 3) or (r[2] shl 5);
res[3]:=(r[2] shr 3) or (r[3] shl 5);
res[4]:=(r[3] shr 3) or (r[4] shl 5);
res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
res[6]:=(r[5] and $7f) shr 3;
{ copy exponent }
{ correct exponent: }
exponent:=(word(r[0])+(1023-129));
res[6]:=res[6] or ((exponent and $f) shl 4);
res[7]:=exponent shr 4;
{ set sign }
res[7]:=res[7] or (r[5] and $80);
real2double:=double(res);
end;
------------------------------------------------------------------
double2real by just mechanically reversed operations
------------------------------------------------------------------
function double2real(d : double) : real48;
var
res : real48;
exponent : word;
begin
{ copy mantissa }
res[0]:=0;
res[1]:=(d[2] shl 3) or (d[1] shr 5);
res[2]:=(d[3] shl 3) or (d[2] shr 5);
res[3]:=(d[4] shl 3) or (d[3] shr 5);
res[4]:=(d[5] shl 3) or (d[4] shr 5);
res[5]:=((d[6] and $f) shl 3) or (d[5] shr 5);
{ copy exponent }
{ correct exponent: }
exponent:= ((d[7] and $7f) shl 4) or ((d[6] shr 4) and $f);
exponent:= exponent +129-1023;
res[0]:= exponent and $ff;
{ set sign }
res[5]:=res[5] or (d[7] and $80);
double2real:=res;
end;
------------------------------------------------------------------
The code above will probably not compile as I use indexes of bytes
bytes on type double.
I don't have a working compiler at this moment.
(FPC is not compiling on OSX)
Would this help you?
Could you write some tests to verify correctness?
Jan
On Sep 20, 2005, at 12:14, Lowell C. Savage wrote:
> What I would still like to see is a "Double2Real" which goes in the
> opposite direction of the "Real2Double". That way, I can read the
> data from the existing files and then save new data back to the same
> files. The loss of precision is not important since the numbers we
> are using have limitations on their precision that are the limiting
> factor (they could probably be "singles" without any trouble--except
> that then they wouldn't be compatible with the data files. And
> performance isn't really an issue since I'll do any calculations as
> doubles (native on x86 platform).
>
> Thanks,
>
> Lowell
>
> Florian Klaempfl <F.Klaempfl at gmx.de> wrote, in part:
>> Lowell C. Savage wrote:
>>
>> > Has anyone come up with a set of routines for converting between
>> Turbo
>> > Pascal's 6-byte "real" format and FPC's real number format?
>>
>> http://www.freepascal.org/docs-html/rtl/system/real2double.html
>
> Thanks. My apologies for not finding it earlier myself.
>
>> > Here's a simple program to demonstrate the issue. In Turbo Pascal
>> 5.5
>> > (Downloaded from
>> http://bdn.borland.com/article/0,1410,20803,00.html)
>> > this code generates a 12-byte file. FPC generates a 16-byte file.
>> > (Actually, the preprocessor directives shown appear to have no
>> effect on
>>
>> What effect should it have?
>
> I would like it to have the same effect whether compiled under Turbo
> Pascal 5.5 or under FPC. In both cases, it should write out a 12-byte
> file. I don't mind if I have to put in a bunch of ifdefs to define
> "real48" and dummy "Double2Real" functions for TP or some such and
> redo the assignments to call "Double2Real". In other words, the code
> can change--but the data (and data format) is forever. :-)
>
>> > this code--not complaining since the docs don't make it appear that
>> it
>> > should, just pointing it out.)
>>
>> The 6 byte real isn't supported by fpc because it isn't handled by the
>> fpu and thus deadly slow. For maybe 12 years, every new PC has an fpu.
>
>
> Heavens no! I don't want to do !!CALCULATIONS!! with the 6-byte
> reals. I just want to be able to read and store them in the existing
> files.
>
>> > Program testrec;
>> > {$IFDEF FPC}
>> > {$MODE TP}
>> > {$PACKRECORDS 1}
>> > {$ENDIF}
>> > type
>> > realrec = record
>> > t1 : real;
>> > t2 : real;
>> > end;
>> >
>> > var
>> > realfile : file of realrec;
>> > r : realrec;
>> > begin
>> > r.t1 := 1.0;
>> > r.t2 := 2.0;
>> > assign ( realfile, 'c:\r.rfl' );
>> > rewrite ( realfile );
>> > write ( realfile, r );
>> > close ( realfile );
>> > end.
>
> Lowell C. Savage
> savage at lanl.gov
> 505-667-6964 (office/msg)
> 360-961-8965 (cell/msg)
> 505-667-4341 (shared fax)
>
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
More information about the fpc-pascal
mailing list