]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - ubuntu/vbox/vboxsf/include/iprt/asm-math.h
2 * IPRT - Assembly Routines for Optimizing some Integers Math Operations.
6 * Copyright (C) 2006-2017 Oracle Corporation
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
26 #ifndef ___iprt_asm_math_h
27 #define ___iprt_asm_math_h
29 #include <iprt/types.h>
31 #if defined(_MSC_VER) && RT_INLINE_ASM_USES_INTRIN
32 # pragma warning(push)
33 # pragma warning(disable:4668) /* Several incorrect __cplusplus uses. */
34 # pragma warning(disable:4255) /* Incorrect __slwpcb prototype. */
37 /* Emit the intrinsics at all optimization levels. */
38 # pragma intrinsic(__emul)
39 # pragma intrinsic(__emulu)
41 # pragma intrinsic(_mul128)
42 # pragma intrinsic(_umul128)
47 /** @defgroup grp_rt_asm_math Interger Math Optimizations
52 * Multiplies two unsigned 32-bit values returning an unsigned 64-bit result.
54 * @returns u32F1 * u32F2.
57 #if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN && defined(RT_ARCH_X86)
58 DECLASM(uint64_t) ASMMult2xU32RetU64(uint32_t u32F1
, uint32_t u32F2
);
60 DECLINLINE(uint64_t) ASMMult2xU32RetU64(uint32_t u32F1
, uint32_t u32F2
)
64 # if RT_INLINE_ASM_GNU_STYLE
65 __asm__
__volatile__("mull %%edx"
67 : "a" (u32F2
), "d" (u32F1
));
68 # elif RT_INLINE_ASM_USES_INTRIN
69 u64
= __emulu(u32F1
, u32F2
);
76 mov dword ptr
[u64
], eax
77 mov dword ptr
[u64
+ 4], edx
82 return (uint64_t)u32F1
* u32F2
;
89 * Multiplies two signed 32-bit values returning a signed 64-bit result.
91 * @returns u32F1 * u32F2.
93 #if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN && defined(RT_ARCH_X86)
94 DECLASM(int64_t) ASMMult2xS32RetS64(int32_t i32F1
, int32_t i32F2
);
96 DECLINLINE(int64_t) ASMMult2xS32RetS64(int32_t i32F1
, int32_t i32F2
)
100 # if RT_INLINE_ASM_GNU_STYLE
101 __asm__
__volatile__("imull %%edx"
103 : "a" (i32F2
), "d" (i32F1
));
104 # elif RT_INLINE_ASM_USES_INTRIN
105 i64
= __emul(i32F1
, i32F2
);
112 mov dword ptr
[i64
], eax
113 mov dword ptr
[i64
+ 4], edx
117 # else /* generic: */
118 return (int64_t)i32F1
* i32F2
;
125 DECLINLINE(uint64_t) ASMMult2xU64Ret2xU64(uint64_t u64F1
, uint64_t u64F2
, uint64_t *pu64ProdHi
)
127 # if defined(RT_ARCH_AMD64) && (RT_INLINE_ASM_GNU_STYLE || RT_INLINE_ASM_USES_INTRIN)
128 # if RT_INLINE_ASM_GNU_STYLE
129 uint64_t u64Low
, u64High
;
130 __asm__
__volatile__("mulq %%rdx"
131 : "=a" (u64Low
), "=d" (u64High
)
132 : "0" (u64F1
), "1" (u64F2
));
133 *pu64ProdHi
= u64High
;
135 # elif RT_INLINE_ASM_USES_INTRIN
136 return _umul128(u64F1
, u64F2
, pu64ProdHi
);
140 # else /* generic: */
144 * ab * cd = b*d + a*d*10 + b*c*10 + a*c*100
146 * Where a, b, c and d are 'digits', and 10 is max digit + 1.
148 * Our digits are 32-bit wide, so instead of 10 we multiply by 4G.
149 * Prod = F1.s.Lo*F2.s.Lo + F1.s.Hi*F2.s.Lo*4G
150 * + F1.s.Lo*F2.s.Hi*4G + F1.s.Hi*F2.s.Hi*4G*4G
159 Prod
.s
.Lo
= ASMMult2xU32RetU64(F1
.s
.Lo
, F2
.s
.Lo
);
161 Tmp1
.u
= ASMMult2xU32RetU64(F1
.s
.Hi
, F2
.s
.Lo
);
162 u64Tmp
= (uint64_t)Prod
.DWords
.dw1
+ Tmp1
.s
.Lo
;
163 Prod
.DWords
.dw1
= (uint32_t)u64Tmp
;
164 Prod
.s
.Hi
= Tmp1
.s
.Hi
;
165 Prod
.s
.Hi
+= u64Tmp
>> 32; /* carry */
167 Tmp1
.u
= ASMMult2xU32RetU64(F1
.s
.Lo
, F2
.s
.Hi
);
168 u64Tmp
= (uint64_t)Prod
.DWords
.dw1
+ Tmp1
.s
.Lo
;
169 Prod
.DWords
.dw1
= (uint32_t)u64Tmp
;
170 u64Tmp
>>= 32; /* carry */
171 u64Tmp
+= Prod
.DWords
.dw2
;
173 Prod
.DWords
.dw2
= (uint32_t)u64Tmp
;
174 Prod
.DWords
.dw3
+= u64Tmp
>> 32; /* carry */
176 Prod
.s
.Hi
+= ASMMult2xU32RetU64(F1
.s
.Hi
, F2
.s
.Hi
);
177 *pu64ProdHi
= Prod
.s
.Hi
;
186 * Divides a 64-bit unsigned by a 32-bit unsigned returning an unsigned 32-bit result.
188 * @returns u64 / u32.
190 #if RT_INLINE_ASM_EXTERNAL && defined(RT_ARCH_X86)
191 DECLASM(uint32_t) ASMDivU64ByU32RetU32(uint64_t u64
, uint32_t u32
);
193 DECLINLINE(uint32_t) ASMDivU64ByU32RetU32(uint64_t u64
, uint32_t u32
)
196 # if RT_INLINE_ASM_GNU_STYLE
198 __asm__
__volatile__("divl %3"
199 : "=a" (u32
), "=d"(uDummy
)
200 : "A" (u64
), "r" (u32
));
204 mov eax
, dword ptr
[u64
]
205 mov edx
, dword ptr
[u64
+ 4]
212 # else /* generic: */
213 return (uint32_t)(u64
/ u32
);
220 * Divides a 64-bit signed by a 32-bit signed returning a signed 32-bit result.
222 * @returns u64 / u32.
224 #if RT_INLINE_ASM_EXTERNAL && defined(RT_ARCH_X86)
225 DECLASM(int32_t) ASMDivS64ByS32RetS32(int64_t i64
, int32_t i32
);
227 DECLINLINE(int32_t) ASMDivS64ByS32RetS32(int64_t i64
, int32_t i32
)
230 # if RT_INLINE_ASM_GNU_STYLE
232 __asm__
__volatile__("idivl %3"
233 : "=a" (i32
), "=d"(iDummy
)
234 : "A" (i64
), "r" (i32
));
238 mov eax
, dword ptr
[i64
]
239 mov edx
, dword ptr
[i64
+ 4]
246 # else /* generic: */
247 return (int32_t)(i64
/ i32
);
254 * Performs 64-bit unsigned by a 32-bit unsigned division with a 32-bit unsigned result,
255 * returning the rest.
257 * @returns u64 % u32.
259 * @remarks It is important that the result is <= UINT32_MAX or we'll overflow and crash.
261 #if RT_INLINE_ASM_EXTERNAL && defined(RT_ARCH_X86)
262 DECLASM(uint32_t) ASMModU64ByU32RetU32(uint64_t u64
, uint32_t u32
);
264 DECLINLINE(uint32_t) ASMModU64ByU32RetU32(uint64_t u64
, uint32_t u32
)
267 # if RT_INLINE_ASM_GNU_STYLE
269 __asm__
__volatile__("divl %3"
270 : "=a" (uDummy
), "=d"(u32
)
271 : "A" (u64
), "r" (u32
));
275 mov eax
, dword ptr
[u64
]
276 mov edx
, dword ptr
[u64
+ 4]
283 # else /* generic: */
284 return (uint32_t)(u64
% u32
);
291 * Performs 64-bit signed by a 32-bit signed division with a 32-bit signed result,
292 * returning the rest.
294 * @returns u64 % u32.
296 * @remarks It is important that the result is <= UINT32_MAX or we'll overflow and crash.
298 #if RT_INLINE_ASM_EXTERNAL && defined(RT_ARCH_X86)
299 DECLASM(int32_t) ASMModS64ByS32RetS32(int64_t i64
, int32_t i32
);
301 DECLINLINE(int32_t) ASMModS64ByS32RetS32(int64_t i64
, int32_t i32
)
304 # if RT_INLINE_ASM_GNU_STYLE
306 __asm__
__volatile__("idivl %3"
307 : "=a" (iDummy
), "=d"(i32
)
308 : "A" (i64
), "r" (i32
));
312 mov eax
, dword ptr
[i64
]
313 mov edx
, dword ptr
[i64
+ 4]
320 # else /* generic: */
321 return (int32_t)(i64
% i32
);
328 * Multiple a 32-bit by a 32-bit integer and divide the result by a 32-bit integer
329 * using a 64 bit intermediate result.
331 * @returns (u32A * u32B) / u32C.
332 * @param u32A The 32-bit value (A).
333 * @param u32B The 32-bit value to multiple by A.
334 * @param u32C The 32-bit value to divide A*B by.
336 * @remarks Architecture specific.
337 * @remarks Make sure the result won't ever exceed 32-bit, because hardware
338 * exception may be raised if it does.
339 * @remarks On x86 this may be used to avoid dragging in 64-bit builtin
340 * arithmetics functions.
342 #if RT_INLINE_ASM_EXTERNAL && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
343 DECLASM(uint32_t) ASMMultU32ByU32DivByU32(uint32_t u32A
, uint32_t u32B
, uint32_t u32C
);
345 DECLINLINE(uint32_t) ASMMultU32ByU32DivByU32(uint32_t u32A
, uint32_t u32B
, uint32_t u32C
)
347 # if RT_INLINE_ASM_GNU_STYLE && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
348 uint32_t u32Result
, u32Spill
;
349 __asm__
__volatile__("mull %2\n\t"
358 return (uint32_t)(((uint64_t)u32A
* u32B
) / u32C
);
365 * Multiple a 64-bit by a 32-bit integer and divide the result by a 32-bit integer
366 * using a 96 bit intermediate result.
368 * @returns (u64A * u32B) / u32C.
369 * @param u64A The 64-bit value.
370 * @param u32B The 32-bit value to multiple by A.
371 * @param u32C The 32-bit value to divide A*B by.
373 * @remarks Architecture specific.
374 * @remarks Make sure the result won't ever exceed 64-bit, because hardware
375 * exception may be raised if it does.
376 * @remarks On x86 this may be used to avoid dragging in 64-bit builtin
377 * arithmetics function.
379 #if RT_INLINE_ASM_EXTERNAL || !defined(__GNUC__) || (!defined(RT_ARCH_AMD64) && !defined(RT_ARCH_X86))
380 DECLASM(uint64_t) ASMMultU64ByU32DivByU32(uint64_t u64A
, uint32_t u32B
, uint32_t u32C
);
382 DECLINLINE(uint64_t) ASMMultU64ByU32DivByU32(uint64_t u64A
, uint32_t u32B
, uint32_t u32C
)
384 # if RT_INLINE_ASM_GNU_STYLE
385 # ifdef RT_ARCH_AMD64
386 uint64_t u64Result
, u64Spill
;
387 __asm__
__volatile__("mulq %2\n\t"
391 : "r" ((uint64_t)u32B
),
392 "r" ((uint64_t)u32C
),
398 __asm__
__volatile__("mull %%ecx \n\t" /* eax = u64Lo.lo = (u64A.lo * u32B).lo
399 edx = u64Lo.hi = (u64A.lo * u32B).hi */
400 "xchg %%eax,%%esi \n\t" /* esi = u64Lo.lo
402 "xchg %%edx,%%edi \n\t" /* edi = u64Low.hi
404 "xchg %%edx,%%ecx \n\t" /* ecx = u32C
406 "mull %%edx \n\t" /* eax = u64Hi.lo = (u64A.hi * u32B).lo
407 edx = u64Hi.hi = (u64A.hi * u32B).hi */
408 "addl %%edi,%%eax \n\t" /* u64Hi.lo += u64Lo.hi */
409 "adcl $0,%%edx \n\t" /* u64Hi.hi += carry */
410 "divl %%ecx \n\t" /* eax = u64Hi / u32C
411 edx = u64Hi % u32C */
412 "movl %%eax,%%edi \n\t" /* edi = u64Result.hi = u64Hi / u32C */
413 "movl %%esi,%%eax \n\t" /* eax = u64Lo.lo */
414 "divl %%ecx \n\t" /* u64Result.lo */
415 "movl %%edi,%%edx \n\t" /* u64Result.hi */
416 : "=A"(u64Result
), "=c"(u32Dummy
),
417 "=S"(u32Dummy
), "=D"(u32Dummy
)
418 : "a"((uint32_t)u64A
),
419 "S"((uint32_t)(u64A
>> 32)),
426 uint64_t u64Lo
= (uint64_t)(u64A
& 0xffffffff) * u32B
;
427 uint64_t u64Hi
= (uint64_t)(u64A
>> 32) * u32B
;
428 u64Hi
+= (u64Lo
>> 32);
429 u
.s
.Hi
= (uint32_t)(u64Hi
/ u32C
);
430 u
.s
.Lo
= (uint32_t)((((u64Hi
% u32C
) << 32) + (u64Lo
& 0xffffffff)) / u32C
);