Skip to content

Commit

Permalink
Fix time issue in FreeBSD (pow() bug)
Browse files Browse the repository at this point in the history
  • Loading branch information
caryr committed Dec 9, 2023
1 parent 58dcf39 commit 5d561f3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
4 changes: 2 additions & 2 deletions elab_sig.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000-2021 Stephen Williams ([email protected])
* Copyright (c) 2000-2023 Stephen Williams ([email protected])
* Copyright CERN 2012 / Stephen Williams ([email protected])
*
* This source code is free software; you can redistribute it
Expand Down Expand Up @@ -1189,7 +1189,7 @@ NetNet* PWire::elaborate_sig(Design*des, NetScope*scope) const
cerr << get_fileline() << ": debug: Create signal " << wtype;
if (set_data_type_)
cout << " " << *set_data_type_;
cout << name_ << unpacked_dimensions << " in scope "
cout << " " << name_ << unpacked_dimensions << " in scope "
<< scope_path(scope) << endl;
}

Expand Down
50 changes: 49 additions & 1 deletion ivtest/ivltests/timeliteral.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module same;
timeunit 1ps;
timeprecision 1ps;

logic passed;

function logic check_time;
realtime result;

Expand Down Expand Up @@ -45,12 +47,29 @@ module same;
check_time = 1'b0;
end
endfunction

initial begin
passed = 1'b1;
#0.499ps;
if ($realtime() != 0.0) begin
$display("Failed: Expected a 0.499ps delay to scale to 0ps of delay, got %f", $realtime());
passed = 1'b0;
end

#0.5ps;
if ($realtime() != 1.0) begin
$display("Failed: Expected a 0.5ps delay to scale to 1ps of delay, got %f", $realtime());
passed = 1'b0;
end
end
endmodule

module max;
timeunit 100s;
timeprecision 1fs;

bit passed;

function logic check_time;
realtime result;

Expand All @@ -74,6 +93,21 @@ module max;
check_time = 1'b0;
end
endfunction

initial begin
passed = 1'b1;
#0.499fs;
if ($realtime() != 0.0) begin
$display("Failed: Expected a 0.499fs delay to scale to 0s of delay, got %f", $realtime());
passed = 1'b0;
end

#0.5fs;
if ($realtime() != 1e-17) begin
$display("Failed: Expected a 0.5fs delay to scale to 1e-17s of delay, got %f", $realtime());
passed = 1'b0;
end
end
endmodule

module top;
Expand Down Expand Up @@ -119,6 +153,20 @@ module top;
passed &= same.check_time();
passed &= max.check_time();

if (passed) $display("PASSED");
#0.499ps;
if ($realtime() != 0.0) begin
$display("Failed: Expected a 0.499fs delay to scale to 0ns of delay, got %f", $realtime());
passed = 1'b0;
end

#0.5ps;
if ($realtime() != 0.001) begin
$display("Failed: Expected a 0.5fs delay to scale to 0.001ns of delay, got %f", $realtime());
passed = 1'b0;
end

#1; // Make sure all the delay tests have completed

if (passed && max.passed && same.passed) $display("PASSED");
end
endmodule
14 changes: 14 additions & 0 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -3178,7 +3178,14 @@ delay_value_simple
if ($1 == 0 || !get_time_unit($1, unit))
yyerror(@1, "internal error: time literal delay.");
else {
#ifdef __FreeBSD__
// Using raw pow() in FreeBSD gives a value that is off by one and this causes
// rounding issues later, so for now use powl() to get the correct result.
long double ldp = powl(10.0, (long double)(unit - pform_get_timeunit()));
double p = (double) ldp;
#else
double p = pow(10.0, (double)(unit - pform_get_timeunit()));
#endif
double time = atof($1) * p;

verireal *v = new verireal(time);
Expand Down Expand Up @@ -3825,7 +3832,14 @@ expr_primary
if ($1 == 0 || !get_time_unit($1, unit))
yyerror(@1, "internal error: time literal.");
else {
#ifdef __FreeBSD__
// Using raw pow() in FreeBSD gives a value that is off by one and this causes
// rounding issues below, so for now use powl() to get the correct result.
long double ldp = powl(10.0, (double)(unit - pform_get_timeunit()));
double p = (double) ldp;
#else
double p = pow(10.0, (double)(unit - pform_get_timeunit()));
#endif
double time = atof($1) * p;
// The time value needs to be rounded at the correct digit
// since this is a normal real value and not a delay that
Expand Down

0 comments on commit 5d561f3

Please sign in to comment.