Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions curs/curs-16-17-optimizari/.gitignore
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion curs/curs-16-17-optimizari/inline/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS=-m32
CFLAGS=-m64

run: no-inline inline
./no-inline
Expand Down
2 changes: 1 addition & 1 deletion curs/curs-16-17-optimizari/loop_unroll/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS=-m32
CFLAGS=-m64

run: normal_loop unrolled_loop
./normal_loop
Expand Down
10 changes: 5 additions & 5 deletions curs/curs-16-17-optimizari/opt_demo/Makefile
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions curs/curs-16-17-optimizari/sse/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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++

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
Expand Down
83 changes: 24 additions & 59 deletions curs/curs-16-17-optimizari/sse/sse.asm
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions curs/curs-16-17-optimizari/sse/test_sse.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
#include <stdlib.h>

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);
Expand Down
6 changes: 3 additions & 3 deletions curs/curs-16-17-optimizari/time_demo/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down