2 * Large integer functions
4 * Copyright 2000 Alexandre Julliard
5 * Copyright 2003 Thomas Mertes
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Note: we use LONGLONG instead of LARGE_INTEGER, because
27 * the latter is a structure and the calling convention for
28 * returning a structure would not be binary-compatible.
30 * FIXME: for platforms that don't have a native LONGLONG type,
31 * we should define LONGLONG as a structure similar to LARGE_INTEGER
32 * and do everything by hand. You are welcome to do it...
35 /******************************************************************************
36 * RtlLargeIntegerAdd (NTDLL.@)
38 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
44 /******************************************************************************
45 * RtlLargeIntegerSubtract (NTDLL.@)
47 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
53 /******************************************************************************
54 * RtlLargeIntegerNegate (NTDLL.@)
56 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
62 /******************************************************************************
63 * RtlLargeIntegerShiftLeft (NTDLL.@)
65 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
71 /******************************************************************************
72 * RtlLargeIntegerShiftRight (NTDLL.@)
74 LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
76 return (ULONGLONG)a >> count;
80 /******************************************************************************
81 * RtlLargeIntegerArithmeticShift (NTDLL.@)
83 LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
85 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
90 /******************************************************************************
91 * RtlLargeIntegerDivide (NTDLL.@)
93 * FIXME: should it be signed division instead?
95 ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
97 ULONGLONG ret = a / b;
98 if (rem) *rem = a - ret * b;
103 /******************************************************************************
104 * RtlConvertLongToLargeInteger (NTDLL.@)
106 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
112 /******************************************************************************
113 * RtlConvertUlongToLargeInteger (NTDLL.@)
115 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
121 /******************************************************************************
122 * RtlEnlargedIntegerMultiply (NTDLL.@)
124 LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
126 return (LONGLONG)a * b;
130 /******************************************************************************
131 * RtlEnlargedUnsignedMultiply (NTDLL.@)
133 ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
135 return (ULONGLONG)a * b;
139 /******************************************************************************
140 * RtlEnlargedUnsignedDivide (NTDLL.@)
142 UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
144 #if defined(__i386__) && defined(__GNUC__)
145 UINT ret, rem, p1, p2;
148 p2 = a & 0xffffffffLL;
150 __asm__("div %4,%%eax"
151 : "=a" (ret), "=d" (rem)
152 : "0" (p2), "1" (p1), "g" (b) );
153 if (remptr) *remptr = rem;
157 if (remptr) *remptr = a % b;
163 /******************************************************************************
164 * RtlExtendedLargeIntegerDivide (NTDLL.@)
166 LONGLONG WINAPI RtlExtendedLargeIntegerDivide( LONGLONG a, INT b, INT *rem )
168 LONGLONG ret = a / b;
169 if (rem) *rem = a - b * ret;
174 /******************************************************************************
175 * RtlExtendedIntegerMultiply (NTDLL.@)
177 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
183 /******************************************************************************
184 * RtlExtendedMagicDivide (NTDLL.@)
186 * This function computes (a * b) >> (64 + shift)
188 * This allows replacing a division by a longlong constant
189 * by a multiplication by the inverse constant.
191 * If 'c' is the constant divisor, the constants 'b' and 'shift'
192 * must be chosen such that b = 2^(64+shift) / c.
193 * Then we have RtlExtendedMagicDivide(a,b,shift) == a * b / 2^(64+shift) == a / c.
195 * Parameter b although defined as LONGLONG is used as ULONGLONG.
197 #define LOWER_32(A) ((A) & 0xffffffff)
198 #define UPPER_32(A) ((A) >> 32)
199 LONGLONG WINAPI RtlExtendedMagicDivide(
214 a_high = UPPER_32((ULONGLONG) -a);
215 a_low = LOWER_32((ULONGLONG) -a);
218 a_high = UPPER_32((ULONGLONG) a);
219 a_low = LOWER_32((ULONGLONG) a);
222 b_high = UPPER_32((ULONGLONG) b);
223 b_low = LOWER_32((ULONGLONG) b);
225 ah_bl = a_high * b_low;
226 al_bh = a_low * b_high;
228 result = (LONGLONG) ((a_high * b_high +
231 UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) + UPPER_32(a_low * b_low))) >> shift);
241 /******************************************************************************
242 * RtlLargeIntegerToChar [NTDLL.@]
244 * Convert an unsigned large integer to a character string.
246 * On success assign a string and return STATUS_SUCCESS.
247 * If base is not 0 (=10), 2, 8, 10 or 16 return STATUS_INVALID_PARAMETER
248 * Writes at most length characters to the string str.
249 * Str is '\0' terminated when length allowes it.
250 * When str fits exactly in length characters the '\0' is ommitted.
251 * When str would be larger than length: return STATUS_BUFFER_OVERFLOW
252 * For str == NULL return STATUS_ACCESS_VIOLATION.
253 * Do not check for value_ptr != NULL (as native DLL).
256 * - Accept base 0 as 10 instead of crashing as native DLL does.
257 * - The native DLL does produce garbage or STATUS_BUFFER_OVERFLOW for
258 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
260 NTSTATUS WINAPI RtlLargeIntegerToChar(
261 const ULONGLONG *value_ptr,
266 ULONGLONG value = *value_ptr;
274 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
275 return STATUS_INVALID_PARAMETER;
283 digit = value % base;
284 value = value / base;
288 *pos = 'A' + digit - 10;
290 } while (value != 0L);
292 len = &buffer[64] - pos;
294 return STATUS_BUFFER_OVERFLOW;
295 } else if (str == NULL) {
296 return STATUS_ACCESS_VIOLATION;
297 } else if (len == length) {
298 memcpy(str, pos, len);
300 memcpy(str, pos, len + 1);
302 return STATUS_SUCCESS;
306 /**************************************************************************
307 * RtlInt64ToUnicodeString (NTDLL.@)
309 * Convert a large unsigned integer to a NULL terminated unicode string.
311 * On success assign a NULL terminated string and return STATUS_SUCCESS.
312 * If base is not 0 (=10), 2, 8, 10 or 16 return STATUS_INVALID_PARAMETER.
313 * If str is too small to hold the string (with the NULL termination):
314 * Set str->Length to the length the string would have (which can be
315 * larger than the MaximumLength) and return STATUS_BUFFER_OVERFLOW.
316 * Do not check for str != NULL (as native DLL).
319 * - Accept base 0 as 10 instead of crashing as native DLL does.
320 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
321 * The native DLL does this when the string would be longer than 31
322 * characters even when the string parameter is long enough.
323 * - The native DLL does produce garbage or STATUS_BUFFER_OVERFLOW for
324 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
326 NTSTATUS WINAPI RtlInt64ToUnicodeString(
337 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
338 return STATUS_INVALID_PARAMETER;
346 digit = value % base;
347 value = value / base;
351 *pos = 'A' + digit - 10;
353 } while (value != 0L);
355 str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
356 if (str->Length >= str->MaximumLength) {
357 return STATUS_BUFFER_OVERFLOW;
359 memcpy(str->Buffer, pos, str->Length + 1);
361 return STATUS_SUCCESS;
365 /******************************************************************************
368 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
374 /******************************************************************************
377 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
383 /******************************************************************************
386 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
392 /******************************************************************************
395 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
401 /******************************************************************************
404 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )