Added FIR example

This commit is contained in:
David Harris 2022-01-20 16:57:36 +00:00
parent ca1f7ce5d3
commit ac28880cd9
4 changed files with 77 additions and 1 deletions

View File

@ -9,7 +9,7 @@ install:
cp ${RISCV}/riscv-isa-sim/arch_test_target/spike/Makefile.include addins/riscv-arch-test/
sed -i '/export TARGETDIR ?=/c\export TARGETDIR ?= ${RISCV}/riscv-isa-sim/arch_test_target' addins/riscv-arch-test/Makefile.include
echo export RISCV_PREFIX = riscv64-unknown-elf- >> addins/riscv-arch-test/Makefile.include
tests/linux-testgen/linux-testvectors/tvLinker.sh
# tests/linux-testgen/linux-testvectors/tvLinker.sh # needs to be run in local directory
regression:
make -C pipelined/regression

33
examples/C/fir/Makefile Normal file
View File

@ -0,0 +1,33 @@
TARGET = fir
$(TARGET).objdump: $(TARGET)
riscv64-unknown-elf-objdump -S -D $(TARGET) > $(TARGET).objdump
spike $(TARGET)
$(TARGET): $(TARGET).c Makefile
riscv64-unknown-elf-gcc -o $(TARGET) -g -O\
-march=rv64gc -mabi=lp64d -mcmodel=medany \
-nostdlib -static -lm -fno-tree-loop-distribute-patterns \
-T../common/test.ld -I../common \
$(TARGET).c ../common/crt.S ../common/syscalls.c
# Compiler flags:
# -o $(TARGET) defines the name of the output file
# -g generates debugging symbols for gdb
# -O turns on basic optimization; -O3 turns on heavy optimization; omit for no optimization
# -march=rv64gc -mabi=lp64d =mcmodel=medany generates code for RV64GC with doubles and long/ptrs = 64 bits
# -static forces static linking (no dynamic shared libraries on bare metal)
# -lm links the math library if necessary (when #include math.h)
# -nostdlib avoids inserting standard startup files and default libraries
# because we are using crt.s on bare metal
# -fno-tree-loop-distribute-patterns turns replacing loops with memcpy/memset in the std library
# -T specifies the linker file
# -I specifies the include path (e.g. for util.h)
# The last line defines the C files to compile.
# crt.S is needed as our startup file to initialize the processor
# syscalls.c implements printf through the HTIF for Spike
# other flags from riscv-tests makefiles that don't seem to be important
# -ffast-math -DPREALLOCATE=1 -std=gnu99 \
# -fno-common -fno-builtin-printf -nostartfiles -lgcc \
clean:
rm -f $(TARGET) $(TARGET).objdump

BIN
examples/C/fir/fir Executable file

Binary file not shown.

43
examples/C/fir/fir.c Normal file
View File

@ -0,0 +1,43 @@
// fir.c
// David_Harris@hmc.edu 20 January 2022
// Finite Impulse Response Filter
#include <stdio.h> // supports printf
#include <math.h>
#include "util.h" // supports verify
void fir(int N, int M, double X[], double c[], double Y[]) {
int i, n;
double sum;
for (n=0; n<N; n++) {
sum = 0;
for (i=0; i<M; i++) {
sum += c[i]*X[n-i+(M-1)];
}
Y[n] = sum;
}
}
int main(void) {
double X[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
double c[5] = {0.2, 0.2, 0.2, 0.2, 0.2};
double Y[15];
double Yexpected[15] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
setStats(1);
fir(15, 5, X, c, Y);
setStats(0);
for (int i=0; i<15; i++) {
int tmp = Y[i];
printf("Y[%d] = %d\n", i, tmp);
}
// verifyDouble doesn't work exaclty because of rounding, so check for almost equal
for (int i=0; i<15; i++) {
if (fabs(Y[i] - Yexpected[i]) > 1e-10) {
//printf("bad case %d\n", i);
return 1;
}
}
return 0;
}