Skip to content

Commit 804bd00

Browse files
Merge pull request #147 from emustafa96/fix/conversion-sign
🐛 Perform sign extension after conversion and rounding
2 parents 0d3e8c4 + 17f1561 commit 804bd00

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/fpnew_cast_multi.sv

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,14 +510,16 @@ module fpnew_cast_multi #(
510510
logic [NUM_FORMATS-1:0] fmt_of_after_round;
511511
logic [NUM_FORMATS-1:0] fmt_uf_after_round;
512512

513-
logic [NUM_INT_FORMATS-1:0][WIDTH-1:0] ifmt_pre_round_abs; // per format
513+
logic [NUM_INT_FORMATS-1:0][WIDTH-1:0] ifmt_pre_round_abs; // per format
514+
logic [NUM_INT_FORMATS-1:0][WIDTH-1:0] ifmt_rounded_signed_res; // per format
514515
logic [NUM_INT_FORMATS-1:0] ifmt_of_after_round;
515516

516517
logic rounded_sign;
517518
logic [WIDTH-1:0] rounded_abs; // absolute value of result after rounding
518519
logic result_true_zero;
519520

520-
logic [WIDTH-1:0] rounded_int_res; // after possible inversion
521+
logic [WIDTH-1:0] rounded_uint_res; // after possible inversion
522+
logic [WIDTH-1:0] rounded_int_res; // sign-extended, after possible inversion
521523
logic rounded_int_res_zero; // after rounding
522524

523525

@@ -536,15 +538,15 @@ module fpnew_cast_multi #(
536538
end
537539
end
538540

539-
// Sign-extend integer result
540-
for (genvar ifmt = 0; ifmt < int'(NUM_INT_FORMATS); ifmt++) begin : gen_int_res_sign_ext
541+
// Zero-extend integer result
542+
for (genvar ifmt = 0; ifmt < int'(NUM_INT_FORMATS); ifmt++) begin : gen_int_res_zero_ext
541543
// Set up some constants
542544
localparam int unsigned INT_WIDTH = fpnew_pkg::int_width(fpnew_pkg::int_format_e'(ifmt));
543545

544546
if (IntFmtConfig[ifmt]) begin : active_format
545547
always_comb begin : assemble_result
546-
// sign-extend reusult
547-
ifmt_pre_round_abs[ifmt] = '{default: final_int[INT_WIDTH-1]};
548+
// zero-extend absolute value result
549+
ifmt_pre_round_abs[ifmt] = '0;
548550
ifmt_pre_round_abs[ifmt][INT_WIDTH-1:0] = final_int[INT_WIDTH-1:0];
549551
end
550552
end else begin : inactive_format
@@ -597,7 +599,25 @@ module fpnew_cast_multi #(
597599
end
598600

599601
// Negative integer result needs to be brought into two's complement
600-
assign rounded_int_res = rounded_sign ? unsigned'(-rounded_abs) : rounded_abs;
602+
assign rounded_uint_res = rounded_sign ? unsigned'(-rounded_abs) : rounded_abs;
603+
604+
// Sign-extend integer result
605+
for (genvar ifmt = 0; ifmt < int'(NUM_INT_FORMATS); ifmt++) begin : gen_int_res_sign_ext
606+
// Set up some constants
607+
localparam int unsigned INT_WIDTH = fpnew_pkg::int_width(fpnew_pkg::int_format_e'(ifmt));
608+
609+
if (IntFmtConfig[ifmt]) begin : active_format
610+
always_comb begin : assemble_result
611+
// zero-extend absolute value result
612+
ifmt_rounded_signed_res[ifmt] = '{default: rounded_uint_res[INT_WIDTH-1]};
613+
ifmt_rounded_signed_res[ifmt][INT_WIDTH-1:0] = rounded_uint_res[INT_WIDTH-1:0];
614+
end
615+
end else begin : inactive_format
616+
assign ifmt_rounded_signed_res[ifmt] = '{default: fpnew_pkg::DONT_CARE};
617+
end
618+
end
619+
620+
assign rounded_int_res = ifmt_rounded_signed_res[int_fmt_q2];
601621
assign rounded_int_res_zero = (rounded_int_res == '0);
602622

603623
// Detect integer overflows after rounding (only positives)

0 commit comments

Comments
 (0)