1 /*
2 * Large integer functions
3 *
4 * Copyright 2000 Alexandre Julliard
5 * Copyright 2003 Thomas Mertes
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winternl.h"
31
32 #ifndef _WIN64
33
34 /*
35 * Note: we use LONGLONG instead of LARGE_INTEGER, because
36 * the latter is a structure and the calling convention for
37 * returning a structure would not be binary-compatible.
38 *
39 * FIXME: for platforms that don't have a native LONGLONG type,
40 * we should define LONGLONG as a structure similar to LARGE_INTEGER
41 * and do everything by hand. You are welcome to do it...
42 */
43
44 /******************************************************************************
45 * RtlLargeIntegerAdd (NTDLL.@)
46 *
47 * Add two 64 bit integers.
48 *
49 * PARAMS
50 * a [I] Initial number.
51 * b [I] Number to add to a.
52 *
53 * RETURNS
54 * The sum of a and b.
55 */
56 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
57 {
58 return a + b;
59 }
60
61
62 /******************************************************************************
63 * RtlLargeIntegerSubtract (NTDLL.@)
64 *
65 * Subtract two 64 bit integers.
66 *
67 * PARAMS
68 * a [I] Initial number.
69 * b [I] Number to subtract from a.
70 *
71 * RETURNS
72 * The difference of a and b.
73 */
74 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
75 {
76 return a - b;
77 }
78
79
80 /******************************************************************************
81 * RtlLargeIntegerNegate (NTDLL.@)
82 *
83 * Negate a 64 bit integer.
84 *
85 * PARAMS
86 * a [I] Initial number.
87 *
88 * RETURNS
89 * The value of a negated.
90 */
91 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
92 {
93 return -a;
94 }
95
96
97 /******************************************************************************
98 * RtlLargeIntegerShiftLeft (NTDLL.@)
99 *
100 * Perform a shift left on a 64 bit integer.
101 *
102 * PARAMS
103 * a [I] Initial number.
104 * count [I] Number of bits to shift by
105 *
106 * RETURNS
107 * The value of a following the shift.
108 */
109 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
110 {
111 return a << count;
112 }
113
114
115 /******************************************************************************
116 * RtlLargeIntegerShiftRight (NTDLL.@)
117 *
118 * Perform a shift right on a 64 bit integer.
119 *
120 * PARAMS
121 * a [I] Initial number.
122 * count [I] Number of bits to shift by
123 *
124 * RETURNS
125 * The value of a following the shift.
126 */
127 LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
128 {
129 return (ULONGLONG)a >> count;
130 }
131
132
133 /******************************************************************************
134 * RtlLargeIntegerArithmeticShift (NTDLL.@)
135 *
136 * Perform an arithmetic shift right on a 64 bit integer.
137 *
138 * PARAMS
139 * a [I] Initial number.
140 * count [I] Number of bits to shift by
141 *
142 * RETURNS
143 * The value of a following the shift.
144 */
145 LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
146 {
147 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
148 return a >> count;
149 }
150
151
152 /******************************************************************************
153 * RtlLargeIntegerDivide (NTDLL.@)
154 *
155 * Divide one 64 bit unsigned integer by another, with remainder.
156 *
157 * PARAMS
158 * a [I] Initial number.
159 * b [I] Number to divide a by
160 * rem [O] Destination for remainder
161 *
162 * RETURNS
163 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
164 *
165 * FIXME
166 * Should it be signed division instead?
167 */
168 ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
169 {
170 ULONGLONG ret = a / b;
171 if (rem) *rem = a - ret * b;
172 return ret;
173 }
174
175
176 /******************************************************************************
177 * RtlConvertLongToLargeInteger (NTDLL.@)
178 *
179 * Convert a 32 bit integer into 64 bits.
180 *
181 * PARAMS
182 * a [I] Number to convert
183 *
184 * RETURNS
185 * a.
186 */
187 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
188 {
189 return a;
190 }
191
192
193 /******************************************************************************
194 * RtlConvertUlongToLargeInteger (NTDLL.@)
195 *
196 * Convert a 32 bit unsigned integer into 64 bits.
197 *
198 * PARAMS
199 * a [I] Number to convert
200 *
201 * RETURNS
202 * a.
203 */
204 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
205 {
206 return a;
207 }
208
209
210 /******************************************************************************
211 * RtlEnlargedIntegerMultiply (NTDLL.@)
212 *
213 * Multiply two integers giving a 64 bit integer result.
214 *
215 * PARAMS
216 * a [I] Initial number.
217 * b [I] Number to multiply a by.
218 *
219 * RETURNS
220 * The product of a and b.
221 */
222 LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
223 {
224 return (LONGLONG)a * b;
225 }
226
227
228 /******************************************************************************
229 * RtlEnlargedUnsignedMultiply (NTDLL.@)
230 *
231 * Multiply two unsigned integers giving a 64 bit unsigned integer result.
232 *
233 * PARAMS
234 * a [I] Initial number.
235 * b [I] Number to multiply a by.
236 *
237 * RETURNS
238 * The product of a and b.
239 */
240 ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
241 {
242 return (ULONGLONG)a * b;
243 }
244
245
246 /******************************************************************************
247 * RtlEnlargedUnsignedDivide (NTDLL.@)
248 *
249 * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
250 *
251 * PARAMS
252 * a [I] Initial number.
253 * b [I] Number to divide a by
254 * remptr [O] Destination for remainder
255 *
256 * RETURNS
257 * The dividend of a and b. If remptr is non-NULL it is set to the remainder.
258 */
259 UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
260 {
261 #if defined(__i386__) && defined(__GNUC__)
262 UINT ret, rem, p1, p2;
263
264 p1 = a >> 32;
265 p2 = a & 0xffffffffLL;
266
267 __asm__("div %4,%%eax"
268 : "=a" (ret), "=d" (rem)
269 : "" (p2), "1" (p1), "g" (b) );
270 if (remptr) *remptr = rem;
271 return ret;
272 #else
273 UINT ret = a / b;
274 if (remptr) *remptr = a % b;
275 return ret;
276 #endif
277 }
278
279
280 /******************************************************************************
281 * RtlExtendedLargeIntegerDivide (NTDLL.@)
282 *
283 * Divide one 64 bit integer by a 32 bit integer, with remainder.
284 *
285 * PARAMS
286 * a [I] Initial number.
287 * b [I] Number to divide a by
288 * rem [O] Destination for remainder
289 *
290 * RETURNS
291 * The dividend of a and b. If rem is non-NULL it is set to the remainder.
292 */
293 LONGLONG WINAPI RtlExtendedLargeIntegerDivide( LONGLONG a, INT b, INT *rem )
294 {
295 LONGLONG ret = a / b;
296 if (rem) *rem = a - b * ret;
297 return ret;
298 }
299
300
301 /******************************************************************************
302 * RtlExtendedIntegerMultiply (NTDLL.@)
303 *
304 * Multiply one 64 bit integer by another 32 bit integer.
305 *
306 * PARAMS
307 * a [I] Initial number.
308 * b [I] Number to multiply a by.
309 *
310 * RETURNS
311 * The product of a and b.
312 */
313 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
314 {
315 return a * b;
316 }
317
318
319 /******************************************************************************
320 * RtlExtendedMagicDivide (NTDLL.@)
321 *
322 * Allows replacing a division by a longlong constant with a multiplication by
323 * the inverse constant.
324 *
325 * RETURNS
326 * (dividend * inverse_divisor) >> (64 + shift)
327 *
328 * NOTES
329 * If the divisor of a division is constant, the constants inverse_divisor and
330 * shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
331 * Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
332 * dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
333 *
334 * The Parameter inverse_divisor although defined as LONGLONG is used as
335 * ULONGLONG.
336 */
337 #define LOWER_32(A) ((A) & 0xffffffff)
338 #define UPPER_32(A) ((A) >> 32)
339 LONGLONG WINAPI RtlExtendedMagicDivide(
340 LONGLONG dividend, /* [I] Dividend to be divided by the constant divisor */
341 LONGLONG inverse_divisor, /* [I] Constant computed manually as 2^(64+shift) / divisor */
342 INT shift) /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
343 {
344 ULONGLONG dividend_high;
345 ULONGLONG dividend_low;
346 ULONGLONG inverse_divisor_high;
347 ULONGLONG inverse_divisor_low;
348 ULONGLONG ah_bl;
349 ULONGLONG al_bh;
350 LONGLONG result;
351 int positive;
352
353 if (dividend < 0) {
354 dividend_high = UPPER_32((ULONGLONG) -dividend);
355 dividend_low = LOWER_32((ULONGLONG) -dividend);
356 positive = 0;
357 } else {
358 dividend_high = UPPER_32((ULONGLONG) dividend);
359 dividend_low = LOWER_32((ULONGLONG) dividend);
360 positive = 1;
361 } /* if */
362 inverse_divisor_high = UPPER_32((ULONGLONG) inverse_divisor);
363 inverse_divisor_low = LOWER_32((ULONGLONG) inverse_divisor);
364
365 ah_bl = dividend_high * inverse_divisor_low;
366 al_bh = dividend_low * inverse_divisor_high;
367
368 result = (LONGLONG) ((dividend_high * inverse_divisor_high +
369 UPPER_32(ah_bl) +
370 UPPER_32(al_bh) +
371 UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
372 UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
373
374 if (positive) {
375 return result;
376 } else {
377 return -result;
378 } /* if */
379 }
380
381
382 /*************************************************************************
383 * RtlInterlockedCompareExchange64 (NTDLL.@)
384 */
385 LONGLONG WINAPI RtlInterlockedCompareExchange64( LONGLONG *dest, LONGLONG xchg, LONGLONG compare )
386 {
387 return interlocked_cmpxchg64( dest, xchg, compare );
388 }
389
390 #endif /* _WIN64 */
391
392 /******************************************************************************
393 * RtlLargeIntegerToChar [NTDLL.@]
394 *
395 * Convert an unsigned large integer to a character string.
396 *
397 * RETURNS
398 * Success: STATUS_SUCCESS. str contains the converted number
399 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
400 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
401 * STATUS_ACCESS_VIOLATION, if str is NULL.
402 *
403 * NOTES
404 * Instead of base 0 it uses 10 as base.
405 * Writes at most length characters to the string str.
406 * Str is '\0' terminated when length allows it.
407 * When str fits exactly in length characters the '\0' is omitted.
408 * If value_ptr is NULL it crashes, as the native function does.
409 *
410 * DIFFERENCES
411 * - Accept base 0 as 10 instead of crashing as native function does.
412 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
413 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
414 */
415 NTSTATUS WINAPI RtlLargeIntegerToChar(
416 const ULONGLONG *value_ptr, /* [I] Pointer to the value to be converted */
417 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
418 ULONG length, /* [I] Length of the str buffer in bytes */
419 PCHAR str) /* [O] Destination for the converted value */
420 {
421 ULONGLONG value = *value_ptr;
422 CHAR buffer[65];
423 PCHAR pos;
424 CHAR digit;
425 ULONG len;
426
427 if (base == 0) {
428 base = 10;
429 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
430 return STATUS_INVALID_PARAMETER;
431 } /* if */
432
433 pos = &buffer[64];
434 *pos = '\0';
435
436 do {
437 pos--;
438 digit = value % base;
439 value = value / base;
440 if (digit < 10) {
441 *pos = '' + digit;
442 } else {
443 *pos = 'A' + digit - 10;
444 } /* if */
445 } while (value != 0L);
446
447 len = &buffer[64] - pos;
448 if (len > length) {
449 return STATUS_BUFFER_OVERFLOW;
450 } else if (str == NULL) {
451 return STATUS_ACCESS_VIOLATION;
452 } else if (len == length) {
453 memcpy(str, pos, len);
454 } else {
455 memcpy(str, pos, len + 1);
456 } /* if */
457 return STATUS_SUCCESS;
458 }
459
460
461 /**************************************************************************
462 * RtlInt64ToUnicodeString (NTDLL.@)
463 *
464 * Convert a large unsigned integer to a '\0' terminated unicode string.
465 *
466 * RETURNS
467 * Success: STATUS_SUCCESS. str contains the converted number
468 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
469 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
470 * (with the '\0' termination). In this case str->Length
471 * is set to the length, the string would have (which can
472 * be larger than the MaximumLength).
473 *
474 * NOTES
475 * Instead of base 0 it uses 10 as base.
476 * If str is NULL it crashes, as the native function does.
477 *
478 * DIFFERENCES
479 * - Accept base 0 as 10 instead of crashing as native function does.
480 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
481 * The native function does this when the string would be longer than 31
482 * characters even when the string parameter is long enough.
483 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
484 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
485 */
486 NTSTATUS WINAPI RtlInt64ToUnicodeString(
487 ULONGLONG value, /* [I] Value to be converted */
488 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
489 UNICODE_STRING *str) /* [O] Destination for the converted value */
490 {
491 WCHAR buffer[65];
492 PWCHAR pos;
493 WCHAR digit;
494
495 if (base == 0) {
496 base = 10;
497 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
498 return STATUS_INVALID_PARAMETER;
499 } /* if */
500
501 pos = &buffer[64];
502 *pos = '\0';
503
504 do {
505 pos--;
506 digit = value % base;
507 value = value / base;
508 if (digit < 10) {
509 *pos = '' + digit;
510 } else {
511 *pos = 'A' + digit - 10;
512 } /* if */
513 } while (value != 0L);
514
515 str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
516 if (str->Length >= str->MaximumLength) {
517 return STATUS_BUFFER_OVERFLOW;
518 } else {
519 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
520 } /* if */
521 return STATUS_SUCCESS;
522 }
523
524
525 #ifdef __i386__
526
527 /******************************************************************************
528 * _alldiv (NTDLL.@)
529 *
530 * Divide two 64 bit unsigned integers.
531 *
532 * PARAMS
533 * a [I] Initial number.
534 * b [I] Number to multiply a by.
535 *
536 * RETURNS
537 * The dividend of a and b.
538 */
539 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
540 {
541 return a / b;
542 }
543
544
545 /******************************************************************************
546 * _allmul (NTDLL.@)
547 *
548 * Multiply two 64 bit integers.
549 *
550 * PARAMS
551 * a [I] Initial number.
552 * b [I] Number to multiply a by.
553 *
554 * RETURNS
555 * The product of a and b.
556 */
557 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
558 {
559 return a * b;
560 }
561
562
563 /******************************************************************************
564 * _allrem (NTDLL.@)
565 *
566 * Calculate the remainder after dividing two 64 bit integers.
567 *
568 * PARAMS
569 * a [I] Initial number.
570 * b [I] Number to divide a by.
571 *
572 * RETURNS
573 * The remainder of a divided by b.
574 */
575 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
576 {
577 return a % b;
578 }
579
580
581 /******************************************************************************
582 * _aulldiv (NTDLL.@)
583 *
584 * Divide two 64 bit unsigned integers.
585 *
586 * PARAMS
587 * a [I] Initial number.
588 * b [I] Number to multiply a by.
589 *
590 * RETURNS
591 * The dividend of a and b.
592 */
593 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
594 {
595 return a / b;
596 }
597
598
599 /******************************************************************************
600 * _aullrem (NTDLL.@)
601 *
602 * Calculate the remainder after dividing two 64 bit unsigned integers.
603 *
604 * PARAMS
605 * a [I] Initial number.
606 * b [I] Number to divide a by.
607 *
608 * RETURNS
609 * The remainder of a divided by b.
610 */
611 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )
612 {
613 return a % b;
614 }
615
616 #endif /* __i386__ */
617
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.