From a0d42a0234fb2dcd1a00884a0b4fbfe8b9f3181d Mon Sep 17 00:00:00 2001
From: jonah <jf3g19@soton.ac.uk>
Date: Sun, 29 Nov 2020 03:47:16 +0000
Subject: [PATCH] added .cpp file for generating LUT

---
 generate_LUT.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 generate_LUT.cpp

diff --git a/generate_LUT.cpp b/generate_LUT.cpp
new file mode 100644
index 0000000..bb1fdce
--- /dev/null
+++ b/generate_LUT.cpp
@@ -0,0 +1,67 @@
+#include <iostream>
+#include <bitset>
+#include <functional>
+#include <stdlib.h>
+#include <cmath>  
+#include <vector>
+#include <fstream>
+
+/*
+- 1st arg is the propgram name
+- 2nd ... last args are the coefficients of the FIR filter
+*/
+
+int clog2(int n)
+{
+    int a = 0;
+    n--;
+    while (n > 0)
+    {
+        a++;
+        n >>= 1;
+    }
+    return a;
+}
+
+
+int main(int argc, char *argv[])
+{
+    std::size_t num_coeff = argc - 1;
+    std::size_t address_bit_width = num_coeff;
+    std::size_t data_bit_width = num_coeff+8;
+    std::vector<double> coefficients;
+
+    //generate a vector of the coefficients
+    for(std::size_t i = 0; i < num_coeff; ++i)
+    {
+        coefficients.push_back(std::stod(argv[i+1]));
+    }
+
+    std::ofstream dest_file;
+    dest_file.open("LUT_cases.txt", std::ofstream::out | std::ofstream::trunc);
+    
+    //for each number in the range 0 ... pow(2,num_coeff-1), multiply each bit by the coefficients
+    //assuming SIGNED 2c multiplication, so num_coeff-1
+    for(std::size_t i = 0; i < pow(2,num_coeff); ++i)
+    {
+        std::bitset<64> bits(i);
+        int sum = 0; 
+
+        for(std::size_t j = 0; j < num_coeff; ++j)
+        {
+            sum += bits[j] * coefficients[j];    
+        }
+
+        //write the value of the input, i, and the corresponding sum, sum, to a file
+        if(sum >= 0)
+            dest_file <<  data_bit_width << "'d" << sum << ", ";
+        else
+            dest_file << "-" << data_bit_width << "'d" << abs(sum) << ", ";
+		
+		if(!(i%8) && i>0)
+			dest_file << "\n";
+    }
+
+    //add the default case and close the file
+    dest_file.close();
+}
\ No newline at end of file
-- 
GitLab