From: Antonio Ospite Date: Fri, 8 Nov 2013 16:31:04 +0000 (+0100) Subject: Add an example about summing an array of integers X-Git-Url: https://git.ao2.it/experiments/inline-assembly.git/commitdiff_plain/56a5616401d150cb39d4133c4a149c0fbc744c82?ds=inline;hp=74cf2a428fabb68f49ea1eb15df96ae898765fc5 Add an example about summing an array of integers --- diff --git a/Makefile b/Makefile index 1e601a4..b350291 100644 --- a/Makefile +++ b/Makefile @@ -29,11 +29,15 @@ ifneq ($(CC),clang) CFLAGS += -Wunsafe-loop-optimizations endif +EXECUTABLES := inline-asm-sum inline-asm-array-sum -inline-asm-sum: inline-asm-sum.o +all: $(EXECUTABLES) clean: - rm -f *~ *.o inline-asm-sum + rm -f *~ *.o $(EXECUTABLES) -test: inline-asm-sum - valgrind --leak-check=full --show-reachable=yes ./inline-asm-sum +test: $(EXECUTABLES) + @for executable in $?; \ + do \ + valgrind --leak-check=full --show-reachable=yes ./$$executable; \ + done diff --git a/inline-asm-array-sum.c b/inline-asm-array-sum.c new file mode 100644 index 0000000..7eb7ae8 --- /dev/null +++ b/inline-asm-array-sum.c @@ -0,0 +1,76 @@ +/* + * inline-asm-array-sum - an example of inline assembly accessing arrays + * + * Copyright (C) 2013 Antonio Ospite + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +#define SIZE 10000001 + +static int array_sum(int *values, unsigned int n) +{ + int sum = 0; + +#if defined(__x86_64__) + __asm__( + "movq $0, %%rdi\n\t" /* i = 0 */ + ".REPEAT:\n\t" + "cmpl %%edi, %%ecx\n\t" /* if (i == n) */ + "je .DONE\n\t" + /* TODO: try incrementing the pointer by 4 */ + "movq (%%rbx,%%rdi,4), %%rdx\n\t" /* tmp = values[i]; 64 bit register */ + "addl %%edx, %%eax\n\t" /* sum += tmp[31:0]; */ + "incl %%edi\n\t" /* i++ */ + "jmp .REPEAT\n\t" + ".DONE:\n\t" + : "=a"(sum) + : "b"(values), "c"(n), "0"(sum) + : "%rdx", "%rdi", "cc" + ); +#else + unsigned int i; + for (i = 0; i < n; i++) + sum += values[i]; +#endif + return sum; +} + +int main(void) +{ + int *values; + int i; + int sum; + int expected; + int n = SIZE; + + srand(time(0)); + + values = malloc(n * sizeof(*values)); + + expected = 0; + for (i = 0; i < n; i++) { + values[i] = rand(); + expected += values[i]; + } + + sum = array_sum(values, n); + + printf("expected = %d; sum = %d; %s\n", expected, sum, sum == expected ? "OK" : "FAILED"); + return (sum == expected); +}