Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cf_math_pkg tool incompatibilty #105

Open
stmach opened this issue Nov 26, 2020 · 3 comments
Open

cf_math_pkg tool incompatibilty #105

stmach opened this issue Nov 26, 2020 · 3 comments

Comments

@stmach
Copy link
Contributor

stmach commented Nov 26, 2020

The use of $clog2 on a (generally non-static) function argument does not compile unless using the last year's [synthesis tool that shall not be named] or newer.

Found here:

return (num_idx > 32'd1) ? unsigned'($clog2(num_idx)) : 32'd1;

This is bad since functions from cf_math_pkg were shoehorned into other common cells and subsequently massively break tool compatibility of IPs using common cells such as fpnew.

I'm not sure what the correct solution to the problem is because the use of clog2 is not illegal per se, just not generally synthesizable unless the argument is known static.

@andreaskurth
Copy link
Contributor

static is a lifetime qualifier, are you sure it is related to that and not to constant expressions?

In general, math system functions, such as $clog2, may be used in constant expressions (20.8). "The operands of a constant expression consist of constant numbers, strings, parameters, constant bit-selects and part selects of parameters, constant function calls, and constant system function calls only." (11.2.1)

"Constant system function calls are calls to certain built-in system functions where the arguments meet conditions outlined in this subclause. When used in constant expressions, these function calls shall be evaluated at elaboration time. The system functions that may be used in constant system function calls are pure functions, i.e., those whose value depends only on their input arguments and that have no side effects. Certain built-in system functions where the arguments are constant expressions are constant system function." (11.2.1)

(Quotes and section references from IEEE 1800-2012.)

Because some EDA tools have bugs concerning constant system function calls, I have had some success defining a constant function clog2:

// Ceiled Binary Logarithm of a Natural Number
//
// Returns the binary logarithm (i.e., the logarithm to the base 2) of a natural number rounded
// towards plus infinity.
//
// Use this as drop-in replacement for the `$clog2` system function where the latter is not
// supported by your toolchain.
// Ceiled Binary Logarithm of a Natural Number
//
// Returns the binary logarithm (i.e., the logarithm to the base 2) of a natural number rounded
// towards plus infinity.
//
// Use this as drop-in replacement for the `$clog2` system function where the latter is not
// supported by your toolchain.
function automatic integer clog2 (input longint unsigned val);
  automatic longint unsigned tmp;

  // pragma translate_off
  `ifndef VERILATOR
  if (val == 0) begin
    $fatal(1, "Logarithm of 0 cannot be represented!");
  end
  `endif
  // pragma translate_on

  tmp = val - 1;
  for (clog2 = 0; tmp > 0; clog2++) begin
    tmp = tmp >> 1;
  end
endfunction

and calling into this clog2 instead of $clog2 with a constant expression. Can you try to add this function to your local cf_math_pkg and replace the call to $clog2 in idx_width with a call to clog2?

@stmach
Copy link
Contributor Author

stmach commented Nov 27, 2020

Ah sorry, of course, I mean constant expressions, not the static qualifier. I guess it was late...
I'll give it a shot and report back.

@evmanz
Copy link

evmanz commented May 6, 2021

For whoever comes with the same problem of compile time errors in DC, the aforementioned solution works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants