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

Idiv lzc opt #621

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions bsg_misc/bsg_idiv_iterative.v
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,28 @@ module bsg_idiv_iterative #(parameter width_p=32, parameter bitstack_p=0, parame
,.clk_i(clk_i)
);

logic [`BSG_SAFE_CLOG2(width_p)-1:0] clz_c_result, clz_a_result;

bsg_counting_leading_zeros #(
.width_p(width_p)
) clz_c (
.a_i(dividend_i)
,.num_zero_o(clz_c_result)
);

bsg_counting_leading_zeros #(
.width_p(width_p)
) clz_a (
.a_i(divisor_i)
,.num_zero_o(clz_a_result)
);

//if the divisor is zero
wire zero_divisor_li = ~(| opA_r);

wire [`BSG_SAFE_CLOG2(width_p)-1:0] div_shift;
assign div_shift = zero_divisor_li ? width_p-1 : clz_a_result - clz_c_result;

wire [1:0] opA_sel_lo;
wire [width_p:0] opA_mux;

Expand All @@ -88,7 +107,8 @@ module bsg_idiv_iterative #(parameter width_p=32, parameter bitstack_p=0, parame

wire [width_p:0] opB_mux, opC_mux;
wire [bits_per_iter_p + 1:0] opB_sel_lo, opC_sel_lo;

wire [`BSG_WIDTH(width_p):0] shift_val;

if (bits_per_iter_p == 2) begin

bsg_mux_one_hot #(.width_p(width_p+1), .els_p(4)) muxB
Expand All @@ -105,20 +125,20 @@ module bsg_idiv_iterative #(parameter width_p=32, parameter bitstack_p=0, parame

end else begin

bsg_mux_one_hot #(.width_p(width_p+1), .els_p(3)) muxB
(.data_i( {opC_r, add1_out, {add1_out[width_p-1:0], opC_r[width_p]}} )
bsg_mux_one_hot #(.width_p(width_p+1), .els_p(4)) muxB
kaamakshee marked this conversation as resolved.
Show resolved Hide resolved
(.data_i( {opC_r, add1_out, {(add1_out << (width_p-shift_val+1)) | opC_r >> shift_val}} )
dpetrisko marked this conversation as resolved.
Show resolved Hide resolved
,.data_o( opB_mux )
,.sel_one_hot_i(opB_sel_lo)
);

bsg_mux_one_hot #(.width_p(width_p+1), .els_p(3)) muxC
(.data_i( {{dividend_msb, dividend_i},add1_out, {opC_r[width_p-1:0], ~add1_out[width_p]}} )
bsg_mux_one_hot #(.width_p(width_p+1), .els_p(4)) muxC
kaamakshee marked this conversation as resolved.
Show resolved Hide resolved
(.data_i( {{dividend_msb, dividend_i},add1_out, {(opC_r << (width_p-shift_val+1)) | ((~add1_out >> width_p) << (width_p-shift_val))}} )
dpetrisko marked this conversation as resolved.
Show resolved Hide resolved
,.data_o( opC_mux )
kaamakshee marked this conversation as resolved.
Show resolved Hide resolved
,.sel_one_hot_i(opC_sel_lo)
);
,.sel_one_hot_i(opC_sel_lo)
);

end

wire opA_ld_lo;
bsg_dff_en#(.width_p(width_p+1)) opA_reg
(.data_i (opA_mux)
Expand Down Expand Up @@ -240,6 +260,8 @@ module bsg_idiv_iterative #(parameter width_p=32, parameter bitstack_p=0, parame
,.opA_is_neg_i (opA_r[width_p])
,.opC_is_neg_i (opC_r[width_p])

,.div_shift (div_shift)

,.opA_sel_o (opA_sel_lo)
,.opA_ld_o (opA_ld_lo)
,.opA_inv_o (opA_inv_lo)
Expand All @@ -256,7 +278,10 @@ module bsg_idiv_iterative #(parameter width_p=32, parameter bitstack_p=0, parame
,.latch_signed_div_o (latch_signed_div_lo)
,.adder1_cin_o (adder1_cin_lo)

,.shift_val (shift_val)

,.v_o(v_o)
,.yumi_i(yumi_i)
);

endmodule // divide
14 changes: 11 additions & 3 deletions bsg_misc/bsg_idiv_iterative_controller.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module bsg_idiv_iterative_controller #(parameter width_p=32, parameter bits_per_
,input opA_is_neg_i
,input opC_is_neg_i

,input [`BSG_SAFE_CLOG2(width_p)-1:0] div_shift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; coding convention is div_shift_i


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add link to Google design doc in header comment

,output logic [1:0] opA_sel_o
,output logic opA_ld_o
,output logic opA_inv_o
Expand All @@ -38,6 +40,8 @@ module bsg_idiv_iterative_controller #(parameter width_p=32, parameter bits_per_
,output logic latch_signed_div_o
,output logic adder1_cin_o

,output logic [`BSG_WIDTH(width_p):0] shift_val
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; coding convention is shift_val_o

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The max value of this seems to be 'BSG_WIDTH(width_p) as it's assigned to width_p or div_shift (which has max value of width_p). I think this port is 1b too wide (or am I misreading?)


,output logic v_o
,input yumi_i
);
Expand All @@ -46,7 +50,7 @@ module bsg_idiv_iterative_controller #(parameter width_p=32, parameter bits_per_
logic r_neg_r;
logic neg_ld;
logic add1_neg_last_r, add2_neg_last_r;

typedef enum logic[5:0]
{WAIT, NEG0, NEG1, SHIFT,
CALC,
Expand All @@ -67,9 +71,13 @@ module bsg_idiv_iterative_controller #(parameter width_p=32, parameter bits_per_
end
end

wire [`BSG_WIDTH(width_p/bits_per_iter_p)-1:0] calc_cyc;
assign calc_cyc = ((!signed_div_r_i) && (bits_per_iter_p==1)) ? div_shift : width_p/bits_per_iter_p;
kaamakshee marked this conversation as resolved.
Show resolved Hide resolved
assign shift_val = (state == SHIFT) && ((!signed_div_r_i) && (bits_per_iter_p==1)) ? div_shift : width_p;

