summaryrefslogtreecommitdiffstats
path: root/private/mvdm/wow16/win87em/emnormal.asm
blob: 8358f30b646862daee35bdc8988ec4c40876f870 (plain) (blame)
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
	page	,132
	subttl	emnormal.asm - Normalize and Round
;***
;emnormal.asm - Normalize and Round
;
;	Copyright (c) 1986-89, Microsoft Corporation
;
;Purpose:
;	Normalize and Round.
;
;	This Module contains Proprietary Information of Microsoft
;	Corporation and should be treated as Confidential.
;
;Revision History:
;	See emulator.hst
;
;*******************************************************************************


ProfBegin NORMAL

;------------------------------------------;
;					   ;
;   Double Precision Normalize and Round   ;
;					   ;
;------------------------------------------;

	even

pub	NODRQQ
; Normalize the double precision number
; All arithmetic operations exit through NODRQQ or RNDR7Q
; Mantissa in DI:BX:CX:DX:BP, Exponent in SI, sign on Stack, old BP on Stack

	MOV	AL,4		; Maximum of 4 word shifts, sign in AH(6)
pub	DNOR
	OR	DI,DI		; See if any bits on in high word
	JNZ	short HIBYT	; If so, go check high byte for zero
	SUB	SI,16		; Drop exponent by shift count
	DEC	AL		; Bump count
	JZ	short NORZERO
	MOV	DI,BX
	MOV	BX,CX
	MOV	CX,DX
	MOV	DX,BP
	XOR	BP,BP
	JMP	DNOR

pub	NORZERO 	       ; Here if result after normalization is zero
	MOV	esi,offset IEEEzero
	MOV	edi,[RESULT]
	POP	eax	    ; Throw out sign on stack
	POP	ebp	    ; Restore Old BP
	JMP	csMOVRQQ    ; Move IEEE zero to TOS and return

	even

pub	HIBYT
	TEST	DI,0FF00H	; See if high byte is zero
	JNZ	short NORMSHF2
	SUB	SI,8		; Drop exponent by shift amount

pub	DN2
	XCHG	AX,DI
	MOV	AH,AL
	MOV	AL,BH
	MOV	BH,BL
	MOV	BL,CH
	MOV	CH,CL
	MOV	CL,DH
	MOV	DH,DL
	XCHG	AX,DI
	XCHG	AX,BP
	MOV	DL,AH
	MOV	AH,AL
	XOR	AL,AL
	XCHG	AX,BP

	even

pub	NORMSHF2
	TEST	DI,8000H	; Normalization complete?
	JNZ	ROUND		; Normalization complete

pub	NORMLP			; Have to shift left
	DEC	SI		; Account for shift in exponent
	SHL	BP,1
	RCL	DX,1
	RCL	CX,1
	RCL	BX,1
	RCL	DI,1
	TEST	DI,8000H	; Check for normalized result
	JZ	NORMLP		; Bit will be set when normalized
	JMP	short ROUND


pub	ROUND24 		; Round to 24 bits
	OR	DX,BP		; See if result fits exactly in 24 bits
	OR	CX,DX
	OR	CL,CH
	MOV	CH,BL
	OR	CX,CX
	JZ	short JEXACT
	OR	[CURerr],Precision   ; Set flag on inexact result
	SHR	AL,1
	JC	short DOWNorCHOP24
	SHR	AL,1
	JC	short UP24

pub	NEAR24			; Round to nearest or Even
	CMP	CX,8000H
	JA	short INC24	; Remainder Bigger then .5 so Bump up
	JB	short MSK24	; Remainder Less then .5 so Mask off
	TEST	BH,1		; Remainder = .5 so see if even
	JZ	short MSK24	; When even Mask
	JMP	short INC24	; When odd Bump up

pub	UP24
	POP	eax		; Get the sign
	PUSH	eax
	SHL	AH,1
	JC	short MSK24	; Trunc neg numbers to move toward + Inf
	JMP	short INC24

pub	DOWNorCHOP24
	SHR	AL,1
	JC	short CHOP24

pub	DOWN24
	POP	eax		; Get the sign
	PUSH	eax
	SHL	AH,1
	JC	short INC24
	JMP	short MSK24	; Trunc Pos numbers to move toward - Inf

pub	INC24
	ADD	BH,1
	ADC	DI,0
	JNC	short MSK24
	MOV	DI,8000H	; Number overflowed from 1,111... to 10,000...
	INC	SI		; Bump up Exponent

pub	CHOP24
MSK24:				; Mask off insignificant bits
	XOR	BP,BP
	MOV	DX,BP
	MOV	CX,DX
	MOV	BL,CL
pub	JEXACT
	JMP	EXACT


	even

pub	NORMSHF 		; from multiply only
	TEST	DI,8000H	; Normalization complete?
	JNZ	ROUND		; Normalization complete

	DEC	SI		; Account for shift in exponent
	SHL	BP,1
	RCL	DX,1
	RCL	CX,1
	RCL	BX,1
	RCL	DI,1

	even
				; mantissa in DI:BX:CX:DX:BP

pub	ROUND			; Drop into ROUND when normalized
	MOV	AL,[CWcntl]	; Pick up hi byte of control word
	SHR	AL,1		; Which has Rounding & Precision Control
	jnc	short ROUND53

pub	ROUND64 		; Round to 64 bits
	SHR	AL,1		; Remove other bit of Precision control
	OR	BP,BP		; See if result fits exactly in 64 bits
	JZ	short EXACT
	OR	[CURerr],Precision   ; Set flag on inexact result
	SHR	AL,1
	JC	short DOWNorCHOP64
	SHR	AL,1
	JC	short UP64

pub	NEAR64			; Round to nearest or Even
	CMP	BP,8000H
	JA	short INC64	; Remainder Bigger then .5 so Bump up
	JB	short MSK64	; Remainder Less then .5 so Mask off
	TEST	DL,1		; Remainder = .5 so see if even
	JZ	short MSK64	; When even Mask

pub	INC64
	XOR	BP,BP		; Need a 0
	ADD	DX,1
	ADC	CX,BP
	ADC	BX,BP
	ADC	DI,BP
	JNC	short EXACT

	MOV	DI,8000H	; Number overflowed from 1,111... to 10,000...
	INC	SI		; Bump up Exponent

	even

CHOP64:
MSK64:				; Mask off insignificant bits

pub	EXACT
	MOV	eax,[RESULT]
	XCHG	eax,edi
	MOV	MB0[edi],DX	   ; Save Mantissa
	MOV	MB2[edi],CX
	MOV	MB4[edi],BX
	MOV	MB6[edi],AX
	POP	eax		; Fetch Sign
	POP	ebp		; Fetch Old BP
	AND	AH,Sign 	; Mask off single precision
	MOV	Flag[edi],AH

	CMP	SI,IexpMax - IexpBias ; Test for overflow
	JGE	short jOVER
	CMP	SI,IexpMin - IexpBias ; Test for Underflow
	JLE	short UNDER

pub	NORTAG
	MOV	Expon[edi],SI
	MOV	byte ptr Tag[edi],0	  ; Number is in range and on TOS so ret
	RET

jOVER:	jmp	OVER

pub	UP64
	POP	eax		; Get the sign
	PUSH	eax
	SHL	AH,1
	JC	short MSK64	; Trunc neg numbers to move toward + Inf
	JMP	short INC64

pub	DOWNorCHOP64
	SHR	AL,1
	JC	short CHOP64

pub	DOWN64
	POP	eax		; Get the sign
	PUSH	eax
	SHL	AH,1
	JC	short INC64
	JMP	short MSK64	; Trunc Pos numbers to move toward - Inf


