8000 added comments for clarity · srcarroll/flexfloat@6bd8feb · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bd8feb

Browse files
authored
added comments for clarity
1 parent 6db8690 commit 6bd8feb

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/flexfloat.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,29 @@
2626

2727
#include "assert.h"
2828

29+
// computes the exponent of a number
2930
int_fast16_t flexfloat_exp(const flexfloat_t *a)
3031
{
31-
int_fast16_t a_exp = EXPONENT(CAST_TO_INT(a->value));
32+
int_fast16_t a_exp = EXPONENT(CAST_TO_INT(a->value)); //exponent value of a
3233

33-
int_fast16_t bias = flexfloat_bias(a->desc);
34+
int_fast16_t bias = flexfloat_bias(a->desc); //in header file. no fucking clue what it does
3435

3536
if(a_exp == 0 || a_exp == INF_EXP)
3637
return a_exp;
3738
else
38-
return (a_exp - BIAS) + bias;
39+
return (a_exp - BIAS) + bias; //why?
3940
}
4041

4142
uint_t flexfloat_frac(const flexfloat_t *a)
4243
{
44+
// CAST_TO_INT(a->value) & MASK_FRAC = significand bits
45+
// NUM_BITS_FRAC - a->desc.frac_bits = ? (I don't know what desc.frac_bits is)
46+
// Returns bit shifted version of significan bits
4347
return (CAST_TO_INT(a->value) & MASK_FRAC) >> (NUM_BITS_FRAC - a->desc.frac_bits);
4448
}
4549

50+
// Does same thing as flexfloat_frac but for denormal (subnormal) numbers.
51+
// I don't understand the contents yet.
4652
uint_t flexfloat_denorm_frac(const flexfloat_t *a, int_fast16_t exp)
4753
{
4854
if(EXPONENT(CAST_TO_INT(a->value)) == 0) // Denormalized backend value
@@ -58,6 +64,7 @@ uint_t flexfloat_denorm_frac(const flexfloat_t *a, int_fast16_t exp)
5864
}
5965

6066
// Pack normalized desc-fraction with desc-relative exponent to backend float
67+
// Gives the binary representation of value given binary representation of sign, exp, and significand
6168
uint_t flexfloat_pack(flexfloat_desc_t desc, bool sign, int_fast16_t exp, uint_t frac)
6269
{
6370
int_fast16_t bias = flexfloat_bias(desc);
@@ -74,6 +81,7 @@ uint_t flexfloat_pack(flexfloat_desc_t desc, bool sign, int_fast16_t exp, uint_t
7481
return PACK(sign, exp, frac << (NUM_BITS_FRAC - desc.frac_bits));
7582
}
7683

84+
// Same thing as flexfloat_pack but for denormalized functions
7785
uint_t flexfloat_denorm_pack(flexfloat_desc_t desc, bool sign, uint_t frac)
7886
{
7987
int_fast16_t bias = flexfloat_bias(desc);
@@ -82,7 +90,14 @@ uint_t flexfloat_denorm_pack(flexfloat_desc_t desc, bool sign, uint_t frac)
8290

8391
uint_t flexfloat_pack_bits(flexfloat_desc_t desc, uint_t bits)
8492
{
93+
// bits >> (desc.exp_bits + desc.frac_bits) shifts bits to right so that sign bit is last
94+
// 0x1 = 1? If so I don't understand the point of the bitwise AND since the only non-zero bit
95+
// after shifting is the sign bit...I think.
8596
bool sign = (bits >> (desc.exp_bits + desc.frac_bits)) & 0x1;
97+
98+
// bits >> desc.frac_bits shifts bits to the right so that only sign bit and exponent bits remain
99+
// 0x1<<desc.exp_bits puts a 1 in the (exp_bits+1) bit location
100+
// ( ) - 1
86101
int_fast16_t exp = (bits >> desc.frac_bits) & ((0x1<<desc.exp_bits) - 1);
87102
uint_t frac = bits & ((UINT_C(1)<<desc.frac_bits) - 1);
88103

@@ -189,9 +204,10 @@ bool flexfloat_inf_rounding(const flexfloat_t *a, int_fast16_t exp, bool sign, b
189204
}
190205

191206
// return a value to sum in order to apply rounding
207+
// what is the difference between EXPONENT(CAST_TO_INT(a->value)) and exp = flexfloat_exp(a)
192208
int_t flexfloat_rounding_value(const flexfloat_t *a, int_fast16_t exp, bool sign)
193209
{
194-
if(EXPONENT(CAST_TO_INT(a->value)) == 0) // Denorm backend format
210+
if(EXPONENT(CAST_TO_INT(a->value)) == 0) // Denorm backend format. Just a rule for subnormal numers when exp=0
195211
{
196212
return flexfloat_denorm_pack(a->desc, sign, 0x1);
197213
}
@@ -239,6 +255,7 @@ void flexfloat_sanitize(flexfloat_t *a)
239255
fegetexceptflag(&flags, FE_ALL_EXCEPT);
240256
#endif
241257
// Rounding mode
258+
// All three cases are doing the same thing. What's the point?
242259
int mode = fegetround();
243260
if(mode == FE_TONEAREST && flexfloat_nearest_rounding(a, exp))
244261
{

0 commit comments

Comments
 (0)
0