Implement the beginnings of the stateblock class, and a first method
[wine/hacks.git] / dlls / ntdll / large_int.c
blob02bc584ec3049ac57e010d1814d326cf1cbad0e3
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdarg.h>
24 #include "ntstatus.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "winternl.h"
31 * Note: we use LONGLONG instead of LARGE_INTEGER, because
32 * the latter is a structure and the calling convention for
33 * returning a structure would not be binary-compatible.
35 * FIXME: for platforms that don't have a native LONGLONG type,
36 * we should define LONGLONG as a structure similar to LARGE_INTEGER
37 * and do everything by hand. You are welcome to do it...
40 /******************************************************************************
41 * RtlLargeIntegerAdd (NTDLL.@)
43 * Add two 64 bit integers.
45 * PARAMS
46 * a [I] Initial number.
47 * b [I] Number to add to a.
49 * RETURNS
50 * The sum of a and b.
52 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
54 return a + b;
58 /******************************************************************************
59 * RtlLargeIntegerSubtract (NTDLL.@)
61 * Subtract two 64 bit integers.
63 * PARAMS
64 * a [I] Initial number.
65 * b [I] Number to subtract from a.
67 * RETURNS
68 * The difference of a and b.
70 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
72 return a - b;
76 /******************************************************************************
77 * RtlLargeIntegerNegate (NTDLL.@)
79 * Negate a 64 bit integer.
81 * PARAMS
82 * a [I] Initial number.
84 * RETURNS
85 * The value of a negated.
87 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
89 return -a;
93 /******************************************************************************
94 * RtlLargeIntegerShiftLeft (NTDLL.@)
96 * Perform a shift left on a 64 bit integer.
98 * PARAMS
99 * a [I] Initial number.
100 * count [I] Number of bits to shift by
102 * RETURNS
103 * The value of a following the shift.
105 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
107 return a << count;
111 /******************************************************************************
112 * RtlLargeIntegerShiftRight (NTDLL.@)
114 * Perform a shift right on a 64 bit integer.
116 * PARAMS
117 * a [I] Initial number.
118 * count [I] Number of bits to shift by
120 * RETURNS
121 * The value of a following the shift.
123 LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
125 return (ULONGLONG)a >> count;
129 /******************************************************************************
130 * RtlLargeIntegerArithmeticShift (NTDLL.@)
132 * Perform an arithmetic shift right on a 64 bit integer.
134 * PARAMS
135 * a [I] Initial number.
136 * count [I] Number of bits to shift by
138 * RETURNS
139 * The value of a following the shift.
141 LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
143 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
144 return a >> count;
148 /******************************************************************************
149 * RtlLargeIntegerDivide (NTDLL.@)
151 * Divide one 64 bit unsigned integer by another, with remainder.
153 * PARAMS
154 * a [I] Initial number.
155 * b [I] Number to divide a by
156 * rem [O] Destination for remainder
158 * RETURNS
159 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
161 * FIXME
162 * Should it be signed division instead?
164 ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
166 ULONGLONG ret = a / b;
167 if (rem) *rem = a - ret * b;
168 return ret;
172 /******************************************************************************
173 * RtlConvertLongToLargeInteger (NTDLL.@)
175 * Convert a 32 bit integer into 64 bits.
177 * PARAMS
178 * a [I] Number to convert
180 * RETURNS
181 * a.
183 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
185 return a;
189 /******************************************************************************
190 * RtlConvertUlongToLargeInteger (NTDLL.@)
192 * Convert a 32 bit unsigned integer into 64 bits.
194 * PARAMS
195 * a [I] Number to convert
197 * RETURNS
198 * a.
200 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
202 return a;
206 /******************************************************************************
207 * RtlEnlargedIntegerMultiply (NTDLL.@)
209 * Multiply two integers giving a 64 bit integer result.
211 * PARAMS
212 * a [I] Initial number.
213 * b [I] Number to multiply a by.
215 * RETURNS
216 * The product of a and b.
218 LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
220 return (LONGLONG)a * b;
224 /******************************************************************************
225 * RtlEnlargedUnsignedMultiply (NTDLL.@)
227 * Multiply two unsigned integers giving a 64 bit unsigned integer result.
229 * PARAMS
230 * a [I] Initial number.
231 * b [I] Number to multiply a by.
233 * RETURNS
234 * The product of a and b.
236 ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
238 return (ULONGLONG)a * b;
242 /******************************************************************************
243 * RtlEnlargedUnsignedDivide (NTDLL.@)
245 * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
247 * PARAMS
248 * a [I] Initial number.
249 * b [I] Number to divide a by
250 * remptr [O] Destination for remainder
252 * RETURNS
253 * The dividend of a and b. If remptr is non-NULL it is set to the remainder.
255 UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
257 #if defined(__i386__) && defined(__GNUC__)
258 UINT ret, rem, p1, p2;
260 p1 = a >> 32;
261 p2 = a & 0xffffffffLL;
263 __asm__("div %4,%%eax"
264 : "=a" (ret), "=d" (rem)
265 : "0" (p2), "1" (p1), "g" (b) );
266 if (remptr) *remptr = rem;
267 return ret;
268 #else
269 UINT ret = a / b;
270 if (remptr) *remptr = a % b;
271 return ret;
272 #endif
276 /******************************************************************************
277 * RtlExtendedLargeIntegerDivide (NTDLL.@)
279 * Divide one 64 bit integer by a 32 bit integer, with remainder.
281 * PARAMS
282 * a [I] Initial number.
283 * b [I] Number to divide a by
284 * rem [O] Destination for remainder
286 * RETURNS
287 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
289 LONGLONG WINAPI RtlExtendedLargeIntegerDivide( LONGLONG a, INT b, INT *rem )
291 LONGLONG ret = a / b;
292 if (rem) *rem = a - b * ret;
293 return ret;
297 /******************************************************************************
298 * RtlExtendedIntegerMultiply (NTDLL.@)
300 * Multiply one 64 bit integer by another 32 bit integer.
302 * PARAMS
303 * a [I] Initial number.
304 * b [I] Number to multiply a by.
306 * RETURNS
307 * The product of a and b.
309 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
311 return a * b;
315 /******************************************************************************
316 * RtlExtendedMagicDivide (NTDLL.@)
318 * Allows replacing a division by a longlong constant with a multiplication by
319 * the inverse constant.
321 * RETURNS
322 * (dividend * inverse_divisor) >> (64 + shift)
324 * NOTES
325 * If the divisor of a division is constant, the constants inverse_divisor and
326 * shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
327 * Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
328 * dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
330 * The Parameter inverse_divisor although defined as LONGLONG is used as
331 * ULONGLONG.
333 #define LOWER_32(A) ((A) & 0xffffffff)
334 #define UPPER_32(A) ((A) >> 32)
335 LONGLONG WINAPI RtlExtendedMagicDivide(
336 LONGLONG dividend, /* [I] Dividend to be divided by the constant divisor */
337 LONGLONG inverse_divisor, /* [I] Constant computed manually as 2^(64+shift) / divisor */
338 INT shift) /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
340 ULONGLONG dividend_high;
341 ULONGLONG dividend_low;
342 ULONGLONG inverse_divisor_high;
343 ULONGLONG inverse_divisor_low;
344 ULONGLONG ah_bl;
345 ULONGLONG al_bh;
346 LONGLONG result;
347 int positive;
349 if (dividend < 0) {
350 dividend_high = UPPER_32((ULONGLONG) -dividend);
351 dividend_low = LOWER_32((ULONGLONG) -dividend);
352 positive = 0;
353 } else {
354 dividend_high = UPPER_32((ULONGLONG) dividend);
355 dividend_low = LOWER_32((ULONGLONG) dividend);
356 positive = 1;
357 } /* if */
358 inverse_divisor_high = UPPER_32((ULONGLONG) inverse_divisor);
359 inverse_divisor_low = LOWER_32((ULONGLONG) inverse_divisor);
361 ah_bl = dividend_high * inverse_divisor_low;
362 al_bh = dividend_low * inverse_divisor_high;
364 result = (LONGLONG) ((dividend_high * inverse_divisor_high +
365 UPPER_32(ah_bl) +
366 UPPER_32(al_bh) +
367 UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
368 UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
370 if (positive) {
371 return result;
372 } else {
373 return -result;
374 } /* if */
378 /******************************************************************************
379 * RtlLargeIntegerToChar [NTDLL.@]
381 * Convert an unsigned large integer to a character string.
383 * RETURNS
384 * Success: STATUS_SUCCESS. str contains the converted number
385 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
386 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
387 * STATUS_ACCESS_VIOLATION, if str is NULL.
389 * NOTES
390 * Instead of base 0 it uses 10 as base.
391 * Writes at most length characters to the string str.
392 * Str is '\0' terminated when length allowes it.
393 * When str fits exactly in length characters the '\0' is ommitted.
394 * If value_ptr is NULL it crashes, as the native function does.
396 * DIFFERENCES
397 * - Accept base 0 as 10 instead of crashing as native function does.
398 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
399 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
401 NTSTATUS WINAPI RtlLargeIntegerToChar(
402 const ULONGLONG *value_ptr, /* [I] Pointer to the value to be converted */
403 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
404 ULONG length, /* [I] Length of the str buffer in bytes */
405 PCHAR str) /* [O] Destination for the converted value */
407 ULONGLONG value = *value_ptr;
408 CHAR buffer[65];
409 PCHAR pos;
410 CHAR digit;
411 ULONG len;
413 if (base == 0) {
414 base = 10;
415 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
416 return STATUS_INVALID_PARAMETER;
417 } /* if */
419 pos = &buffer[64];
420 *pos = '\0';
422 do {
423 pos--;
424 digit = value % base;
425 value = value / base;
426 if (digit < 10) {
427 *pos = '0' + digit;
428 } else {
429 *pos = 'A' + digit - 10;
430 } /* if */
431 } while (value != 0L);
433 len = &buffer[64] - pos;
434 if (len > length) {
435 return STATUS_BUFFER_OVERFLOW;
436 } else if (str == NULL) {
437 return STATUS_ACCESS_VIOLATION;
438 } else if (len == length) {
439 memcpy(str, pos, len);
440 } else {
441 memcpy(str, pos, len + 1);
442 } /* if */
443 return STATUS_SUCCESS;
447 /**************************************************************************
448 * RtlInt64ToUnicodeString (NTDLL.@)
450 * Convert a large unsigned integer to a '\0' terminated unicode string.
452 * RETURNS
453 * Success: STATUS_SUCCESS. str contains the converted number
454 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
455 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
456 * (with the '\0' termination). In this case str->Length
457 * is set to the length, the string would have (which can
458 * be larger than the MaximumLength).
460 * NOTES
461 * Instead of base 0 it uses 10 as base.
462 * If str is NULL it crashes, as the native function does.
464 * DIFFERENCES
465 * - Accept base 0 as 10 instead of crashing as native function does.
466 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
467 * The native function does this when the string would be longer than 31
468 * characters even when the string parameter is long enough.
469 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
470 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
472 NTSTATUS WINAPI RtlInt64ToUnicodeString(
473 ULONGLONG value, /* [I] Value to be converted */
474 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
475 UNICODE_STRING *str) /* [O] Destination for the converted value */
477 WCHAR buffer[65];
478 PWCHAR pos;
479 WCHAR digit;
481 if (base == 0) {
482 base = 10;
483 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
484 return STATUS_INVALID_PARAMETER;
485 } /* if */
487 pos = &buffer[64];
488 *pos = '\0';
490 do {
491 pos--;
492 digit = value % base;
493 value = value / base;
494 if (digit < 10) {
495 *pos = '0' + digit;
496 } else {
497 *pos = 'A' + digit - 10;
498 } /* if */
499 } while (value != 0L);
501 str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
502 if (str->Length >= str->MaximumLength) {
503 return STATUS_BUFFER_OVERFLOW;
504 } else {
505 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
506 } /* if */
507 return STATUS_SUCCESS;
511 /******************************************************************************
512 * _alldiv (NTDLL.@)
514 * Divide two 64 bit unsigned integers.
516 * PARAMS
517 * a [I] Initial number.
518 * b [I] Number to multiply a by.
520 * RETURNS
521 * The dividend of a and b.
523 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
525 return a / b;
529 /******************************************************************************
530 * _allmul (NTDLL.@)
532 * Multiply two 64 bit integers.
534 * PARAMS
535 * a [I] Initial number.
536 * b [I] Number to multiply a by.
538 * RETURNS
539 * The product of a and b.
541 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
543 return a * b;
547 /******************************************************************************
548 * _allrem (NTDLL.@)
550 * Calculate the remainder after dividing two 64 bit integers.
552 * PARAMS
553 * a [I] Initial number.
554 * b [I] Number to divide a by.
556 * RETURNS
557 * The remainder of a divided by b.
559 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
561 return a % b;
565 /******************************************************************************
566 * _aulldiv (NTDLL.@)
568 * Divide two 64 bit unsigned integers.
570 * PARAMS
571 * a [I] Initial number.
572 * b [I] Number to multiply a by.
574 * RETURNS
575 * The dividend of a and b.
577 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
579 return a / b;
583 /******************************************************************************
584 * _aullrem (NTDLL.@)
586 * Calculate the remainder after dividing two 64 bit unsigned integers.
588 * PARAMS
589 * a [I] Initial number.
590 * b [I] Number to divide a by.
592 * RETURNS
593 * The remainder of a divided by b.
595 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )
597 return a % b;