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
26 * Note: we use LONGLONG instead of LARGE_INTEGER, because
27 * the latter is a structure and the calling convention for
28 * returning a structure would not be binary-compatible.
30 * FIXME: for platforms that don't have a native LONGLONG type,
31 * we should define LONGLONG as a structure similar to LARGE_INTEGER
32 * and do everything by hand. You are welcome to do it...
35 /******************************************************************************
36 * RtlLargeIntegerAdd (NTDLL.@)
38 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
44 /******************************************************************************
45 * RtlLargeIntegerSubtract (NTDLL.@)
47 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
53 /******************************************************************************
54 * RtlLargeIntegerNegate (NTDLL.@)
56 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
62 /******************************************************************************
63 * RtlLargeIntegerShiftLeft (NTDLL.@)
65 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
71 /******************************************************************************
72 * RtlLargeIntegerShiftRight (NTDLL.@)
74 LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
76 return (ULONGLONG)a >> count;
80 /******************************************************************************
81 * RtlLargeIntegerArithmeticShift (NTDLL.@)
83 LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
85 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
90 /******************************************************************************
91 * RtlLargeIntegerDivide (NTDLL.@)
93 * FIXME: should it be signed division instead?
95 ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
97 ULONGLONG ret = a / b;
98 if (rem) *rem = a - ret * b;
103 /******************************************************************************
104 * RtlConvertLongToLargeInteger (NTDLL.@)
106 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
112 /******************************************************************************
113 * RtlConvertUlongToLargeInteger (NTDLL.@)
115 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
121 /******************************************************************************
122 * RtlEnlargedIntegerMultiply (NTDLL.@)
124 LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
126 return (LONGLONG)a * b;
130 /******************************************************************************
131 * RtlEnlargedUnsignedMultiply (NTDLL.@)
133 ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
135 return (ULONGLONG)a * b;
139 /******************************************************************************
140 * RtlEnlargedUnsignedDivide (NTDLL.@)
142 UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
144 #if defined(__i386__) && defined(__GNUC__)
145 UINT ret, rem, p1, p2;
148 p2 = a & 0xffffffffLL;
150 __asm__("div %4,%%eax"
151 : "=a" (ret), "=d" (rem)
152 : "0" (p2), "1" (p1), "g" (b) );
153 if (remptr) *remptr = rem;
157 if (remptr) *remptr = a % b;
163 /******************************************************************************
164 * RtlExtendedLargeIntegerDivide (NTDLL.@)
166 LONGLONG WINAPI RtlExtendedLargeIntegerDivide( LONGLONG a, INT b, INT *rem )
168 LONGLONG ret = a / b;
169 if (rem) *rem = a - b * ret;
174 /******************************************************************************
175 * RtlExtendedIntegerMultiply (NTDLL.@)
177 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
183 /******************************************************************************
184 * RtlExtendedMagicDivide (NTDLL.@)
186 * Allows replacing a division by a longlong constant with a multiplication by
187 * the inverse constant.
190 * (dividend * inverse_divisor) >> (64 + shift)
193 * If the divisor of a division is constant, the constants inverse_divisor and
194 * shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
195 * Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
196 * dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
198 * The Parameter inverse_divisor although defined as LONGLONG is used as
201 #define LOWER_32(A) ((A) & 0xffffffff)
202 #define UPPER_32(A) ((A) >> 32)
203 LONGLONG WINAPI RtlExtendedMagicDivide(
204 LONGLONG dividend, /* [I] Dividend to be divided by the constant divisor */
205 LONGLONG inverse_divisor, /* [I] Constant computed manually as 2^(64+shift) / divisor */
206 INT shift) /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
208 ULONGLONG dividend_high;
209 ULONGLONG dividend_low;
210 ULONGLONG inverse_divisor_high;
211 ULONGLONG inverse_divisor_low;
218 dividend_high = UPPER_32((ULONGLONG) -dividend);
219 dividend_low = LOWER_32((ULONGLONG) -dividend);
222 dividend_high = UPPER_32((ULONGLONG) dividend);
223 dividend_low = LOWER_32((ULONGLONG) dividend);
226 inverse_divisor_high = UPPER_32((ULONGLONG) inverse_divisor);
227 inverse_divisor_low = LOWER_32((ULONGLONG) inverse_divisor);
229 ah_bl = dividend_high * inverse_divisor_low;
230 al_bh = dividend_low * inverse_divisor_high;
232 result = (LONGLONG) ((dividend_high * inverse_divisor_high +
235 UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
236 UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
246 /******************************************************************************
247 * RtlLargeIntegerToChar [NTDLL.@]
249 * Convert an unsigned large integer to a character string.
252 * Success: STATUS_SUCCESS. str contains the converted number
253 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
254 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
255 * STATUS_ACCESS_VIOLATION, if str is NULL.
258 * Instead of base 0 it uses 10 as base.
259 * Writes at most length characters to the string str.
260 * Str is '\0' terminated when length allowes it.
261 * When str fits exactly in length characters the '\0' is ommitted.
262 * If value_ptr is NULL it crashes, as the native function does.
265 * - Accept base 0 as 10 instead of crashing as native function does.
266 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
267 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
269 NTSTATUS WINAPI RtlLargeIntegerToChar(
270 const ULONGLONG *value_ptr, /* [I] Pointer to the value to be converted */
271 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
272 ULONG length, /* [I] Length of the str buffer in bytes */
273 PCHAR str) /* [O] Destination for the converted value */
275 ULONGLONG value = *value_ptr;
283 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
284 return STATUS_INVALID_PARAMETER;
292 digit = value % base;
293 value = value / base;
297 *pos = 'A' + digit - 10;
299 } while (value != 0L);
301 len = &buffer[64] - pos;
303 return STATUS_BUFFER_OVERFLOW;
304 } else if (str == NULL) {
305 return STATUS_ACCESS_VIOLATION;
306 } else if (len == length) {
307 memcpy(str, pos, len);
309 memcpy(str, pos, len + 1);
311 return STATUS_SUCCESS;
315 /**************************************************************************
316 * RtlInt64ToUnicodeString (NTDLL.@)
318 * Convert a large unsigned integer to a '\0' terminated unicode string.
321 * Success: STATUS_SUCCESS. str contains the converted number
322 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
323 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
324 * (with the '\0' termination). In this case str->Length
325 * is set to the length, the string would have (which can
326 * be larger than the MaximumLength).
329 * Instead of base 0 it uses 10 as base.
330 * If str is NULL it crashes, as the native function does.
333 * - Accept base 0 as 10 instead of crashing as native function does.
334 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
335 * The native function does this when the string would be longer than 31
336 * characters even when the string parameter is long enough.
337 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
338 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
340 NTSTATUS WINAPI RtlInt64ToUnicodeString(
341 ULONGLONG value, /* [I] Value to be converted */
342 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
343 UNICODE_STRING *str) /* [O] Destination for the converted value */
351 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
352 return STATUS_INVALID_PARAMETER;
360 digit = value % base;
361 value = value / base;
365 *pos = 'A' + digit - 10;
367 } while (value != 0L);
369 str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
370 if (str->Length >= str->MaximumLength) {
371 return STATUS_BUFFER_OVERFLOW;
373 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
375 return STATUS_SUCCESS;
379 /******************************************************************************
382 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
388 /******************************************************************************
391 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
397 /******************************************************************************
400 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
406 /******************************************************************************
409 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
415 /******************************************************************************
418 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )