Skip to content

Instantly share code, notes, and snippets.

@arosh
Created January 25, 2015 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arosh/2dc1fe3a93e13f273044 to your computer and use it in GitHub Desktop.
Save arosh/2dc1fe3a93e13f273044 to your computer and use it in GitHub Desktop.
rand

環境

$ clang++ -version
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

コンパイルオプション

$ clang++ -std=c++11 -O2 -S (source.cpp)

forループ版

ソースコード

int f(const int n, const int m) {
  int ret = 0;
  for(int i = 0; i != n; ++i) {
    for(int j = 0; j != m; ++j) {
      ret = (i*ret)+j;
    }
  }
  return ret;
}

出力

	.section	__TEXT,__text,regular,pure_instructions
	.globl	__Z1fii
	.align	4, 0x90
__Z1fii:                                ## @_Z1fii
	.cfi_startproc
## BB#0:
	pushq	%rbp
Ltmp2:
	.cfi_def_cfa_offset 16
Ltmp3:
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
Ltmp4:
	.cfi_def_cfa_register %rbp
	xorl	%eax, %eax
	testl	%edi, %edi
	je	LBB0_6
## BB#1:                                ## %.preheader.lr.ph
	xorl	%r8d, %r8d
	xorl	%eax, %eax
	.align	4, 0x90
LBB0_2:                                 ## %.preheader
                                        ## =>This Loop Header: Depth=1
                                        ##     Child Loop BB0_3 Depth 2
	testl	%esi, %esi
	movl	$0, %edx
	je	LBB0_5
	.align	4, 0x90
LBB0_3:                                 ## %.lr.ph
                                        ##   Parent Loop BB0_2 Depth=1
                                        ## =>  This Inner Loop Header: Depth=2
	movl	%eax, %ecx
	movl	%edx, %eax
	leal	1(%rax), %edx
	imull	%r8d, %ecx
	addl	%ecx, %eax
	cmpl	%edx, %esi
                                        ## kill: EAX<def> EAX<kill> RAX<kill>
	jne	LBB0_3
## BB#4:                                ## %._crit_edge.loopexit
                                        ##   in Loop: Header=BB0_2 Depth=1
	leal	-1(%rcx,%rdx), %eax
LBB0_5:                                 ## %._crit_edge
                                        ##   in Loop: Header=BB0_2 Depth=1
	incl	%r8d
	cmpl	%edi, %r8d
	jne	LBB0_2
LBB0_6:                                 ## %._crit_edge6
	popq	%rbp
	retq
	.cfi_endproc


.subsections_via_symbols

rangeクラス版

namespace {
struct range {
  struct iter {
    int m_value;
    inline iter(const int value) : m_value(value) { }
    inline int operator*() const { return m_value; }
    inline bool operator!=(const iter & rhs) { return m_value != rhs.m_value; }
    inline void operator++() { ++m_value; }
  };
  const iter m_stop;
  inline range(const int stop) : m_stop(stop) { }
  inline iter begin() const { return iter(0); };
  inline iter end()   const { return m_stop; }
};
};

int f(const int n, const int m) {
  int ret = 0;
  for(const int i : range(n)) {
    for(const int j : range(m)) {
      ret = (i*ret)+j;
    }
  }
  return ret;
}

出力

	.section	__TEXT,__text,regular,pure_instructions
	.globl	__Z1fii
	.align	4, 0x90
__Z1fii:                                ## @_Z1fii
	.cfi_startproc
## BB#0:
	pushq	%rbp
Ltmp2:
	.cfi_def_cfa_offset 16
Ltmp3:
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
Ltmp4:
	.cfi_def_cfa_register %rbp
                                        ## kill: ESI<def> ESI<kill> RSI<def>
	xorl	%eax, %eax
	testl	%edi, %edi
	je	LBB0_6
## BB#1:                                ## %.preheader.lr.ph
	xorl	%eax, %eax
	xorl	%r8d, %r8d
	.align	4, 0x90
