-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJcl8087.pas
More file actions
339 lines (299 loc) · 11.4 KB
/
Copy pathJcl8087.pas
File metadata and controls
339 lines (299 loc) · 11.4 KB
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
{**************************************************************************************************}
{ }
{ Project JEDI Code Library (JCL) }
{ }
{ The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); }
{ you may not use this file except in compliance with the License. You may obtain a copy of the }
{ License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF }
{ ANY KIND, either express or implied. See the License for the specific language governing rights }
{ and limitations under the License. }
{ }
{ The Original Code is Jcl8087.pas }
{ }
{ The Initial Developer of the Original Code is Marcel van Brakel. }
{ Portions created by Marcel van Brakel are Copyright Marcel van Brakel. All rights reserved. }
{ }
{ Contributor(s): }
{ Marcel van Brakel }
{ ESB Consultancy }
{ Robert Marquardt (marquardt) }
{ Robert Rossmair (rrossmair) }
{ Matthias Thoma (mthoma) }
{ Petr Vones }
{ }
{**************************************************************************************************}
{ }
{ This unit contains various routine for manipulating the math coprocessor. This includes such }
{ things as querying and setting the rounding precision of floating point operations and }
{ retrieving the coprocessor's status word. }
{ }
{**************************************************************************************************}
{ }
{ Last modified: $Date:: $ }
{ Revision: $Rev:: $ }
{ Author: $Author:: $ }
{ }
{**************************************************************************************************}
unit Jcl8087;
{$I jcl.inc}
interface
{$IFDEF UNITVERSIONING}
uses
JclUnitVersioning;
{$ENDIF UNITVERSIONING}
type
T8087Precision = (pcSingle, pcReserved, pcDouble, pcExtended);
T8087Rounding = (rcNearestOrEven, rcDownInfinity, rcUpInfinity, rcChopOrTruncate);
T8087Infinity = (icProjective, icAffine);
T8087Exception = (emInvalidOp, emDenormalizedOperand, emZeroDivide, emOverflow,
emUnderflow, emPrecision);
T8087Exceptions = set of T8087Exception;
const
All8087Exceptions = [Low(T8087Exception)..High(T8087Exception)];
function Get8087ControlWord: Word;
function Get8087Infinity: T8087Infinity;
function Get8087Precision: T8087Precision;
function Get8087Rounding: T8087Rounding;
function Get8087StatusWord(ClearExceptions: Boolean): Word;
function Set8087Infinity(const Infinity: T8087Infinity): T8087Infinity;
function Set8087Precision(const Precision: T8087Precision): T8087Precision;
function Set8087Rounding(const Rounding: T8087Rounding): T8087Rounding;
function Set8087ControlWord(const Control: Word): Word;
function ClearPending8087Exceptions: T8087Exceptions;
function GetPending8087Exceptions: T8087Exceptions;
function GetMasked8087Exceptions: T8087Exceptions;
function SetMasked8087Exceptions(Exceptions: T8087Exceptions; ClearBefore: Boolean = True): T8087Exceptions;
function Mask8087Exceptions(Exceptions: T8087Exceptions): T8087Exceptions;
function Unmask8087Exceptions(Exceptions: T8087Exceptions; ClearBefore: Boolean = True): T8087Exceptions;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$URL$';
Revision: '$Revision$';
Date: '$Date$';
LogPath: 'JCL\source\common';
Extra: '';
Data: nil
);
{$ENDIF UNITVERSIONING}
implementation
const
X87ExceptBits = $3F;
function Get8087ControlWord: Word;
asm
FSTCW Result
FWAIT
end;
function Get8087Infinity: T8087Infinity;
begin
Result := T8087Infinity((Get8087ControlWord and $1000) shr 12);
end;
function Get8087Precision: T8087Precision;
begin
Result := T8087Precision((Get8087ControlWord and $0300) shr 8);
end;
function Get8087Rounding: T8087Rounding;
begin
Result := T8087Rounding((Get8087ControlWord and $0C00) shr 10);
end;
{$IFDEF CPU64}
function Get8087StatusWord(ClearExceptions: Boolean): Word;
asm
TEST ClearExceptions, ClearExceptions
JZ @@Without
FSTSW Result // get status word (clears exceptions)
JMP @@Leave
@@Without:
FNSTSW Result // get status word (without clearing exceptions)
@@Leave:
end;
{$ELSE}
function Get8087StatusWord(ClearExceptions: Boolean): Word;
begin
if ClearExceptions then
asm
FSTSW Result // get status word (clears exceptions)
end
else
asm
FNSTSW Result // get status word (without clearing exceptions)
end;
end;
{$ENDIF CPU64}
function Set8087Infinity(const Infinity: T8087Infinity): T8087Infinity;
var
CW: Word;
begin
CW := Get8087ControlWord;
Result := T8087Infinity((CW and $1000) shr 12);
Set8087ControlWord((CW and $EFFF) or (Word(Infinity) shl 12));
end;
function Set8087Precision(const Precision: T8087Precision): T8087Precision;
var
CW: Word;
begin
CW := Get8087ControlWord;
Result := T8087Precision((CW and $0300) shr 8);
Set8087ControlWord((CW and $FCFF) or (Word(Precision) shl 8));
end;
function Set8087Rounding(const Rounding: T8087Rounding): T8087Rounding;
var
CW: Word;
begin
CW := Get8087ControlWord;
Result := T8087Rounding((CW and $0C00) shr 10);
Set8087ControlWord((CW and $F3FF) or (Word(Rounding) shl 10));
end;
function Set8087ControlWord(const Control: Word): Word;
var
StackControl: Word;
asm
MOV StackControl, Control
FNCLEX
FSTCW Result // save the old control word
FLDCW StackControl // load the new control word
end;
function ClearPending8087Exceptions: T8087Exceptions;
function GetSW: Word;
asm
FNSTSW Result
AND Result, X87ExceptBits
FNCLEX
end;
var
SW: Word;
begin
SW := GetSW;
Result := [];
if (SW and $01) <> 0 then
Include(Result, emInvalidOp);
if (SW and $02) <> 0 then
Include(Result, emDenormalizedOperand);
if (SW and $04) <> 0 then
Include(Result, emZeroDivide);
if (SW and $08) <> 0 then
Include(Result, emOverflow);
if (SW and $10) <> 0 then
Include(Result, emUnderflow);
if (SW and $20) <> 0 then
Include(Result, emPrecision);
end;
function GetPending8087Exceptions: T8087Exceptions;
function GetSW: Word;
asm
FNSTSW Result
AND Result, X87ExceptBits
end;
var
SW: Word;
begin
SW := GetSW;
Result := [];
if (SW and $01) <> 0 then
Include(Result, emInvalidOp);
if (SW and $02) <> 0 then
Include(Result, emDenormalizedOperand);
if (SW and $04) <> 0 then
Include(Result, emZeroDivide);
if (SW and $08) <> 0 then
Include(Result, emOverflow);
if (SW and $10) <> 0 then
Include(Result, emUnderflow);
if (SW and $20) <> 0 then
Include(Result, emPrecision);
end;
function GetMasked8087Exceptions: T8087Exceptions;
function GetCW: Word;
asm
FSTCW Result
AND Result, X87ExceptBits
end;
var
CW: Word;
begin
CW := GetCW;
Result := [];
if (CW and $01) <> 0 then
Include(Result, emInvalidOp);
if (CW and $02) <> 0 then
Include(Result, emDenormalizedOperand);
if (CW and $04) <> 0 then
Include(Result, emZeroDivide);
if (CW and $08) <> 0 then
Include(Result, emOverflow);
if (CW and $10) <> 0 then
Include(Result, emUnderflow);
if (CW and $20) <> 0 then
Include(Result, emPrecision);
end;
function SetMasked8087Exceptions(Exceptions: T8087Exceptions; ClearBefore: Boolean): T8087Exceptions;
function ClearPendingExceptions: Word;
asm
FNCLEX // clear pending exceptions
end;
function SetCW(NewCW: Word): Word;
var
StackNewCW: Word;
asm
FSTCW Result
FWAIT
MOV StackNewCW, NewCW
MOV AX, Result
AND AX, NOT X87ExceptBits // mask exception mask bits 0..5
OR StackNewCW, AX
FLDCW StackNewCW
end;
var
OldCW, NewCW: Word;
begin
if ClearBefore then
ClearPendingExceptions;
NewCW := 0;
if emInvalidOp in Exceptions then
NewCW := NewCW or $01;
if emDenormalizedOperand in Exceptions then
NewCW := NewCW or $02;
if emZeroDivide in Exceptions then
NewCW := NewCW or $04;
if emOverflow in Exceptions then
NewCW := NewCW or $08;
if emUnderflow in Exceptions then
NewCW := NewCW or $10;
if emPrecision in Exceptions then
NewCW := NewCW or $20;
OldCW := SetCW(NewCW);
Result := [];
if (OldCW and $01) <> 0 then
Include(Result, emInvalidOp);
if (OldCW and $02) <> 0 then
Include(Result, emDenormalizedOperand);
if (OldCW and $04) <> 0 then
Include(Result, emZeroDivide);
if (OldCW and $08) <> 0 then
Include(Result, emOverflow);
if (OldCW and $10) <> 0 then
Include(Result, emUnderflow);
if (OldCW and $20) <> 0 then
Include(Result, emPrecision);
end;
function Mask8087Exceptions(Exceptions: T8087Exceptions): T8087Exceptions;
begin
Result := GetMasked8087Exceptions;
Exceptions := Exceptions + Result;
SetMasked8087Exceptions(Exceptions, False);
end;
function Unmask8087Exceptions(Exceptions: T8087Exceptions; ClearBefore: Boolean): T8087Exceptions;
begin
Result := GetMasked8087Exceptions;
Exceptions := Result - Exceptions;
SetMasked8087Exceptions(Exceptions, ClearBefore);
end;
{$IFDEF UNITVERSIONING}
initialization
RegisterUnitVersion(HInstance, UnitVersioning);
finalization
UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}
end.