G4.9.2 An Arithmetic Shift That Keeps the Sign

EasyImplementIcarus · browser samples / server checksReview the topic

Shift a signed 12-bit sample right while keeping its sign, and divide it by a power of two the way C rounds.

Hardware context: A cipher round that rotates a word by a key-dependent amount, and a sensor filter that scales signed samples. Shift a signed sample right and divide it rounding toward zero.

A sensor sends 12-bit two's-complement samples on a plain logic port. A filter needs two things for each sample. One is a fast scale-down by 2^n that keeps the sign. The other is an exact quotient that must match the C reference model.

This is an Implement problem. Write sra12. y is x shifted right by n places, filling with copies of the sign bit. From n = 12 up every bit of y is the sign bit. q is x divided by 2^n and rounded toward zero, as integer division in C does.

The check applies three (x, n) pairs, one per nanosecond, and records y and q after each.

Signals in the Timeline

SignalWidthRole
script48input of each check
Full specification
The module (keep the name and the ports)
module sra12 (
  input  logic [11:0] x,         // a two's-complement sample on a plain (unsigned) port
  input  logic [3:0]  n,         // 0 to 15 places
  output logic [11:0] y,         // x shifted right by n, keeping the sign
  output logic [11:0] q          // x divided by 2^n, rounded toward zero
);
  // your design
endmodule
x, ny (shift)q (division)
100, 22525
−7, 1−4−3
−1, 13−10

The result fields are y_stepK and q_stepK for the steps 0 to 2, shown as signed decimal numbers.

Examples

Example 1

Positive Values
Input
x=100 n=2; x=2047 n=4; x=7 n=1
Output
y_step0 = 25, q_step0 = 25, y_step1 = 127, q_step1 = 127, y_step2 = 3, q_step2 = 3

For positive samples the shift and the division give the same answers, with zeros shifted in.

Example 2

Negative Values That Divide Exactly
Input
x=-100 n=2; x=-2048 n=4; x=-8 n=3
Output
y_step0 = -25, q_step0 = -25, y_step1 = -128, q_step1 = -128, y_step2 = -1, q_step2 = -1

With no remainder the two outputs agree again. The sign bit is copied into the vacated places.

Example 3

Negative Values with a Remainder
Input
x=-7 n=1; x=-1 n=1; x=-101 n=2
Output
y_step0 = -4, q_step0 = -3, y_step1 = -1, q_step1 = 0, y_step2 = -26, q_step2 = -25

Now they differ. The shift rounds down: −7 shifted by 1 is −4, and −1 stays −1. The division rounds toward zero: −3 and 0.

Constraints and Contract

  • Inputs are known 0/1 values. Each pair is held for one nanosecond and the outputs are sampled at its end.
  • Keep the module name and the ports, including the unsigned declaration of x. The / and % operators are refused.

Hints

Hint 1

On an unsigned value, >>> fills with zeros. Read x as signed first, with $signed or a signed copy.

Hint 2

For a positive x the shift and the division agree. For a negative x the shift rounds down, toward minus infinity.

Hint 3

Adding 2^n − 1 to a negative x before the shift makes it round toward zero. That sum needs more than 12 bits when n is large.

What the grader checks: Eight directed and ten seeded cases of three steps each cover every shift amount, both signs and exact and inexact divisions. y and q are compared with a model written in JavaScript after every step.

18 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 3 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

Positive Values

Input
x=100 n=2; x=2047 n=4; x=7 n=1
Expected output
y_step0 = 25, q_step0 = 25, y_step1 = 127, q_step1 = 127, y_step2 = 3, q_step2 = 3

For positive samples the shift and the division give the same answers, with zeros shifted in.

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

Icarus · browser samples / server checks