Hello all,
I hope you are able to make significant progress towards Lab 2. Here are a few instructions for those who are still struggling. First of all the input to your code will always be bfloat 16 formats. For example, consider the following case.
x: .word 16544
In this case, the word 16544 is stored in the RISC-V processor as following
0 10000001 010000
Sign Exponent Mantissa
You can just directly use any decimal to binary conversion websites to realize this. If you want to realize what number does this 16544 represents in real-world binary, then we can use the following formula.
Resulting Number = (-1)sign bit * (1+fraction) * 2 exponent – bias
Here Sign bit = 0
Exponent = (10000001)2 = 12910
Fraction = 0*2-1 + 1*2-2 + 0*2-3 + 0*2-4 + 0*2-5 + 0 * 2-6 + … = 0.25
Resulting Number = (-1)0 * (1 + 0.25) * 2 129-127
Resulting Number = 1.25 * 4 = 5
Now that you know, 16544 is the bfloat16 representation of 5, the question may arise as to why do we use bfloat16 format, the reason is floating-point We can’t store something like 5.25 directly in the RISC-V register, while 5.25 can be written as 16552 in blfoat16 format, whose binary representation is 0 10000001 0101000 directly in the RISC-V registers.
Now, arithmetic on these float 16 formats is not-straightforward. The number is represented as (1+fraction) * 2xyz. If two numbers don’t have the same exponent then we can’t do the addition. Simply put, you can’t add 1*22 with 1*23. However, once the powers are the same you can perform the addition as 1 * 22 + 2*22 = 3*22
A similar idea is used here if two numbers have different exponents your task is to make them equal and do the arithmetic. I would request you to follow this video.
Addition of single precision floating point numbers
After the exponent matching, you can either add or subtract.
Subtraction is similar to the addition. However, if x = 5 and y = 6, and you’re required to perform x – y, you can swap the x & y. But you need to change the sign bit of the resulting number such that the resulting number is negative.
For the testing, I have been getting many questions, and we won’t be testing the negative numbers in this lab. So you need not worry about test cases such as -5 – (-5). All the values of x and y will be positive, including 0. Also, another thing that I would like to stress is that if y> x, and you’re required to do the subtraction (x-y) the resulting number should be negative.
To generate your own test cases you can follow the following steps.
1. Use this website to convert your decimal number to IEEE 754 32 bit format.
I used the input 0, the resulting number is 00111111 10000000 00000000 00000000.
Here I will only consider the first 16 bits “00111111 10000000”.
2. Convert the binary to decimal number using this website.
(0011111110000000)2 = (16256)10
3. So if your program works correctly you can feed in bfloat16 formats such as 16544 and 16256, and expect the output to be 16576.
There is a mistake in the doc file that has been uploaded, where -4.25 is said to be 49312, however, it should be 49288.
For the submission, please submit your code, that can be opened using the text editors. Do not submit a pdf file for the code. For this lab, all I expect is the code, no screenshots or pdf file is required.
Finally, the deadline for this Lab is extended to Friday at 11.59 PM.
All the best,
-Gaurav