summaryrefslogtreecommitdiffstats
path: root/EdkModulePkg/Universal/Ebc/Dxe/Ia32/Ia32Math.asm
blob: 1d0bd2f2e608c2250a3a31b655232a5693c93bd4 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
  TITLE   Ia32math.asm: Generic math routines for EBC interpreter running on IA32 processor

;------------------------------------------------------------------------------
;
; Copyright (c) 2006, Intel Corporation                                                         
; All rights reserved. This program and the accompanying materials                          
; are licensed and made available under the terms and conditions of the BSD License         
; which accompanies this distribution.  The full text of the license may be found at        
; http://opensource.org/licenses/bsd-license.php                                            
;                                                                                           
; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             
; 
; Module Name:
;
;   Ia32math.asm
; 
; Abstract:
; 
;   Generic math routines for EBC interpreter running on IA32 processor
;
;------------------------------------------------------------------------------

  .686P
  .XMM
  .MODEL SMALL
  .CODE

LeftShiftU64  PROTO C  Operand:  QWORD, CountIn: QWORD
RightShiftU64 PROTO C  Operand:  QWORD, CountIn: QWORD
ARightShift64 PROTO C  Operand:  QWORD, CountIn: QWORD
MulU64x64     PROTO C  Value1:   QWORD, Value2:  QWORD, ResultHigh: DWORD
MulS64x64     PROTO C  Value1:   QWORD, Value2:  QWORD, ResultHigh: DWORD
DivU64x64     PROTO C  Dividend: QWORD, Divisor: QWORD, Remainder:  DWORD, Error: DWORD
DivS64x64     PROTO C  Dividend: QWORD, Divisor: QWORD, Remainder:  DWORD, Error: DWORD

  
LeftShiftU64 PROC C Operand: QWORD, CountIn: QWORD

;------------------------------------------------------------------------------
; UINT64
; LeftShiftU64 (
;   IN UINT64   Operand,
;   IN UINT64   CountIn
;   )
;
; Routine Description:
;  
;   Left-shift a 64-bit value.
;
; Arguments:
;
;   Operand - the value to shift
;   Count   - shift count
;
; Returns:
;
;   Operand << Count
;------------------------------------------------------------------------------

  push   ecx
  ;
  ; if (CountIn > 63) return 0;
  ;
  cmp    dword ptr CountIn[4], 0
  jne    _LeftShiftU64_Overflow
  mov    ecx, dword ptr CountIn[0]
  cmp    ecx, 63
  jbe    _LeftShiftU64_Calc
  
_LeftShiftU64_Overflow:
  xor    eax, eax
  xor    edx, edx
  jmp    _LeftShiftU64_Done
  
_LeftShiftU64_Calc:
  mov    eax, dword ptr Operand[0]
  mov    edx, dword ptr Operand[4]
  
  shld   edx, eax, cl
  shl    eax, cl
  cmp    ecx, 32
  jc     short _LeftShiftU64_Done
  
  mov    edx, eax
  xor    eax, eax
  
_LeftShiftU64_Done:
  pop    ecx
  ret

LeftShiftU64 ENDP


RightShiftU64 PROC C Operand: QWORD, CountIn: QWORD

;------------------------------------------------------------------------------
; UINT64
; RightShiftU64 (
;   IN UINT64   Operand,
;   IN UINT64   CountIn
;   )
;
; Routine Description:
;  
;   Right-shift an unsigned 64-bit value.
;
; Arguments:
;
;   Operand - the value to shift
;   Count   - shift count
;
; Returns:
;
;   Operand >> Count
;------------------------------------------------------------------------------

  push   ecx
  ;
  ; if (CountIn > 63) return 0;
  ;
  cmp    dword ptr CountIn[4], 0
  jne    _RightShiftU64_Overflow
  mov    ecx, dword ptr CountIn[0]
  cmp    ecx, 63
  jbe    _RightShiftU64_Calc
  
_RightShiftU64_Overflow:
  xor    eax, eax
  xor    edx, edx
  jmp    _RightShiftU64_Done
  
_RightShiftU64_Calc:
  mov    eax, dword ptr Operand[0]
  mov    edx, dword ptr Operand[4]
  
  shrd   eax, edx, cl
  shr    edx, cl
  cmp    ecx, 32
  jc     short _RightShiftU64_Done
  
  mov    eax, edx
  xor    edx, edx
  
_RightShiftU64_Done:
  pop    ecx
  ret

RightShiftU64 ENDP


