G1.4.2 Round Half to Even in Fixed Point

MediumImplementIcarus · browser samples / server checksReview the topic

Round a signed Q8.4 number to the nearest whole number, sending every tie to the even neighbour.

Hardware context: A fixed-point gain stage of an audio mixer and the display rounding of a digital scale. Round a signed Q8.4 number to the nearest whole number with ties to even.

A digital scale averages many readings and shows a whole number of grams. Its first version rounded every half upward. Over a day of weighing, the totals came out too high.

This is an Implement problem. Write round_even. The input x is a signed Q8.4 number. The output y is the nearest whole number. When x is exactly half way between two whole numbers, y is the even one of the two.

The rule is the same for negative values: -2.5 rounds to -2 and -3.5 rounds to -4.

The check applies eight values of x, one per nanosecond, and records y after each.

Signals in the Timeline

SignalWidthRole
script96input of each check
Full specification
The module (keep the name and the ports)
module round_even (
  input  logic signed [11:0] x,   // signed Q8.4: -128.0 to 127.9375
  output logic signed [8:0]  y    // x rounded to a whole number, ties to even
);
  // your design
endmodule
Fraction part of x, above the whole number below ity
Less than one halfThe whole number below x.
More than one halfThe whole number above x.
Exactly one halfThe even one of the two.

The output has nine bits because the largest tie, 127.5, rounds up to 128.

The result fields are named y_step0 to y_step7, shown as signed decimal numbers.

Examples

Example 1

Values with No Tie
Input
x = 1.25; x = 1.75; x = 2.0625; x = 2.9375; x = 0.4375; x = 0.5625; x = 10; x = 99.875
Output
y_step0 = 1, y_step1 = 2, y_step2 = 2, y_step3 = 3, y_step4 = 0, y_step5 = 1, y_step6 = 10, y_step7 = 100

Each value goes to its nearest whole number: 1.25 gives 1 and 1.75 gives 2. No value here is exactly half way, so every rounding rule agrees.

Example 2

Positive Ties
Input
x = 0.5; x = 1.5; x = 2.5; x = 3.5; x = 4.5; x = 5.5; x = 126.5; x = 127.5
Output
y_step0 = 0, y_step1 = 2, y_step2 = 2, y_step3 = 4, y_step4 = 4, y_step5 = 6, y_step6 = 126, y_step7 = 128

Each value is exactly half way. 0.5 gives 0 and 1.5 gives 2: the even neighbour each time. 127.5 gives 128, which is why y has nine bits.

Example 3

Negative Values with No Tie
Input
x = -1.25; x = -1.75; x = -2.0625; x = -2.9375; x = -0.4375; x = -0.5625; x = -10; x = -99.875
Output
y_step0 = -1, y_step1 = -2, y_step2 = -2, y_step3 = -3, y_step4 = 0, y_step5 = -1, y_step6 = -10, y_step7 = -100

-1.25 gives -1 and -1.75 gives -2. The nearest whole number is found the same way on both sides of zero.

Constraints and Contract

  • Inputs are known 0/1 values. Each value is held for one nanosecond and y is sampled at its end.
  • Keep the module name and the ports. The design is combinational.

Hints

Hint 1

Split x into its top eight bits and its low four bits. In two's complement the top bits are the whole number at or below x, even when x is negative.

Hint 2

The low four bits count sixteenths above that whole number, from 0 to 15. One half is eight sixteenths.

Hint 3

There are three cases: above eight, below eight and exactly eight. In the last case, look at the lowest bit of the whole number to see if it is odd.

What the grader checks: Seven directed and eight seeded cases of eight steps each run on the block. Every output is compared with a model written in JavaScript. Other rounding modes are outside this problem.

15 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Values with No Tie

Input
x = 1.25; x = 1.75; x = 2.0625; x = 2.9375; x = 0.4375; x = 0.5625; x = 10; x = 99.875
Expected output
y_step0 = 1, y_step1 = 2, y_step2 = 2, y_step3 = 3, y_step4 = 0, y_step5 = 1, y_step6 = 10, y_step7 = 100

Each value goes to its nearest whole number: 1.25 gives 1 and 1.75 gives 2. No value here is exactly half way, so every rounding rule agrees.

Run samples executes all 3 public checks. Check all tests runs 15 checks on the server.

Icarus · browser samples / server checks