Here is the problem statement -
a: 5 bit representation, where 2 MSB bits are integer part and 3 LSB bits are fractional b: 5 bit representation, where 2 MSB bits are integer part and 3 LSB bits are fractional c: 11 bit representation, where MSB bit is integer part and 10 LSB bits are fractional
I am trying to write C code to perform:
d = a * b + c
How to do this optimally, what data-structures to use etc
Thanks, adding some more details - a & b are uint8_t (unsigned char), c is uint16_t (unsigned short int).
Taking the least 5 bits of the uint8_t to represent a & b Taking the least 11 bits of the unit_16_t to represent c
Using appropriate bit masks to extract the integer and fractional parts such as
a.int = (a >> 3) & 0x3
a.frac = a & 0x7
b.int = (b >> 3) & 0x3
b.frac = b & 0x7
Now I'm thinking I am over complicating the solution by separating the integer and fractional parts.
Suppose I want to multiple 2.31 with 1.05.
We can multiply 231 with 105 and divide later by 10000.
So you don't need to separate the integer and fractional parts of the original real number.
Along these lines, what is a good solution? // a - 5 bits, least 3 bits are fractional part, upper 2 bits are integer part
// b - 5 bits, least 3 bits are fractional part, upper 2 bits are integer part
// c - 11 bits, least 10 bits are fractional part, MSB is integer part
#define uint8_t (unsigned char)
#define uint16_t (unsigned short int)
uint16_t compute(uint8_t a, uint8_t b, uint16_t c)
{
uint16_t multval = a * b; // the least 6 bits represent the fractional part, the upper 4 bits represent integer part
uint8_t ab_int = multval >> 6; // integer part of a*b
uint8_t ab_frac = multval & 0x3F; // fractional part of a*b
uint16_t ab_adjusted = (ab_int << 10) | ab_frac;
uint16 sum = c + ab_adjusted;
return sum;
}