ntoskrnl.exe: Implement ExAcquireFastMutex and ExReleaseFastMutex.
[wine.git] / dlls / ntdll / large_int.c
blobcc77d239f399748222f79f753d086075a24811e1
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winternl.h"
31 #include "wine/asm.h"
33 #ifndef _WIN64
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.
50 * PARAMS
51 * a [I] Initial number.
52 * b [I] Number to add to a.
54 * RETURNS
55 * The sum of a and b.
57 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
59 return a + b;
63 /******************************************************************************
64 * RtlLargeIntegerSubtract (NTDLL.@)
66 * Subtract two 64 bit integers.
68 * PARAMS
69 * a [I] Initial number.
70 * b [I] Number to subtract from a.
72 * RETURNS
73 * The difference of a and b.
75 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
77 return a - b;
81 /******************************************************************************
82 * RtlLargeIntegerNegate (NTDLL.@)
84 * Negate a 64 bit integer.
86 * PARAMS
87 * a [I] Initial number.
89 * RETURNS
90 * The value of a negated.
92 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
94 return -a;
98 /******************************************************************************
99 * RtlLargeIntegerShiftLeft (NTDLL.@)
101 * Perform a shift left on a 64 bit integer.
103 * PARAMS
104 * a [I] Initial number.
105 * count [I] Number of bits to shift by
107 * RETURNS
108 * The value of a following the shift.
110 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
112 return a << count;
116 /******************************************************************************
117 * RtlLargeIntegerShiftRight (NTDLL.@)
119 * Perform a shift right on a 64 bit integer.
121 * PARAMS
122 * a [I] Initial number.
123 * count [I] Number of bits to shift by
125 * RETURNS
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.
139 * PARAMS
140 * a [I] Initial number.
141 * count [I] Number of bits to shift by
143 * RETURNS
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 */
149 return a >> count;
153 /******************************************************************************
154 * RtlLargeIntegerDivide (NTDLL.@)
156 * Divide one 64 bit unsigned integer by another, with remainder.
158 * PARAMS
159 * a [I] Initial number.
160 * b [I] Number to divide a by
161 * rem [O] Destination for remainder
163 * RETURNS
164 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
166 * FIXME
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;
173 return ret;
177 /******************************************************************************
178 * RtlConvertLongToLargeInteger (NTDLL.@)
180 * Convert a 32 bit integer into 64 bits.
182 * PARAMS
183 * a [I] Number to convert
185 * RETURNS
186 * a.
188 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
190 return a;
194 /******************************************************************************
195 * RtlConvertUlongToLargeInteger (NTDLL.@)
197 * Convert a 32 bit unsigned integer into 64 bits.
199 * PARAMS
200 * a [I] Number to convert
202 * RETURNS
203 * a.
205 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
207 return a;
211 /******************************************************************************
212 * RtlEnlargedIntegerMultiply (NTDLL.@)
214 * Multiply two integers giving a 64 bit integer result.
216 * PARAMS
217 * a [I] Initial number.
218 * b [I] Number to multiply a by.
220 * RETURNS
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.
234 * PARAMS
235 * a [I] Initial number.
236 * b [I] Number to multiply a by.
238 * RETURNS
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.
252 * PARAMS
253 * a [I] Initial number.
254 * b [I] Number to divide a by
255 * remptr [O] Destination for remainder
257 * RETURNS
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__)
263 UINT ret, rem;
265 __asm__("divl %4"
266 : "=a" (ret), "=d" (rem)
267 : "0" ((UINT)a), "1" ((UINT)(a >> 32)), "g" (b) );
268 if (remptr) *remptr = rem;
269 return ret;
270 #else
271 UINT ret = a / b;
272 if (remptr) *remptr = a % b;
273 return ret;
274 #endif
278 /******************************************************************************
279 * RtlExtendedLargeIntegerDivide (NTDLL.@)
281 * Divide one 64 bit integer by a 32 bit integer, with remainder.
283 * PARAMS
284 * a [I] Initial number.
285 * b [I] Number to divide a by
286 * rem [O] Destination for remainder
288 * RETURNS
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;
295 return ret;
299 /******************************************************************************
300 * RtlExtendedIntegerMultiply (NTDLL.@)
302 * Multiply one 64 bit integer by another 32 bit integer.
304 * PARAMS
305 * a [I] Initial number.
306 * b [I] Number to multiply a by.
308 * RETURNS
309 * The product of a and b.
311 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
313 return a * b;
317 /******************************************************************************
318 * RtlExtendedMagicDivide (NTDLL.@)
320 * Allows replacing a division by a longlong constant with a multiplication by
321 * the inverse constant.
323 * RETURNS
324 * (dividend * inverse_divisor) >> (64 + shift)
326 * NOTES
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
333 * ULONGLONG.
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;
346 ULONGLONG ah_bl;
347 ULONGLONG al_bh;
348 LONGLONG result;
349 int positive;
351 if (dividend < 0) {
352 dividend_high = UPPER_32((ULONGLONG) -dividend);
353 dividend_low = LOWER_32((ULONGLONG) -dividend);
354 positive = 0;
355 } else {
356 dividend_high = UPPER_32((ULONGLONG) dividend);
357 dividend_low = LOWER_32((ULONGLONG) dividend);
358 positive = 1;
359 } /* if */
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 +
367 UPPER_32(ah_bl) +
368 UPPER_32(al_bh) +
369 UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
370 UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
372 if (positive) {
373 return result;
374 } else {
375 return -result;
376 } /* if */
380 /*************************************************************************
381 * RtlInterlockedCompareExchange64 (NTDLL.@)
383 LONGLONG WINAPI RtlInterlockedCompareExchange64( LONGLONG *dest, LONGLONG xchg, LONGLONG compare )
385 return interlocked_cmpxchg64( dest, xchg, compare );
388 #endif /* _WIN64 */
390 /******************************************************************************
391 * RtlLargeIntegerToChar [NTDLL.@]
393 * Convert an unsigned large integer to a character string.
395 * RETURNS
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.
401 * NOTES
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.
408 * DIFFERENCES
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;
420 CHAR buffer[65];
421 PCHAR pos;
422 CHAR digit;
423 ULONG len;
425 if (base == 0) {
426 base = 10;
427 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
428 return STATUS_INVALID_PARAMETER;
429 } /* if */
431 pos = &buffer[64];
432 *pos = '\0';
434 do {
435 pos--;
436 digit = value % base;
437 value = value / base;
438 if (digit < 10) {
439 *pos = '0' + digit;
440 } else {
441 *pos = 'A' + digit - 10;
442 } /* if */
443 } while (value != 0L);
445 len = &buffer[64] - pos;
446 if (len > length) {
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);
452 } else {
453 memcpy(str, pos, len + 1);
454 } /* if */
455 return STATUS_SUCCESS;
459 /**************************************************************************
460 * RtlInt64ToUnicodeString (NTDLL.@)
462 * Convert a large unsigned integer to a '\0' terminated unicode string.
464 * RETURNS
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).
472 * NOTES
473 * Instead of base 0 it uses 10 as base.
474 * If str is NULL it crashes, as the native function does.
476 * DIFFERENCES
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 */
489 WCHAR buffer[65];
490 PWCHAR pos;
491 WCHAR digit;
493 if (base == 0) {
494 base = 10;
495 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
496 return STATUS_INVALID_PARAMETER;
497 } /* if */
499 pos = &buffer[64];
500 *pos = '\0';
502 do {
503 pos--;
504 digit = value % base;
505 value = value / base;
506 if (digit < 10) {
507 *pos = '0' + digit;
508 } else {
509 *pos = 'A' + digit - 10;
510 } /* if */
511 } while (value != 0L);
513 str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
514 if (str->Length >= str->MaximumLength) {
515 return STATUS_BUFFER_OVERFLOW;
516 } else {
517 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
518 } /* if */
519 return STATUS_SUCCESS;
523 #ifdef __i386__
525 /******************************************************************************
526 * _alldiv (NTDLL.@)
528 * Divide two 64 bit unsigned integers.
530 * PARAMS
531 * a [I] Initial number.
532 * b [I] Number to divide a by.
534 * RETURNS
535 * The dividend of a and b.
537 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
539 return a / b;
543 /******************************************************************************
544 * _allmul (NTDLL.@)
546 * Multiply two 64 bit integers.
548 * PARAMS
549 * a [I] Initial number.
550 * b [I] Number to multiply a by.
552 * RETURNS
553 * The product of a and b.
555 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
557 return a * b;
561 /******************************************************************************
562 * _allrem (NTDLL.@)
564 * Calculate the remainder after dividing two 64 bit integers.
566 * PARAMS
567 * a [I] Initial number.
568 * b [I] Number to divide a by.
570 * RETURNS
571 * The remainder of a divided by b.
573 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
575 return a % b;
579 /******************************************************************************
580 * _aulldiv (NTDLL.@)
582 * Divide two 64 bit unsigned integers.
584 * PARAMS
585 * a [I] Initial number.
586 * b [I] Number to divide a by.
588 * RETURNS
589 * The dividend of a and b.
591 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
593 return a / b;
596 /******************************************************************************
597 * _allshl (NTDLL.@)
599 * Shift a 64 bit integer to the left.
601 * PARAMS
602 * a [I] Initial number.
603 * b [I] Number to shift a by to the left.
605 * RETURNS
606 * The left-shifted value.
608 LONGLONG WINAPI _allshl( LONGLONG a, LONG b )
610 return a << b;
613 /******************************************************************************
614 * _allshr (NTDLL.@)
616 * Shift a 64 bit integer to the right.
618 * PARAMS
619 * a [I] Initial number.
620 * b [I] Number to shift a by to the right.
622 * RETURNS
623 * The right-shifted value.
625 LONGLONG WINAPI _allshr( LONGLONG a, LONG b )
627 return a >> b;
630 /******************************************************************************
631 * _alldvrm (NTDLL.@)
633 * Divide two 64 bit integers.
635 * PARAMS
636 * a [I] Initial number.
637 * b [I] Number to divide a by.
639 * RETURNS
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,
644 "pushl %ebp\n\t"
645 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
646 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
647 "movl %esp,%ebp\n\t"
648 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
649 "pushl 20(%ebp)\n\t"
650 "pushl 16(%ebp)\n\t"
651 "pushl 12(%ebp)\n\t"
652 "pushl 8(%ebp)\n\t"
653 "call " __ASM_NAME("_allrem") "\n\t"
654 "movl %edx,%ebx\n\t"
655 "pushl %eax\n\t"
656 "pushl 20(%ebp)\n\t"
657 "pushl 16(%ebp)\n\t"
658 "pushl 12(%ebp)\n\t"
659 "pushl 8(%ebp)\n\t"
660 "call " __ASM_NAME("_alldiv") "\n\t"
661 "popl %ecx\n\t"
662 "leave\n\t"
663 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
664 __ASM_CFI(".cfi_same_value %ebp\n\t")
665 "ret $16" )
667 /******************************************************************************
668 * _aullrem (NTDLL.@)
670 * Calculate the remainder after dividing two 64 bit unsigned integers.
672 * PARAMS
673 * a [I] Initial number.
674 * b [I] Number to divide a by.
676 * RETURNS
677 * The remainder of a divided by b.
679 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )
681 return a % b;
684 /******************************************************************************
685 * _aullshr (NTDLL.@)
687 * Shift a 64 bit unsigned integer to the right.
689 * PARAMS
690 * a [I] Initial number.
691 * b [I] Number to shift a by to the right.
693 * RETURNS
694 * The right-shifted value.
696 ULONGLONG WINAPI _aullshr( ULONGLONG a, LONG b )
698 return a >> b;
701 /******************************************************************************
702 * _aulldvrm (NTDLL.@)
704 * Divide two 64 bit unsigned integers.
706 * PARAMS
707 * a [I] Initial number.
708 * b [I] Number to divide a by.
710 * RETURNS
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,
715 "pushl %ebp\n\t"
716 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
717 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
718 "movl %esp,%ebp\n\t"
719 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
720 "pushl 20(%ebp)\n\t"
721 "pushl 16(%ebp)\n\t"
722 "pushl 12(%ebp)\n\t"
723 "pushl 8(%ebp)\n\t"
724 "call " __ASM_NAME("_aullrem") "\n\t"
725 "movl %edx,%ebx\n\t"
726 "pushl %eax\n\t"
727 "pushl 20(%ebp)\n\t"
728 "pushl 16(%ebp)\n\t"
729 "pushl 12(%ebp)\n\t"
730 "pushl 8(%ebp)\n\t"
731 "call " __ASM_NAME("_aulldiv") "\n\t"
732 "popl %ecx\n\t"
733 "leave\n\t"
734 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
735 __ASM_CFI(".cfi_same_value %ebp\n\t")
736 "ret $16" )
738 #endif /* __i386__ */