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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define WIN32_NO_STATUS
32 * Note: we use LONGLONG instead of LARGE_INTEGER, because
33 * the latter is a structure and the calling convention for
34 * returning a structure would not be binary-compatible.
36 * FIXME: for platforms that don't have a native LONGLONG type,
37 * we should define LONGLONG as a structure similar to LARGE_INTEGER
38 * and do everything by hand. You are welcome to do it...
41 /******************************************************************************
42 * RtlLargeIntegerAdd (NTDLL.@)
44 * Add two 64 bit integers.
47 * a [I] Initial number.
48 * b [I] Number to add to a.
53 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
59 /******************************************************************************
60 * RtlLargeIntegerSubtract (NTDLL.@)
62 * Subtract two 64 bit integers.
65 * a [I] Initial number.
66 * b [I] Number to subtract from a.
69 * The difference of a and b.
71 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
77 /******************************************************************************
78 * RtlLargeIntegerNegate (NTDLL.@)
80 * Negate a 64 bit integer.
83 * a [I] Initial number.
86 * The value of a negated.
88 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
94 /******************************************************************************
95 * RtlLargeIntegerShiftLeft (NTDLL.@)
97 * Perform a shift left on a 64 bit integer.
100 * a [I] Initial number.
101 * count [I] Number of bits to shift by
104 * The value of a following the shift.
106 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
112 /******************************************************************************
113 * RtlLargeIntegerShiftRight (NTDLL.@)
115 * Perform a shift right on a 64 bit integer.
118 * a [I] Initial number.
119 * count [I] Number of bits to shift by
122 * The value of a following the shift.
124 LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
126 return (ULONGLONG)a >> count;
130 /******************************************************************************
131 * RtlLargeIntegerArithmeticShift (NTDLL.@)
133 * Perform an arithmetic shift right on a 64 bit integer.
136 * a [I] Initial number.
137 * count [I] Number of bits to shift by
140 * The value of a following the shift.
142 LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
144 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
149 /******************************************************************************
150 * RtlLargeIntegerDivide (NTDLL.@)
152 * Divide one 64 bit unsigned integer by another, with remainder.
155 * a [I] Initial number.
156 * b [I] Number to divide a by
157 * rem [O] Destination for remainder
160 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
163 * Should it be signed division instead?
165 ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
167 ULONGLONG ret = a / b;
168 if (rem) *rem = a - ret * b;
173 /******************************************************************************
174 * RtlConvertLongToLargeInteger (NTDLL.@)
176 * Convert a 32 bit integer into 64 bits.
179 * a [I] Number to convert
184 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
190 /******************************************************************************
191 * RtlConvertUlongToLargeInteger (NTDLL.@)
193 * Convert a 32 bit unsigned integer into 64 bits.
196 * a [I] Number to convert
201 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
207 /******************************************************************************
208 * RtlEnlargedIntegerMultiply (NTDLL.@)
210 * Multiply two integers giving a 64 bit integer result.
213 * a [I] Initial number.
214 * b [I] Number to multiply a by.
217 * The product of a and b.
219 LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
221 return (LONGLONG)a * b;
225 /******************************************************************************
226 * RtlEnlargedUnsignedMultiply (NTDLL.@)
228 * Multiply two unsigned integers giving a 64 bit unsigned integer result.
231 * a [I] Initial number.
232 * b [I] Number to multiply a by.
235 * The product of a and b.
237 ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
239 return (ULONGLONG)a * b;
243 /******************************************************************************
244 * RtlEnlargedUnsignedDivide (NTDLL.@)
246 * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
249 * a [I] Initial number.
250 * b [I] Number to divide a by
251 * remptr [O] Destination for remainder
254 * The dividend of a and b. If remptr is non-NULL it is set to the remainder.
256 UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
258 #if defined(__i386__) && defined(__GNUC__)
259 UINT ret, rem, p1, p2;
262 p2 = a & 0xffffffffLL;
264 __asm__("div %4,%%eax"
265 : "=a" (ret), "=d" (rem)
266 : "0" (p2), "1" (p1), "g" (b) );
267 if (remptr) *remptr = rem;
271 if (remptr) *remptr = a % b;
277 /******************************************************************************
278 * RtlExtendedLargeIntegerDivide (NTDLL.@)
280 * Divide one 64 bit integer by a 32 bit integer, with remainder.
283 * a [I] Initial number.
284 * b [I] Number to divide a by
285 * rem [O] Destination for remainder
288 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
290 LONGLONG WINAPI RtlExtendedLargeIntegerDivide( LONGLONG a, INT b, INT *rem )
292 LONGLONG ret = a / b;
293 if (rem) *rem = a - b * ret;
298 /******************************************************************************
299 * RtlExtendedIntegerMultiply (NTDLL.@)
301 * Multiply one 64 bit integer by another 32 bit integer.
304 * a [I] Initial number.
305 * b [I] Number to multiply a by.
308 * The product of a and b.
310 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
316 /******************************************************************************
317 * RtlExtendedMagicDivide (NTDLL.@)
319 * Allows replacing a division by a longlong constant with a multiplication by
320 * the inverse constant.
323 * (dividend * inverse_divisor) >> (64 + shift)
326 * If the divisor of a division is constant, the constants inverse_divisor and
327 * shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
328 * Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
329 * dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
331 * The Parameter inverse_divisor although defined as LONGLONG is used as
334 #define LOWER_32(A) ((A) & 0xffffffff)
335 #define UPPER_32(A) ((A) >> 32)
336 LONGLONG WINAPI RtlExtendedMagicDivide(
337 LONGLONG dividend, /* [I] Dividend to be divided by the constant divisor */
338 LONGLONG inverse_divisor, /* [I] Constant computed manually as 2^(64+shift) / divisor */
339 INT shift) /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
341 ULONGLONG dividend_high;
342 ULONGLONG dividend_low;
343 ULONGLONG inverse_divisor_high;
344 ULONGLONG inverse_divisor_low;
351 dividend_high = UPPER_32((ULONGLONG) -dividend);
352 dividend_low = LOWER_32((ULONGLONG) -dividend);
355 dividend_high = UPPER_32((ULONGLONG) dividend);
356 dividend_low = LOWER_32((ULONGLONG) dividend);
359 inverse_divisor_high = UPPER_32((ULONGLONG) inverse_divisor);
360 inverse_divisor_low = LOWER_32((ULONGLONG) inverse_divisor);
362 ah_bl = dividend_high * inverse_divisor_low;
363 al_bh = dividend_low * inverse_divisor_high;
365 result = (LONGLONG) ((dividend_high * inverse_divisor_high +
368 UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
369 UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
380 /******************************************************************************
381 * RtlLargeIntegerToChar [NTDLL.@]
383 * Convert an unsigned large integer to a character string.
386 * Success: STATUS_SUCCESS. str contains the converted number
387 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
388 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
389 * STATUS_ACCESS_VIOLATION, if str is NULL.
392 * Instead of base 0 it uses 10 as base.
393 * Writes at most length characters to the string str.
394 * Str is '\0' terminated when length allows it.
395 * When str fits exactly in length characters the '\0' is omitted.
396 * If value_ptr is NULL it crashes, as the native function does.
399 * - Accept base 0 as 10 instead of crashing as native function does.
400 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
401 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
403 NTSTATUS WINAPI RtlLargeIntegerToChar(
404 const ULONGLONG *value_ptr, /* [I] Pointer to the value to be converted */
405 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
406 ULONG length, /* [I] Length of the str buffer in bytes */
407 PCHAR str) /* [O] Destination for the converted value */
409 ULONGLONG value = *value_ptr;
417 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
418 return STATUS_INVALID_PARAMETER;
426 digit = value % base;
427 value = value / base;
431 *pos = 'A' + digit - 10;
433 } while (value != 0L);
435 len = &buffer[64] - pos;
437 return STATUS_BUFFER_OVERFLOW;
438 } else if (str == NULL) {
439 return STATUS_ACCESS_VIOLATION;
440 } else if (len == length) {
441 memcpy(str, pos, len);
443 memcpy(str, pos, len + 1);
445 return STATUS_SUCCESS;
449 /**************************************************************************
450 * RtlInt64ToUnicodeString (NTDLL.@)
452 * Convert a large unsigned integer to a '\0' terminated unicode string.
455 * Success: STATUS_SUCCESS. str contains the converted number
456 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
457 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
458 * (with the '\0' termination). In this case str->Length
459 * is set to the length, the string would have (which can
460 * be larger than the MaximumLength).
463 * Instead of base 0 it uses 10 as base.
464 * If str is NULL it crashes, as the native function does.
467 * - Accept base 0 as 10 instead of crashing as native function does.
468 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
469 * The native function does this when the string would be longer than 31
470 * characters even when the string parameter is long enough.
471 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
472 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
474 NTSTATUS WINAPI RtlInt64ToUnicodeString(
475 ULONGLONG value, /* [I] Value to be converted */
476 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
477 UNICODE_STRING *str) /* [O] Destination for the converted value */
485 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
486 return STATUS_INVALID_PARAMETER;
494 digit = value % base;
495 value = value / base;
499 *pos = 'A' + digit - 10;
501 } while (value != 0L);
503 str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
504 if (str->Length >= str->MaximumLength) {
505 return STATUS_BUFFER_OVERFLOW;
507 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
509 return STATUS_SUCCESS;
515 /******************************************************************************
518 * Divide two 64 bit unsigned integers.
521 * a [I] Initial number.
522 * b [I] Number to multiply a by.
525 * The dividend of a and b.
527 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
533 /******************************************************************************
536 * Multiply two 64 bit integers.
539 * a [I] Initial number.
540 * b [I] Number to multiply a by.
543 * The product of a and b.
545 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
551 /******************************************************************************
554 * Calculate the remainder after dividing two 64 bit integers.
557 * a [I] Initial number.
558 * b [I] Number to divide a by.
561 * The remainder of a divided by b.
563 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
569 /******************************************************************************
572 * Divide two 64 bit unsigned integers.
575 * a [I] Initial number.
576 * b [I] Number to multiply a by.
579 * The dividend of a and b.
581 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
587 /******************************************************************************
590 * Calculate the remainder after dividing two 64 bit unsigned integers.
593 * a [I] Initial number.
594 * b [I] Number to divide a by.
597 * The remainder of a divided by b.
599 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )
604 #endif /* __i386__ */