Issue 14936 - Dividing by a power of 2 slow on 32bit
Summary: Dividing by a power of 2 slow on 32bit
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P1 enhancement
Assignee: No Owner
URL:
Keywords: performance, pull
Depends on:
Blocks:
 
Reported: 2015-08-19 16:51 UTC by Iain Buclaw
Modified: 2020-07-22 23:59 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Iain Buclaw 2015-08-19 16:51:31 UTC
Noticed from results in: http://bugzilla.gdcproject.org/show_bug.cgi?id=180

"""
for: dmd -O -inline -release
  tByte: 0.004,872 secs
 tShort: 0.006,896 secs
   tInt: 0.008,672 secs
  tLong: 0.036,864 secs
"""

Reduced test:
long test(long l)
{
  return l / 2;
}


Compiles down to: http://goo.gl/10dWmf
long example.test(long):
	push   %ebp
	mov    %esp,%ebp
	push   %eax
	xor    %ecx,%ecx
	mov    0xc(%ebp),%edx
	push   %ebx
	mov    $0x2,%ebx
	mov    0x8(%ebp),%eax
	push   %ecx
	push   %ebx
	push   %edx
	push   %eax
	call   __divdi3        ; <--- !!!
	add    $0x10,%esp
	pop    %ebx
	mov    %ebp,%esp
	pop    %ebp
	ret    $0x8
	add    %al,(%eax)


In comparison to GDC: http://goo.gl/XaBqdA
	push	%ebx
	mov	12(%esp), %ebx
	xor	%edx, %edx
	mov	8(%esp), %ecx
	mov	%ebx, %eax
	shr	$31, %eax
	add	%ecx, %eax
	adc	%ebx, %edx
	shrd	$1, %edx, %eax
	pop	%ebx
	sar	%edx
	ret


You can exchange '2' for '4', '8', '16'... '128' to observe the difference (optimization changes for 256 and higher).
Comment 1 Dlang Bot 2020-07-22 10:53:52 UTC
@WalterBright created dlang/dmd pull request #11445 "fix Issue 14936 - Dividing by a power of 2 slow on 32bit" fixing this issue:

- fix Issue 14936 - Dividing by a power of 2 slow on 32bit

https://github.com/dlang/dmd/pull/11445
Comment 2 Dlang Bot 2020-07-22 23:59:06 UTC
dlang/dmd pull request #11445 "fix Issue 14936 - Dividing by a power of 2 slow on 32bit" was merged into master:

- 5badb70971549986a9a68ebb42614e446f3af1dd by Walter Bright:
  fix Issue 14936 - Dividing by a power of 2 slow on 32bit

https://github.com/dlang/dmd/pull/11445