1 |
49 |
qaztronic |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// Copyright (C) 2019 Authors and OPENCORES.ORG ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// This source file may be used and distributed without ////
|
6 |
|
|
//// restriction provided that this copyright statement is not ////
|
7 |
|
|
//// removed from the file and that any derivative work contains ////
|
8 |
|
|
//// the original copyright notice and the associated disclaimer. ////
|
9 |
|
|
//// ////
|
10 |
|
|
//// This source file is free software; you can redistribute it ////
|
11 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
12 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
13 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
14 |
|
|
//// later version. ////
|
15 |
|
|
//// ////
|
16 |
|
|
//// This source is distributed in the hope that it will be ////
|
17 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
18 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
19 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
20 |
|
|
//// details. ////
|
21 |
|
|
//// ////
|
22 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
23 |
|
|
//// Public License along with this source; if not, download it ////
|
24 |
|
|
//// from http://www.opencores.org/lgpl.shtml ////
|
25 |
|
|
//// ////
|
26 |
|
|
//////////////////////////////////////////////////////////////////////
|
27 |
|
|
|
28 |
|
|
typedef int array_shape_t[]; // same shape convention as numpy
|
29 |
|
|
typedef int array_index_t[];
|
30 |
|
|
|
31 |
|
|
class numeric_array #(type T = shortreal);
|
32 |
|
|
array_shape_t shape;
|
33 |
|
|
int dim;
|
34 |
|
|
T a_1d[];
|
35 |
|
|
T a_2d[][];
|
36 |
|
|
T a_3d[][][];
|
37 |
|
|
|
38 |
|
|
// --------------------------------------------------------------------
|
39 |
|
|
function void set_entry(array_index_t i, T value);
|
40 |
|
|
case(dim)
|
41 |
|
|
1: a_1d[i[0]] = value;
|
42 |
|
|
2: a_2d[i[0]][i[1]] = value;
|
43 |
|
|
3: a_3d[i[0]][i[1]][i[2]] = value; //[z][y][x]
|
44 |
|
|
default: $stop; // not supported
|
45 |
|
|
endcase
|
46 |
|
|
endfunction
|
47 |
|
|
|
48 |
|
|
// --------------------------------------------------------------------
|
49 |
|
|
function T get_entry(array_index_t i);
|
50 |
|
|
case(dim)
|
51 |
|
|
1: get_entry = a_1d[i[0]];
|
52 |
|
|
2: get_entry = a_2d[i[0]][i[1]];
|
53 |
|
|
3: get_entry = a_3d[i[0]][i[1]][i[2]]; //[z][y][x]
|
54 |
|
|
default: $stop; // not supported
|
55 |
|
|
endcase
|
56 |
|
|
endfunction
|
57 |
|
|
|
58 |
|
|
// --------------------------------------------------------------------
|
59 |
|
|
function longint to_bits(array_index_t i);
|
60 |
|
|
case(type(T))
|
61 |
|
|
type(shortreal): to_bits = $shortrealtobits(get_entry(i));
|
62 |
|
|
type(real): to_bits = $realtobits(get_entry(i));
|
63 |
|
|
default: $stop; // not supported
|
64 |
|
|
endcase
|
65 |
|
|
endfunction
|
66 |
|
|
|
67 |
|
|
// --------------------------------------------------------------------
|
68 |
|
|
function int bits;
|
69 |
|
|
case(type(T))
|
70 |
|
|
type(shortreal): bits = 32;
|
71 |
|
|
type(real): bits = 64;
|
72 |
|
|
default: $stop; // not supported
|
73 |
|
|
endcase
|
74 |
|
|
endfunction
|
75 |
|
|
|
76 |
|
|
// --------------------------------------------------------------------
|
77 |
|
|
function void new_2d(array_shape_t shape);
|
78 |
|
|
a_2d = new[shape[0]];
|
79 |
|
|
foreach(a_2d[y])
|
80 |
|
|
a_2d[y] = new[shape[1]];
|
81 |
|
|
endfunction
|
82 |
|
|
|
83 |
|
|
// --------------------------------------------------------------------
|
84 |
|
|
function void new_3d(array_shape_t shape);
|
85 |
|
|
a_3d = new[shape[0]];
|
86 |
|
|
foreach(a_3d[z])
|
87 |
|
|
begin
|
88 |
|
|
a_3d[z] = new[shape[1]];
|
89 |
|
|
foreach(a_3d[z,y])
|
90 |
|
|
a_3d[z][y] = new[shape[2]];
|
91 |
|
|
end
|
92 |
|
|
endfunction
|
93 |
|
|
|
94 |
|
|
// --------------------------------------------------------------------
|
95 |
|
|
function void make_new;
|
96 |
|
|
case(dim)
|
97 |
|
|
1: a_1d = new[shape[0]];
|
98 |
|
|
2: new_2d(shape);
|
99 |
|
|
3: new_3d(shape);
|
100 |
|
|
default: $stop; // not supported
|
101 |
|
|
endcase
|
102 |
|
|
endfunction
|
103 |
|
|
|
104 |
|
|
// --------------------------------------------------------------------
|
105 |
|
|
function void make_2d_constant(T value=0.0);
|
106 |
|
|
$display("### value = %x", $shortrealtobits(value));
|
107 |
|
|
make_new();
|
108 |
|
|
foreach(a_2d[y,x])
|
109 |
|
|
a_2d[y][x] = value;
|
110 |
|
|
endfunction
|
111 |
|
|
|
112 |
|
|
// --------------------------------------------------------------------
|
113 |
|
|
function new(array_shape_t shape);
|
114 |
|
|
this.shape = shape;
|
115 |
|
|
this.dim = shape.size();
|
116 |
|
|
make_new();
|
117 |
|
|
endfunction
|
118 |
|
|
|
119 |
|
|
// --------------------------------------------------------------------
|
120 |
|
|
endclass
|