Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / ntdll / large_int.c
blob1f52211d135b9cb753b74688564e5d4b7050167b
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 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
45 return a + b;
49 /******************************************************************************
50 * RtlLargeIntegerSubtract (NTDLL.@)
52 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
54 return a - b;
58 /******************************************************************************
59 * RtlLargeIntegerNegate (NTDLL.@)
61 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
63 return -a;
67 /******************************************************************************
68 * RtlLargeIntegerShiftLeft (NTDLL.@)
70 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
72 return a << count;
76 /******************************************************************************
77 * RtlLargeIntegerShiftRight (NTDLL.@)
79 LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
81 return (ULONGLONG)a >> count;
85 /******************************************************************************
86 * RtlLargeIntegerArithmeticShift (NTDLL.@)
88 LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
90 /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
91 return a >> count;
95 /******************************************************************************
96 * RtlLargeIntegerDivide (NTDLL.@)
98 * FIXME: should it be signed division instead?
100 ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
102 ULONGLONG ret = a / b;
103 if (rem) *rem = a - ret * b;
104 return ret;
108 /******************************************************************************
109 * RtlConvertLongToLargeInteger (NTDLL.@)
111 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
113 return a;
117 /******************************************************************************
118 * RtlConvertUlongToLargeInteger (NTDLL.@)
120 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
122 return a;
126 /******************************************************************************
127 * RtlEnlargedIntegerMultiply (NTDLL.@)
129 LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
131 return (LONGLONG)a * b;
135 /******************************************************************************
136 * RtlEnlargedUnsignedMultiply (NTDLL.@)
138 ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
140 return (ULONGLONG)a * b;
144 /******************************************************************************
145 * RtlEnlargedUnsignedDivide (NTDLL.@)
147 UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
149 #if defined(__i386__) && defined(__GNUC__)
150 UINT ret, rem, p1, p2;
152 p1 = a >> 32;
153 p2 = a & 0xffffffffLL;
155 __asm__("div %4,%%eax"
156 : "=a" (ret), "=d" (rem)
157 : "0" (p2), "1" (p1), "g" (b) );
158 if (remptr) *remptr = rem;
159 return ret;
160 #else
161 UINT ret = a / b;
162 if (remptr) *remptr = a % b;
163 return ret;
164 #endif
168 /******************************************************************************
169 * RtlExtendedLargeIntegerDivide (NTDLL.@)
171 LONGLONG WINAPI RtlExtendedLargeIntegerDivide( LONGLONG a, INT b, INT *rem )
173 LONGLONG ret = a / b;
174 if (rem) *rem = a - b * ret;
175 return ret;
179 /******************************************************************************
180 * RtlExtendedIntegerMultiply (NTDLL.@)
182 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
184 return a * b;
188 /******************************************************************************
189 * RtlExtendedMagicDivide (NTDLL.@)
191 * Allows replacing a division by a longlong constant with a multiplication by
192 * the inverse constant.
194 * RETURNS
195 * (dividend * inverse_divisor) >> (64 + shift)
197 * NOTES
198 * If the divisor of a division is constant, the constants inverse_divisor and
199 * shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
200 * Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
201 * dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
203 * The Parameter inverse_divisor although defined as LONGLONG is used as
204 * ULONGLONG.
206 #define LOWER_32(A) ((A) & 0xffffffff)
207 #define UPPER_32(A) ((A) >> 32)
208 LONGLONG WINAPI RtlExtendedMagicDivide(
209 LONGLONG dividend, /* [I] Dividend to be divided by the constant divisor */
210 LONGLONG inverse_divisor, /* [I] Constant computed manually as 2^(64+shift) / divisor */
211 INT shift) /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
213 ULONGLONG dividend_high;
214 ULONGLONG dividend_low;
215 ULONGLONG inverse_divisor_high;
216 ULONGLONG inverse_divisor_low;
217 ULONGLONG ah_bl;
218 ULONGLONG al_bh;
219 LONGLONG result;
220 int positive;
222 if (dividend < 0) {
223 dividend_high = UPPER_32((ULONGLONG) -dividend);
224 dividend_low = LOWER_32((ULONGLONG) -dividend);
225 positive = 0;
226 } else {
227 dividend_high = UPPER_32((ULONGLONG) dividend);
228 dividend_low = LOWER_32((ULONGLONG) dividend);
229 positive = 1;
230 } /* if */
231 inverse_divisor_high = UPPER_32((ULONGLONG) inverse_divisor);
232 inverse_divisor_low = LOWER_32((ULONGLONG) inverse_divisor);
234 ah_bl = dividend_high * inverse_divisor_low;
235 al_bh = dividend_low * inverse_divisor_high;
237 result = (LONGLONG) ((dividend_high * inverse_divisor_high +
238 UPPER_32(ah_bl) +
239 UPPER_32(al_bh) +
240 UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
241 UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
243 if (positive) {
244 return result;
245 } else {
246 return -result;
247 } /* if */
251 /******************************************************************************
252 * RtlLargeIntegerToChar [NTDLL.@]
254 * Convert an unsigned large integer to a character string.
256 * RETURNS
257 * Success: STATUS_SUCCESS. str contains the converted number
258 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
259 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
260 * STATUS_ACCESS_VIOLATION, if str is NULL.
262 * NOTES
263 * Instead of base 0 it uses 10 as base.
264 * Writes at most length characters to the string str.
265 * Str is '\0' terminated when length allowes it.
266 * When str fits exactly in length characters the '\0' is ommitted.
267 * If value_ptr is NULL it crashes, as the native function does.
269 * DIFFERENCES
270 * - Accept base 0 as 10 instead of crashing as native function does.
271 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
272 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
274 NTSTATUS WINAPI RtlLargeIntegerToChar(
275 const ULONGLONG *value_ptr, /* [I] Pointer to the value to be converted */
276 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
277 ULONG length, /* [I] Length of the str buffer in bytes */
278 PCHAR str) /* [O] Destination for the converted value */
280 ULONGLONG value = *value_ptr;
281 CHAR buffer[65];
282 PCHAR pos;
283 CHAR digit;
284 ULONG len;
286 if (base == 0) {
287 base = 10;
288 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
289 return STATUS_INVALID_PARAMETER;
290 } /* if */
292 pos = &buffer[64];
293 *pos = '\0';
295 do {
296 pos--;
297 digit = value % base;
298 value = value / base;
299 if (digit < 10) {
300 *pos = '0' + digit;
301 } else {
302 *pos = 'A' + digit - 10;
303 } /* if */
304 } while (value != 0L);
306 len = &buffer[64] - pos;
307 if (len > length) {
308 return STATUS_BUFFER_OVERFLOW;
309 } else if (str == NULL) {
310 return STATUS_ACCESS_VIOLATION;
311 } else if (len == length) {
312 memcpy(str, pos, len);
313 } else {
314 memcpy(str, pos, len + 1);
315 } /* if */
316 return STATUS_SUCCESS;
320 /**************************************************************************
321 * RtlInt64ToUnicodeString (NTDLL.@)
323 * Convert a large unsigned integer to a '\0' terminated unicode string.
325 * RETURNS
326 * Success: STATUS_SUCCESS. str contains the converted number
327 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
328 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
329 * (with the '\0' termination). In this case str->Length
330 * is set to the length, the string would have (which can
331 * be larger than the MaximumLength).
333 * NOTES
334 * Instead of base 0 it uses 10 as base.
335 * If str is NULL it crashes, as the native function does.
337 * DIFFERENCES
338 * - Accept base 0 as 10 instead of crashing as native function does.
339 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
340 * The native function does this when the string would be longer than 31
341 * characters even when the string parameter is long enough.
342 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
343 * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
345 NTSTATUS WINAPI RtlInt64ToUnicodeString(
346 ULONGLONG value, /* [I] Value to be converted */
347 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
348 UNICODE_STRING *str) /* [O] Destination for the converted value */
350 WCHAR buffer[65];
351 PWCHAR pos;
352 WCHAR digit;
354 if (base == 0) {
355 base = 10;
356 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
357 return STATUS_INVALID_PARAMETER;
358 } /* if */
360 pos = &buffer[64];
361 *pos = '\0';
363 do {
364 pos--;
365 digit = value % base;
366 value = value / base;
367 if (digit < 10) {
368 *pos = '0' + digit;
369 } else {
370 *pos = 'A' + digit - 10;
371 } /* if */
372 } while (value != 0L);
374 str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
375 if (str->Length >= str->MaximumLength) {
376 return STATUS_BUFFER_OVERFLOW;
377 } else {
378 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
379 } /* if */
380 return STATUS_SUCCESS;
384 /******************************************************************************
385 * _alldiv (NTDLL.@)
387 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
389 return a / b;
393 /******************************************************************************
394 * _allmul (NTDLL.@)
396 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
398 return a * b;
402 /******************************************************************************
403 * _allrem (NTDLL.@)
405 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
407 return a % b;
411 /******************************************************************************
412 * _aulldiv (NTDLL.@)
414 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
416 return a / b;
420 /******************************************************************************
421 * _aullrem (NTDLL.@)
423 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )
425 return a % b;