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
31 * Note: we use LONGLONG instead of LARGE_INTEGER, because
32 * the latter is a structure and the calling convention for
33 * returning a structure would not be binary-compatible.
35 * FIXME: for platforms that don't have a native LONGLONG type,
36 * we should define LONGLONG as a structure similar to LARGE_INTEGER
37 * and do everything by hand. You are welcome to do it...
40 /******************************************************************************
41 * RtlLargeIntegerAdd (NTDLL.@)
43 * Add two 64 bit integers.
46 * a [I] Initial number.
47 * b [I] Number to add to a.
52 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
58 /******************************************************************************
59 * RtlLargeIntegerSubtract (NTDLL.@)
61 * Subtract two 64 bit integers.
64 * a [I] Initial number.
65 * b [I] Number to subtract from a.
68 * The difference of a and b.
70 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
76 /******************************************************************************
77 * RtlLargeIntegerNegate (NTDLL.@)
79 * Negate a 64 bit integer.
82 * a [I] Initial number.
85 * The value of a negated.
87 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
93 /******************************************************************************
94 * RtlLargeIntegerShiftLeft (NTDLL.@)
96 * Perform a shift left on a 64 bit integer.
99 * a [I] Initial number.
100 * count [I] Number of bits to shift by
103 * The value of a following the shift.
105 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
111 /******************************************************************************
112 * RtlLargeIntegerShiftRight (NTDLL.@)
114 * Perform a shift right on a 64 bit integer.
117 * a [I] Initial number.
118 * count [I] Number of bits to shift by
121 * The value of a following the shift.
123 LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
125 return (ULONGLONG)a >> count;
129 /******************************************************************************
130 * RtlLargeIntegerArithmeticShift (NTDLL.@)
132 * Perform an arithmetic shift right on a 64 bit integer.
135 * a [I] Initial number.
136 * count [I] Number of bits to shift by
139 * The value of a following the shift.
141 LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
143 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
148 /******************************************************************************
149 * RtlLargeIntegerDivide (NTDLL.@)
151 * Divide one 64 bit unsigned integer by another, with remainder.
154 * a [I] Initial number.
155 * b [I] Number to divide a by
156 * rem [O] Destination for remainder
159 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
162 * Should it be signed division instead?
164 ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
166 ULONGLONG ret = a / b;
167 if (rem) *rem = a - ret * b;
172 /******************************************************************************
173 * RtlConvertLongToLargeInteger (NTDLL.@)
175 * Convert a 32 bit integer into 64 bits.
178 * a [I] Number to convert
183 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
189 /******************************************************************************
190 * RtlConvertUlongToLargeInteger (NTDLL.@)
192 * Convert a 32 bit unsigned integer into 64 bits.
195 * a [I] Number to convert
200 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
206 /******************************************************************************
207 * RtlEnlargedIntegerMultiply (NTDLL.@)
209 * Multiply two integers giving a 64 bit integer result.
212 * a [I] Initial number.
213 * b [I] Number to multiply a by.
216 * The product of a and b.
218 LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
220 return (LONGLONG)a * b;
224 /******************************************************************************
225 * RtlEnlargedUnsignedMultiply (NTDLL.@)
227 * Multiply two unsigned integers giving a 64 bit unsigned integer result.
230 * a [I] Initial number.
231 * b [I] Number to multiply a by.
234 * The product of a and b.
236 ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
238 return (ULONGLONG)a * b;
242 /******************************************************************************
243 * RtlEnlargedUnsignedDivide (NTDLL.@)
245 * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
248 * a [I] Initial number.
249 * b [I] Number to divide a by
250 * remptr [O] Destination for remainder
253 * The dividend of a and b. If remptr is non-NULL it is set to the remainder.
255 UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
257 #if defined(__i386__) && defined(__GNUC__)
258 UINT ret, rem, p1, p2;
261 p2 = a & 0xffffffffLL;
263 __asm__("div %4,%%eax"
264 : "=a" (ret), "=d" (rem)
265 : "0" (p2), "1" (p1), "g" (b) );
266 if (remptr) *remptr = rem;
270 if (remptr) *remptr = a % b;
276 /******************************************************************************
277 * RtlExtendedLargeIntegerDivide (NTDLL.@)
279 * Divide one 64 bit integer by a 32 bit integer, with remainder.
282 * a [I] Initial number.
283 * b [I] Number to divide a by
284 * rem [O] Destination for remainder
287 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
289 LONGLONG WINAPI RtlExtendedLargeIntegerDivide( LONGLONG a, INT b, INT *rem )
291 LONGLONG ret = a / b;
292 if (rem) *rem = a - b * ret;
297 /******************************************************************************
298 * RtlExtendedIntegerMultiply (NTDLL.@)
300 * Multiply one 64 bit integer by another 32 bit integer.
303 * a [I] Initial number.
304 * b [I] Number to multiply a by.
307 * The product of a and b.
309 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
315 /******************************************************************************
316 * RtlExtendedMagicDivide (NTDLL.@)
318 * Allows replacing a division by a longlong constant with a multiplication by
319 * the inverse constant.
322 * (dividend * inverse_divisor) >> (64 + shift)
325 * If the divisor of a division is constant, the constants inverse_divisor and
326 * shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
327 * Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
328 * dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
330 * The Parameter inverse_divisor although defined as LONGLONG is used as
333 #define LOWER_32(A) ((A) & 0xffffffff)
334 #define UPPER_32(A) ((A) >> 32)
335 LONGLONG WINAPI RtlExtendedMagicDivide(
336 LONGLONG dividend, /* [I] Dividend to be divided by the constant divisor */
337 LONGLONG inverse_divisor, /* [I] Constant computed manually as 2^(64+shift) / divisor */
338 INT shift) /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
340 ULONGLONG dividend_high;
341 ULONGLONG dividend_low;
342 ULONGLONG inverse_divisor_high;
343 ULONGLONG inverse_divisor_low;
350 dividend_high = UPPER_32((ULONGLONG) -dividend);
351 dividend_low = LOWER_32((ULONGLONG) -dividend);
354 dividend_high = UPPER_32((ULONGLONG) dividend);
355 dividend_low = LOWER_32((ULONGLONG) dividend);
358 inverse_divisor_high = UPPER_32((ULONGLONG) inverse_divisor);
359 inverse_divisor_low = LOWER_32((ULONGLONG) inverse_divisor);
361 ah_bl = dividend_high * inverse_divisor_low;
362 al_bh = dividend_low * inverse_divisor_high;
364 result = (LONGLONG) ((dividend_high * inverse_divisor_high +
367 UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
368 UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
378 /******************************************************************************
379 * RtlLargeIntegerToChar [NTDLL.@]
381 * Convert an unsigned large integer to a character string.
384 * Success: STATUS_SUCCESS. str contains the converted number
385 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
386 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
387 * STATUS_ACCESS_VIOLATION, if str is NULL.
390 * Instead of base 0 it uses 10 as base.
391 * Writes at most length characters to the string str.
392 * Str is '\0' terminated when length allows it.
393 * When str fits exactly in length characters the '\0' is omitted.
394 * If value_ptr is NULL it crashes, as the native function does.
397 * - Accept base 0 as 10 instead of crashing as native function does.
398 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
399 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
401 NTSTATUS WINAPI RtlLargeIntegerToChar(
402 const ULONGLONG *value_ptr, /* [I] Pointer to the value to be converted */
403 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
404 ULONG length, /* [I] Length of the str buffer in bytes */
405 PCHAR str) /* [O] Destination for the converted value */
407 ULONGLONG value = *value_ptr;
415 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
416 return STATUS_INVALID_PARAMETER;
424 digit = value % base;
425 value = value / base;
429 *pos = 'A' + digit - 10;
431 } while (value != 0L);
433 len = &buffer[64] - pos;
435 return STATUS_BUFFER_OVERFLOW;
436 } else if (str == NULL) {
437 return STATUS_ACCESS_VIOLATION;
438 } else if (len == length) {
439 memcpy(str, pos, len);
441 memcpy(str, pos, len + 1);
443 return STATUS_SUCCESS;
447 /**************************************************************************
448 * RtlInt64ToUnicodeString (NTDLL.@)
450 * Convert a large unsigned integer to a '\0' terminated unicode string.
453 * Success: STATUS_SUCCESS. str contains the converted number
454 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
455 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
456 * (with the '\0' termination). In this case str->Length
457 * is set to the length, the string would have (which can
458 * be larger than the MaximumLength).
461 * Instead of base 0 it uses 10 as base.
462 * If str is NULL it crashes, as the native function does.
465 * - Accept base 0 as 10 instead of crashing as native function does.
466 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
467 * The native function does this when the string would be longer than 31
468 * characters even when the string parameter is long enough.
469 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
470 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
472 NTSTATUS WINAPI RtlInt64ToUnicodeString(
473 ULONGLONG value, /* [I] Value to be converted */
474 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
475 UNICODE_STRING *str) /* [O] Destination for the converted value */
483 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
484 return STATUS_INVALID_PARAMETER;
492 digit = value % base;
493 value = value / base;
497 *pos = 'A' + digit - 10;
499 } while (value != 0L);
501 str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
502 if (str->Length >= str->MaximumLength) {
503 return STATUS_BUFFER_OVERFLOW;
505 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
507 return STATUS_SUCCESS;
511 /******************************************************************************
514 * Divide two 64 bit unsigned integers.
517 * a [I] Initial number.
518 * b [I] Number to multiply a by.
521 * The dividend of a and b.
523 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
529 /******************************************************************************
532 * Multiply two 64 bit integers.
535 * a [I] Initial number.
536 * b [I] Number to multiply a by.
539 * The product of a and b.
541 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
547 /******************************************************************************
550 * Calculate the remainder after dividing two 64 bit integers.
553 * a [I] Initial number.
554 * b [I] Number to divide a by.
557 * The remainder of a divided by b.
559 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
565 /******************************************************************************
568 * Divide two 64 bit unsigned integers.
571 * a [I] Initial number.
572 * b [I] Number to multiply a by.
575 * The dividend of a and b.
577 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
583 /******************************************************************************
586 * Calculate the remainder after dividing two 64 bit unsigned integers.
589 * a [I] Initial number.
590 * b [I] Number to divide a by.
593 * The remainder of a divided by b.
595 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )