Recent

Author Topic: Microsoft Single <-> IEEE?  (Read 7410 times)

ozznixon

  • Full Member
  • ***
  • Posts: 119
    • http://www.modernpascal.com/
Microsoft Single <-> IEEE?
« on: August 18, 2018, 02:45:50 pm »
This code is from a Turbo Pascal project - that I need to port to Lazarus - anyone know the correct formula/types to do this from GWBASIC -> FPC and back?

Code: Pascal  [Select][+][-]
  1. Function bsrealtolong(b: longint) : longint;
  2.  
  3. { Converts a 4 Byte Microsoft format single precision Real Variable as
  4.   used in earlier versions of QuickBASIC and GW-BASIC to IEEE 6 Byte Real }
  5.  
  6.   Var m    : array[0..3] of byte Absolute b;
  7.       l    : longint;
  8.       r    : real;
  9.       ieee : Array [0..5] of Byte Absolute r;
  10.  
  11.   begin
  12.     FillChar(r, sizeof(r), 0);
  13.  
  14.     ieee[0]  := m[3];
  15.     ieee[3]  := m[0];
  16.     ieee[4]  := m[1];
  17.     ieee[5]  := m[2];
  18.  
  19.     bsrealtolong := round(r);
  20.   end;  { MStoIEEE }
  21.  
  22.  
  23. function longtobsreal(l: longint): longint;
  24.  
  25. { Note that this will only be effective where the accuracy required
  26.   can be obtained in the 23 bits that are available. }
  27.  
  28.   Var ie    : real;
  29.       ms    : longint;
  30.       ieee  : Array [0..5] of Byte Absolute ie;
  31.       m     : array[0..3] of byte Absolute ms;
  32.  
  33.   begin
  34.     ie := l;
  35.  
  36.     m[3] := ieee[0];
  37.     m[0] := ieee[3];
  38.     m[1] := ieee[4];
  39.     m[2] := ieee[5];
  40.  
  41.     longtobsreal := ms;
  42.   end; { IEEEtoMS }
  43.  
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Microsoft Single <-> IEEE?
« Reply #1 on: August 18, 2018, 02:49:31 pm »
This code is from a Turbo Pascal project - that I need to port to Lazarus - anyone know the correct formula/types to do this from GWBASIC -> FPC and back?

Code: Pascal  [Select][+][-]
  1. Function bsrealtolong(b: longint) : longint;
  2.  
  3. { Converts a 4 Byte Microsoft format single precision Real Variable as
  4.   used in earlier versions of QuickBASIC and GW-BASIC to IEEE 6 Byte Real }
  5.  
  6.   Var m    : array[0..3] of byte Absolute b;
  7.       l    : longint;
  8.       r    : real;
  9.       ieee : Array [0..5] of Byte Absolute r;
  10.  
  11.   begin
  12.     FillChar(r, sizeof(r), 0);
  13.  
  14.     ieee[0]  := m[3];
  15.     ieee[3]  := m[0];
  16.     ieee[4]  := m[1];
  17.     ieee[5]  := m[2];
  18.  
  19.     bsrealtolong := round(r);
  20.   end;  { MStoIEEE }
  21.  
  22.  
  23. function longtobsreal(l: longint): longint;
  24.  
  25. { Note that this will only be effective where the accuracy required
  26.   can be obtained in the 23 bits that are available. }
  27.  
  28.   Var ie    : real;
  29.       ms    : longint;
  30.       ieee  : Array [0..5] of Byte Absolute ie;
  31.       m     : array[0..3] of byte Absolute ms;
  32.  
  33.   begin
  34.     ie := l;
  35.  
  36.     m[3] := ieee[0];
  37.     m[0] := ieee[3];
  38.     m[1] := ieee[4];
  39.     m[2] := ieee[5];
  40.  
  41.     longtobsreal := ms;
  42.   end; { IEEEtoMS }
  43.  
if my memory serves me right the dos era real is defined as real48 in delphi/fpc.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Microsoft Single <-> IEEE?
« Reply #2 on: August 18, 2018, 02:51:10 pm »
It is hard to say without the intention behind the code.

Maybe they hacked this together to avoid relying on hardware FPU support, something that is fairly normal (FPC always has used hardware FPU support).

FPC does not support 6 byte real, so the most logical solution, is to just make it a single and see if that works.

Thaddy

  • Hero Member
  • *****
  • Posts: 14198
  • Probably until I exterminate Putin.