ARightShift64 PROC C Operand: QWORD, CountIn: QWORD

;------------------------------------------------------------------------------
; INT64
; ARightShift64 (
;   IN INT64  Operand,
;   IN UINT64 CountIn
;   )
;
; Routine Description:
;  
;   Arithmatic shift a 64 bit signed value.
;
; Arguments:
;
;   Operand - the value to shift
;   Count   - shift count
;
; Returns:
;
;  Operand >> Count
;------------------------------------------------------------------------------

  push   ecx
  ;
  ; If they exceeded the max shift count, then return either 0 or all F's
  ; depending on the sign bit.
  ;
  cmp    dword ptr CountIn[4], 0
  jne    _ARightShiftU64_Overflow
  mov    ecx, dword ptr CountIn[0]
  cmp    ecx, 63
  jbe    _ARightShiftU64_Calc
  
_ARightShiftU64_Overflow:
  ;
  ; Check the sign bit of Operand
  ;
  bt     dword ptr Operand[4], 31
  jnc    _ARightShiftU64_Return_Zero
  ;
  ; return -1
  ;
  or     eax, 0FFFFFFFFh
  or     edx, 0FFFFFFFFh
  jmp    _ARightShiftU64_Done

_ARightShiftU64_Return_Zero:
  xor    eax, eax
  xor    edx, edx
  jmp    _ARightShiftU64_Done
  
_ARightShiftU64_Calc:
  mov    eax, dword ptr Operand[0]
  mov    edx, dword ptr Operand[4]

  shrd   eax, edx, cl
  sar    edx, cl
  cmp    ecx, 32
  jc     short _ARightShiftU64_Done

  ;
  ; if ecx >= 32, then eax = edx, and edx = sign bit
  ;
  mov    eax, edx
  sar    edx, 31

_ARightShiftU64_Done:
  pop    ecx
  ret

ARightShift64 ENDP


MulU64x64 PROC C  Value1: QWORD, Value2: QWORD, ResultHigh: DWORD

;------------------------------------------------------------------------------
; UINT64 
; MulU64x64 (
;   UINT64 Value1, 
;   UINT64 Value2, 
;   UINT64 *ResultHigh
;   )
;
; Routine Description:
; 
;   Multiply two unsigned 64-bit values.
;
; Arguments:
;
;   Value1      - first value to multiply
;   Value2      - value to multiply by Value1
;   ResultHigh  - result to flag overflows
;
; Returns:
; 
;   Value1 * Value2
;   The 128-bit result is the concatenation of *ResultHigh and the return value 
;------------------------------------------------------------------------------

  push   ebx
  push   ecx
  mov    ebx, ResultHigh           ; ebx points to the high 4 words of result
  ;
  ; The result consists of four double-words.
  ; Here we assume their names from low to high: dw0, dw1, dw2, dw3
  ;
  mov    eax, dword ptr Value1[0]
  mul    dword ptr Value2[0]
  push   eax                       ; eax contains final result of dw0, push it
  mov    ecx, edx                  ; ecx contains partial result of dw1
  
  mov    eax, dword ptr Value1[4]
  mul    dword ptr Value2[0]
  add    ecx, eax                  ; add eax to partial result of dw1
  adc    edx, 0    
  mov    dword ptr [ebx], edx      ; lower double-word of ResultHigh contains partial result of dw2
    
  mov    eax, dword ptr Value1[0]
  mul    dword ptr Value2[4]
  add    ecx, eax                  ; add eax to partial result of dw1
  push   ecx                      ; ecx contains final result of dw1, push it
  adc    edx, 0
  mov    ecx, edx                  ; ecx contains partial result of dw2, together with ResultHigh

  mov    eax, dword ptr Value1[4]
  mul    dword ptr Value2[4]
  add    ecx, eax                  ; add eax to partial result of dw2
  adc    edx, 0
  add    dword ptr [ebx], ecx      ; lower double-word of ResultHigh contains final result of dw2
  adc    edx, 0
  mov    dword ptr [ebx + 4], edx  ; high double-word of ResultHigh contains final result of dw3
    
  pop    edx                       ; edx contains the final result of dw1
  pop    eax                       ; edx contains the final result of dw0
  pop    ecx
  pop    ebx
  ret

MulU64x64 ENDP


MulS64x64  PROC C  Value1: QWORD, Value2: QWORD, ResultHigh: DWORD

