-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAndroid.Extended.pas
148 lines (122 loc) · 3.88 KB
/
Android.Extended.pas
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
unit Android.Extended;
{
Copyright (C) 1988-1991 Apple Computer, Inc. All rights reserved.
Machine-independent I/O routines for IEEE floating-point numbers.
NaN's and infinities are converted to HUGE_VAL or HUGE, which happens to be infinity on IEEE machines. Unfortunately,
it is impossible to preserve NaN's in a machine-independent way. Infinities are, however, preserved on IEEE machines.
These routines have been tested on the following machines:
Apple Macintosh, MPW 3.1 C compiler
Apple Macintosh, THINK C compiler
Silicon Graphics IRIS, MIPS compiler
Cray X/MP and Y/MP
Digital Equipment VAX
Implemented by Malcolm Slaney and Ken Turkowski.
Malcolm Slaney contributions during 1988-1990 include big- and little-endian file I/O, conversion to and from
Motorola's extended 80-bit floating-point format, and conversions to and from IEEE single-precision floating-point
format.
In 1991, Ken Turkowski implemented the conversions to and from IEEE double-precision format, added more precision to
the extended conversions, and accommodated conversions involving +/- infinity, NaN's, and denormalized numbers.
}
interface
{$INCLUDE Android.Config.inc}
{$INCLUDE Android.LibDefs.inc}
procedure ConvertToIeeeExtended(num: Double; bytes: PByte);
function ConvertFromIeeeExtended(bytes: PByte): Double;
implementation
function frexp(Value: Double; exp: PLongInt): Double; cdecl; external libm Name 'frexp';
function ldexp(Value: Double; param: LongInt): Double; cdecl; external libm Name 'ldexp';
function floor(Value: Double): Double; cdecl; external libm Name 'floor';
function FloatToUnsigned(f: Double): LongWord;
begin
Result := LongWord(LongInt(Trunc(f - 2147483648.0)) + 2147483647) + 1;
end;
function UnsignedToFloat(u: LongWord): Double;
begin
Result := LongInt(u - 2147483647 - 1) + Double(2147483648.0);
end;
procedure ConvertToIeeeExtended(num: Double; bytes: PByte);
var
sign, expon: LongInt;
fMant, fsMant: Double;
hiMant, loMant: LongWord;
begin
if num < 0 then
begin
sign := $8000;
num := -num;
end
else
sign := 0;
if num = 0 then
begin
expon := 0;
hiMant := 0;
loMant := 0;
end
else
begin
fMant := frexp(num, @expon);
if (expon > 16384) or not (fMant < 1) then { Infinity or NaN }
begin
expon := sign or $7FFF;
hiMant := 0;
loMant := 0;
end
else
begin
Inc(expon, 16382);
if expon < 0 then { denormalized }
begin
fMant := ldexp(fMant, expon);
expon := 0;
end;
expon := expon or sign;
fMant := ldexp(fMant, 32);
fsMant := floor(fMant);
hiMant := FloatToUnsigned(fsMant);
fMant := ldexp(fMant - fsMant, 32);
fsMant := floor(fMant);
loMant := FloatToUnsigned(fsMant);
end;
end;
bytes[9] := expon shr 8;
bytes[8] := expon and $FF;
bytes[7] := hiMant shr 24;
bytes[6] := hiMant shr 16;
bytes[5] := hiMant shr 8;
bytes[4] := hiMant;
bytes[3] := loMant shr 24;
bytes[2] := loMant shr 16;
bytes[1] := loMant shr 8;
bytes[0] := loMant;
end;
function ConvertFromIeeeExtended(bytes: PByte): Double;
var
f: Double;
expon: LongInt;
hiMant, loMant: LongWord;
begin
expon := (LongInt(bytes[9] and $7F) shl 8) or bytes[8];
hiMant := (LongWord(bytes[7]) shl 24) or (LongWord(bytes[6]) shl 16) or (LongWord(bytes[5]) shl 8) or bytes[4];
loMant := (LongWord(bytes[3]) shl 24) or (LongWord(bytes[2]) shl 16) or (LongWord(bytes[1]) shl 8) or bytes[0];
if (expon = 0) and (hiMant = 0) and (loMant = 0) then
f := 0.0
else
begin
if expon = $7FFF then { Infinity or NaN }
f := 1.7e308
else
begin
Dec(expon, 16383);
Dec(expon, 31);
f := ldexp(UnsignedToFloat(hiMant), expon);
Dec(expon, 32);
f := f + ldexp(UnsignedToFloat(loMant), expon);
end;
end;
if bytes[9] and $80 <> 0 then
Result := -f
else
Result := f;
end;
end.