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
35 * Note: we use LONGLONG instead of LARGE_INTEGER, because
36 * the latter is a structure and the calling convention for
37 * returning a structure would not be binary-compatible.
39 * FIXME: for platforms that don't have a native LONGLONG type,
40 * we should define LONGLONG as a structure similar to LARGE_INTEGER
41 * and do everything by hand. You are welcome to do it...
44 /******************************************************************************
45 * RtlLargeIntegerAdd (NTDLL.@)
47 * Add two 64 bit integers.
50 * a [I] Initial number.
51 * b [I] Number to add to a.
56 LONGLONG WINAPI
RtlLargeIntegerAdd( LONGLONG a
, LONGLONG b
)
62 /******************************************************************************
63 * RtlLargeIntegerSubtract (NTDLL.@)
65 * Subtract two 64 bit integers.
68 * a [I] Initial number.
69 * b [I] Number to subtract from a.
72 * The difference of a and b.
74 LONGLONG WINAPI
RtlLargeIntegerSubtract( LONGLONG a
, LONGLONG b
)
80 /******************************************************************************
81 * RtlLargeIntegerNegate (NTDLL.@)
83 * Negate a 64 bit integer.
86 * a [I] Initial number.
89 * The value of a negated.
91 LONGLONG WINAPI
RtlLargeIntegerNegate( LONGLONG a
)
97 /******************************************************************************
98 * RtlLargeIntegerShiftLeft (NTDLL.@)
100 * Perform a shift left on a 64 bit integer.
103 * a [I] Initial number.
104 * count [I] Number of bits to shift by
107 * The value of a following the shift.
109 LONGLONG WINAPI
RtlLargeIntegerShiftLeft( LONGLONG a
, INT count
)
115 /******************************************************************************
116 * RtlLargeIntegerShiftRight (NTDLL.@)
118 * Perform a shift right on a 64 bit integer.
121 * a [I] Initial number.
122 * count [I] Number of bits to shift by
125 * The value of a following the shift.
127 LONGLONG WINAPI
RtlLargeIntegerShiftRight( LONGLONG a
, INT count
)
129 return (ULONGLONG
)a
>> count
;
133 /******************************************************************************
134 * RtlLargeIntegerArithmeticShift (NTDLL.@)
136 * Perform an arithmetic shift right on a 64 bit integer.
139 * a [I] Initial number.
140 * count [I] Number of bits to shift by
143 * The value of a following the shift.
145 LONGLONG WINAPI
RtlLargeIntegerArithmeticShift( LONGLONG a
, INT count
)
147 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
152 /******************************************************************************
153 * RtlLargeIntegerDivide (NTDLL.@)
155 * Divide one 64 bit unsigned integer by another, with remainder.
158 * a [I] Initial number.
159 * b [I] Number to divide a by
160 * rem [O] Destination for remainder
163 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
166 * Should it be signed division instead?
168 ULONGLONG WINAPI
RtlLargeIntegerDivide( ULONGLONG a
, ULONGLONG b
, ULONGLONG
*rem
)
170 ULONGLONG ret
= a
/ b
;
171 if (rem
) *rem
= a
- ret
* b
;
176 /******************************************************************************
177 * RtlConvertLongToLargeInteger (NTDLL.@)
179 * Convert a 32 bit integer into 64 bits.
182 * a [I] Number to convert
187 LONGLONG WINAPI
RtlConvertLongToLargeInteger( LONG a
)
193 /******************************************************************************
194 * RtlConvertUlongToLargeInteger (NTDLL.@)
196 * Convert a 32 bit unsigned integer into 64 bits.
199 * a [I] Number to convert
204 ULONGLONG WINAPI
RtlConvertUlongToLargeInteger( ULONG a
)
210 /******************************************************************************
211 * RtlEnlargedIntegerMultiply (NTDLL.@)
213 * Multiply two integers giving a 64 bit integer result.
216 * a [I] Initial number.
217 * b [I] Number to multiply a by.
220 * The product of a and b.
222 LONGLONG WINAPI
RtlEnlargedIntegerMultiply( INT a
, INT b
)
224 return (LONGLONG
)a
* b
;
228 /******************************************************************************
229 * RtlEnlargedUnsignedMultiply (NTDLL.@)
231 * Multiply two unsigned integers giving a 64 bit unsigned integer result.
234 * a [I] Initial number.
235 * b [I] Number to multiply a by.
238 * The product of a and b.
240 ULONGLONG WINAPI
RtlEnlargedUnsignedMultiply( UINT a
, UINT b
)
242 return (ULONGLONG
)a
* b
;
246 /******************************************************************************
247 * RtlEnlargedUnsignedDivide (NTDLL.@)
249 * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
252 * a [I] Initial number.
253 * b [I] Number to divide a by
254 * remptr [O] Destination for remainder
257 * The dividend of a and b. If remptr is non-NULL it is set to the remainder.
259 UINT WINAPI
RtlEnlargedUnsignedDivide( ULONGLONG a
, UINT b
, UINT
*remptr
)
261 #if defined(__i386__) && defined(__GNUC__)
265 : "=a" (ret
), "=d" (rem
)
266 : "0" ((UINT
)a
), "1" ((UINT
)(a
>> 32)), "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
);
379 /*************************************************************************
380 * RtlInterlockedCompareExchange64 (NTDLL.@)
382 LONGLONG WINAPI
RtlInterlockedCompareExchange64( LONGLONG
*dest
, LONGLONG xchg
, LONGLONG compare
)
384 return interlocked_cmpxchg64( dest
, xchg
, compare
);
389 /******************************************************************************
390 * RtlLargeIntegerToChar [NTDLL.@]
392 * Convert an unsigned large integer to a character string.
395 * Success: STATUS_SUCCESS. str contains the converted number
396 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
397 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
398 * STATUS_ACCESS_VIOLATION, if str is NULL.
401 * Instead of base 0 it uses 10 as base.
402 * Writes at most length characters to the string str.
403 * Str is '\0' terminated when length allows it.
404 * When str fits exactly in length characters the '\0' is omitted.
405 * If value_ptr is NULL it crashes, as the native function does.
408 * - Accept base 0 as 10 instead of crashing as native function does.
409 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
410 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
412 NTSTATUS WINAPI
RtlLargeIntegerToChar(
413 const ULONGLONG
*value_ptr
, /* [I] Pointer to the value to be converted */
414 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
415 ULONG length
, /* [I] Length of the str buffer in bytes */
416 PCHAR str
) /* [O] Destination for the converted value */
418 ULONGLONG value
= *value_ptr
;
426 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
427 return STATUS_INVALID_PARAMETER
;
435 digit
= value
% base
;
436 value
= value
/ base
;
440 *pos
= 'A' + digit
- 10;
442 } while (value
!= 0L);
444 len
= &buffer
[64] - pos
;
446 return STATUS_BUFFER_OVERFLOW
;
447 } else if (str
== NULL
) {
448 return STATUS_ACCESS_VIOLATION
;
449 } else if (len
== length
) {
450 memcpy(str
, pos
, len
);
452 memcpy(str
, pos
, len
+ 1);
454 return STATUS_SUCCESS
;
458 /**************************************************************************
459 * RtlInt64ToUnicodeString (NTDLL.@)
461 * Convert a large unsigned integer to a '\0' terminated unicode string.
464 * Success: STATUS_SUCCESS. str contains the converted number
465 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
466 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
467 * (with the '\0' termination). In this case str->Length
468 * is set to the length, the string would have (which can
469 * be larger than the MaximumLength).
472 * Instead of base 0 it uses 10 as base.
473 * If str is NULL it crashes, as the native function does.
476 * - Accept base 0 as 10 instead of crashing as native function does.
477 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
478 * The native function does this when the string would be longer than 31
479 * characters even when the string parameter is long enough.
480 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
481 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
483 NTSTATUS WINAPI
RtlInt64ToUnicodeString(
484 ULONGLONG value
, /* [I] Value to be converted */
485 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
486 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
494 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
495 return STATUS_INVALID_PARAMETER
;
503 digit
= value
% base
;
504 value
= value
/ base
;
508 *pos
= 'A' + digit
- 10;
510 } while (value
!= 0L);
512 str
->Length
= (&buffer
[64] - pos
) * sizeof(WCHAR
);
513 if (str
->Length
>= str
->MaximumLength
) {
514 return STATUS_BUFFER_OVERFLOW
;
516 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
518 return STATUS_SUCCESS
;
524 /******************************************************************************
527 * Divide two 64 bit unsigned integers.
530 * a [I] Initial number.
531 * b [I] Number to divide a by.
534 * The dividend of a and b.
536 LONGLONG WINAPI
_alldiv( LONGLONG a
, LONGLONG b
)
542 /******************************************************************************
545 * Multiply two 64 bit integers.
548 * a [I] Initial number.
549 * b [I] Number to multiply a by.
552 * The product of a and b.
554 LONGLONG WINAPI
_allmul( LONGLONG a
, LONGLONG b
)
560 /******************************************************************************
563 * Calculate the remainder after dividing two 64 bit integers.
566 * a [I] Initial number.
567 * b [I] Number to divide a by.
570 * The remainder of a divided by b.
572 LONGLONG WINAPI
_allrem( LONGLONG a
, LONGLONG b
)
578 /******************************************************************************
581 * Divide two 64 bit unsigned integers.
584 * a [I] Initial number.
585 * b [I] Number to divide a by.
588 * The dividend of a and b.
590 ULONGLONG WINAPI
_aulldiv( ULONGLONG a
, ULONGLONG b
)
595 /******************************************************************************
598 * Shift a 64 bit integer to the left.
601 * a [I] Initial number.
602 * b [I] Number to shift a by to the left.
605 * The left-shifted value.
607 LONGLONG WINAPI
_allshl( LONGLONG a
, LONG b
)
612 /******************************************************************************
615 * Shift a 64 bit integer to the right.
618 * a [I] Initial number.
619 * b [I] Number to shift a by to the right.
622 * The right-shifted value.
624 LONGLONG WINAPI
_allshr( LONGLONG a
, LONG b
)
629 /******************************************************************************
632 * Calculate the remainder after dividing two 64 bit unsigned integers.
635 * a [I] Initial number.
636 * b [I] Number to divide a by.
639 * The remainder of a divided by b.
641 ULONGLONG WINAPI
_aullrem( ULONGLONG a
, ULONGLONG b
)
646 /******************************************************************************
649 * Shift a 64 bit unsigned integer to the right.
652 * a [I] Initial number.
653 * b [I] Number to shift a by to the right.
656 * The right-shifted value.
658 ULONGLONG WINAPI
_aullshr( ULONGLONG a
, LONG b
)
663 #endif /* __i386__ */