;------------------------------------------------------------------------------
; INT64
; MulS64x64 (
;   INT64 Value1,
;   INT64 Value2,
;   INT64 *ResultHigh
;   )
;
; Routine Description:
;  
;   Multiply two signed 64-bit values.
;
; Arguments:
;
;   Value1      - first value to multiply
;   Value2      - value to multiply by Value1
;   ResultHigh  - result to flag overflows
;
; Returns:
;
;   Value1 * Value2
;   The 128-bit result is the concatenation of *ResultHigh and the return value 
;------------------------------------------------------------------------------

  push   ebx
  push   ecx
  mov    ebx, ResultHigh           ; ebx points to the high 4 words of result
  xor    ecx, ecx                 ; the lowest bit of ecx flags the sign
    
  mov    edx, dword ptr Value1[4]
  bt     edx, 31
  jnc    short _MulS64x64_A_Positive
  ;
  ; a is negative
  ;
  mov    eax, dword ptr Value1[0]
  not    edx
  not    eax
  add    eax, 1
  adc    edx, 0
  mov    dword ptr Value1[0], eax
  mov    dword ptr Value1[4], edx
  btc    ecx, 0
    
_MulS64x64_A_Positive:
  mov    edx, dword ptr Value2[4]
  bt     edx, 31
  jnc    short _MulS64x64_B_Positive
  ;
  ; b is negative
  ;
  mov    eax, dword ptr Value2[0]
  not    edx
  not    eax
  add    eax, 1
  adc    edx, 0
  mov    dword ptr Value2[0], eax
  mov    dword ptr Value2[4], edx
  btc    ecx, 0
    
_MulS64x64_B_Positive:
  invoke MulU64x64, Value1, Value2, ResultHigh
  bt     ecx, 0
  jnc    short _MulS64x64_Done
  ;
  ;negate the result
  ;
  not    eax
  not    edx
  not    dword ptr [ebx]
  not    dword ptr [ebx + 4]
  add    eax, 1
  adc    edx, 0
  adc    dword ptr [ebx], 0
  adc    dword ptr [ebx + 4], 0
    
_MulS64x64_Done:
  pop    ecx
  pop    ebx
  ret

MulS64x64 ENDP


DivU64x64 PROC C  Dividend: QWORD, Divisor: QWORD, Remainder: DWORD, Error: DWORD, 

;------------------------------------------------------------------------------
; UINT64
; DivU64x64 (
;   IN  UINT64   Dividend,
;   IN  UINT64   Divisor,
;   OUT UINT64   *Remainder OPTIONAL,
;   OUT UINT32   *Error
;   )
;
; Routine Description:
; 
;   This routine allows a 64 bit value to be divided with a 64 bit value returns 
;   64bit result and the Remainder
;
; Arguments:
;
;   Dividend    - dividend
;   Divisor     - divisor
;   ResultHigh  - result to flag overflows
;   Error       - flag for error
;
; Returns:
; 
;   Dividend / Divisor
;   Remainder = Dividend mod Divisor
;------------------------------------------------------------------------------

  push   ecx
  
  mov    eax, Error
  mov    dword ptr [eax], 0 
  
  cmp    dword ptr Divisor[0], 0
  jne    _DivU64x64_Valid
  cmp    dword ptr Divisor[4], 0
  jne    _DivU64x64_Valid
  ;
  ; the divisor is zero
  ;
  mov    dword ptr [eax], 1 
  cmp    Remainder, 0
  je     _DivU64x64_Invalid_Return
  ;
  ; fill the remainder if the pointer is not null
  ;
  mov    eax, Remainder
  mov    dword ptr [eax], 0
  mov    dword ptr [eax + 4], 80000000h
  
_DivU64x64_Invalid_Return:
  xor    eax, eax
  mov    edx, 80000000h
  jmp    _DivU64x64_Done

_DivU64x64_Valid:
  ;
  ; let edx and eax contain the intermediate result of remainder
  ;
  xor    edx, edx
  xor    eax, eax
  mov    ecx, 64
  
_DivU64x64_Wend:
  ;
  ; shift dividend left one
  ;
  shl    dword ptr Dividend[0], 1
  rcl    dword ptr Dividend[4], 1    
  ;
  ; rotate intermediate result of remainder left one
  ;
  rcl    eax, 1
  rcl    edx, 1 
  
  cmp    edx, dword ptr Divisor[4]
  ja     _DivU64x64_Sub_Divisor
  jb     _DivU64x64_Cont
  cmp    eax, dword ptr Divisor[0]
  jb     _DivU64x64_Cont
  
