Esempio n. 1
0
    def test_combo_mux(self):
        f = self.parse_and_get_function(
            """
        void test(bool a,
             ac_channel<int> &in,
             ac_channel<int> &out1,
             ac_channel<int> &out2) {
        const int inv = in.read();
        if(a) {
          out1.write(inv);
        } else {
          out2.write(inv);
        }
      }""", None, ["in"], ["out1", "out2"])

        def v32(n):
            return ir_value.Value(bits_mod.SBits(value=int(n), bit_count=32))

        def v1(n):
            return ir_value.Value(bits_mod.UBits(value=int(n), bit_count=1))

        args = dict(a=v1(0),
                    in_z=v32(10),
                    in_vz=v1(1),
                    out1_vz=v1(1),
                    out2_vz=v1(0))
        result = ir_interpreter.run_function_kwargs(f, args)
        self.assertEqual(str(result), "(0, 0, 0, 1, 10)")

        args = dict(a=v1(0),
                    in_z=v32(10),
                    in_vz=v1(1),
                    out1_vz=v1(1),
                    out2_vz=v1(1))
        result = ir_interpreter.run_function_kwargs(f, args)
        self.assertEqual(str(result), "(1, 0, 0, 1, 10)")

        args = dict(a=v1(1),
                    in_z=v32(22),
                    in_vz=v1(1),
                    out1_vz=v1(1),
                    out2_vz=v1(0))
        result = ir_interpreter.run_function_kwargs(f, args)
        self.assertEqual(str(result), "(1, 1, 22, 0, 0)")

        args = dict(a=v1(1),
                    in_z=v32(22),
                    in_vz=v1(0),
                    out1_vz=v1(1),
                    out2_vz=v1(0))
        result = ir_interpreter.run_function_kwargs(f, args)
        self.assertEqual(str(result), "(1, 0, 22, 0, 0)")
Esempio n. 2
0
 def test_class_methods(self):
     source = """
 class Rectangle {
   int width;
   int height;
   int getArea(){
     return width * height;
   }
 };
 void Rectangle::set_values(int w, int h) {
   width = w;
   height = h;
 }
 int Rectangle::get_width(){
   return width;
 }
 int Rectangle::get_height(){
   return height;
 }
 int test(int a, int b) {
   Rectangle rect;
   rect.width = 0;
   rect.height = 0;
   rect.set_values(a, b);
   return rect.getArea() + rect.get_height() + rect.get_width();
 }
 """
     f = self.parse_and_get_function(source)
     aval = ir_value.Value(bits_mod.SBits(value=int(6), bit_count=32))
     bval = ir_value.Value(bits_mod.SBits(value=int(3), bit_count=32))
     args = dict(a=aval, b=bval)
     result = ir_interpreter.run_function_kwargs(f, args)
     result_int = int(ctypes.c_int32(int(str(result))).value)
     self.assertEqual(27, result_int)
Esempio n. 3
0
    def test_mandelbrot(self):
        translator = xlscc_translator.Translator("mypackage")

        my_parser = ext_c_parser.XLSccParser()

        source_path = runfiles.get_path(
            "xls/contrib/xlscc/translate/testdata/mandelbrot_test.cc")
        binary_path = runfiles.get_path(
            "xls/contrib/xlscc/translate/mandelbrot_test")

        nx = 48
        ny = 32

        with open(source_path) as f:
            content = f.read()

        # Hackily do the preprocessing, since in build environments we do not have a
        # cpp binary available (and if we did relying on it here without a
        # build-system-noted dependency would be non-hermetic).
        content = re.sub("^#if !NO_TESTBENCH$.*^#endif$", "", content, 0,
                         re.MULTILINE | re.DOTALL)
        content = re.sub(re.compile("//.*?\n"), "", content)

        f = self.create_tempfile(content=content)
        ast = pycparser.parse_file(f.full_path,
                                   use_cpp=False,
                                   parser=my_parser)

        cpp_out = None
        with os.popen(binary_path) as osf:
            cpp_out = osf.read()
        parsed_cpp_out = eval(cpp_out)

        translator.parse(ast)

        p = translator.gen_ir()
        f = p.get_function("mandelbrot")

        result_arr = parsed_cpp_out

        for y in range(0, ny):
            for x in range(0, nx):
                xx = float(x) / nx
                yy = float(y) / ny

                xi = int(xx * 2.5 - 1.8) * (1 << 16)
                yi = int(yy * 2.2 - 1.1) * (1 << 16)

                args = dict(c_r=ir_value.Value(
                    bits_mod.SBits(value=int(xi), bit_count=32)),
                            c_i=ir_value.Value(
                                bits_mod.SBits(value=int(yi), bit_count=32)))

                result = ir_interpreter.run_function_kwargs(f, args)

                result_sai32 = int(str(result))
                result_arr[y][x] = result_sai32

        self.assertEqual(parsed_cpp_out, result_arr)
