summaryrefslogtreecommitdiff
path: root/rtl/inc/flt_pack.inc
blob: e3de128ea8e2c524ea67eb98ea2f87fcf5f6b230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
{
    This file isolates platform-specific routines which perform packing and
    unpacking of ValReal FP values during float <-> ASCII conversions.
    These routines, mostly, were gathered from various places of FPC RTL.

 ****************************************************************************
}
{
    Note about inlining: since unpack_float is used only once in str_real,
    it can be safely inlined; however pack_float is used several times in
    val_real, so its inlining does not seem practical, except of the case
    when this procedure simply calls the SoftFPU implementation.
}
// ---------------------------------------------------------------------
//
// single; format [MSB]: 1 sign bit, 8 bit exponent, 23 bit mantissa
//
// ---------------------------------------------------------------------
{$if defined(VALREAL_32) and not defined(VALREAL_PACK)}
{$if defined(fpc_softfpu_implementation)
     or ( defined(FPC_SYSTEM_HAS_extractFloat32Frac)
      and defined(FPC_SYSTEM_HAS_extractFloat32Exp)
      and defined(FPC_SYSTEM_HAS_extractFloat32Sign)
        )}

function unpack_float( const f: ValReal; out minus: boolean ): TDIY_FP; {$ifdef grisu1_inline}inline;{$endif}
begin
    unpack_float.f := extractFloat32Frac( float32( f ) );
    unpack_float.e := extractFloat32Exp( float32( f ) );
    minus := ( extractFloat32Sign( float32( f ) ) <> 0 );
end;

{$else not fpc_softfpu_implementation}

function unpack_float( const f: ValReal; out minus: boolean ): TDIY_FP; {$ifdef grisu1_inline}inline;{$endif}
type
    TSplitFloat = packed record
      case byte of
        0: ( f: ValReal );
        1: ( b: array [ 0 .. 3 ] of byte );
        2: ( w: array [ 0 .. 1 ] of word );
        3: ( d: dword );
    end;
var
    split: TSplitFloat;
begin
    split.f := f;
 {$ifdef endian_big}
    minus := ( split.b[0] and $80 <> 0 );
    unpack_float.e := ( split.w[0] shr 7 ) and $FF;
 {$else endian_little}
    minus := ( split.b[3] and $80 <> 0 );
    unpack_float.e := ( split.w[1] shr 7 ) and $FF;
 {$endif endian}
    unpack_float.f := split.d and $007FFFFF;
end;

{$endif fpc_softfpu_implementation}
{$endif unpack float32}

{$if defined(VALREAL_32) and defined(VALREAL_PACK)}
{$ifdef fpc_softfpu_implementation}

procedure pack_float( out f: ValReal; minus: boolean; exp: integer; const m: dword ); {$ifdef grisu1_inline}inline;{$endif}
begin
    f := float32rec( packFloat32( ord(minus), exp, m ) );
end;

{$else not fpc_softfpu_implementation}

procedure pack_float( out f: ValReal; minus: boolean; exp: integer; m: dword ); // {$ifdef grisu1_inline}inline;{$endif}
type
    TSplitFloat = packed record
      case byte of
        0: ( f: ValReal );
        1: ( b: array [ 0 .. 3 ] of byte );
        2: ( w: array [ 0 .. 1 ] of word );
        3: ( d: dword );
    end;
var
    split: TSplitFloat;
begin
    split.d := m;
 {$ifdef endian_big}
    split.w[0] := split.w[0] + ( exp and $FF ) shl 7;
    if minus then
        split.b[0] := split.b[0] or $80;
 {$else endian_little}
    split.w[1] := split.w[1] + ( exp and $FF ) shl 7;
    if minus then
        split.b[3] := split.b[3] or $80;
 {$endif endian}
    f := split.f;
end;

{$endif fpc_softfpu_implementation}
{$endif pack float32}

// ---------------------------------------------------------------------
//
// double; format [MSB]: 1 sign bit, 11 bit exponent, 52 bit mantissa
//
// ---------------------------------------------------------------------
{$if defined(VALREAL_64) and not defined(VALREAL_PACK)}
{$ifdef cpujvm}

function unpack_float( const f: ValReal; out minus: boolean ): TDIY_FP; {$ifdef grisu1_inline}inline;{$endif}
var
    doublebits: int64;
begin
    doublebits := JLDouble.doubleToLongBits( f );
    minus := ( doublebits < 0 );
    unpack_float.e := ( doublebits shr 52 ) and $7FF;
    unpack_float.f := ( doublebits and $000FFFFFFFFFFFFF );
end;

{$else not cpujvm}

{$if defined(fpc_softfpu_implementation)
     or ( defined(FPC_SYSTEM_HAS_extractFloat64Frac)
      and defined(FPC_SYSTEM_HAS_extractFloat64Exp)
      and defined(FPC_SYSTEM_HAS_extractFloat64Sign)
        )}