_DivU64x64_Sub_Divisor:
  ;
  ; If intermediate result of remainder is larger than
  ; or equal to divisor, then set the lowest bit of dividend,
  ; and subtract divisor from intermediate remainder
  ;
  bts    dword ptr Dividend[0], 0
  sub    eax, dword ptr Divisor[0]
  sbb    edx, dword ptr Divisor[4]
  
_DivU64x64_Cont:
  loop   _DivU64x64_Wend
  
  cmp    Remainder, 0
  je     _DivU64x64_Assign
  mov    ecx, Remainder
  mov    dword ptr [ecx], eax
  mov    dword ptr [ecx + 4], edx
 
_DivU64x64_Assign:
  mov    eax, dword ptr Dividend[0]
  mov    edx, dword ptr Dividend[4]
  
_DivU64x64_Done:
  pop    ecx
  ret
  
DivU64x64 ENDP

DivS64x64 PROC C  Dividend: QWORD, Divisor: QWORD, Remainder: DWORD, Error: DWORD, 

;------------------------------------------------------------------------------
; INT64
; DivU64x64 (
;   IN  INT64   Dividend,
;   IN  INT64   Divisor,
;   OUT UINT64   *Remainder OPTIONAL,
;   OUT UINT32   *Error
;   )
;
; Routine Description:
; 
;   This routine allows a 64 bit signed value to be divided with a 64 bit 
;   signed value returns 64bit result and the Remainder.
;
; Arguments:
;
;   Dividend    - dividend
;   Divisor     - divisor
;   ResultHigh  - result to flag overflows
;   Error       - flag for error
;
; Returns:
; 
;   Dividend / Divisor
;   Remainder = Dividend mod Divisor
;------------------------------------------------------------------------------

  push   ecx
  
  mov    eax, Error
  mov    dword ptr [eax], 0 
  
  cmp    dword ptr Divisor[0], 0
  jne    _DivS64x64_Valid
  cmp    dword ptr Divisor[4], 0
  jne    _DivS64x64_Valid
  ;
  ; the divisor is zero
  ;
  mov    dword ptr [eax], 1 
  cmp    Remainder, 0
  je     _DivS64x64_Invalid_Return
  ;
  ; fill the remainder if the pointer is not null
  ;
  mov    eax, Remainder
  mov    dword ptr [eax], 0
  mov    dword ptr [eax + 4], 80000000h
  
_DivS64x64_Invalid_Return:
  xor    eax, eax
  mov    edx, 80000000h
  jmp    _DivS64x64_Done

_DivS64x64_Valid:
  ;
  ; The lowest bit of ecx flags the sign of quotient,
  ; The seconde lowest bit flags the sign of remainder
  ;
  xor    ecx, ecx                 
  
  mov    edx, dword ptr Dividend[4]
  bt     edx, 31
  jnc    short _DivS64x64_Dividend_Positive
  ;
  ; dividend is negative
  ;
  mov    eax, dword ptr Dividend[0]
  not    edx
  not    eax
  add    eax, 1
  adc    edx, 0
  mov    dword ptr Dividend[0], eax
  mov    dword ptr Dividend[4], edx
  ;
  ; set both the flags for signs of quotient and remainder
  ;
  btc    ecx, 0
  btc    ecx, 1
    
_DivS64x64_Dividend_Positive:
  mov    edx, dword ptr Divisor[4]
  bt     edx, 31
  jnc    short _DivS64x64_Divisor_Positive
  ;
  ; divisor is negative
  ;
  mov    eax, dword ptr Divisor[0]
  not    edx
  not    eax
  add    eax, 1
  adc    edx, 0
  mov    dword ptr Divisor[0], eax
  mov    dword ptr Divisor[4], edx
  ;
  ; just complement the flag for sign of quotient
  ;
  btc    ecx, 0
    
_DivS64x64_Divisor_Positive:
  invoke DivU64x64, Dividend, Divisor, Remainder, Error
  bt     ecx, 0
  jnc    short _DivS64x64_Remainder
  ;
  ; negate the quotient
  ;
  not    eax
  not    edx
  add    eax, 1
  adc    edx, 0
    
_DivS64x64_Remainder:
  bt     ecx, 1
  jnc    short _DivS64x64_Done
  ;
  ; negate the remainder
  ;
  mov    ecx, remainder
  not    dword ptr [ecx]
  not    dword ptr [ecx + 4]
  add    dword ptr [ecx], 1
  adc    dword ptr [ecx + 4], 0
  
_DivS64x64_Done:
  pop    ecx
  ret

DivS64x64 ENDP

END