summaryrefslogtreecommitdiffstats
path: root/private/ntos/rtl/mips/largeint.s
blob: d051d2fc7f6200ccb0c8921c7225be0778ab2549 (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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
//      TITLE("Large Integer Arithmetic")
//++
//
// Copyright (c) 1990  Microsoft Corporation
//
// Module Name:
//
//    largeint.s
//
// Abstract:
//
//    This module implements routines for performing extended integer
//    arithmtic.
//
// Author:
//
//    David N. Cutler (davec) 18-Apr-1990
//
// Environment:
//
//    Any mode.
//
// Revision History:
//
//--

#include "ksmips.h"

        SBTTL("Large Integer Add")
//++
//
// LARGE_INTEGER
// RtlLargeIntegerAdd (
//    IN LARGE_INTEGER Addend1,
//    IN LARGE_INTEGER Addend2
//    )
//
// Routine Description:
//
//    This function adds a signed large integer to a signed large integer and
//    returns the signed large integer result.
//
// Arguments:
//
//    Addend1 (a2, a3) - Supplies the first addend value.
//
//    Addend2 (4 * 4(sp), 4 * 5(sp)) - Supplies the second addend value.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlLargeIntegerAdd)

        lw      t0,4 * 4(sp)            // get low part of addend2 value
        lw      t1,4 * 5(sp)            // get high part of addend2 value
        addu    t0,t0,a2                // add low parts of large integer
        addu    t1,t1,a3                // add high parts of large integer
        sltu    t2,t0,a2                // generate carry from low part
        addu    t1,t1,t2                // add carry to high part
        sw      t0,0(a0)                // store low part of result
        sw      t1,4(a0)                // store high part of result
        move    v0,a0                   // set function return register
        j       ra                      // return

        .end    RtlLargeIntegerAdd

        SBTTL("Convert Long to Large Integer")
//++
//
// LARGE_INTEGER
// RtlConvertLongToLargeInteger (
//     IN LONG SignedInteger
//     )
//
// Routine Description:
//
//     This function converts the a signed integer to a signed large integer
//     and returns the result.
//
// Arguments:
//
//     SignedInteger (a1) - Supplies the value to convert.
//
// Return Value:
//
//     The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlConvertLongToLargeInteger)

        sra     a2,a1,31                // compute high part of result
        sw      a1,0(a0)                // store low part of result
        sw      a2,4(a0)                // store high part of result
        move    v0,a0                   // set function return register
        j       ra                      // return

        .end    RtlConvertLongToLargeInteger

        SBTTL("Convert Ulong to Large Integer")
//++
//
// LARGE_INTEGER
// RtlConvertUlongToLargeInteger (
//     IN LONG UnsignedInteger
//     )
//
// Routine Description:
//
//     This function converts the an unsigned integer to a signed large
//     integer and returns the result.
//
// Arguments:
//
//     UnsignedInteger (a1) - Supplies the value to convert.
//
// Return Value:
//
//     The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlConvertUlongToLargeInteger)

        sw      a1,0(a0)                // store low part of result
        sw      zero,4(a0)              // store high part of result
        move    v0,a0                   // set function return register
        j       ra                      // return

        .end    RtlConvertUlongToLargeInteger

        SBTTL("Enlarged Signed Integer Multiply")
//++
//
// LARGE_INTEGER
// RtlEnlargedIntegerMultiply (
//    IN LONG Multiplicand,
//    IN LONG Multiplier
//    )
//
// Routine Description:
//
//    This function multiplies a signed integer by an signed integer and
//    returns a signed large integer result.
//
// Arguments:
//
//    Multiplicand (a1) - Supplies the multiplicand value.
//
//    Multiplier (a2) - Supplies the multiplier value.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlEnlargedIntegerMultiply)

        mult    a1,a2                  // multiply longword value
        mflo    t0                     // get low 32-bits of result
        mfhi    t1                     // get high 32-bits of result
        sw      t0,0(a0)               // set low part of result
        sw      t1,4(a0)               // set high part of result
        move    v0,a0                  // set function return register
        j       ra                     // return

        .end    RtlEnlargedIntegerMultiply)

        SBTTL("Enlarged Unsigned Integer Multiply")