Re: Microsoft Single <-> IEEE?
« Reply #3 on: August 18, 2018, 04:34:46 pm »
For calculations, indeed, usually a double or even a single is OK.
There is quite good real48 support in math.pas together with rtl-extra/real48utils , but maybe that is only in trunk.
The type is indeed called real48. On this forum there is a complete unit somewhere on which that support was based.
See here: http://forum.lazarus.freepascal.org/index.php/topic,33636.msg218320.html#msg218320
And the patch here:
mantis https://bugs.freepascal.org/view.php?id=30460
« Last Edit: August 18, 2018, 05:12:17 pm by Thaddy »
Specialize a type, not a var.

ozznixon

  • Full Member
  • ***
  • Posts: 119
    • http://www.modernpascal.com/
Re: Microsoft Single <-> IEEE?
« Reply #4 on: August 18, 2018, 04:56:47 pm »

Quote
if my memory serves me right the dos era real is defined as real48 in delphi/fpc.

Awesome - thanks - I will try and make sure the old code works with QWK and PCBoard. ;-)

Ozz
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

Thaddy

  • Hero Member
  • *****
  • Posts: 14198
  • Probably until I exterminate Putin.
Re: Microsoft Single <-> IEEE?
« Reply #5 on: August 18, 2018, 05:14:22 pm »
You probably still want the real48utils in it too. Math on its own does little.
With the rtl-extra stuff even the calculations will work.
Note that if it is not in 3.0.4. you can download it (real48utils.pas) from trunk: it does not rely on new compiler features.
It contains most if not all overloaded math operators for old school real.
« Last Edit: August 18, 2018, 05:16:19 pm by Thaddy »
Specialize a type, not a var.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Microsoft Single <-> IEEE?
« Reply #6 on: August 18, 2018, 06:05:33 pm »
I do not think real48 is related to MBF.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Microsoft Single <-> IEEE?
« Reply #7 on: August 18, 2018, 06:49:42 pm »
If mbf is the problem, there are enough sources on it

Thaddy

  • Hero Member
  • *****
  • Posts: 14198
  • Probably until I exterminate Putin.
Re: Microsoft Single <-> IEEE?
« Reply #8 on: August 18, 2018, 07:02:15 pm »
I do not think real48 is related to MBF.
After re-reading I see. Totally forgot about that.
Well... I have to write a helper unit ;) O:-)
Specialize a type, not a var.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Microsoft Single <-> IEEE?
« Reply #9 on: August 18, 2018, 07:35:13 pm »
I do not think real48 is related to MBF.
After re-reading I see. Totally forgot about that.
Well... I have to write a helper unit ;) O:-)

Huh? If the OP's code worked on TP, it should work also in FP (even if just in TP mode). He just needs to change any "real" to "real48". Wasn't that the question?  ;D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Microsoft Single <-> IEEE?
« Reply #10 on: August 18, 2018, 08:26:58 pm »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Microsoft Single <-> IEEE?
« Reply #11 on: August 18, 2018, 08:39:48 pm »
Wasn't that the question?  ;D

No.

Ok, the question was, in fact, if the piece of code given would translate the old MS-FP format to a Real48 and, if not, what the formula is. IIRC the algorithm was in one of the old Borland manuals/tech-notes; I'll try to find it and post it here.

edit:
Found this in the last SWAG (along with the OP's code which is apparently by Trevor Carlson):
Code: Pascal  [Select][+][-]
  1. {===========================================================================
  2. Date: 10-09-93 (23:23)
  3. From: J.P. Ritchey
  4. Subj: MSBIN to IEEE
  5. ---------------------------------------------------------------------------
  6. GE>         Does anyone have any code for Converting MSBIN format
  7. GE>         numbers into IEEE?  }
  8.  
  9. {$A-,B-,D-,E+,F-,I-,L-,N+,O-,R-,S-,V-}
  10. unit BFLOAT;
  11. (*
  12.             MicroSoft Binary Float to IEEE format Conversion
  13.                     Copyright (c) 1989 J.P. Ritchey
  14.                             Version 1.0
  15.  
  16.          This software is released to the public domain.  Though
  17.          tested, there could be some errors.  Any reports of bugs
  18.          discovered would be appreciated. Send reports to
  19.                  Pat Ritchey     Compuserve ID 72537,2420
  20. *)
  21.  

Rather than post it in its entire glory, I'm attaching it as a zip.
Remember that this is actual TP code: substitute Real by Real48, pack your arrays, convert the asm blocks, etc.

Have fun!  8)
« Last Edit: August 18, 2018, 09:46:13 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ozznixon

  • Full Member
  • ***
  • Posts: 119
    • http://www.modernpascal.com/
Re: Microsoft Single <-> IEEE?
« Reply #12 on: January 17, 2022, 06:36:30 am »
Just wanted to say thanks. Today, I find myself working on some old code that uses MS C Single... and ended right back here.  8)
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

 

TinyPortal © 2005-2018