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
30 * Note: we use LONGLONG instead of LARGE_INTEGER, because
31 * the latter is a structure and the calling convention for
32 * returning a structure would not be binary-compatible.
34 * FIXME: for platforms that don't have a native LONGLONG type,
35 * we should define LONGLONG as a structure similar to LARGE_INTEGER
36 * and do everything by hand. You are welcome to do it...
39 /******************************************************************************
40 * RtlLargeIntegerAdd (NTDLL.@)
42 * Add two 64 bit integers.
45 * a [I] Initial number.
46 * b [I] Number to add to a.
51 LONGLONG WINAPI
RtlLargeIntegerAdd( LONGLONG a
, LONGLONG b
)
57 /******************************************************************************
58 * RtlLargeIntegerSubtract (NTDLL.@)
60 * Subtract two 64 bit integers.
63 * a [I] Initial number.
64 * b [I] Number to subtract from a.
67 * The difference of a and b.
69 LONGLONG WINAPI
RtlLargeIntegerSubtract( LONGLONG a
, LONGLONG b
)
75 /******************************************************************************
76 * RtlLargeIntegerNegate (NTDLL.@)
78 * Negate a 64 bit integer.
81 * a [I] Initial number.
84 * The value of a negated.
86 LONGLONG WINAPI
RtlLargeIntegerNegate( LONGLONG a
)
92 /******************************************************************************
93 * RtlLargeIntegerShiftLeft (NTDLL.@)
95 * Perform a shift left on a 64 bit integer.
98 * a [I] Initial number.
99 * count [I] Number of bits to shift by
102 * The value of a following the shift.
104 LONGLONG WINAPI
RtlLargeIntegerShiftLeft( LONGLONG a
, INT count
)
110 /******************************************************************************
111 * RtlLargeIntegerShiftRight (NTDLL.@)
113 * Perform a shift right on a 64 bit integer.
116 * a [I] Initial number.
117 * count [I] Number of bits to shift by
120 * The value of a following the shift.
122 LONGLONG WINAPI
RtlLargeIntegerShiftRight( LONGLONG a
, INT count
)
124 return (ULONGLONG
)a
>> count
;
128 /******************************************************************************
129 * RtlLargeIntegerArithmeticShift (NTDLL.@)
131 * Perform an arithmetic shift right on a 64 bit integer.
134 * a [I] Initial number.
135 * count [I] Number of bits to shift by
138 * The value of a following the shift.
140 LONGLONG WINAPI
RtlLargeIntegerArithmeticShift( LONGLONG a
, INT count
)
142 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
147 /******************************************************************************
148 * RtlLargeIntegerDivide (NTDLL.@)
150 * Divide one 64 bit unsigned integer by another, with remainder.
153 * a [I] Initial number.
154 * b [I] Number to divide a by
155 * rem [O] Destination for remainder
158 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
161 * Should it be signed division instead?
163 ULONGLONG WINAPI
RtlLargeIntegerDivide( ULONGLONG a
, ULONGLONG b
, ULONGLONG
*rem
)
165 ULONGLONG ret
= a
/ b
;
166 if (rem
) *rem
= a
- ret
* b
;
171 /******************************************************************************
172 * RtlConvertLongToLargeInteger (NTDLL.@)
174 * Convert a 32 bit integer into 64 bits.
177 * a [I] Number to convert
182 LONGLONG WINAPI
RtlConvertLongToLargeInteger( LONG a
)
188 /******************************************************************************
189 * RtlConvertUlongToLargeInteger (NTDLL.@)
191 * Convert a 32 bit unsigned integer into 64 bits.
194 * a [I] Number to convert
199 ULONGLONG WINAPI
RtlConvertUlongToLargeInteger( ULONG a
)
205 /******************************************************************************
206 * RtlEnlargedIntegerMultiply (NTDLL.@)
208 * Multiply two integers giving a 64 bit integer result.
211 * a [I] Initial number.
212 * b [I] Number to multiply a by.
215 * The product of a and b.
217 LONGLONG WINAPI
RtlEnlargedIntegerMultiply( INT a
, INT b
)
219 return (LONGLONG
)a
* b
;
223 /******************************************************************************
224 * RtlEnlargedUnsignedMultiply (NTDLL.@)
226 * Multiply two unsigned integers giving a 64 bit unsigned integer result.
229 * a [I] Initial number.
230 * b [I] Number to multiply a by.
233 * The product of a and b.
235 ULONGLONG WINAPI
RtlEnlargedUnsignedMultiply( UINT a
, UINT b
)
237 return (ULONGLONG
)a
* b
;
241 /******************************************************************************
242 * RtlEnlargedUnsignedDivide (NTDLL.@)
244 * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
247 * a [I] Initial number.
248 * b [I] Number to divide a by
249 * remptr [O] Destination for remainder
252 * The dividend of a and b. If remptr is non-NULL it is set to the remainder.
254 UINT WINAPI
RtlEnlargedUnsignedDivide( ULONGLONG a
, UINT b
, UINT
*remptr
)
256 #if defined(__i386__) && defined(__GNUC__)
257 UINT ret
, rem
, p1
, p2
;
260 p2
= a
& 0xffffffffLL
;
262 __asm__("div %4,%%eax"
263 : "=a" (ret
), "=d" (rem
)
264 : "0" (p2
), "1" (p1
), "g" (b
) );
265 if (remptr
) *remptr
= rem
;
269 if (remptr
) *remptr
= a
% b
;
275 /******************************************************************************
276 * RtlExtendedLargeIntegerDivide (NTDLL.@)
278 * Divide one 64 bit integer by a 32 bit integer, with remainder.
281 * a [I] Initial number.
282 * b [I] Number to divide a by
283 * rem [O] Destination for remainder
286 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
288 LONGLONG WINAPI
RtlExtendedLargeIntegerDivide( LONGLONG a
, INT b
, INT
*rem
)
290 LONGLONG ret
= a
/ b
;
291 if (rem
) *rem
= a
- b
* ret
;
296 /******************************************************************************
297 * RtlExtendedIntegerMultiply (NTDLL.@)
299 * Multiply one 64 bit integer by another 32 bit integer.
302 * a [I] Initial number.
303 * b [I] Number to multiply a by.
306 * The product of a and b.
308 LONGLONG WINAPI
RtlExtendedIntegerMultiply( LONGLONG a
, INT b
)
314 /******************************************************************************
315 * RtlExtendedMagicDivide (NTDLL.@)
317 * Allows replacing a division by a longlong constant with a multiplication by
318 * the inverse constant.
321 * (dividend * inverse_divisor) >> (64 + shift)
324 * If the divisor of a division is constant, the constants inverse_divisor and
325 * shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
326 * Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
327 * dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
329 * The Parameter inverse_divisor although defined as LONGLONG is used as
332 #define LOWER_32(A) ((A) & 0xffffffff)
333 #define UPPER_32(A) ((A) >> 32)
334 LONGLONG WINAPI
RtlExtendedMagicDivide(
335 LONGLONG dividend
, /* [I] Dividend to be divided by the constant divisor */
336 LONGLONG inverse_divisor
, /* [I] Constant computed manually as 2^(64+shift) / divisor */
337 INT shift
) /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
339 ULONGLONG dividend_high
;
340 ULONGLONG dividend_low
;
341 ULONGLONG inverse_divisor_high
;
342 ULONGLONG inverse_divisor_low
;
349 dividend_high
= UPPER_32((ULONGLONG
) -dividend
);
350 dividend_low
= LOWER_32((ULONGLONG
) -dividend
);
353 dividend_high
= UPPER_32((ULONGLONG
) dividend
);
354 dividend_low
= LOWER_32((ULONGLONG
) dividend
);
357 inverse_divisor_high
= UPPER_32((ULONGLONG
) inverse_divisor
);
358 inverse_divisor_low
= LOWER_32((ULONGLONG
) inverse_divisor
);
360 ah_bl
= dividend_high
* inverse_divisor_low
;
361 al_bh
= dividend_low
* inverse_divisor_high
;
363 result
= (LONGLONG
) ((dividend_high
* inverse_divisor_high
+
366 UPPER_32(LOWER_32(ah_bl
) + LOWER_32(al_bh
) +
367 UPPER_32(dividend_low
* inverse_divisor_low
))) >> shift
);
377 /******************************************************************************
378 * RtlLargeIntegerToChar [NTDLL.@]
380 * Convert an unsigned large integer to a character string.
383 * Success: STATUS_SUCCESS. str contains the converted number
384 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
385 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
386 * STATUS_ACCESS_VIOLATION, if str is NULL.
389 * Instead of base 0 it uses 10 as base.
390 * Writes at most length characters to the string str.
391 * Str is '\0' terminated when length allows it.
392 * When str fits exactly in length characters the '\0' is omitted.
393 * If value_ptr is NULL it crashes, as the native function does.
396 * - Accept base 0 as 10 instead of crashing as native function does.
397 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
398 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
400 NTSTATUS WINAPI
RtlLargeIntegerToChar(
401 const ULONGLONG
*value_ptr
, /* [I] Pointer to the value to be converted */
402 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
403 ULONG length
, /* [I] Length of the str buffer in bytes */
404 PCHAR str
) /* [O] Destination for the converted value */
406 ULONGLONG value
= *value_ptr
;
414 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
415 return STATUS_INVALID_PARAMETER
;
423 digit
= value
% base
;
424 value
= value
/ base
;
428 *pos
= 'A' + digit
- 10;
430 } while (value
!= 0L);
432 len
= &buffer
[64] - pos
;
434 return STATUS_BUFFER_OVERFLOW
;
435 } else if (str
== NULL
) {
436 return STATUS_ACCESS_VIOLATION
;
437 } else if (len
== length
) {
438 memcpy(str
, pos
, len
);
440 memcpy(str
, pos
, len
+ 1);
442 return STATUS_SUCCESS
;
446 /**************************************************************************
447 * RtlInt64ToUnicodeString (NTDLL.@)
449 * Convert a large unsigned integer to a '\0' terminated unicode string.
452 * Success: STATUS_SUCCESS. str contains the converted number
453 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
454 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
455 * (with the '\0' termination). In this case str->Length
456 * is set to the length, the string would have (which can
457 * be larger than the MaximumLength).
460 * Instead of base 0 it uses 10 as base.
461 * If str is NULL it crashes, as the native function does.
464 * - Accept base 0 as 10 instead of crashing as native function does.
465 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
466 * The native function does this when the string would be longer than 31
467 * characters even when the string parameter is long enough.
468 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
469 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
471 NTSTATUS WINAPI
RtlInt64ToUnicodeString(
472 ULONGLONG value
, /* [I] Value to be converted */
473 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
474 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
482 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
483 return STATUS_INVALID_PARAMETER
;
491 digit
= value
% base
;
492 value
= value
/ base
;
496 *pos
= 'A' + digit
- 10;
498 } while (value
!= 0L);
500 str
->Length
= (&buffer
[64] - pos
) * sizeof(WCHAR
);
501 if (str
->Length
>= str
->MaximumLength
) {
502 return STATUS_BUFFER_OVERFLOW
;
504 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
506 return STATUS_SUCCESS
;
510 /******************************************************************************
513 * Divide two 64 bit unsigned integers.
516 * a [I] Initial number.
517 * b [I] Number to multiply a by.
520 * The dividend of a and b.
522 LONGLONG WINAPI
_alldiv( LONGLONG a
, LONGLONG b
)
528 /******************************************************************************
531 * Multiply two 64 bit integers.
534 * a [I] Initial number.
535 * b [I] Number to multiply a by.
538 * The product of a and b.
540 LONGLONG WINAPI
_allmul( LONGLONG a
, LONGLONG b
)
546 /******************************************************************************
549 * Calculate the remainder after dividing two 64 bit integers.
552 * a [I] Initial number.
553 * b [I] Number to divide a by.
556 * The remainder of a divided by b.
558 LONGLONG WINAPI
_allrem( LONGLONG a
, LONGLONG b
)
564 /******************************************************************************
567 * Divide two 64 bit unsigned integers.
570 * a [I] Initial number.
571 * b [I] Number to multiply a by.
574 * The dividend of a and b.
576 ULONGLONG WINAPI
_aulldiv( ULONGLONG a
, ULONGLONG b
)
582 /******************************************************************************
585 * Calculate the remainder after dividing two 64 bit unsigned integers.
588 * a [I] Initial number.
589 * b [I] Number to divide a by.
592 * The remainder of a divided by b.
594 ULONGLONG WINAPI
_aullrem( ULONGLONG a
, ULONGLONG b
)