function unpack_float( const f: ValReal; out minus: boolean ): TDIY_FP; {$ifdef grisu1_inline}inline;{$endif}
begin
    unpack_float.f := extractFloat64Frac( float64( f ) );
    unpack_float.e := extractFloat64Exp( float64( f ) );
    minus := ( extractFloat64Sign( float64( f ) ) <> 0 );
end;

{$else not fpc_softfpu_implementation}

function unpack_float( const f: ValReal; out minus: boolean ): TDIY_FP; {$ifdef grisu1_inline}inline;{$endif}
type
    TSplitFloat = packed record
      case byte of
        0: ( f: ValReal );
        1: ( b: array [ 0 .. 7 ] of byte );
        2: ( w: array [ 0 .. 3 ] of word );
        3: ( d: array [ 0 .. 1 ] of dword );
        4: ( l: qword );
    end;
var
    doublebits: TSplitFloat;
begin
  {$ifdef FPC_DOUBLE_HILO_SWAPPED}
    // high and low dword are swapped when using the arm fpa
    doublebits.d[0] := TSplitFloat(f).d[1];
    doublebits.d[1] := TSplitFloat(f).d[0];
  {$else not FPC_DOUBLE_HILO_SWAPPED}
    doublebits.f := f;
  {$endif FPC_DOUBLE_HILO_SWAPPED}
  {$ifdef endian_big}
    minus := ( doublebits.b[0] and $80 <>0 );
    unpack_float.e := ( doublebits.w[0] shr 4 ) and $7FF;
  {$else endian_little}
    minus := ( doublebits.b[7] and $80 <> 0 );
    unpack_float.e := ( doublebits.w[3] shr 4 ) and $7FF;
  {$endif endian}
    unpack_float.f := doublebits.l and $000FFFFFFFFFFFFF;
end;

{$endif fpc_softfpu_implementation}
{$endif cpujvm}
{$endif unpack float64}

{$if defined(VALREAL_64) and defined(VALREAL_PACK)}
{$ifdef cpujvm}

procedure pack_float( out f: ValReal; minus: boolean; exp: integer; const m: qword ); {$ifdef grisu1_inline}inline;{$endif}
var
    doublebits: int64;
begin
    doublebits := ( m and $000FFFFFFFFFFFFF ) + ( qword( exp and $7FF ) shl 52 ) + ( qword( ord(minus) ) shl 63 );
    f := JLDouble.longBitsToDouble( doublebits );
end;

{$else not cpujvm}

{$ifdef fpc_softfpu_implementation}

procedure pack_float( out f: ValReal; minus: boolean; exp: integer; const m: qword ); {$ifdef grisu1_inline}inline;{$endif}
begin
    f := packFloat64( ord(minus), exp, m );
end;

{$else not fpc_softfpu_implementation}

procedure pack_float( out f: ValReal; minus: boolean; exp: integer; const m: qword ); // {$ifdef grisu1_inline}inline;{$endif}
type
    TSplitFloat = packed record
      case byte of
        0: ( f: ValReal );
        1: ( b: array [ 0 .. 7 ] of byte );
        2: ( w: array [ 0 .. 3 ] of word );
        3: ( d: array [ 0 .. 1 ] of dword );
        4: ( l: qword );
    end;
var
    doublebits: TSplitFloat;
begin
    doublebits.l := m;
  {$ifdef endian_big}
    doublebits.w[0] := doublebits.w[0] + ( exp and $7FF ) shl 4;
    if minus then
        doublebits.b[0] := doublebits.b[0] or $80;
  {$else endian_little}
    doublebits.w[3] := doublebits.w[3] + ( exp and $7FF ) shl 4;
    if minus then
        doublebits.b[7] := doublebits.b[7] or $80;
  {$endif endian}
  {$ifdef FPC_DOUBLE_HILO_SWAPPED}
    // high and low dword are swapped when using the arm fpa
    TSplitFloat(f).d[1] := doublebits.d[0];
    TSplitFloat(f).d[0] := doublebits.d[1];
  {$else not FPC_DOUBLE_HILO_SWAPPED}
    f := doublebits.f;
  {$endif FPC_DOUBLE_HILO_SWAPPED}
end;

{$endif fpc_softfpu_implementation}
{$endif cpujvm}
{$endif pack float64}

// ---------------------------------------------------------------------
//
// extended; format [MSB]: 1 Sign bit, 15 bit exponent, 64 bit mantissa (explicit integer bit is included)
//
// ---------------------------------------------------------------------
{$if defined(VALREAL_80) and not defined(VALREAL_PACK)}
{$ifdef fpc_softfpu_implementation}

function unpack_float( const f: ValReal; out minus: boolean ): TDIY_FP; {$ifdef grisu1_inline}inline;{$endif}
begin
    unpack_float.fh := 0;
    unpack_float.f := extractFloatx80Frac( f );
    unpack_float.e := extractFloatx80Exp( f );
    minus := ( extractFloatx80Sign( f ) <> 0 );
end;

