I understand function overloading is not supported in SV, wish it is - or atleast as a new feature maybe? Now, back to reality - I am looking at implementing some common math functions such as mean. Say it has to work on vector and matrix (as in typical Matlab). I have few workarounds that I’ve done earlier for same, but am looking at improving the same. Basically I am using a parameterized class and static function to sort of mimic function overloading. But compiler throws error as below. So how do I write code that depends on “type” of the arguments to a class method?
Thanks
Sample code below. In the method mean I need to decide/delcare the rval to be a single real number of a dyn-array based on input type - being vector/matrix (1-d or 2-d dyn array respectively).
package p;
timeunit 1ns; timeprecision 1ns;
typedef real ml_vec_t[];
typedef real ml_matrix_t[][];
typedef real ml_mda_t[][][];
typedef enum bit [1:0] {
ML_MEAN_ROW, ML_MEAN_COL, ML_MEAN_ALL} ml_mean_dim_t;
class MathLibVec #(
type INP_T = ml_vec_t,
type OUT_T = ml_vec_t,
type MOD_T = ml_vec_t
);
static function OUT_T mean(INP_T in_val,
ml_mean_dim_t dim = ML_MEAN_COL);
OUT_T rval;
int num_upk_dim;
int num_rows;
int num_cols;
num_upk_dim = $unpacked_dimensions (in_val);
if (num_upk_dim == 2) begin : ml_matrix
num_rows = in_val.size();
num_cols = in_val[0].size();
rval = new [num_cols];
end : ml_matrix
return rval;
endfunction : mean
endclass
endpackage : p
module m;
import p::*;
typedef int int_darr_1d_t [];
typedef int int_darr_2d_t [] [];
int_darr_1d_t ida_1d;
int_darr_2d_t ida_2d;
ml_vec_t m_rda_1d;
initial begin
ida_1d = '{1,-2.1,3,4.2};
m_rda_1d = MathLibVec #(int_darr_1d_t)::mean(ida_1d);
#10 $finish (2);
end
endmodule : m
Error:
# ** Error: (vsim-3701) ../src/sv_fn_olad.sv(29): 'size' is not a field or method in 'in_val[0]'.
# Time: 0 ns Iteration: 0 Region: /p::MathLibVec #(int$[], real$[], real$[]) File: ../src/sv_fn_olad.sv
# ** Error: (vsim-3701) ../src/sv_fn_olad.sv(29): 'size' is not a field or method in 'in_val[0]'.
# Time: 0 ns Iteration: 0 Region: /p::MathLibVec #(int$[], real$[], real$[]) File: ../src/sv_fn_olad.sv
# Error loading design