相关疑难解决方法(0)

当被除数为64位且商为32位时,如何使gcc或clang使用64位/32位除法而不是128位/64位除法?

Currently, from research and various attempts, I'm pretty sure that the only solution to this problem is to use assembly. I'm posting this question to show an existing problem, and maybe get attention from compiler developers, or get some hits from searches about similar problems.

If anything changes in the future, I will accept it as an answer.

This is a very related question for MSVC.


In x86_64 machines, it is faster to use div/idiv with a 32-bit …

c x86 gcc clang compiler-optimization

5
推荐指数
1
解决办法
1064
查看次数

在某些情况下,在x86-64 Intel / AMD CPU上,128bit / 64bit硬件无符号除法能否比64bit / 32bit除法更快?

可以通过硬件128bit / 64bit除法指令执行缩放的64bit / 32bit除法,例如:

; Entry arguments: Dividend in EAX, Divisor in EBX
shl rax, 32  ;Scale up the Dividend by 2^32
xor rdx,rdx
and rbx, 0xFFFFFFFF  ;Clear any garbage that might have been in the upper half of RBX
div rbx  ; RAX = RDX:RAX / RBX
Run Code Online (Sandbox Code Playgroud)

...在某些特殊情况下,比硬件64位/ 32位除法指令执行的缩放64位/ 32位除法更快,例如:

; Entry arguments: Dividend in EAX, Divisor in EBX
mov edx,eax  ;Scale up the Dividend by 2^32
xor eax,eax
div ebx  ; EAX = EDX:EAX / EBX
Run Code Online (Sandbox Code Playgroud)

“某些特殊情况”是指异常的红利和除数。我只想比较 …

performance x86 assembly x86-64 integer-division

1
推荐指数
1
解决办法
107
查看次数