//++
//
// LARGE_INTEGER
// RtlEnlargedUnsignedMultiply (
//    IN ULONG Multiplicand,
//    IN ULONG Multiplier
//    )
//
// Routine Description:
//
//    This function multiplies an unsigned integer by an unsigned integer
//    and returns a signed large integer result.
//
// Arguments:
//
//    Multiplicand (a1) - Supplies the multiplicand value.
//
//    Multiplier (a2) - Supplies the multiplier value.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlEnlargedUnsignedMultiply)

        multu   a1,a2                  // multiply longword value
        mflo    t0                     // get low 32-bits of result
        mfhi    t1                     // get high 32-bits of result
        sw      t0,0(a0)               // set low part of result
        sw      t1,4(a0)               // set high part of result
        move    v0,a0                  // set function return register
        j       ra                     // return

        .end    RtlEnlargedUnsignedMultiply)

        SBTTL("Enlarged Unsigned Divide")
//++
//
// ULONG
// RtlEnlargedUnsignedDivide (
//    IN ULARGE_INTEGER Dividend,
//    IN ULONG Divisor,
//    IN PULONG Remainder.
//    )
//
// Routine Description:
//
//    This function divides an unsigned large integer by an unsigned long
//    and returns the resultant quotient and optionally the remainder.
//
//    N.B. It is assumed that no overflow will occur.
//
// Arguments:
//
//    Dividend (a0, a1) - Supplies the dividend value.
//
//    Divisor (a2) - Supplies the divisor value.
//
//    Remainder (a3) - Supplies an optional pointer to a variable that
//        receives the remainder.
//
// Return Value:
//
//    The unsigned long integer quotient is returned as the function value.
//
//--

        LEAF_ENTRY(RtlEnlargedUnsignedDivide)

        sltu    v1,a1,a2                // check if overflow will occur
        beq     zero,a2,20f             // if eq, attempted division by zero
        dsll    a1,a1,32                // left justify hihg part of dividend
        beq     zero,v1,30f             // if eq, overflow will occur
        dsll    a0,a0,32                // zero extend low part of dividend
        dsrl    a0,a0,32                //
        or      a0,a0,a1                // merge high and low part of dividend
        ddivu   a0,a2                   // compute quotient value
        mflo    v0                      // set quotient value
        beq     zero,a3,10f             // if eq, remainder not requested
        mfhi    a0                      // load remainder
        sw      a0,0(a3)                // store longword remainder
10:     j       ra                      // return


20:     break   DIVIDE_BY_ZERO_BREAKPOINT // attempted division by zero
        j       ra                      //

30:     break   DIVIDE_OVERFLOW_BREAKPOINT // division value overflows result
        j       ra                      //

        .end    RtlEnlargedUnsignedDivide

        SBTTL("Extended Large Integer Divide")
//++
//
// LARGE_INTEGER
// RtlExtendedLargeIntegerDivide (
//    IN LARGE_INTEGER Dividend,
//    IN ULONG Divisor,
//    IN PULONG Remainder.
//    )
//
// Routine Description:
//
//    This function divides an unsigned large integer by an unsigned long
//    and returns the resultant quotient and optionally the remainder.
//
// Arguments:
//
//    Dividend (a2, a3) - Supplies the dividend value.
//
//    Divisor (4 * 4(sp)) - Supplies the divisor value.
//
//    Remainder (4 * 5(sp)- Supplies an optional pointer to a variable
//      that receives the remainder.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlExtendedLargeIntegerDivide)

        .set    noreorder
        .set    noat
        move    v0,a0                   // set function return register
        lw      a1,4 * 4(sp)            // get divisor value
        lw      t0,4 * 5(sp)            // get address to store remainder
        beq     zero,a1,30f             // if eq, attempted division by zero
        li      t1,63                   // set loop count
        move    t2,zero                 // clear partial remainder