{$else not fpc_softfpu_implementation}

function unpack_float( const f: ValReal; out minus: boolean ): TDIY_FP; {$ifdef grisu1_inline}inline;{$endif}
type
    TSplitFloat = packed record
      case byte of
        0: ( f: ValReal );
        1: ( b: array [ 0 .. 9 ] of byte );
        2: ( l: qword; e: word )
    end;
var
    split: TSplitFloat;
begin
    split.f := f;
  {$ifdef endian_big}
    {$error Big endian extended double [80-bit] is not implemented}
  {$else endian_little}
    minus := ( split.b[9] and $80 <> 0 );
    unpack_float.e := split.e and $7FFF;
    unpack_float.f := split.l;
    unpack_float.fh := 0;
  {$endif endian}
end;

{$endif fpc_softfpu_implementation}
{$endif unpack floatx80}

{$if defined(VALREAL_80) and defined(VALREAL_PACK)}
{$ifdef fpc_softfpu_implementation}

procedure pack_float( out f: ValReal; minus: boolean; exp: integer; const m: qword ); {$ifdef grisu1_inline}inline;{$endif}
begin
    f := packFloatx80( ord(minus), exp, m );
end;

{$else not fpc_softfpu_implementation}

procedure pack_float( out f: ValReal; minus: boolean; exp: integer; const m: qword ); // {$ifdef grisu1_inline}inline;{$endif}
type
    TSplitFloat = packed record
      case byte of
        0: ( f: ValReal );
        1: ( b: array [ 0 .. 9 ] of byte );
        2: ( l: qword; e: word )
    end;
var
    split: TSplitFloat;
begin
  {$ifdef endian_big}
    {$error Big endian extended double [80-bit] is not implemented}
  {$else endian_little}
    split.l := m;
    split.e := exp and $7FFF;
    if minus then
        split.b[9] := split.b[9] or $80;
  {$endif endian}
    f := split.f;
end;

{$endif fpc_softfpu_implementation}
{$endif pack floatx80}

// ---------------------------------------------------------------------
//
// float128; format [MSB]: 1 Sign bit, 15 bit exponent, 112 bit mantissa
//
// ---------------------------------------------------------------------
{$if defined(VALREAL_128) and not defined(VALREAL_PACK)}
{$ifdef fpc_softfpu_implementation}

function unpack_float( const f: ValReal; out minus: boolean ): TDIY_FP; {$ifdef grisu1_inline}inline;{$endif}
begin
    unpack_float.fh := extractFloat128Frac0( f );
    unpack_float.f := extractFloat128Frac1( f );
    unpack_float.e := extractFloat128Exp( f );
    minus := ( extractFloat128Sign( f ) <> 0 );
end;

{$else not fpc_softfpu_implementation}

function unpack_float( const f: ValReal; out minus: boolean ): TDIY_FP; {$ifdef grisu1_inline}inline;{$endif}
type
    TSplitFloat = packed record
      case byte of
        0: ( f: ValReal );
        1: ( b: array [ 0 .. 15 ] of byte );
        2: ( w: array [ 0 .. 7 ] of word );
        3: ( l: array [ 0 .. 1 ] of qword );
    end;
var
    split: TSplitFloat;
begin
    split.f := f;
  {$ifdef endian_big}
    {$error Big endian long double [128-bit] is not implemented}
  {$else endian_little}
    minus := ( split.b[15] and $80 <> 0 );
    unpack_float.e := split.w[7] and $7FFF;
    unpack_float.f := split.l[0];
    unpack_float.fh := split.l[1] and $0000FFFFFFFFFFFF;
  {$endif endian}
end;

{$endif fpc_softfpu_implementation}
{$endif unpack float128}

{$if defined(VALREAL_128) and defined(VALREAL_PACK)}
{$ifdef fpc_softfpu_implementation}

procedure pack_float( out f: ValReal; minus: boolean; exp: integer; const h, l: qword ); {$ifdef grisu1_inline}inline;{$endif}
begin
    f := packFloat128( ord(minus), exp, h, l );
end;

{$else not fpc_softfpu_implementation}

procedure pack_float( out f: ValReal; minus: boolean; exp: integer; const h, l: qword ); // {$ifdef grisu1_inline}inline;{$endif}
type
    TSplitFloat = packed record
      case byte of
        0: ( f: ValReal );
        1: ( b: array [ 0 .. 15 ] of byte );
        2: ( w: array [ 0 .. 7 ] of word );
        3: ( l: array [ 0 .. 1 ] of qword );
    end;
var
    split: TSplitFloat;
begin
  {$ifdef endian_big}
    {$error Big endian long double [128-bit] is not implemented}
  {$else endian_little}
    split.l[0] := l;
    split.l[1] := h;
    split.w[7] := exp and $7FFF;
    if minus then
        split.b[15] := split.b[15] or $80;
  {$endif endian}
    f := split.f;
end;

{$endif fpc_softfpu_implementation}
{$endif pack float128}