logic [`BSG_WIDTH(width_p/bits_per_iter_p)-1:0] calc_cnt;
wire calc_up_li = (state == CALC) && (calc_cnt < width_p/bits_per_iter_p);
wire calc_done = (calc_cnt == width_p/bits_per_iter_p);
wire calc_up_li = (state == CALC) && (calc_cnt < calc_cyc);
wire calc_done = (calc_cnt == calc_cyc);
bsg_counter_clear_up#(.max_val_p(width_p/bits_per_iter_p)
,.init_val_p(0)
,.disable_overflow_warning_p(1)) calc_counter
Expand Down
23 changes: 18 additions & 5 deletions testing/bsg_misc/bsg_idiv_iterative/Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# FIXME: set up VCS, should modified for different users.
include ../../../../bsg_cadenv/cadenv.mk
export BASEJUMP_STL_DIR=$(abspath ../../..)
INCDIR = +incdir+$(BASEJUMP_STL_DIR)/bsg_misc

PLI_OPTS = -P mypli.tab get_stim.c +acc
VCS_OP += +vcs+vcdpluson -debug_access+all
VCS_DEFINES =
CC = gcc
CFLAGS = -g
SRC=unsigned.c
SRC1=signed.c

ifdef SIGN
VCS_DEFINES += +define+SIGN=$(SIGN)
endif
ifdef UNSIGN
VCS_DEFINES += +define+UNSIGN=$(UNSIGN)
endif
ifdef WIDTH
VCS_DEFINES += +define+WIDTH=$(WIDTH)
endif
Expand All @@ -22,13 +25,23 @@ VCS_DEFINES += +define+ITERS=$(ITERS)
endif

vcs:
$(VCS) -full64 -sverilog $(VCS_OP) -Mupdate -f divide.files $(LIB_OPTS) $(PLI_OPTS) $(VCS_DEFINES)
$(VCS) -full64 -sverilog $(VCS_OP) -Mupdate -f sv.include -f divide.files $(LIB_OPTS) $(PLI_OPTS) $(VCS_DEFINES)
#./simv +vcs+stop+232100
./simv

xvcs:
$(VCS) -full64 -sverilog -Mupdate -RI -line -f divide.files $(LIB_OPTS) $(PLI_OPTS)

test: $(SRC)
$(CC) -o unsigned.o -c $^ $(CFLAGS) -lm -std=c99 -D ITERS=$(ITERS) -D WIDTH=$(WIDTH)
$(CC) unsigned.o -o test
./test

test1: $(SRC1)
$(CC) -o signed.o -c $^ $(CFLAGS) -lm -std=c99 -D ITERS=$(ITERS) -D WIDTH=$(WIDTH)
$(CC) signed.o -o test1
./test1

clean:
$(RM) -r csrc simv.daidir vcs.key simv ucli.key vcdplus.vpd s_output.txt u_output.txt u.txt s.txt
dve64:
Expand All @@ -38,4 +51,4 @@ gtk:
gtkwave vcd
verify:
egrep [0-9] u_expected.txt | diff -w u_output.txt -
egrep [0-9] s_expected.txt | diff -w s_output.txt -
# egrep [0-9] s_expected.txt | diff -w s_output.txt -
1 change: 0 additions & 1 deletion testing/bsg_misc/bsg_idiv_iterative/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Testbench:

- Set design parameters for design like bits/iteration and design width in the make command
UNSIGN - Test unsigned (default don't test)
SIGN - Test signed (default don't test)
WIDTH - design width
BITS_PER_ITER - 1 or 2 bits per iteration generated (different hardware designs)
Expand Down
4 changes: 4 additions & 0 deletions testing/bsg_misc/bsg_idiv_iterative/signed.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
// RISC-V ISA Manual: Section 7.2 - Division Operations
#include <stdio.h>
#include <math.h>
#ifndef WIDTH
#define WIDTH 4
#endif
#ifndef ITERS
#define ITERS 1 << (WIDTH * 2)
#endif
// #define ITERS 10000

// Function to compute quotient
Expand Down
5 changes: 5 additions & 0 deletions testing/bsg_misc/bsg_idiv_iterative/sv.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$BASEJUMP_STL_DIR/bsg_misc/bsg_counting_leading_zeros.v
$BASEJUMP_STL_DIR/bsg_misc/bsg_priority_encode.v
$BASEJUMP_STL_DIR/bsg_misc/bsg_priority_encode_one_hot_out.v
$BASEJUMP_STL_DIR/bsg_misc/bsg_encode_one_hot.v
$BASEJUMP_STL_DIR/bsg_misc/bsg_scan.v
4 changes: 1 addition & 3 deletions testing/bsg_misc/bsg_idiv_iterative/test_bsg.v
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ module test_bsg;
$fwrite(f1,"%d %d %d %d\n", s_dividend, s_divisor, s_quotient, s_remainder);
$fwrite(f3,"%d %d\n", s_dividend, s_divisor);

`endif

// do the unsigned case
`ifdef UNSIGN
`else
u_dividend = dividend;
u_divisor = divisor;

Expand Down
6 changes: 5 additions & 1 deletion testing/bsg_misc/bsg_idiv_iterative/unsigned.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Program to produce a file with the expected output
//jl Program to produce a file with the expected output
// for all possible inputs to an unsigned 4-bit divider pursuant to
// RISC-V ISA Manual: Section 7.2 - Division Operations

#include <stdio.h>
#include <math.h>
#include <limits.h>
#ifndef WIDTH
#define WIDTH 4
#endif
#ifndef ITERS
#define ITERS 1 << (WIDTH*2)
#endif
// #define ITERS 10000

// Function to compute quotient
Expand Down