G1.5.1 Decode a Half-Precision Word

MediumImplementIcarus · browser samples / server checksReview the topic

Split an IEEE 754 half-precision word into its class, sign, true exponent and significand.

Hardware context: The weight debug unit of a half-precision accelerator and the reference model of a half-precision adder. Decode a half-precision word into class, sign, true exponent and significand.

A machine-learning accelerator stores its weights in half precision. The debug unit must show each weight as a class, a sign, an exponent and a significand. The first version printed nonsense for every very small weight.

This is an Implement problem. Write half_decode. It reads one 16-bit word: a sign bit, a 5-bit exponent field with bias 15, and a 10-bit fraction.

For a finite word, the value is (-1)^sign x sig x 2^(exp - 10). The table gives the outputs for each class.

The check applies three words, one per nanosecond, and records all four outputs after each.

Signals in the Timeline

SignalWidthRole
script48input of each check
Full specification
The module (keep the name and the ports)
module half_decode (
  input  logic        [15:0] h,      // an IEEE 754 half-precision word
  output logic        [2:0]  cls,    // 0 zero, 1 subnormal, 2 normal, 3 infinity, 4 NaN
  output logic               sign,   // the sign bit
  output logic signed [5:0]  exp,    // the true exponent: -14 to 15, or 0
  output logic        [10:0] sig     // the significand with its leading bit, or 0
);
  // your design
endmodule
Exponent field eFraction fclsexpsig
000 (zero)00
0not 01 (subnormal)-14f with a 0 in front
1 to 30any2 (normal)e - 15f with a 1 in front
3103 (infinity)00
31not 04 (NaN)00

sign is bit 15 of the word for every class, zeros and NaNs included.

The result fields are named cls_step0, sign_step0, exp_step0 and sig_step0 for the first word, up to the same names with step2.

Examples

Example 1

Normal Numbers Near One
Input
h = 3c00; h = c000; h = 3555
Output
cls_step0 = normal (2), sign_step0 = no, exp_step0 = 0, sig_step0 = 0x400, cls_step1 = normal (2), sign_step1 = yes, exp_step1 = 1, sig_step1 = 0x400, cls_step2 = normal (2), sign_step2 = no, exp_step2 = -2, sig_step2 = 0x555

3c00 has e = 15, so exp is 0 and sig is 400 in hex: the hidden 1 and ten zeros. That is 1.0. c000 is -2.0. The third word is a normal number a little below 0.5.

Example 2

Two Zeros and a Normal Number
Input
h = 0000; h = 8000; h = 4248
Output
cls_step0 = zero (0), sign_step0 = no, exp_step0 = 0, sig_step0 = 0x0, cls_step1 = zero (0), sign_step1 = yes, exp_step1 = 0, sig_step1 = 0x0, cls_step2 = normal (2), sign_step2 = no, exp_step2 = 1, sig_step2 = 0x648

Both zeros have cls 0, exp 0 and sig 0, but their sign bits differ. The third word is a normal number with exponent 2.

Example 3

The Largest and Smallest Normal Numbers
Input
h = 7bff; h = 0400; h = fbff
Output
cls_step0 = normal (2), sign_step0 = no, exp_step0 = 15, sig_step0 = 0x7ff, cls_step1 = normal (2), sign_step1 = no, exp_step1 = -14, sig_step1 = 0x400, cls_step2 = normal (2), sign_step2 = yes, exp_step2 = 15, sig_step2 = 0x7ff

7bff has e = 30, so exp is 15 and every fraction bit is 1: 65504. 0400 is the smallest normal number, with exp -14. fbff is -65504.

Constraints and Contract

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

Hints

Hint 1

First classify. Only two exponent fields are special: all zeros and all ones. The fraction then tells the two classes of each apart.

Hint 2

The exponent field is unsigned. In a wider or real-valued expression, e - 15 wraps to a huge number for a small field. Put a 0 bit in front and make it signed first.

Hint 3

A subnormal has no hidden 1, and its exponent is the same as that of the smallest normal number.

What the grader checks: Ten directed and twelve seeded cases of three words each run on the block. All four outputs are compared after every word with a model written in JavaScript. The model itself is checked against the half-precision type of Node.

22 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

Normal Numbers Near One

Input
h = 3c00; h = c000; h = 3555
Expected output
cls_step0 = normal (2), sign_step0 = no, exp_step0 = 0, sig_step0 = 0x400, cls_step1 = normal (2), sign_step1 = yes, exp_step1 = 1, sig_step1 = 0x400, cls_step2 = normal (2), sign_step2 = no, exp_step2 = -2, sig_step2 = 0x555

3c00 has e = 15, so exp is 0 and sig is 400 in hex: the hidden 1 and ten zeros. That is 1.0. c000 is -2.0. The third word is a normal number a little below 0.5.

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

Icarus · browser samples / server checks