10:     sra     t3,t2,31                // replicate partial remainder high bit
        sll     t2,t2,1                 // shift next dividend bit
        srl     t4,a3,31                // into the partial remainder
        or      t2,t2,t4                //
        sll     a3,a3,1                 // double left shift dividend
        srl     t4,a2,31                //
        or      a3,a3,t4                //
        sltu    t4,t2,a1                // check if partial remainder less
        subu    t4,t4,1                 // convert to 0 or -1
        or      t4,t4,t3                // merge with partial remainder high bit
        and     t5,t4,a1                // select divisor or 0
        sll     a2,a2,1                 //
        subu    a2,a2,t4                // merge quotient bit
        subu    t2,t2,t5                // subtract out divisor
        bne     zero,t1,10b             // if ne, more iterations to go
        subu    t1,t1,1                 // decrement iteration count
        beq     zero,t0,20f             // if eq, remainder not requested
        sw      a2,0(a0)                // store low part of quotient
        sw      t2,0(t0)                // store longword remainder
20:     j       ra                      // return
        sw      a3,4(a0)                // store high part of quotient
        .set    at
        .set    reorder

30:     break   DIVIDE_BY_ZERO_BREAKPOINT // attempted division by zero
        j       ra                      //

        .end    RtlExtendedLargeIntegerDivide

        SBTTL("Extended Magic Divide")
//++
//
// LARGE_INTEGER
// RtlExtendedMagicDivide (
//    IN LARGE_INTEGER Dividend,
//    IN LARGE_INTEGER MagicDivisor,
//    IN CCHAR ShiftCount
//    )
//
// Routine Description:
//
//    This function divides a signed large integer by an unsigned large integer
//    and returns the signed large integer result. The division is performed
//    using reciprocal multiplication of a signed large integer value by an
//    unsigned large integer fraction which represents the most significant
//    64-bits of the reciprocal divisor rounded up in its least significant bit
//    and normalized with respect to bit 63. A shift count is also provided
//    which is used to truncate the fractional bits from the result value.
//
// Arguments:
//
//    Dividend (a2, a3) - Supplies the dividend value.
//
//    MagicDivisor (4 * 4(sp), 4 * 5(sp)) - Supplies the magic divisor value
//       which is a 64-bit multiplicative reciprocal.
//
//    Shiftcount (4 * 6(sp)) - Supplies the right shift adjustment value.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlExtendedMagicDivide)

        move    t0,a2                   // assume dividend is positive
        move    t1,a3                   //
        bgez    a3,10f                  // if gez, positive dividend
        subu    t0,zero,t0              // negate low part of dividend
        subu    t1,zero,t1              // negate high part of dividend
        sltu    t2,zero,t0              // set borrow from high part
        subu    t1,t1,t2                // subtract out out borrow
10:     lw      a1,4 * 4(sp)            // get low part of magic dividor
        lw      t2,4 * 5(sp)            // get high part of magic divisor
        lbu     v0,4 * 6(sp)            // get shift count

//
// Compute low 32-bits of dividend times low 32-bits of divisor.
//

        multu   t0,a1                   //
        mfhi    t3                      // save high 32-bits of product

//
// Compute low 32-bits of dividend time high 32-bits of divisor.
//

        multu   t0,t2                   //
        mflo    t4                      // save low 32-bits of product
        mfhi    t5                      // save high 32-bits of product

//
// Compute high 32-bits of dividend times low 32-bits of divisor.
//

        multu   t1,a1                   //
        mflo    t6                      // save loow 32-bits of product
        mfhi    t7                      // save high 32-bits of product

//
// Compute high 32-bits of dividend times high 32-bits of divisor.
//

        multu   t1,t2                   //
        mflo    t8                      // save low 32-bits of product
        mfhi    t9                      // save high 32-bits of product