Esempio n. 4
0
 def test_globalconst(self):
     f = self.parse_and_get_function("""
   const int foo[6] = {2,4,5,3,2,1};
   int test(int a) {
     return foo[a];
   }
 """)
     aval = ir_value.Value(bits_mod.SBits(value=int(2), bit_count=32))
     args = dict(a=aval)
     result = ir_interpreter.run_function_kwargs(f, args)
     result_int = int(ctypes.c_int32(int(str(result))).value)
     self.assertEqual(5, result_int)
Esempio n. 5
0
 def one_in_one_out(self, source, a_input, b_input, expected_output):
     f = self.parse_and_get_function("""
   int test(sai32 a, sai32 b) {
     """ + source + """
   }
 """)
     aval = ir_value.Value(bits_mod.SBits(value=int(a_input), bit_count=32))
     bval = ir_value.Value(bits_mod.SBits(value=int(b_input), bit_count=32))
     args = dict(a=aval, b=bval)
     result = ir_interpreter.run_function_kwargs(f, args)
     self.assertEqual(expected_output,
                      int(ctypes.c_int32(int(str(result))).value))
Esempio n. 6
0
 def test_enum_autocount(self):
     source = """
 enum states{w, x, y=5, z};
 int test(int a, int b){
     return a+z+b+x;
 }
 """
     f = self.parse_and_get_function(source)
     aval = ir_value.Value(bits_mod.SBits(value=int(2), bit_count=32))
     bval = ir_value.Value(bits_mod.SBits(value=int(3), bit_count=32))
     args = dict(a=aval, b=bval)
     result = ir_interpreter.run_function_kwargs(f, args)
     result_int = int(ctypes.c_int32(int(str(result))).value)
     self.assertEqual(12, result_int)
Esempio n. 7
0
 def test_arrayref(self):
     f = self.parse_and_get_function("""
   void array_update(int o[2], int x) {
     o[0] = x;
   }
   int test(int a, int b) {
     int s[2] = {0,b};
     array_update(s, a);
     return s[0] + s[1];
   }
 """)
     aval = ir_value.Value(bits_mod.SBits(value=int(22), bit_count=32))
     bval = ir_value.Value(bits_mod.SBits(value=int(10), bit_count=32))
     args = dict(a=aval, b=bval)
     result = ir_interpreter.run_function_kwargs(f, args)
     result_int = int(ctypes.c_int32(int(str(result))).value)
     self.assertEqual(32, result_int)
Esempio n. 8
0
    def test_structref(self):
        somestruct = hls_types_pb2.HLSStructType()

        int_type = hls_types_pb2.HLSIntType()
        int_type.signed = True
        int_type.width = 18

        translated_hls_type = hls_types_pb2.HLSType()
        translated_hls_type.as_int.CopyFrom(int_type)

        hls_field = hls_types_pb2.HLSNamedType()
        hls_field.name = "x"
        hls_field.hls_type.CopyFrom(translated_hls_type)
        somestruct.fields.add().CopyFrom(hls_field)

        hls_field = hls_types_pb2.HLSNamedType()
        hls_field.name = "y"
        hls_field.hls_type.CopyFrom(translated_hls_type)
        somestruct.fields.add().CopyFrom(hls_field)

        somestructtype = hls_types_pb2.HLSType()
        somestructtype.as_struct.CopyFrom(somestruct)
        hls_types_by_name = {"SomeStruct": somestructtype}

        f = self.parse_and_get_function(
            """
      void struct_update(SomeStruct &o, sai18 x) {
        o.x.set_slc(0, x);
      }
      int test(sai32 a, int b) {
        SomeStruct s;
        s.x = 0;
        s.y = b;
        struct_update(s, a);
        return s.x + s.y;
      }
    """, hls_types_by_name)
        aval = ir_value.Value(bits_mod.SBits(value=int(22), bit_count=32))
        bval = ir_value.Value(bits_mod.SBits(value=int(10), bit_count=32))
        args = dict(a=aval, b=bval)
        result = ir_interpreter.run_function_kwargs(f, args)
        result_int = int(ctypes.c_int32(int(str(result))).value)
        self.assertEqual(32, result_int)
Esempio n. 9
0
 def test_ref_params(self):
     f = self.parse_and_get_function("""
   void add_in_place(int &o, int x) {
     o += x;
   }
   int add_in_place_2(int x, int &o) {
     o += x;
     return o*2;
   }
   int test(int a, int b){
     add_in_place(a, b);
     return add_in_place_2(b, a) + a;
   }
 """)
     aval = ir_value.Value(bits_mod.SBits(value=int(11), bit_count=32))
     bval = ir_value.Value(bits_mod.SBits(value=int(3), bit_count=32))
     args = dict(a=aval, b=bval)
     result = ir_interpreter.run_function_kwargs(f, args)
     result_int = int(ctypes.c_int32(int(str(result))).value)
     self.assertEqual(51, result_int)