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
29 * Note: we use LONGLONG instead of LARGE_INTEGER, because
30 * the latter is a structure and the calling convention for
31 * returning a structure would not be binary-compatible.
33 * FIXME: for platforms that don't have a native LONGLONG type,
34 * we should define LONGLONG as a structure similar to LARGE_INTEGER
35 * and do everything by hand. You are welcome to do it...
38 /******************************************************************************
39 * RtlLargeIntegerAdd (NTDLL.@)
41 * Add two 64 bit integers.
44 * a [I] Initial number.
45 * b [I] Number to add to a.
50 LONGLONG WINAPI
RtlLargeIntegerAdd( LONGLONG a
, LONGLONG b
)
56 /******************************************************************************
57 * RtlLargeIntegerSubtract (NTDLL.@)
59 * Subtract two 64 bit integers.
62 * a [I] Initial number.
63 * b [I] Number to subtract from a.
66 * The difference of a and b.
68 LONGLONG WINAPI
RtlLargeIntegerSubtract( LONGLONG a
, LONGLONG b
)
74 /******************************************************************************
75 * RtlLargeIntegerNegate (NTDLL.@)
77 * Negate a 64 bit integer.
80 * a [I] Initial number.
83 * The value of a negated.
85 LONGLONG WINAPI
RtlLargeIntegerNegate( LONGLONG a
)
91 /******************************************************************************
92 * RtlLargeIntegerShiftLeft (NTDLL.@)
94 * Perform a shift left on a 64 bit integer.
97 * a [I] Initial number.
98 * count [I] Number of bits to shift by
101 * The value of a following the shift.
103 LONGLONG WINAPI
RtlLargeIntegerShiftLeft( LONGLONG a
, INT count
)
109 /******************************************************************************
110 * RtlLargeIntegerShiftRight (NTDLL.@)
112 * Perform a shift right on a 64 bit integer.
115 * a [I] Initial number.
116 * count [I] Number of bits to shift by
119 * The value of a following the shift.
121 LONGLONG WINAPI
RtlLargeIntegerShiftRight( LONGLONG a
, INT count
)
123 return (ULONGLONG
)a
>> count
;
127 /******************************************************************************
128 * RtlLargeIntegerArithmeticShift (NTDLL.@)
130 * Perform an arithmetic shift right on a 64 bit integer.
133 * a [I] Initial number.
134 * count [I] Number of bits to shift by
137 * The value of a following the shift.
139 LONGLONG WINAPI
RtlLargeIntegerArithmeticShift( LONGLONG a
, INT count
)
141 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
146 /******************************************************************************
147 * RtlLargeIntegerDivide (NTDLL.@)
149 * Divide one 64 bit unsigned integer by another, with remainder.
152 * a [I] Initial number.
153 * b [I] Number to divide a by
154 * rem [O] Destination for remainder
157 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
160 * Should it be signed division instead?
162 ULONGLONG WINAPI
RtlLargeIntegerDivide( ULONGLONG a
, ULONGLONG b
, ULONGLONG
*rem
)
164 ULONGLONG ret
= a
/ b
;
165 if (rem
) *rem
= a
- ret
* b
;
170 /******************************************************************************
171 * RtlConvertLongToLargeInteger (NTDLL.@)
173 * Convert a 32 bit integer into 64 bits.
176 * a [I] Number to convert
181 LONGLONG WINAPI
RtlConvertLongToLargeInteger( LONG a
)
187 /******************************************************************************
188 * RtlConvertUlongToLargeInteger (NTDLL.@)
190 * Convert a 32 bit unsigned integer into 64 bits.
193 * a [I] Number to convert
198 ULONGLONG WINAPI
RtlConvertUlongToLargeInteger( ULONG a
)
204 /******************************************************************************
205 * RtlEnlargedIntegerMultiply (NTDLL.@)
207 * Multiply two integers giving a 64 bit integer result.
210 * a [I] Initial number.
211 * b [I] Number to multiply a by.
214 * The product of a and b.
216 LONGLONG WINAPI
RtlEnlargedIntegerMultiply( INT a
, INT b
)
218 return (LONGLONG
)a
* b
;
222 /******************************************************************************
223 * RtlEnlargedUnsignedMultiply (NTDLL.@)
225 * Multiply two unsigned integers giving a 64 bit unsigned integer result.
228 * a [I] Initial number.
229 * b [I] Number to multiply a by.
232 * The product of a and b.
234 ULONGLONG WINAPI
RtlEnlargedUnsignedMultiply( UINT a
, UINT b
)
236 return (ULONGLONG
)a
* b
;
240 /******************************************************************************
241 * RtlEnlargedUnsignedDivide (NTDLL.@)
243 * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
246 * a [I] Initial number.
247 * b [I] Number to divide a by
248 * remptr [O] Destination for remainder
251 * The dividend of a and b. If remptr is non-NULL it is set to the remainder.
253 UINT WINAPI
RtlEnlargedUnsignedDivide( ULONGLONG a
, UINT b
, UINT
*remptr
)
255 #if defined(__i386__) && defined(__GNUC__)
256 UINT ret
, rem
, p1
, p2
;
259 p2
= a
& 0xffffffffLL
;
261 __asm__("div %4,%%eax"
262 : "=a" (ret
), "=d" (rem
)
263 : "0" (p2
), "1" (p1
), "g" (b
) );
264 if (remptr
) *remptr
= rem
;
268 if (remptr
) *remptr
= a
% b
;
274 /******************************************************************************
275 * RtlExtendedLargeIntegerDivide (NTDLL.@)
277 * Divide one 64 bit integer by a 32 bit integer, with remainder.
280 * a [I] Initial number.
281 * b [I] Number to divide a by
282 * rem [O] Destination for remainder
285 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
287 LONGLONG WINAPI
RtlExtendedLargeIntegerDivide( LONGLONG a
, INT b
, INT
*rem
)
289 LONGLONG ret
= a
/ b
;
290 if (rem
) *rem
= a
- b
* ret
;
295 /******************************************************************************
296 * RtlExtendedIntegerMultiply (NTDLL.@)
298 * Multiply one 64 bit integer by another 32 bit integer.
301 * a [I] Initial number.
302 * b [I] Number to multiply a by.
305 * The product of a and b.
307 LONGLONG WINAPI
RtlExtendedIntegerMultiply( LONGLONG a
, INT b
)
313 /******************************************************************************
314 * RtlExtendedMagicDivide (NTDLL.@)
316 * Allows replacing a division by a longlong constant with a multiplication by
317 * the inverse constant.
320 * (dividend * inverse_divisor) >> (64 + shift)
323 * If the divisor of a division is constant, the constants inverse_divisor and
324 * shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
325 * Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
326 * dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
328 * The Parameter inverse_divisor although defined as LONGLONG is used as
331 #define LOWER_32(A) ((A) & 0xffffffff)
332 #define UPPER_32(A) ((A) >> 32)
333 LONGLONG WINAPI
RtlExtendedMagicDivide(
334 LONGLONG dividend
, /* [I] Dividend to be divided by the constant divisor */
335 LONGLONG inverse_divisor
, /* [I] Constant computed manually as 2^(64+shift) / divisor */
336 INT shift
) /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
338 ULONGLONG dividend_high
;
339 ULONGLONG dividend_low
;
340 ULONGLONG inverse_divisor_high
;
341 ULONGLONG inverse_divisor_low
;
348 dividend_high
= UPPER_32((ULONGLONG
) -dividend
);
349 dividend_low
= LOWER_32((ULONGLONG
) -dividend
);
352 dividend_high
= UPPER_32((ULONGLONG
) dividend
);
353 dividend_low
= LOWER_32((ULONGLONG
) dividend
);
356 inverse_divisor_high
= UPPER_32((ULONGLONG
) inverse_divisor
);
357 inverse_divisor_low
= LOWER_32((ULONGLONG
) inverse_divisor
);
359 ah_bl
= dividend_high
* inverse_divisor_low
;
360 al_bh
= dividend_low
* inverse_divisor_high
;
362 result
= (LONGLONG
) ((dividend_high
* inverse_divisor_high
+
365 UPPER_32(LOWER_32(ah_bl
) + LOWER_32(al_bh
) +
366 UPPER_32(dividend_low
* inverse_divisor_low
))) >> shift
);
376 /******************************************************************************
377 * RtlLargeIntegerToChar [NTDLL.@]
379 * Convert an unsigned large integer to a character string.
382 * Success: STATUS_SUCCESS. str contains the converted number
383 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
384 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
385 * STATUS_ACCESS_VIOLATION, if str is NULL.
388 * Instead of base 0 it uses 10 as base.
389 * Writes at most length characters to the string str.
390 * Str is '\0' terminated when length allows it.
391 * When str fits exactly in length characters the '\0' is omitted.
392 * If value_ptr is NULL it crashes, as the native function does.
395 * - Accept base 0 as 10 instead of crashing as native function does.
396 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
397 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
399 NTSTATUS WINAPI
RtlLargeIntegerToChar(
400 const ULONGLONG
*value_ptr
, /* [I] Pointer to the value to be converted */
401 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
402 ULONG length
, /* [I] Length of the str buffer in bytes */
403 PCHAR str
) /* [O] Destination for the converted value */
405 ULONGLONG value
= *value_ptr
;
413 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
414 return STATUS_INVALID_PARAMETER
;
422 digit
= value
% base
;
423 value
= value
/ base
;
427 *pos
= 'A' + digit
- 10;
429 } while (value
!= 0L);
431 len
= &buffer
[64] - pos
;
433 return STATUS_BUFFER_OVERFLOW
;
434 } else if (str
== NULL
) {
435 return STATUS_ACCESS_VIOLATION
;
436 } else if (len
== length
) {
437 memcpy(str
, pos
, len
);
439 memcpy(str
, pos
, len
+ 1);
441 return STATUS_SUCCESS
;
445 /**************************************************************************
446 * RtlInt64ToUnicodeString (NTDLL.@)
448 * Convert a large unsigned integer to a '\0' terminated unicode string.
451 * Success: STATUS_SUCCESS. str contains the converted number
452 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
453 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
454 * (with the '\0' termination). In this case str->Length
455 * is set to the length, the string would have (which can
456 * be larger than the MaximumLength).
459 * Instead of base 0 it uses 10 as base.
460 * If str is NULL it crashes, as the native function does.
463 * - Accept base 0 as 10 instead of crashing as native function does.
464 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
465 * The native function does this when the string would be longer than 31
466 * characters even when the string parameter is long enough.
467 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
468 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
470 NTSTATUS WINAPI
RtlInt64ToUnicodeString(
471 ULONGLONG value
, /* [I] Value to be converted */
472 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
473 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
481 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
482 return STATUS_INVALID_PARAMETER
;
490 digit
= value
% base
;
491 value
= value
/ base
;
495 *pos
= 'A' + digit
- 10;
497 } while (value
!= 0L);
499 str
->Length
= (&buffer
[64] - pos
) * sizeof(WCHAR
);
500 if (str
->Length
>= str
->MaximumLength
) {
501 return STATUS_BUFFER_OVERFLOW
;
503 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
505 return STATUS_SUCCESS
;
509 /******************************************************************************
512 * Divide two 64 bit unsigned integers.
515 * a [I] Initial number.
516 * b [I] Number to multiply a by.
519 * The dividend of a and b.
521 LONGLONG WINAPI
_alldiv( LONGLONG a
, LONGLONG b
)
527 /******************************************************************************
530 * Multiply two 64 bit integers.
533 * a [I] Initial number.
534 * b [I] Number to multiply a by.
537 * The product of a and b.
539 LONGLONG WINAPI
_allmul( LONGLONG a
, LONGLONG b
)
545 /******************************************************************************
548 * Calculate the remainder after dividing two 64 bit integers.
551 * a [I] Initial number.
552 * b [I] Number to divide a by.
555 * The remainder of a divided by b.
557 LONGLONG WINAPI
_allrem( LONGLONG a
, LONGLONG b
)
563 /******************************************************************************
566 * Divide two 64 bit unsigned integers.
569 * a [I] Initial number.
570 * b [I] Number to multiply a by.
573 * The dividend of a and b.
575 ULONGLONG WINAPI
_aulldiv( ULONGLONG a
, ULONGLONG b
)
581 /******************************************************************************
584 * Calculate the remainder after dividing two 64 bit unsigned integers.
587 * a [I] Initial number.
588 * b [I] Number to divide a by.
591 * The remainder of a divided by b.
593 ULONGLONG WINAPI
_aullrem( ULONGLONG a
, ULONGLONG b
)