//
// Add partial results to form high 64-bits of result.
//

        addu    t0,t3,t4                //
        sltu    t1,t0,t4                // generate carry
        addu    t0,t0,t6                //
        sltu    t2,t0,t6                // generate carry
        addu    t2,t1,t2                // combine carries
        addu    t1,t2,t5                //
        sltu    t2,t1,t5                // generate carry
        addu    t1,t1,t7                //
        sltu    t3,t1,t7                // generate carry
        addu    t2,t2,t3                // combine carries
        addu    t1,t1,t8                //
        sltu    t3,t1,t8                // generate carry
        addu    t2,t2,t3                // combine carries
        addu    t2,t2,t9                //

//
// Right shift the result by the specified shift count and negate result
// if necessary.
//

        li      v1,32                   // compute left shift count
        subu    v1,v1,v0                //
        bgtz    v1,20f                  // if gtz, shift less that 32-bits

//
// Shift count is greater than or equal 32 bits - high half of result is zero,
// low half is the high half shifted right by remaining count.
//

        move    t1,zero                 // set high half of result
        srl     t0,t2,v0                // set low half of result
        b       30f                     //

//
// Shift count is less than 32-bits - high half of result is the high half
// of product shifted right by count, low half of result is the shifted out
// bits of the high half combined with the rigth shifted low half of the
// product.
//

20:     srl     t0,t1,v0                // shift low half right count bits
        srl     t1,t2,v0                // shift high half right count bits
        beq     zero,v0,30f             // if eq, no more shifts necessary
        sll     t2,t2,v1                // isolate shifted out bits of high half
        or      t0,t0,t2                // combine bits for low half of result

//
// Negate result if neccessary.
//

30:     bgez    a3,40f                  // if gez, positive result
        subu    t0,zero,t0              // negate low half of result
        subu    t1,zero,t1              // negate high half of result
        beq     zero,t0,40f             // if eq, negation complete
        subu    t1,t1,1                 // convert high part to ones complement
40:     sw      t0,0(a0)                // store low half of result
        sw      t1,4(a0)                // store high half of result
        move    v0,a0                   // set function return register
        j       ra                      // return

        .end    RtlExtendedMagicDivide

        SBTTL("Large Integer Divide")
//++
//
// LARGE_INTEGER
// RtlLargeIntegerDivide (
//    IN LARGE_INTEGER Dividend,
//    IN LARGE_INTEGER Divisor,
//    IN PLARGE_INTEGER Remainder.
//    )
//
// Routine Description:
//
//    This function divides an unsigned large integer by an unsigned
//    large and returns the resultant quotient and optionally the remainder.
//
// Arguments:
//
//    Dividend (a2, a3) - Supplies the dividend value.
//
//    Divisor (4 * 4(sp), 4 * 5(sp)) - Supplies the divisor value.
//
//    Remainder (4 * 6(sp)- Supplies an optional pointer to a variable
//      that receives the remainder.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlLargeIntegerDivide)

        .set    noreorder
        .set    noat
        move    v0,a0                   // set function return register
        lw      a1,4 * 4(sp)            // get low part of divisor
        lw      t0,4 * 5(sp)            // get high part of divisor
        lw      t1,4 * 6(sp)            // get address to store remainder
        or      v1,t0,a1                // combine low and high parts
        beq     zero,v1,60f             // if eq, attempted division by zero
        li      t2,63                   // set loop count
        move    t3,zero                 // clear partial remainder
        move    t4,zero                 //
10:     sll     t4,t4,1                 // shift next dividend bit
        srl     t5,t3,31                // into the partial remainder
        or      t4,t4,t5                //
        sll     t3,t3,1                 //
        srl     t5,a3,31                //
        or      t3,t3,t5                //
        sll     a3,a3,1                 // double left shift dividend
        srl     t5,a2,31                //
        or      a3,a3,t5                //
        sltu    t5,t4,t0                // check if partial remainder less
        beq     zero,t5,20f             // if eq, partial remainder not less
        sll     a2,a2,1                 //
        bne     zero,t2,10b             // if ne, more iterations to go
        subu    t2,t2,1                 // decrement iteration count
        beq     zero,t1,50f             // if eq, remainder not requested
        sw      a2,0(a0)                // store low part of quotient
        sw      t3,0(t1)                // store large integer remainder
        sw      t4,4(t1)                //
        j       ra                      // return
        sw      a3,4(a0)                // store high part of quotient

20:     bne     t0,t4,30f               // if ne, partial remainder greater
        sltu    t5,t3,a1                // check is partial remainder less
        bne     zero,t5,40f             // if ne, partial remainder less
        nop                             //
30:     or      a2,a2,1                 // merge quotient bit
        subu    t4,t4,t0                // subtract out divisor high part
        sltu    t5,t3,a1                // set borrow from high part
        subu    t4,t4,t5                // subtract borrow from high part
        subu    t3,t3,a1                // subtract out divisor low
40:     bne     zero,t2,10b             // if ne, more iterations to go
        subu    t2,t2,1                 // decrement iteration count
        beq     zero,t1,50f             // if eq, remainder not requested
        sw      a2,0(a0)                // store low part of quotient
        sw      t3,0(t1)                // store large integer remainder
        sw      t4,4(t1)                //
50:     j       ra                      // return
        sw      a3,4(a0)                // store high part of quotient
        .set    at
        .set    reorder

60:     break   DIVIDE_BY_ZERO_BREAKPOINT // attempted division by zero
        j       ra                      //

        .end    RtlLargeIntegerDivide

        SBTTL("Extended Integer Multiply")
//++
//
// LARGE_INTEGER
// RtlExtendedIntegerMultiply (
//    IN LARGE_INTEGER Multiplicand,
//    IN LONG Multiplier
//    )
//
// Routine Description:
//
//    This function multiplies a signed large integer by a signed integer and
//    returns the signed large integer result.
//
// Arguments:
//
//    Multiplicand (a2, a3) - Supplies the multiplicand value.
//
//    Multiplier (4 * 4(sp)) - Supplies the multiplier value.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlExtendedIntegerMultiply)

        lw      a1,4 * 4(sp)            // get multiplier value
        xor     t9,a1,a3                // compute sign of result
        move    t0,a1                   // assume multiplier positive
        bgez    a1,10f                  // if gez, positive multiplier
        subu    t0,zero,t0              // negate multiplier
10:     move    t1,a2                   // assume multiplicand positive
        move    t2,a3                   //
        bgez    a3,20f                  // if gez, positive multiplicand
        subu    t1,zero,t1              // negate multiplicand
        subu    t2,zero,t2              //
        sltu    t3,zero,t1              // compute borrow from high part
        subu    t2,t2,t3                // subtract out borrow

//
// Compute low 32-bits of multiplier times the low 32-bit of multiplicand.
//

20:     multu   t0,t1                   //
        mflo    t4                      // save low 32-bits of product
        mfhi    t5                      // save high 32-bits of product

//
// Compute low 32-bits of multiplier times the high 32-bits of multiplicand.
//

        multu   t0,t2                   //
        mflo    t6                      // save low 32-bits of product
        mfhi    t7                      // save high 32-bits of product

//
// Add partial results to form high 64-bits of result.
//

        addu    t5,t5,t6                //
        sltu    t3,t5,t6                // generate carry
        addu    t6,t3,t7                //

//
// Negate result if neccessary.
//

        bgez    t9,40f                  // if gez, positive result
        subu    t4,zero,t4              // negate low half of result
        subu    t5,zero,t5              // negate high half of result
        subu    t6,zero,t6              // negate extended part of result
        beq     zero,t4,30f             // if eq, negation complete
        subu    t5,t5,1                 // convert high part to ones complement
        subu    t6,t6,1                 // convert extended part to ones complement
        b       40f                     //

30:     beq     zero,t5,40f             // if eq, negation complete
        subu    t6,t6,1                 // convert extended part to ones complement

//
// Store final result.
//

40:     sw      t4,0(a0)                // store low half of result
        sw      t5,4(a0)                // store high half of result
        move    v0,a0                   // set function return register
        j       ra                      // return

        .end    RtlExtendedIntegerMultiply

        SBTTL("Large Integer Negate")
