riched20: Sign-compare warnings fix.
[wine/multimedia.git] / dlls / ntdll / large_int.c
blobb5da2d635b41c4288d1a2fa1a689bbcf8d299140
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"
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.
44 * PARAMS
45 * a [I] Initial number.
46 * b [I] Number to add to a.
48 * RETURNS
49 * The sum of a and b.
51 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
53 return a + b;
57 /******************************************************************************
58 * RtlLargeIntegerSubtract (NTDLL.@)
60 * Subtract two 64 bit integers.
62 * PARAMS
63 * a [I] Initial number.
64 * b [I] Number to subtract from a.
66 * RETURNS
67 * The difference of a and b.
69 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
71 return a - b;
75 /******************************************************************************
76 * RtlLargeIntegerNegate (NTDLL.@)
78 * Negate a 64 bit integer.
80 * PARAMS
81 * a [I] Initial number.
83 * RETURNS
84 * The value of a negated.
86 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
88 return -a;
92 /******************************************************************************
93 * RtlLargeIntegerShiftLeft (NTDLL.@)
95 * Perform a shift left on a 64 bit integer.
97 * PARAMS
98 * a [I] Initial number.
99 * count [I] Number of bits to shift by
101 * RETURNS
102 * The value of a following the shift.
104 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
106 return a << count;
110 /******************************************************************************
111 * RtlLargeIntegerShiftRight (NTDLL.@)
113 * Perform a shift right on a 64 bit integer.
115 * PARAMS
116 * a [I] Initial number.
117 * count [I] Number of bits to shift by
119 * RETURNS
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.
133 * PARAMS
134 * a [I] Initial number.
135 * count [I] Number of bits to shift by
137 * RETURNS
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 */
143 return a >> count;
147 /******************************************************************************
148 * RtlLargeIntegerDivide (NTDLL.@)
150 * Divide one 64 bit unsigned integer by another, with remainder.
152 * PARAMS
153 * a [I] Initial number.
154 * b [I] Number to divide a by
155 * rem [O] Destination for remainder
157 * RETURNS
158 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
160 * FIXME
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;
167 return ret;
171 /******************************************************************************
172 * RtlConvertLongToLargeInteger (NTDLL.@)
174 * Convert a 32 bit integer into 64 bits.
176 * PARAMS
177 * a [I] Number to convert
179 * RETURNS
180 * a.
182 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
184 return a;
188 /******************************************************************************
189 * RtlConvertUlongToLargeInteger (NTDLL.@)
191 * Convert a 32 bit unsigned integer into 64 bits.
193 * PARAMS
194 * a [I] Number to convert
196 * RETURNS
197 * a.
199 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
201 return a;
205 /******************************************************************************
206 * RtlEnlargedIntegerMultiply (NTDLL.@)
208 * Multiply two integers giving a 64 bit integer result.
210 * PARAMS
211 * a [I] Initial number.
212 * b [I] Number to multiply a by.
214 * RETURNS
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.
228 * PARAMS
229 * a [I] Initial number.
230 * b [I] Number to multiply a by.
232 * RETURNS
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.
246 * PARAMS
247 * a [I] Initial number.
248 * b [I] Number to divide a by
249 * remptr [O] Destination for remainder
251 * RETURNS
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;
259 p1 = a >> 32;
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;
266 return ret;
267 #else
268 UINT ret = a / b;
269 if (remptr) *remptr = a % b;
270 return ret;
271 #endif
275 /******************************************************************************
276 * RtlExtendedLargeIntegerDivide (NTDLL.@)
278 * Divide one 64 bit integer by a 32 bit integer, with remainder.
280 * PARAMS
281 * a [I] Initial number.
282 * b [I] Number to divide a by
283 * rem [O] Destination for remainder
285 * RETURNS
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;
292 return ret;
296 /******************************************************************************
297 * RtlExtendedIntegerMultiply (NTDLL.@)
299 * Multiply one 64 bit integer by another 32 bit integer.
301 * PARAMS
302 * a [I] Initial number.
303 * b [I] Number to multiply a by.
305 * RETURNS
306 * The product of a and b.
308 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
310 return a * b;
314 /******************************************************************************
315 * RtlExtendedMagicDivide (NTDLL.@)
317 * Allows replacing a division by a longlong constant with a multiplication by
318 * the inverse constant.
320 * RETURNS
321 * (dividend * inverse_divisor) >> (64 + shift)
323 * NOTES
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
330 * ULONGLONG.
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;
343 ULONGLONG ah_bl;
344 ULONGLONG al_bh;
345 LONGLONG result;
346 int positive;
348 if (dividend < 0) {
349 dividend_high = UPPER_32((ULONGLONG) -dividend);
350 dividend_low = LOWER_32((ULONGLONG) -dividend);
351 positive = 0;
352 } else {
353 dividend_high = UPPER_32((ULONGLONG) dividend);
354 dividend_low = LOWER_32((ULONGLONG) dividend);
355 positive = 1;
356 } /* if */
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 +
364 UPPER_32(ah_bl) +
365 UPPER_32(al_bh) +
366 UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
367 UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
369 if (positive) {
370 return result;
371 } else {
372 return -result;
373 } /* if */
377 /******************************************************************************
378 * RtlLargeIntegerToChar [NTDLL.@]
380 * Convert an unsigned large integer to a character string.
382 * RETURNS
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.
388 * NOTES
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.
395 * DIFFERENCES
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;
407 CHAR buffer[65];
408 PCHAR pos;
409 CHAR digit;
410 ULONG len;
412 if (base == 0) {
413 base = 10;
414 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
415 return STATUS_INVALID_PARAMETER;
416 } /* if */
418 pos = &buffer[64];
419 *pos = '\0';
421 do {
422 pos--;
423 digit = value % base;
424 value = value / base;
425 if (digit < 10) {
426 *pos = '0' + digit;
427 } else {
428 *pos = 'A' + digit - 10;
429 } /* if */
430 } while (value != 0L);
432 len = &buffer[64] - pos;
433 if (len > length) {
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);
439 } else {
440 memcpy(str, pos, len + 1);
441 } /* if */
442 return STATUS_SUCCESS;
446 /**************************************************************************
447 * RtlInt64ToUnicodeString (NTDLL.@)
449 * Convert a large unsigned integer to a '\0' terminated unicode string.
451 * RETURNS
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).
459 * NOTES
460 * Instead of base 0 it uses 10 as base.
461 * If str is NULL it crashes, as the native function does.
463 * DIFFERENCES
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 */
476 WCHAR buffer[65];
477 PWCHAR pos;
478 WCHAR digit;
480 if (base == 0) {
481 base = 10;
482 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
483 return STATUS_INVALID_PARAMETER;
484 } /* if */
486 pos = &buffer[64];
487 *pos = '\0';
489 do {
490 pos--;
491 digit = value % base;
492 value = value / base;
493 if (digit < 10) {
494 *pos = '0' + digit;
495 } else {
496 *pos = 'A' + digit - 10;
497 } /* if */
498 } while (value != 0L);
500 str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
501 if (str->Length >= str->MaximumLength) {
502 return STATUS_BUFFER_OVERFLOW;
503 } else {
504 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
505 } /* if */
506 return STATUS_SUCCESS;
510 /******************************************************************************
511 * _alldiv (NTDLL.@)
513 * Divide two 64 bit unsigned integers.
515 * PARAMS
516 * a [I] Initial number.
517 * b [I] Number to multiply a by.
519 * RETURNS
520 * The dividend of a and b.
522 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
524 return a / b;
528 /******************************************************************************
529 * _allmul (NTDLL.@)
531 * Multiply two 64 bit integers.
533 * PARAMS
534 * a [I] Initial number.
535 * b [I] Number to multiply a by.
537 * RETURNS
538 * The product of a and b.
540 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
542 return a * b;
546 /******************************************************************************
547 * _allrem (NTDLL.@)
549 * Calculate the remainder after dividing two 64 bit integers.
551 * PARAMS
552 * a [I] Initial number.
553 * b [I] Number to divide a by.
555 * RETURNS
556 * The remainder of a divided by b.
558 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
560 return a % b;
564 /******************************************************************************
565 * _aulldiv (NTDLL.@)
567 * Divide two 64 bit unsigned integers.
569 * PARAMS
570 * a [I] Initial number.
571 * b [I] Number to multiply a by.
573 * RETURNS
574 * The dividend of a and b.
576 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
578 return a / b;
582 /******************************************************************************
583 * _aullrem (NTDLL.@)
585 * Calculate the remainder after dividing two 64 bit unsigned integers.
587 * PARAMS
588 * a [I] Initial number.
589 * b [I] Number to divide a by.
591 * RETURNS
592 * The remainder of a divided by b.
594 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )
596 return a % b;