Half-Up Rounder (rounding_halfup)
Half-up rounding and truncation for a variable-width signed input.
Design
The processing of the input stream is dependent on the value of binary_point:
binary_point\(= 0\): Input data is truncated from above (losing the most-significant bits of the input data), to the output width.
binary_point\(> 0\): Rounding is performed by adding 0.5 and truncating, causing any fractional value of 0.5 and above to round up and anything else to round down. This is implemented by right shifting bybinary_point\(- 1\), one is added, and then right shifted by one. The value is then truncated from above to the output width. The result of this operation is that any value \(\geq x.5\) will round up to \(x+1\) and any value \(< x.5\) will round down to \(x\).
The mathematical representation of the implementation (assuming that truncation from above will not affect the value) is given in (113).
In (113):
\(y[n]\) is the output values.
\(x[n]\) is the input values.
In addition it is possible to enable saturation. This clips the output if either the incoming value has a larger fixed point magnitude than that which can be expressed within the outputs number of bits, or rounding has taken place which has caused the saturation limit to be exceeded.
Implementation
The primitive is pipelined so that a new sample can be inserted into the primitive on every rising edge of clk. The data_valid_in signal allows each input sample to be marked as valid or not. The pipeline advances every rising edge of clk regardless of if valid data is passed to the primitive or not. The data_valid_out signal is a delayed version of data_valid_in that indicates which outputs were calculated based on valid inputs.
The clk_en signal provides a clock enable for the primitive. When set low no data will enter or leave the primitive and all calculations are stopped. When set high the module will operate normally.
Interface
Generics
input_width_g(integer): Sets width ofdata_insignal.
output_width_g(integer): Sets width ofdata_outsignal.
saturation_en_g(boolean): Enables saturation on data when rounding and/or truncation to a reduced output resolution has caused an overflow.
Ports
clk(std_logic), in: Clock. Inputs and outputs registered on rising edge.
reset(std_logic), in: Reset. Active high, synchronous with rising edge of clock.
clk_en(std_logic), in: Clock Enable. If low the module will not operate.
data_in(signed,input_width_gbits), in: Input value for primitive.
data_valid_in(std_logic), in:data_inis valid whendata_valid_inis high.
binary_point(integer), in: Number of fractional bits in the least significant bits of thedata_insignal.
data_out(signed,output_width_gbits), out: Output value for primitive.
data_valid_out(std_logic), out:data_outis valid whendata_valid_outis high.
Dependencies
The dependencies to other elements in OpenCPI are:
None.
There is also a dependency on:
ieee.std_logic_1164
ieee.numeric_std
Limitations
Limitations of rounding_halfup are:
A small positive bias will be introduced when using this primitive.
If this causes an issue convergent rounding may be used instead using the Half Even Rounder Primitive.