jROUND24:
	jmp	ROUND24

pub	ROUND53 		; Round to 53 bits (or 24)
	SHR	AL,1
	JNC	short jROUND24

	XCHG	BP,AX		; See if result fits exactly in 53 bits
	OR	AL,AH
	OR	AL,DL
	MOV	AH,DH
	AND	AH,007H
	AND	DH,0F8H
	XCHG	BP,AX
	OR	BP,BP
	JZ	EXACT
	OR	[CURerr],Precision   ; Set flag on inexact result
	SHR	AL,1
	JC	short DOWNorCHOP53
	SHR	AL,1
	JC	short UP53

pub	NEAR53			; Round to nearest or Even
	CMP	BP,0400H
	JA	short INC53	; Remainder Bigger then .5 so Bump up
	JB	short MSK53	; Remainder Less then .5 so Mask off
	TEST	DH,08H		; Remainder = .5 so see if even
	JZ	short MSK53	; When even Mask
	JMP	short INC53	; When odd Bump up


pub	UNDER
MUNDER: 	; Masked Underflow
	MOV	esi,offset IEEEzero
	CALL	csMOVRQQ
	MOV	Flag[edi],AH	     ; Overstore correct sign
	RET


pub	UP53
	POP	eax		; Get the sign
	PUSH	eax
	SHL	AH,1
	JC	short MSK53	; Trunc neg numbers to move toward + Inf
	JMP	short INC53

pub	DOWNorCHOP53
	SHR	AL,1
	JC	short CHOP53

pub	DOWN53
	POP	eax		; Get the sign
	PUSH	eax
	SHL	AH,1
	JC	short INC53
	JMP	short MSK53	; Trunc Pos numbers to move toward - Inf

pub	INC53
	XOR	BP,BP		; Need a 0
	ADD	DH,08H
	ADC	CX,BP
	ADC	BX,BP
	ADC	DI,BP
	JNC	short MSK53

	MOV	DI,8000H	; Number overflowed from 1,111... to 10,000...
	INC	SI		; Bump up Exponent

pub	CHOP53
MSK53:				; Mask off insignificant bits
	XOR	BP,BP
	XOR	DL,DL		; Note: The garbage in DH was masked off at ROUND53
	JMP	EXACT

PAGE

pub	OVER			    ; Here if number overflowed
MOVER:		; The masked response to rounding depends on whether rounding
		;  is directed or not.	If it is then Overflow flag is not set
		;  but precision is. Also the result is set to Inf or Biggest
		;  If rounding is not directed then Overflow is set and result
		;  is set to Inf.

	OR	[CURerr],Overflow
	MOV	AL,[CWcntl]
	SHR	AL,1
	SHR	AL,1
	SHR	AL,1
	JC	short MOvDNorCHP

	SHR	AL,1
	JC	short MOvUP

MOvNEAR:	; Masked Overflow Near Rounding

pub	SignedInfinity			; Return signed infinity

	MOV	esi,offset IEEEinfinity
	CALL	csMOVRQQ
	MOV	Flag[edi],AH		 ; Overstore the proper sign
	RET

pub	MOvDNorCHP
	SHR	AL,1
	JC	short MOvCHOP

pub	MOvDOWN 			; Masked Overflow Down Rounding
	OR	[CURerr],Precision
	TEST	AH,Sign 		; Positive goes to biggest
	JNZ	short SignedInfinity

MOvCHOP:	; Masked Overflow Chop Rounding
pub	SignedBiggest
	MOV	esi,offset IEEEbiggest
	CALL	csMOVRQQ
	MOV	Flag[edi],AH		 ; Overstore the proper sign
	RET

pub	MOvUP				; Masked Overflow Up Rounding
	OR	[CURerr],Precision
	TEST	AH,Sign 		; Negative goes to biggest
	JZ	short SignedInfinity
	JMP	SignedBiggest

ProfEnd  NORMAL