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
23 #include "wine/port.h"
28 #define WIN32_NO_STATUS
36 * Note: we use LONGLONG instead of LARGE_INTEGER, because
37 * the latter is a structure and the calling convention for
38 * returning a structure would not be binary-compatible.
40 * FIXME: for platforms that don't have a native LONGLONG type,
41 * we should define LONGLONG as a structure similar to LARGE_INTEGER
42 * and do everything by hand. You are welcome to do it...
45 /******************************************************************************
46 * RtlLargeIntegerAdd (NTDLL.@)
48 * Add two 64 bit integers.
51 * a [I] Initial number.
52 * b [I] Number to add to a.
57 LONGLONG WINAPI
RtlLargeIntegerAdd( LONGLONG a
, LONGLONG b
)
63 /******************************************************************************
64 * RtlLargeIntegerSubtract (NTDLL.@)
66 * Subtract two 64 bit integers.
69 * a [I] Initial number.
70 * b [I] Number to subtract from a.
73 * The difference of a and b.
75 LONGLONG WINAPI
RtlLargeIntegerSubtract( LONGLONG a
, LONGLONG b
)
81 /******************************************************************************
82 * RtlLargeIntegerNegate (NTDLL.@)
84 * Negate a 64 bit integer.
87 * a [I] Initial number.
90 * The value of a negated.
92 LONGLONG WINAPI
RtlLargeIntegerNegate( LONGLONG a
)
98 /******************************************************************************
99 * RtlLargeIntegerShiftLeft (NTDLL.@)
101 * Perform a shift left on a 64 bit integer.
104 * a [I] Initial number.
105 * count [I] Number of bits to shift by
108 * The value of a following the shift.
110 LONGLONG WINAPI
RtlLargeIntegerShiftLeft( LONGLONG a
, INT count
)
116 /******************************************************************************
117 * RtlLargeIntegerShiftRight (NTDLL.@)
119 * Perform a shift right on a 64 bit integer.
122 * a [I] Initial number.
123 * count [I] Number of bits to shift by
126 * The value of a following the shift.
128 LONGLONG WINAPI
RtlLargeIntegerShiftRight( LONGLONG a
, INT count
)
130 return (ULONGLONG
)a
>> count
;
134 /******************************************************************************
135 * RtlLargeIntegerArithmeticShift (NTDLL.@)
137 * Perform an arithmetic shift right on a 64 bit integer.
140 * a [I] Initial number.
141 * count [I] Number of bits to shift by
144 * The value of a following the shift.
146 LONGLONG WINAPI
RtlLargeIntegerArithmeticShift( LONGLONG a
, INT count
)
148 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
153 /******************************************************************************
154 * RtlLargeIntegerDivide (NTDLL.@)
156 * Divide one 64 bit unsigned integer by another, with remainder.
159 * a [I] Initial number.
160 * b [I] Number to divide a by
161 * rem [O] Destination for remainder
164 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
167 * Should it be signed division instead?
169 ULONGLONG WINAPI
RtlLargeIntegerDivide( ULONGLONG a
, ULONGLONG b
, ULONGLONG
*rem
)
171 ULONGLONG ret
= a
/ b
;
172 if (rem
) *rem
= a
- ret
* b
;
177 /******************************************************************************
178 * RtlConvertLongToLargeInteger (NTDLL.@)
180 * Convert a 32 bit integer into 64 bits.
183 * a [I] Number to convert
188 LONGLONG WINAPI
RtlConvertLongToLargeInteger( LONG a
)
194 /******************************************************************************
195 * RtlConvertUlongToLargeInteger (NTDLL.@)
197 * Convert a 32 bit unsigned integer into 64 bits.
200 * a [I] Number to convert
205 ULONGLONG WINAPI
RtlConvertUlongToLargeInteger( ULONG a
)
211 /******************************************************************************
212 * RtlEnlargedIntegerMultiply (NTDLL.@)
214 * Multiply two integers giving a 64 bit integer result.
217 * a [I] Initial number.
218 * b [I] Number to multiply a by.
221 * The product of a and b.
223 LONGLONG WINAPI
RtlEnlargedIntegerMultiply( INT a
, INT b
)
225 return (LONGLONG
)a
* b
;
229 /******************************************************************************
230 * RtlEnlargedUnsignedMultiply (NTDLL.@)
232 * Multiply two unsigned integers giving a 64 bit unsigned integer result.
235 * a [I] Initial number.
236 * b [I] Number to multiply a by.
239 * The product of a and b.
241 ULONGLONG WINAPI
RtlEnlargedUnsignedMultiply( UINT a
, UINT b
)
243 return (ULONGLONG
)a
* b
;
247 /******************************************************************************
248 * RtlEnlargedUnsignedDivide (NTDLL.@)
250 * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
253 * a [I] Initial number.
254 * b [I] Number to divide a by
255 * remptr [O] Destination for remainder
258 * The dividend of a and b. If remptr is non-NULL it is set to the remainder.
260 UINT WINAPI
RtlEnlargedUnsignedDivide( ULONGLONG a
, UINT b
, UINT
*remptr
)
262 #if defined(__i386__) && defined(__GNUC__)
266 : "=a" (ret
), "=d" (rem
)
267 : "0" ((UINT
)a
), "1" ((UINT
)(a
>> 32)), "g" (b
) );
268 if (remptr
) *remptr
= rem
;
272 if (remptr
) *remptr
= a
% b
;
278 /******************************************************************************
279 * RtlExtendedLargeIntegerDivide (NTDLL.@)
281 * Divide one 64 bit integer by a 32 bit integer, with remainder.
284 * a [I] Initial number.
285 * b [I] Number to divide a by
286 * rem [O] Destination for remainder
289 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
291 LONGLONG WINAPI
RtlExtendedLargeIntegerDivide( LONGLONG a
, INT b
, INT
*rem
)
293 LONGLONG ret
= a
/ b
;
294 if (rem
) *rem
= a
- b
* ret
;
299 /******************************************************************************
300 * RtlExtendedIntegerMultiply (NTDLL.@)
302 * Multiply one 64 bit integer by another 32 bit integer.
305 * a [I] Initial number.
306 * b [I] Number to multiply a by.
309 * The product of a and b.
311 LONGLONG WINAPI
RtlExtendedIntegerMultiply( LONGLONG a
, INT b
)
317 /******************************************************************************
318 * RtlExtendedMagicDivide (NTDLL.@)
320 * Allows replacing a division by a longlong constant with a multiplication by
321 * the inverse constant.
324 * (dividend * inverse_divisor) >> (64 + shift)
327 * If the divisor of a division is constant, the constants inverse_divisor and
328 * shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
329 * Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
330 * dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
332 * The Parameter inverse_divisor although defined as LONGLONG is used as
335 #define LOWER_32(A) ((A) & 0xffffffff)
336 #define UPPER_32(A) ((A) >> 32)
337 LONGLONG WINAPI
RtlExtendedMagicDivide(
338 LONGLONG dividend
, /* [I] Dividend to be divided by the constant divisor */
339 LONGLONG inverse_divisor
, /* [I] Constant computed manually as 2^(64+shift) / divisor */
340 INT shift
) /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
342 ULONGLONG dividend_high
;
343 ULONGLONG dividend_low
;
344 ULONGLONG inverse_divisor_high
;
345 ULONGLONG inverse_divisor_low
;
352 dividend_high
= UPPER_32((ULONGLONG
) -dividend
);
353 dividend_low
= LOWER_32((ULONGLONG
) -dividend
);
356 dividend_high
= UPPER_32((ULONGLONG
) dividend
);
357 dividend_low
= LOWER_32((ULONGLONG
) dividend
);
360 inverse_divisor_high
= UPPER_32((ULONGLONG
) inverse_divisor
);
361 inverse_divisor_low
= LOWER_32((ULONGLONG
) inverse_divisor
);
363 ah_bl
= dividend_high
* inverse_divisor_low
;
364 al_bh
= dividend_low
* inverse_divisor_high
;
366 result
= (LONGLONG
) ((dividend_high
* inverse_divisor_high
+
369 UPPER_32(LOWER_32(ah_bl
) + LOWER_32(al_bh
) +
370 UPPER_32(dividend_low
* inverse_divisor_low
))) >> shift
);
380 /*************************************************************************
381 * RtlInterlockedCompareExchange64 (NTDLL.@)
383 LONGLONG WINAPI
RtlInterlockedCompareExchange64( LONGLONG
*dest
, LONGLONG xchg
, LONGLONG compare
)
385 return interlocked_cmpxchg64( dest
, xchg
, compare
);
390 /******************************************************************************
391 * RtlLargeIntegerToChar [NTDLL.@]
393 * Convert an unsigned large integer to a character string.
396 * Success: STATUS_SUCCESS. str contains the converted number
397 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
398 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
399 * STATUS_ACCESS_VIOLATION, if str is NULL.
402 * Instead of base 0 it uses 10 as base.
403 * Writes at most length characters to the string str.
404 * Str is '\0' terminated when length allows it.
405 * When str fits exactly in length characters the '\0' is omitted.
406 * If value_ptr is NULL it crashes, as the native function does.
409 * - Accept base 0 as 10 instead of crashing as native function does.
410 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
411 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
413 NTSTATUS WINAPI
RtlLargeIntegerToChar(
414 const ULONGLONG
*value_ptr
, /* [I] Pointer to the value to be converted */
415 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
416 ULONG length
, /* [I] Length of the str buffer in bytes */
417 PCHAR str
) /* [O] Destination for the converted value */
419 ULONGLONG value
= *value_ptr
;
427 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
428 return STATUS_INVALID_PARAMETER
;
436 digit
= value
% base
;
437 value
= value
/ base
;
441 *pos
= 'A' + digit
- 10;
443 } while (value
!= 0L);
445 len
= &buffer
[64] - pos
;
447 return STATUS_BUFFER_OVERFLOW
;
448 } else if (str
== NULL
) {
449 return STATUS_ACCESS_VIOLATION
;
450 } else if (len
== length
) {
451 memcpy(str
, pos
, len
);
453 memcpy(str
, pos
, len
+ 1);
455 return STATUS_SUCCESS
;
459 /**************************************************************************
460 * RtlInt64ToUnicodeString (NTDLL.@)
462 * Convert a large unsigned integer to a '\0' terminated unicode string.
465 * Success: STATUS_SUCCESS. str contains the converted number
466 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
467 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
468 * (with the '\0' termination). In this case str->Length
469 * is set to the length, the string would have (which can
470 * be larger than the MaximumLength).
473 * Instead of base 0 it uses 10 as base.
474 * If str is NULL it crashes, as the native function does.
477 * - Accept base 0 as 10 instead of crashing as native function does.
478 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
479 * The native function does this when the string would be longer than 31
480 * characters even when the string parameter is long enough.
481 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
482 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
484 NTSTATUS WINAPI
RtlInt64ToUnicodeString(
485 ULONGLONG value
, /* [I] Value to be converted */
486 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
487 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
495 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
496 return STATUS_INVALID_PARAMETER
;
504 digit
= value
% base
;
505 value
= value
/ base
;
509 *pos
= 'A' + digit
- 10;
511 } while (value
!= 0L);
513 str
->Length
= (&buffer
[64] - pos
) * sizeof(WCHAR
);
514 if (str
->Length
>= str
->MaximumLength
) {
515 return STATUS_BUFFER_OVERFLOW
;
517 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
519 return STATUS_SUCCESS
;
525 /******************************************************************************
528 * Divide two 64 bit unsigned integers.
531 * a [I] Initial number.
532 * b [I] Number to divide a by.
535 * The dividend of a and b.
537 LONGLONG WINAPI
_alldiv( LONGLONG a
, LONGLONG b
)
543 /******************************************************************************
546 * Multiply two 64 bit integers.
549 * a [I] Initial number.
550 * b [I] Number to multiply a by.
553 * The product of a and b.
555 LONGLONG WINAPI
_allmul( LONGLONG a
, LONGLONG b
)
561 /******************************************************************************
564 * Calculate the remainder after dividing two 64 bit integers.
567 * a [I] Initial number.
568 * b [I] Number to divide a by.
571 * The remainder of a divided by b.
573 LONGLONG WINAPI
_allrem( LONGLONG a
, LONGLONG b
)
579 /******************************************************************************
582 * Divide two 64 bit unsigned integers.
585 * a [I] Initial number.
586 * b [I] Number to divide a by.
589 * The dividend of a and b.
591 ULONGLONG WINAPI
_aulldiv( ULONGLONG a
, ULONGLONG b
)
596 /******************************************************************************
599 * Shift a 64 bit integer to the left.
602 * a [I] Initial number.
603 * b [I] Number to shift a by to the left.
606 * The left-shifted value.
608 LONGLONG WINAPI
_allshl( LONGLONG a
, LONG b
)
613 /******************************************************************************
616 * Shift a 64 bit integer to the right.
619 * a [I] Initial number.
620 * b [I] Number to shift a by to the right.
623 * The right-shifted value.
625 LONGLONG WINAPI
_allshr( LONGLONG a
, LONG b
)
630 /******************************************************************************
633 * Divide two 64 bit integers.
636 * a [I] Initial number.
637 * b [I] Number to divide a by.
640 * Returns the quotient of a and b in edx:eax.
641 * Returns the remainder of a and b in ebx:ecx.
643 __ASM_STDCALL_FUNC( _alldvrm
, 16,
645 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
646 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
648 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
653 "call " __ASM_NAME("_allrem") "\n\t"
660 "call " __ASM_NAME("_alldiv") "\n\t"
663 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
664 __ASM_CFI(".cfi_same_value %ebp\n\t")
667 /******************************************************************************
670 * Calculate the remainder after dividing two 64 bit unsigned integers.
673 * a [I] Initial number.
674 * b [I] Number to divide a by.
677 * The remainder of a divided by b.
679 ULONGLONG WINAPI
_aullrem( ULONGLONG a
, ULONGLONG b
)
684 /******************************************************************************
687 * Shift a 64 bit unsigned integer to the right.
690 * a [I] Initial number.
691 * b [I] Number to shift a by to the right.
694 * The right-shifted value.
696 ULONGLONG WINAPI
_aullshr( ULONGLONG a
, LONG b
)
701 /******************************************************************************
702 * _aulldvrm (NTDLL.@)
704 * Divide two 64 bit unsigned integers.
707 * a [I] Initial number.
708 * b [I] Number to divide a by.
711 * Returns the quotient of a and b in edx:eax.
712 * Returns the remainder of a and b in ebx:ecx.
714 __ASM_STDCALL_FUNC( _aulldvrm
, 16,
716 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
717 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
719 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
724 "call " __ASM_NAME("_aullrem") "\n\t"
731 "call " __ASM_NAME("_aulldiv") "\n\t"
734 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
735 __ASM_CFI(".cfi_same_value %ebp\n\t")
738 #endif /* __i386__ */