LBB0_2:                                 ## %.preheader
                                        ## =>This Loop Header: Depth=1
                                        ##     Child Loop BB0_3 Depth 2
	movl	$1, %r9d
	testl	%esi, %esi
	movl	$0, %edx
	je	LBB0_5
	.align	4, 0x90
LBB0_3:                                 ## %.lr.ph
                                        ##   Parent Loop BB0_2 Depth=1
                                        ## =>  This Inner Loop Header: Depth=2
                                        ## kill: R9D<def> R9D<kill> R9<def>
	movl	%eax, %ecx
	imull	%r8d, %ecx
	leal	(%rcx,%rdx), %eax
	incl	%edx
	leal	-1(%rsi,%r9), %r10d
	leal	-1(%r9), %r9d
	cmpl	$1, %r10d
	jne	LBB0_3
## BB#4:                                ## %._crit_edge.loopexit
                                        ##   in Loop: Header=BB0_2 Depth=1
	subl	%r9d, %ecx
	movl	%ecx, %eax
LBB0_5:                                 ## %._crit_edge
                                        ##   in Loop: Header=BB0_2 Depth=1
	incl	%r8d
	cmpl	%edi, %r8d
	jne	LBB0_2
LBB0_6:                                 ## %._crit_edge7
	popq	%rbp
	retq
	.cfi_endproc


.subsections_via_symbols

結果

$ diff -u rand_for.s rand_range.s
--- rand_for.s	2015-01-25 20:50:01.000000000 +0900
+++ rand_range.s	2015-01-25 20:49:05.000000000 +0900
@@ -12,16 +12,18 @@
 	movq	%rsp, %rbp
 Ltmp4:
 	.cfi_def_cfa_register %rbp
+                                        ## kill: ESI<def> ESI<kill> RSI<def>
 	xorl	%eax, %eax
 	testl	%edi, %edi
 	je	LBB0_6
 ## BB#1:                                ## %.preheader.lr.ph
-	xorl	%r8d, %r8d
 	xorl	%eax, %eax
+	xorl	%r8d, %r8d
 	.align	4, 0x90
 LBB0_2:                                 ## %.preheader
                                         ## =>This Loop Header: Depth=1
                                         ##     Child Loop BB0_3 Depth 2
+	movl	$1, %r9d
 	testl	%esi, %esi
 	movl	$0, %edx
 	je	LBB0_5
@@ -29,23 +31,25 @@
 LBB0_3:                                 ## %.lr.ph
                                         ##   Parent Loop BB0_2 Depth=1
                                         ## =>  This Inner Loop Header: Depth=2
+                                        ## kill: R9D<def> R9D<kill> R9<def>
 	movl	%eax, %ecx
-	movl	%edx, %eax
-	leal	1(%rax), %edx
 	imull	%r8d, %ecx
-	addl	%ecx, %eax
-	cmpl	%edx, %esi
-                                        ## kill: EAX<def> EAX<kill> RAX<kill>
+	leal	(%rcx,%rdx), %eax
+	incl	%edx
+	leal	-1(%rsi,%r9), %r10d
+	leal	-1(%r9), %r9d
+	cmpl	$1, %r10d
 	jne	LBB0_3
 ## BB#4:                                ## %._crit_edge.loopexit
                                         ##   in Loop: Header=BB0_2 Depth=1
-	leal	-1(%rcx,%rdx), %eax
+	subl	%r9d, %ecx
+	movl	%ecx, %eax
 LBB0_5:                                 ## %._crit_edge
                                         ##   in Loop: Header=BB0_2 Depth=1
 	incl	%r8d
 	cmpl	%edi, %r8d
 	jne	LBB0_2
-LBB0_6:                                 ## %._crit_edge6
+LBB0_6:                                 ## %._crit_edge7
 	popq	%rbp
 	retq
 	.cfi_endproc

目くじらを建てるほどでは無さそう

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment