|
|
1.1 root 1: /*++
2:
3: Module Name:
4:
5: divlarge.c
6:
7: Abstract:
8:
9: This module implements the runtime library large integer divide
10: routines.
11:
12: N.B. These routines use a one bit at a time algorithm and is slow.
13: They should be used only when absolutely necessary.
14:
15: Revision History:
16:
17: --*/
18:
19: #include "windows.h"
20: #include "divlarge.h"
21:
22: LARGE_INTEGER
23: WINAPI
24: LargeIntegerDivide (
25: IN LARGE_INTEGER Dividend,
26: IN LARGE_INTEGER Divisor,
27: OUT PLARGE_INTEGER Remainder OPTIONAL
28: )
29:
30: /*++
31:
32: Routine Description:
33:
34: This routine divides an unsigned 64-bit dividend by an unsigned 64-bit
35: divisor and returns a 64-bit quotient, and optionally a 64-bit remainder.
36:
37: Arguments:
38:
39: Dividend - Supplies the 64-bit dividend for the divide operation.
40:
41: Divisor - Supplies the 64-bit divisor for the divide operation.
42:
43: Remainder - Supplies an optional pointer to a variable which receives
44: the remainder
45:
46: Return Value:
47:
48: The 64-bit quotient is returned as the function value.
49:
50: --*/
51:
52: {
53:
54: ULONG Index = 64;
55: LARGE_INTEGER Partial = {0, 0};
56: LARGE_INTEGER Quotient;
57:
58: //
59: // Loop through the dividend bits and compute the quotient and remainder.
60: //
61:
62: Quotient = Dividend;
63: do {
64:
65: //
66: // Shift the next dividend bit into the parital remainder and shift
67: // the partial quotient (dividend) left one bit.
68: //
69:
70: Partial.HighPart = (Partial.HighPart << 1) | (Partial.LowPart >> 31);
71: Partial.LowPart = (Partial.LowPart << 1) | ((ULONG)Quotient.HighPart >> 31);
72: Quotient.HighPart = (Quotient.HighPart << 1) | (Quotient.LowPart >> 31);
73: Quotient.LowPart <<= 1;
74:
75: //
76: // If the partial remainder is greater than or equal to the divisor,
77: // then subtract the divisor from the partial remainder and insert a
78: // one bit into the quotient.
79: //
80:
81: if (((ULONG)Partial.HighPart > (ULONG)Divisor.HighPart) ||
82: ((Partial.HighPart == Divisor.HighPart) &&
83: (Partial.LowPart >= Divisor.LowPart))) {
84:
85: Quotient.LowPart |= 1;
86: Partial.HighPart -= Divisor.HighPart;
87: if (Partial.LowPart < Divisor.LowPart) {
88: Partial.HighPart -= 1;
89: }
90:
91: Partial.LowPart -= Divisor.LowPart;
92: }
93:
94: Index -= 1;
95: } while (Index > 0);
96:
97: //
98: // If the remainder is requested, then return the 64-bit remainder.
99: //
100:
101: if (ARGUMENT_PRESENT(Remainder)) {
102: *Remainder = Partial;
103: }
104:
105: //
106: // Return the 64-bit quotient.
107: //
108:
109: return Quotient;
110: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.