Lab 7
Lab 7
1 Introduction
This lab introduces the floating point instructions available in RISC-V instruction set
architecture and how it works in number system.
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.
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.
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
following binary representation:
Interpretation:
3
• Mantissa: The value of mantissa is 1.110100110000000000000002, including the
implied leading bit.
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.8lf\n",calculatePI(PI, n,
sign));
}
// OUTPUT
// The approximation of Pi is 3.14159265
7
8
4.2.2 Paste the screenshots of both Integer and Floating Registers Section highlighting
the values of used registers.
9
10
11