// normalize_mult.sv
// This module normalizes a 48-bit mantissa product and adjusts the exponent accordingly.
// It also extracts the guard and sticky bits for IEEE-754 rounding.

module normalize_mult (
    input  logic [47:0] mantissa_in,     // Input mantissa (product of 24-bit x 24-bit)
    input  logic [9:0]  exponent_in,     // Input exponent (after bias adjustment)
    output logic [22:0] mantissa_out,    // Normalized 23-bit mantissa
    output logic [9:0]  exponent_out,    // Adjusted exponent after normalization
    output logic        guard_bit,       // Guard bit for rounding
    output logic        sticky_bit       // Sticky bit for rounding
);

    logic [47:0] mant_shifted;
    logic [5:0]  shift_amount;
    logic [9:0]  exponent_adj;
    logic sticky;

    always_comb begin
        // Default assignments
        shift_amount  = 0;
        mant_shifted  = mantissa_in;
        exponent_adj  = exponent_in;

        if (mantissa_in[47] == 1'b1) begin
            // Overflow: shift right by 1 and increment exponent
            mant_shifted = mantissa_in >> 1;
            exponent_adj = exponent_in + 1;
            sticky       = mantissa_in[0];
        end
        else begin
            // Normalize: shift left until MSB '1' is at bit 46
            for (int i = 0; i <= 46; i++) begin
                if (mantissa_in[46 - i] == 1'b1) begin
                    shift_amount = i;
                    break;
                end
            end
            mant_shifted = mantissa_in << shift_amount;
            exponent_adj = exponent_in - shift_amount;
            sticky       = | mant_shifted[22:0];       // Sticky bit = OR of remaining bits
        end

        // Extract normalized mantissa and rounding bits
        mantissa_out = mant_shifted[46:24];       // Correct 23-bit mantissa
        exponent_out = exponent_adj;
        guard_bit    = mant_shifted[23];          // Guard bit
        sticky_bit   = sticky;
    end

endmodule

