Last active
December 17, 2015 14:58
-
-
Save kinoshita-lab/5627815 to your computer and use it in GitHub Desktop.
look for a best way to impl counter..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The Code: | |
#include <cstdio> | |
#define MAX_COUNT (11) | |
void someFunction() | |
{ | |
puts("unko"); | |
} | |
int counter_for_if = 0; | |
void counter_if_process() | |
{ | |
counter_for_if++; | |
if (counter_for_if >= MAX_COUNT) { | |
counter_for_if = 0; | |
someFunction(); | |
} | |
} | |
int counter_for_conditional_op = 0; | |
void counter_conditional_op() | |
{ | |
counter_for_conditional_op = ++counter_for_conditional_op >= MAX_COUNT ? 0 : counter_for_conditional_op; | |
if (counter_for_conditional_op == 0) { | |
someFunction(); | |
} | |
} | |
int counter_for_modulo = 0; | |
void modulo_counter_process() | |
{ | |
counter_for_modulo = (++counter_for_modulo) % MAX_COUNT; | |
if (counter_for_modulo == 0) { | |
someFunction(); | |
} | |
} | |
---------------------------------------------------------------------------- | |
Compile with: | |
clang++ -O3 -S -mllvm --x86-asm-syntax=intel unko.cpp -o unko.intel.s | |
where | |
clang++ -v | |
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn) | |
Target: x86_64-apple-darwin12.2.0 | |
---------------------------------------------------------------------------- | |
The Result (simpilified): | |
__Z18counter_if_processv: ## @_Z18counter_if_processv | |
push RBP | |
mov RBP, RSP | |
mov EAX, DWORD PTR [RIP + _counter_for_if] | |
inc EAX | |
mov DWORD PTR [RIP + _counter_for_if], EAX | |
cmp EAX, 11 | |
jl LBB1_1 | |
mov DWORD PTR [RIP + _counter_for_if], 0 | |
lea RDI, QWORD PTR [RIP + L_.str] | |
pop RBP | |
jmp _puts ## TAILCALL | |
LBB1_1: | |
pop RBP | |
ret | |
__Z22counter_conditional_opv: ## @_Z22counter_conditional_opv | |
push RBP | |
mov RBP, RSP | |
mov EAX, DWORD PTR [RIP + _counter_for_conditional_op] | |
inc EAX | |
xor ECX, ECX | |
cmp EAX, 10 | |
cmovle ECX, EAX | |
mov DWORD PTR [RIP + _counter_for_conditional_op], ECX | |
test ECX, ECX | |
je LBB2_2 | |
pop RBP | |
ret | |
LBB2_2: | |
lea RDI, QWORD PTR [RIP + L_.str] | |
pop RBP | |
jmp _puts ## TAILCALL | |
__Z22modulo_counter_processv: ## @_Z22modulo_counter_processv | |
push RBP | |
mov RBP, RSP | |
mov EAX, DWORD PTR [RIP + _counter_for_modulo] | |
lea ECX, DWORD PTR [RAX + 1] | |
movsxd RCX, ECX | |
imul RCX, RCX, 780903145 | |
mov RDX, RCX | |
shr RDX, 63 | |
sar RCX, 33 | |
add ECX, EDX | |
imul ECX, ECX, 11 | |
neg ECX | |
lea EAX, DWORD PTR [RAX + RCX + 1] | |
mov DWORD PTR [RIP + _counter_for_modulo], EAX | |
test EAX, EAX | |
je LBB3_2 | |
pop RBP | |
ret | |
LBB3_2: | |
lea RDI, QWORD PTR [RIP + L_.str] | |
pop RBP | |
jmp _puts ## TAILCALL | |
.section __TEXT,__cstring,cstring_literals | |
L_.str: ## @.str | |
.asciz "unko" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment