winecfg: Update Portuguese translation.
[wine/multimedia.git] / dlls / ntdll / large_int.c
blob46068b3a1529d1d070582ad3e2bbf31b6c7a27b0
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 <stdarg.h>
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winternl.h"
29 #ifndef _WIN64
32 * Note: we use LONGLONG instead of LARGE_INTEGER, because
33 * the latter is a structure and the calling convention for
34 * returning a structure would not be binary-compatible.
36 * FIXME: for platforms that don't have a native LONGLONG type,
37 * we should define LONGLONG as a structure similar to LARGE_INTEGER
38 * and do everything by hand. You are welcome to do it...
41 /******************************************************************************
42 * RtlLargeIntegerAdd (NTDLL.@)
44 * Add two 64 bit integers.
46 * PARAMS
47 * a [I] Initial number.
48 * b [I] Number to add to a.
50 * RETURNS
51 * The sum of a and b.
53 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
55 return a + b;
59 /******************************************************************************
60 * RtlLargeIntegerSubtract (NTDLL.@)
62 * Subtract two 64 bit integers.
64 * PARAMS
65 * a [I] Initial number.
66 * b [I] Number to subtract from a.
68 * RETURNS
69 * The difference of a and b.
71 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
73 return a - b;
77 /******************************************************************************
78 * RtlLargeIntegerNegate (NTDLL.@)
80 * Negate a 64 bit integer.
82 * PARAMS
83 * a [I] Initial number.
85 * RETURNS
86 * The value of a negated.
88 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
90 return -a;
94 /******************************************************************************
95 * RtlLargeIntegerShiftLeft (NTDLL.@)
97 * Perform a shift left on a 64 bit integer.
99 * PARAMS
100 * a [I] Initial number.
101 * count [I] Number of bits to shift by
103 * RETURNS
104 * The value of a following the shift.
106 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
108 return a << count;
112 /******************************************************************************
113 * RtlLargeIntegerShiftRight (NTDLL.@)
115 * Perform a shift right on a 64 bit integer.
117 * PARAMS
118 * a [I] Initial number.
119 * count [I] Number of bits to shift by
121 * RETURNS
122 * The value of a following the shift.
124 LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
126 return (ULONGLONG)a >> count;
130 /******************************************************************************
131 * RtlLargeIntegerArithmeticShift (NTDLL.@)
133 * Perform an arithmetic shift right on a 64 bit integer.
135 * PARAMS
136 * a [I] Initial number.
137 * count [I] Number of bits to shift by
139 * RETURNS
140 * The value of a following the shift.
142 LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
144 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
145 return a >> count;
149 /******************************************************************************
150 * RtlLargeIntegerDivide (NTDLL.@)
152 * Divide one 64 bit unsigned integer by another, with remainder.
154 * PARAMS
155 * a [I] Initial number.
156 * b [I] Number to divide a by
157 * rem [O] Destination for remainder
159 * RETURNS
160 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
162 * FIXME
163 * Should it be signed division instead?
165 ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
167 ULONGLONG ret = a / b;
168 if (rem) *rem = a - ret * b;
169 return ret;
173 /******************************************************************************
174 * RtlConvertLongToLargeInteger (NTDLL.@)
176 * Convert a 32 bit integer into 64 bits.
178 * PARAMS
179 * a [I] Number to convert
181 * RETURNS
182 * a.
184 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
186 return a;
190 /******************************************************************************
191 * RtlConvertUlongToLargeInteger (NTDLL.@)
193 * Convert a 32 bit unsigned integer into 64 bits.
195 * PARAMS
196 * a [I] Number to convert
198 * RETURNS
199 * a.
201 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
203 return a;
207 /******************************************************************************
208 * RtlEnlargedIntegerMultiply (NTDLL.@)
210 * Multiply two integers giving a 64 bit integer result.
212 * PARAMS
213 * a [I] Initial number.
214 * b [I] Number to multiply a by.
216 * RETURNS
217 * The product of a and b.
219 LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
221 return (LONGLONG)a * b;
225 /******************************************************************************
226 * RtlEnlargedUnsignedMultiply (NTDLL.@)
228 * Multiply two unsigned integers giving a 64 bit unsigned integer result.
230 * PARAMS
231 * a [I] Initial number.
232 * b [I] Number to multiply a by.
234 * RETURNS
235 * The product of a and b.
237 ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
239 return (ULONGLONG)a * b;
243 /******************************************************************************
244 * RtlEnlargedUnsignedDivide (NTDLL.@)
246 * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
248 * PARAMS
249 * a [I] Initial number.
250 * b [I] Number to divide a by
251 * remptr [O] Destination for remainder
253 * RETURNS
254 * The dividend of a and b. If remptr is non-NULL it is set to the remainder.
256 UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
258 #if defined(__i386__) && defined(__GNUC__)
259 UINT ret, rem, p1, p2;
261 p1 = a >> 32;
262 p2 = a & 0xffffffffLL;
264 __asm__("div %4,%%eax"
265 : "=a" (ret), "=d" (rem)
266 : "0" (p2), "1" (p1), "g" (b) );
267 if (remptr) *remptr = rem;
268 return ret;
269 #else
270 UINT ret = a / b;
271 if (remptr) *remptr = a % b;
272 return ret;
273 #endif
277 /******************************************************************************
278 * RtlExtendedLargeIntegerDivide (NTDLL.@)
280 * Divide one 64 bit integer by a 32 bit integer, with remainder.
282 * PARAMS
283 * a [I] Initial number.
284 * b [I] Number to divide a by
285 * rem [O] Destination for remainder
287 * RETURNS
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;
294 return ret;
298 /******************************************************************************
299 * RtlExtendedIntegerMultiply (NTDLL.@)
301 * Multiply one 64 bit integer by another 32 bit integer.
303 * PARAMS
304 * a [I] Initial number.
305 * b [I] Number to multiply a by.
307 * RETURNS
308 * The product of a and b.
310 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
312 return a * b;
316 /******************************************************************************
317 * RtlExtendedMagicDivide (NTDLL.@)
319 * Allows replacing a division by a longlong constant with a multiplication by
320 * the inverse constant.
322 * RETURNS
323 * (dividend * inverse_divisor) >> (64 + shift)
325 * NOTES
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
332 * ULONGLONG.
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;
345 ULONGLONG ah_bl;
346 ULONGLONG al_bh;
347 LONGLONG result;
348 int positive;
350 if (dividend < 0) {
351 dividend_high = UPPER_32((ULONGLONG) -dividend);
352 dividend_low = LOWER_32((ULONGLONG) -dividend);
353 positive = 0;
354 } else {
355 dividend_high = UPPER_32((ULONGLONG) dividend);
356 dividend_low = LOWER_32((ULONGLONG) dividend);
357 positive = 1;
358 } /* if */
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 +
366 UPPER_32(ah_bl) +
367 UPPER_32(al_bh) +
368 UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
369 UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
371 if (positive) {
372 return result;
373 } else {
374 return -result;
375 } /* if */
378 #endif /* _WIN64 */
380 /******************************************************************************
381 * RtlLargeIntegerToChar [NTDLL.@]
383 * Convert an unsigned large integer to a character string.
385 * RETURNS
386 * Success: STATUS_SUCCESS. str contains the converted number
387 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
388 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
389 * STATUS_ACCESS_VIOLATION, if str is NULL.
391 * NOTES
392 * Instead of base 0 it uses 10 as base.
393 * Writes at most length characters to the string str.
394 * Str is '\0' terminated when length allows it.
395 * When str fits exactly in length characters the '\0' is omitted.
396 * If value_ptr is NULL it crashes, as the native function does.
398 * DIFFERENCES
399 * - Accept base 0 as 10 instead of crashing as native function does.
400 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
401 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
403 NTSTATUS WINAPI RtlLargeIntegerToChar(
404 const ULONGLONG *value_ptr, /* [I] Pointer to the value to be converted */
405 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
406 ULONG length, /* [I] Length of the str buffer in bytes */
407 PCHAR str) /* [O] Destination for the converted value */
409 ULONGLONG value = *value_ptr;
410 CHAR buffer[65];
411 PCHAR pos;
412 CHAR digit;
413 ULONG len;
415 if (base == 0) {
416 base = 10;
417 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
418 return STATUS_INVALID_PARAMETER;
419 } /* if */
421 pos = &buffer[64];
422 *pos = '\0';
424 do {
425 pos--;
426 digit = value % base;
427 value = value / base;
428 if (digit < 10) {
429 *pos = '0' + digit;
430 } else {
431 *pos = 'A' + digit - 10;
432 } /* if */
433 } while (value != 0L);
435 len = &buffer[64] - pos;
436 if (len > length) {
437 return STATUS_BUFFER_OVERFLOW;
438 } else if (str == NULL) {
439 return STATUS_ACCESS_VIOLATION;
440 } else if (len == length) {
441 memcpy(str, pos, len);
442 } else {
443 memcpy(str, pos, len + 1);
444 } /* if */
445 return STATUS_SUCCESS;
449 /**************************************************************************
450 * RtlInt64ToUnicodeString (NTDLL.@)
452 * Convert a large unsigned integer to a '\0' terminated unicode string.
454 * RETURNS
455 * Success: STATUS_SUCCESS. str contains the converted number
456 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
457 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
458 * (with the '\0' termination). In this case str->Length
459 * is set to the length, the string would have (which can
460 * be larger than the MaximumLength).
462 * NOTES
463 * Instead of base 0 it uses 10 as base.
464 * If str is NULL it crashes, as the native function does.
466 * DIFFERENCES
467 * - Accept base 0 as 10 instead of crashing as native function does.
468 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
469 * The native function does this when the string would be longer than 31
470 * characters even when the string parameter is long enough.
471 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
472 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
474 NTSTATUS WINAPI RtlInt64ToUnicodeString(
475 ULONGLONG value, /* [I] Value to be converted */
476 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
477 UNICODE_STRING *str) /* [O] Destination for the converted value */
479 WCHAR buffer[65];
480 PWCHAR pos;
481 WCHAR digit;
483 if (base == 0) {
484 base = 10;
485 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
486 return STATUS_INVALID_PARAMETER;
487 } /* if */
489 pos = &buffer[64];
490 *pos = '\0';
492 do {
493 pos--;
494 digit = value % base;
495 value = value / base;
496 if (digit < 10) {
497 *pos = '0' + digit;
498 } else {
499 *pos = 'A' + digit - 10;
500 } /* if */
501 } while (value != 0L);
503 str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
504 if (str->Length >= str->MaximumLength) {
505 return STATUS_BUFFER_OVERFLOW;
506 } else {
507 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
508 } /* if */
509 return STATUS_SUCCESS;
513 #ifdef __i386__
515 /******************************************************************************
516 * _alldiv (NTDLL.@)
518 * Divide two 64 bit unsigned integers.
520 * PARAMS
521 * a [I] Initial number.
522 * b [I] Number to multiply a by.
524 * RETURNS
525 * The dividend of a and b.
527 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
529 return a / b;
533 /******************************************************************************
534 * _allmul (NTDLL.@)
536 * Multiply two 64 bit integers.
538 * PARAMS
539 * a [I] Initial number.
540 * b [I] Number to multiply a by.
542 * RETURNS
543 * The product of a and b.
545 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
547 return a * b;
551 /******************************************************************************
552 * _allrem (NTDLL.@)
554 * Calculate the remainder after dividing two 64 bit integers.
556 * PARAMS
557 * a [I] Initial number.
558 * b [I] Number to divide a by.
560 * RETURNS
561 * The remainder of a divided by b.
563 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
565 return a % b;
569 /******************************************************************************
570 * _aulldiv (NTDLL.@)
572 * Divide two 64 bit unsigned integers.
574 * PARAMS
575 * a [I] Initial number.
576 * b [I] Number to multiply a by.
578 * RETURNS
579 * The dividend of a and b.
581 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
583 return a / b;
587 /******************************************************************************
588 * _aullrem (NTDLL.@)
590 * Calculate the remainder after dividing two 64 bit unsigned integers.
592 * PARAMS
593 * a [I] Initial number.
594 * b [I] Number to divide a by.
596 * RETURNS
597 * The remainder of a divided by b.
599 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )
601 return a % b;
604 #endif /* __i386__ */