diff --git a/curs/curs-16-17-optimizari/.gitignore b/curs/curs-16-17-optimizari/.gitignore new file mode 100644 index 00000000..b6799fe5 --- /dev/null +++ b/curs/curs-16-17-optimizari/.gitignore @@ -0,0 +1,25 @@ +.vscode/ + +# Build artifacts +*.o +*~ + +# Example binaries +gprof_demo/gprof_demo +gprof_demo/profile_data.txt +gprof_demo/gmon.out +inline/no-inline +inline/inline +loop_unroll/normal_loop +loop_unroll/unrolled_loop +opt_demo/test +opt_demo/test_o1 +opt_demo/test_o2 +opt_demo/test_o3 +opt_demo/test_unroll +prefetch/prefetch +prefetch/no-prefetch +sse/test_sse +time_demo/clk +time_demo/rdtscp +time_demo/papi \ No newline at end of file diff --git a/curs/curs-16-17-optimizari/inline/Makefile b/curs/curs-16-17-optimizari/inline/Makefile index fe929297..14a6eb69 100644 --- a/curs/curs-16-17-optimizari/inline/Makefile +++ b/curs/curs-16-17-optimizari/inline/Makefile @@ -1,4 +1,4 @@ -CFLAGS=-m32 +CFLAGS=-m64 run: no-inline inline ./no-inline diff --git a/curs/curs-16-17-optimizari/loop_unroll/Makefile b/curs/curs-16-17-optimizari/loop_unroll/Makefile index c4946472..1bab8511 100644 --- a/curs/curs-16-17-optimizari/loop_unroll/Makefile +++ b/curs/curs-16-17-optimizari/loop_unroll/Makefile @@ -1,4 +1,4 @@ -CFLAGS=-m32 +CFLAGS=-m64 run: normal_loop unrolled_loop ./normal_loop diff --git a/curs/curs-16-17-optimizari/opt_demo/Makefile b/curs/curs-16-17-optimizari/opt_demo/Makefile index 80aa2232..ec585dba 100644 --- a/curs/curs-16-17-optimizari/opt_demo/Makefile +++ b/curs/curs-16-17-optimizari/opt_demo/Makefile @@ -1,19 +1,19 @@ all: test test_o1 test_o2 test_o3 test_unroll test: test.c - gcc -Wall -m32 -O0 -o test test.c + gcc -Wall -m64 -O0 -o $@ $< test_o1: test.c - gcc -Wall -m32 -O1 -o test_o1 test.c + gcc -Wall -m64 -O1 -o $@ $< test_o2: test.c - gcc -Wall -m32 -O2 -o test_o2 test.c + gcc -Wall -m64 -O2 -o $@ $< test_o3: test.c - gcc -Wall -m32 -O3 -o test_o3 test.c + gcc -Wall -m64 -O3 -o $@ $< test_unroll: test.c - gcc -Wall -funroll-loops -m32 -O3 -o test_unroll test.c + gcc -Wall -funroll-loops -m64 -O3 -o $@ $< clean: rm -rf test test_o* test_unroll diff --git a/curs/curs-16-17-optimizari/sse/Makefile b/curs/curs-16-17-optimizari/sse/Makefile index 6697163d..6d7f01d2 100644 --- a/curs/curs-16-17-optimizari/sse/Makefile +++ b/curs/curs-16-17-optimizari/sse/Makefile @@ -1,5 +1,5 @@ -ASM_FLAGS = -f elf32 -g -F dwarf -C_FLAGS = -g -m32 -O3 +ASM_FLAGS = -f elf64 -g -F dwarf +C_FLAGS = -g -m64 -O3 NASM=nasm CC=gcc CPP=g++ @@ -7,7 +7,7 @@ CPP=g++ all: test_sse test_sse: test_sse.o sse.o - gcc -g -O3 -m32 -o $@ $^ + gcc -g -O3 -m64 -o $@ $^ # test_sa.o: test_sa.asm # nasm -f elf32 -g -F dwarf test_sa.asm diff --git a/curs/curs-16-17-optimizari/sse/sse.asm b/curs/curs-16-17-optimizari/sse/sse.asm index 69871be8..bfc41190 100644 --- a/curs/curs-16-17-optimizari/sse/sse.asm +++ b/curs/curs-16-17-optimizari/sse/sse.asm @@ -1,74 +1,39 @@ - BITS 32 + BITS 64 + DEFAULT REL + GLOBAL sum_array_sse sum_array_sse: - push ebp - mov ebp, esp - push esi - push edi - - push ebx - mov ecx, [ebp + 20] ; ecx = n - mov esi, [ebp + 8] ; esi = a - mov edi, [ebp + 12] ; edi = b - mov ebx, [ebp + 16] ; ebx = c - ; n = n / 16 shr ecx, 4 + ; SysV x64 ABI: rdi=a, rsi=b, rdx=c, ecx=n xor eax, eax cmp eax, ecx - jge end -begin: - movdqu xmm0, [esi] - movdqu xmm1, [edi] + jge .end +.begin: + movdqu xmm0, [rdi + rax*4] + movdqu xmm1, [rsi + rax*4] paddd xmm0, xmm1 - movdqu [ebx], xmm0 - - add esi, 16 - add edi, 16 - add ebx, 16 - add eax, 4 + movdqu [rdx + rax*4], xmm0 + + add eax, 4 cmp eax, ecx - jl begin -end: - pop ebx - pop edi - pop esi - leave + jl .begin +.end: ret - -GLOBAL sum_array_plain + GLOBAL sum_array_plain sum_array_plain: - push ebp - mov ebp, esp - push esi - push edi - push ebx - push edx - - mov ecx, [ebp + 20] ; ecx = n - mov esi, [ebp + 8] ; esi = a - mov edi, [ebp + 12] ; edi = b - mov ebx, [ebp + 16] ; ebx = c - + ; SysV x64 ABI: rdi=a, rsi=b, rdx=c, ecx=n xor eax, eax cmp eax, ecx - jge pend -pbegin: - mov edx, [esi+eax*4] - add edx, [edi+eax*4] - mov [ebx+eax*4], edx - - ;; add esi, 4 - ;; add edi, 4 - ;; add ebx, 4 - add eax, 1 + jge .pend +.pbegin: + mov r8d, [rdi + rax*4] + add r8d, [rsi + rax*4] + mov [rdx + rax*4], r8d + + add eax, 1 cmp eax, ecx - jl pbegin -pend: - pop edx - pop ebx - pop edi - pop esi - leave + jl .pbegin +.pend: ret diff --git a/curs/curs-16-17-optimizari/sse/test_sse.c b/curs/curs-16-17-optimizari/sse/test_sse.c index 55328077..70a5ee21 100644 --- a/curs/curs-16-17-optimizari/sse/test_sse.c +++ b/curs/curs-16-17-optimizari/sse/test_sse.c @@ -3,9 +3,10 @@ #include uint64_t rdtsc(void) { - uint64_t result; - __asm__ __volatile__ ("rdtsc" : "=A" (result)); - return result; + uint32_t lo, hi; + + __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); + return ((uint64_t)hi << 32) | lo; } void sum_array_sse(uint32_t *a, uint32_t *b, uint32_t *c, int n); diff --git a/curs/curs-16-17-optimizari/time_demo/Makefile b/curs/curs-16-17-optimizari/time_demo/Makefile index e0e879ac..d2900bd0 100644 --- a/curs/curs-16-17-optimizari/time_demo/Makefile +++ b/curs/curs-16-17-optimizari/time_demo/Makefile @@ -1,10 +1,10 @@ -all: clk rdtscp papi +all: clk rdtscp clk: clk.c - gcc -O3 -m32 -Wall clk.c -o clk + gcc -O3 -m64 -Wall clk.c -o clk rdtscp: rdtscp.c - gcc -O3 -m32 -Wall rdtscp.c -o rdtscp + gcc -O3 -m64 -Wall rdtscp.c -o rdtscp papi: papi.c gcc -O3 -Wall papi.c -I$(PAPI_DIR) -L$(PAPI_DIR) -o papi -lpapi