//++
//
// LARGE_INTEGER
// RtlLargeIntegerNegate (
//    IN LARGE_INTEGER Subtrahend
//    )
//
// Routine Description:
//
//    This function negates a signed large integer and returns the signed
//    large integer result.
//
// Arguments:
//
//    Subtrahend (a2, a3) - Supplies the subtrahend value.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlLargeIntegerNegate)

        subu    a2,zero,a2              // negate low part of subtrahend
        subu    a3,zero,a3              // negate high part of subtrahend
        sltu    t0,zero,a2              // compute borrow from high part
        subu    a3,a3,t0                // subtract borrow from high part
        sw      a2,0(a0)                // store low part of result
        sw      a3,4(a0)                // store high part of result
        move    v0,a0                   // set function return register
        j       ra                      // return

        .end    RtlLargeIntegerNegate

        SBTTL("Large Integer Subtract")
//++
//
// LARGE_INTEGER
// RtlLargeIntegerSubtract (
//    IN LARGE_INTEGER Minuend,
//    IN LARGE_INTEGER Subtrahend
//    )
//
// Routine Description:
//
//    This function subtracts a signed large integer from a signed large
//    integer and returns the signed large integer result.
//
// Arguments:
//
//    Minuend (a2, a3) - Supplies the minuend value.
//
//    Subtrahend (4 * 4(sp), 4 * 5(sp)) - Supplies the subtrahend value.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlLargeIntegerSubtract)

        lw      a1,4 * 4(sp)            // get low part of subtrahend
        lw      t2,4 * 5(sp)            // get high part of subtrahend
        subu    t0,a2,a1                // subtract low parts
        subu    t1,a3,t2                // subtract high parts
        sltu    t3,a2,a1                // generate borrow from high part
        subu    t1,t1,t3                // subtract borrow
        sw      t0,0(a0)                // store low part of result
        sw      t1,4(a0)                // store high part of result
        move    v0,a0                   // set function return register
        j       ra                      // return

        .end    RtlLargeIntegerSubtract

        SBTTL("Large Integer Shift Left")
//++
//
// LARGE_INTEGER
// RtlLargeIntegerShiftLeft (
//    IN LARGE_INTEGER LargeInteger,
//    IN CCHAR ShiftCount
//    )
//
// Routine Description:
//
//    This function shifts a signed large integer left by an unsigned
//    integer modulo 64 and returns the shifted signed large integer
//    result.
//
//    N.B. No test is made for significant bits shifted out of the result.
//
// Arguments:
//
//    LargeInteger (a2, a3) - Supplies the large integer to be shifted.
//
//    ShiftCount (4 * 4(sp)) - Supplies the left shift count.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlLargeIntegerShiftLeft)

        lbu     a1,4 * 4(sp)            // get shift count
        move    v0,a0                   // set function return register
        and     a1,a1,0x3f              // truncate shift count mod 64

//
// Left shift the operand by the specified shift count.
//

        li      v1,32                   // compute right shift count
        subu    v1,v1,a1                //
        bgtz    v1,10f                  // if gtz, shift less that 32-bits

//
// Shift count is greater than or equal 32 bits - low half of result is zero,
// high half is the low half shifted left by remaining count.
//

        sll     a3,a2,a1                // set high half of result
        sw      zero,0(a0)              // store low part of reuslt
        sw      a3,4(a0)                // store high part of result
        j       ra                      // return

//
// Shift count is less than 32-bits - high half of result is the high half
// of operand shifted left by count combined with the low half of the operand
// shifted right, low half of result is the low half shifted left.
//

10:     sll     a3,a3,a1                // shift high half left count bits
        beq     zero,a1,20f             // if eq, no more shifts necessary
        srl     t0,a2,v1                // isolate shifted out bits of low half
        sll     a2,a2,a1                // shift low half left count bits
        or      a3,a3,t0                // combine bits for high half of result
20:     sw      a2,0(a0)                // store low part of reuslt
        sw      a3,4(a0)                // store high part of result
        j       ra                      // return

        .end    RtlLargeIntegerShiftLeft

        SBTTL("Large Integer Logical Shift Right")
//++
//
// LARGE_INTEGER
// RtlLargeIntegerShiftRight (
//    IN LARGE_INTEGER LargeInteger,
//    IN CCHAR ShiftCount
//    )
//
// Routine Description:
//
//    This function shifts an unsigned large integer right by an unsigned
//    integer modulo 64 and returns the shifted unsigned large integer
//    result.
//
// Arguments:
//
//    LargeInteger (a2, a3) - Supplies the large integer to be shifted.
//
//    ShiftCount (4 * 4(sp)) - Supplies the right shift count.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlLargeIntegerShiftRight)

        lbu     a1,4 * 4(sp)            // get shift count
        move    v0,a0                   // set function return register
        and     a1,a1,0x3f              // truncate shift count mod 64

//
// Right shift the operand by the specified shift count.
//

        li      v1,32                   // compute left shift count
        subu    v1,v1,a1                //
        bgtz    v1,10f                  // if gtz, shift less that 32-bits

//
// Shift count is greater than or equal 32 bits - high half of result is
// zero, low half is the high half shifted right by remaining count.
//

        srl     a2,a3,a1                // set low half of result
        sw      a2,0(a0)                // store low part of reuslt
        sw      zero,4(a0)              // store high part of result
        j       ra                      // return

//
// Shift count is less than 32-bits - high half of result is the high half
// of operand shifted right by count, low half of result is the shifted out
// bits of the high half combined with the right shifted low half of the
// operand.
//

10:     srl     a2,a2,a1                // shift low half right count bits
        beq     zero,a1,20f             // if eq, no more shifts necessary
        sll     t0,a3,v1                // isolate shifted out bits of high half
        srl     a3,a3,a1                // shift high half right count bits
        or      a2,a2,t0                // combine bits for low half of result
20:     sw      a2,0(a0)                // store low part of reuslt
        sw      a3,4(a0)                // store high part of result
        j       ra                      // return

        .end    RtlLargeIntegerShiftRight

        SBTTL("Large Integer Arithmetic Shift Right")
//++
//
// LARGE_INTEGER
// RtlLargeIntegerArithmeticShift (
//    IN LARGE_INTEGER LargeInteger,
//    IN CCHAR ShiftCount
//    )
//
// Routine Description:
//
//    This function shifts a signed large integer right by an unsigned
//    integer modulo 64 and returns the shifted signed large integer
//    result.
//
// Arguments:
//
//    LargeInteger (a1, a2) - Supplies the large integer to be shifted.
//
//    ShiftCount (a3) - Supplies the right shift count.
//
// Return Value:
//
//    The large integer result is stored at the address supplied by a0.
//
//--

        LEAF_ENTRY(RtlLargeIntegerArithmeticShift)

        lbu     a1,4 * 4(sp)            // get shift count
        move    v0,a0                   // set function return register
        and     a1,a1,0x3f              // truncate shift count mod 64

//
// Right shift the operand by the specified shift count.
//

        li      v1,32                   // compute left shift count
        subu    v1,v1,a1                //
        bgtz    v1,10f                  // if gtz, shift less that 32-bits

//
// Shift count is greater than or equal 32 bits - high half of result is
// zero, low half is the high half shifted right by remaining count.
//

        sra     a2,a3,a1                // set low half of result
        sra     a3,a3,31                // set high half of result
        sw      a2,0(a0)                // store low part of reuslt
        sw      a3,4(a0)                // store high part of result
        j       ra                      // return

//
// Shift count is less than 32-bits - high half of result is the high half
// of operand shifted right by count, low half of result is the shifted out
// bits of the high half combined with the right shifted low half of the
// operand.
//

10:     srl     a2,a2,a1                // shift low half right count bits
        beq     zero,a1,20f             // if eq, no more shifts necessary
        sll     t0,a3,v1                // isolate shifted out bits of high half
        sra     a3,a3,a1                // shift high half right count bits
        or      a2,a2,t0                // combine bits for low half of result
20:     sw      a2,0(a0)                // store low part of reuslt
        sw      a3,4(a0)                // store high part of result
        j       ra                      // return

        .end    RtlLargeIntegerArithmeticShift