Lab 7
Lab 7
1 Introduction
This lab introduces the floating point instructions available in RISC-V instruction set ar-
chitecture and how it works in number system.
• A significand that contains the number’s digits. Negative significands represent nega-
tive numbers.
1
• An exponent that says where the decimal (or binary) point is placed relative to the
beginning of the significand. Negative exponents represent numbers that are very
small (i.e. close to zero).
Such a format satisfies all the requirements:
• It can represent numbers at wildly different magnitudes (limited by the length of the
exponent).
• It provides the same relative accuracy at all magnitudes (limited by the length of the
significand).
• It allows calculations across magnitudes: multiplying a very large and a very small
number preserves the accuracy of both in the result.
1. Sign: Represents the sign of the number (positive or negative). It is usually repre-
sented using one bit, where 0 represents positive and 1 represents negative.
2. Mantissa: Represents the significant digits of the number. It is a binary fraction
that follows the sign bit. The mantissa contains the significant digits of the number,
including the fractional part.
3. Exponent: Represents the scale or magnitude of the number. It determines the
position of the decimal point relative to the beginning of the mantissa. The exponent
is usually biased to allow for both positive and negative exponents.
2
Note that there are some peculiarities:
• The actual bit sequence is the sign bit first, followed by the exponent and finally the
significand bits.
• The exponent does not have a sign; instead an exponent bias is subtracted from it
(127 for single and 1023 for double precision). This, and the bit sequence, allows
floating-point numbers to be compared and sorted correctly even when interpreting
them as integers.
• The significand’s most significant digit is omitted and assumed to be 1, except for
subnormal numbers which are marked by an all-0 exponent and allow a number range
beyond the smallest numbers given in the table above, at the cost of precision.
• There are separate positive and a negative zero values, differing in the sign bit, where
all other bits are 0. These must be considered equal even though their bit patterns
are different.
• There are special positive and negative infinity values, where the exponent is all 1-bits
and the significand is all 0-bits. These are the results of calculations where the positive
range of the exponent is exceeded, or division of a regular number by zero.
• There are special not a number (or NaN) values where the exponent is all 1-bits
and the significand is not all 0-bits. These represent the result of various undefined
calculations (like multiplying 0 and infinity, any calculation involving a NaN value,
or application-specific cases). Even bit-identical NaN values must not be considered
equal.
If this seems too abstract and you want to see how some specific values look like in IEE 754,
try the Float Toy, or the IEEE 754 Visualization.
2.4 Example:
Let’s consider the single-precision floating-point number format. Suppose we have the fol-
lowing binary representation:
Interpretation:
• Sign bit: Since the sign bit is 0, the number is positive.
3
• Mantissa: The value of mantissa is 1.110100110000000000000002 , including the im-
plied leading bit.
• Final Value: 1.110100110000000000000002 ×2(130−127) = 1.110100110000000000000002 ×
23 = 14.5937510
4
Figure 1: Floating-Point Register Set
5
Figure 3: RISC-V Floating Point Extension
4 Laboratory Tasks
Write a RISC-V code to calculate the value Pi using Nilkantha’s series using floating-point
instructions.
6
Below is example C code for the calculation of PI using Nilkantha’s series.
// Function to calculate PI
double calculatePI ( double PI , double n ,
double sign )
{
// Add for 1000000 terms
for ( int i = 0; i <= 1000000; i ++) {
PI = PI + ( sign * (4 / (( n ) * ( n + 1)
* ( n + 2) ) ) ) ;
// Driver code
void main ()
{
// Function call
printf (" The approximation of Pi is %0.8 lf \ n " , calculatePI ( PI , n ,
sign ) ) ;
}
// OUTPUT
// The approximation of Pi is 3.14159265
7
4.2 RISC-V Equivalent Code
4.2.1 Write an equivalent RISC-V code in Venus and try to get as many correct
decimal digits as possible through efficient coding. Refer to the exam-
ple given in lab along with single and double precision floating point
instructions table.
for:
bge s0, s1, end
addi t3, t1, 2
addi t4, t1, 1
mul t5, t3, t4
mul t6, t5, t1
fcvt.s.w ft6, t6
fdiv.s fs0, fs2, ft6
fmul.s fs1, ft2, fs0
fadd.s ft0, ft0, fs1
fmul.s ft2, ft2, ft3
addi t1, t1, 2
addi s0, s0, 1
j for
end:
8
/**********************************************************/
4.2.2 Paste the screenshots of both Integer and Floating Registers Section
highlighting the values of used registers.