RISC-V Assembly Language - Negative Float/Double

Project #10A

(64-bit architecture)

Demonstrate testing for negative floating-point numbers. A test of each bit described in the "starter code" below.

Demonstrate true and false results. Print the number tested and the test results.

Project #10B

(32-bit architecture)

Do Project 10A again except for 32-bit architecture.

Project #10C

(64-bit architecture or 32-bit architecture or 128-bit architecture if available)

Re-do project 10A except test all of the fclass bits defined below except 8 and 9.

Starter Code

# ================================================================== # 64-bit Architecture ## ================================================================= # If you want to specifically test negative normal numbers, # negative subnormal numbers, or negative zero, you can use the # class instruction. # a. instruction: fclass.s rd, rs1 # b. how it works: It inspects rs1 and writes a 10-bit bitmask to # integer register rd. # c. bit positions for negative values in rd: # Bit 0: negative infinity (-inf) # Bit 1: negative normal number (-normal) # Bit 2: negative subnormal number (-subnormal) # Bit 3: negative zero (-0.0) (-0) # d. example assembly code # fclass.s x5, fa0 # get classification bitmask of fa0 # andi x5, x5, 15 # check if any of the last 4 bits are set # bnez x5, is_negative # branch if negative # ================================================================== .data pi: .double 3.141592653589793 .text .global main main: # ---- load pi into a register la t0, pi # load address of pi fld ft0, 0(t0) # load pi into register ft0 # ---- test if (double) is negative fclass.d t0, ft0 # test register ft0 andi t0, t0, 15 # mask test bits (0b1111) bnez t0, is_negative # branch if negative

How fclass Works

The fclass.s rd, rs1 instruction examines the floating-point value in register rs1 and writes a 10-bit bitmask to integer register rd.
Each bit represents a specific class of number:

Bit in rdMeaning of the Bit
0Negative infinity (-inf)
1Negative normal number
2Negative subnormal number
3Negative zero (-0)
4Positive zero (+0)
5Positive subnormal number
6Positive normal number
7Positive infinity (+inf)
8Signaling NaN (sNaN)
9Quiet NaN (qNaN)