I am analyzing a predecessor codes(codes run on microcontroller) handling pointing point, but I don't understand how things work. I have got to know how to convert flat to decimal and the other way around. However, what he did was used customized bit fields for exponent:6 bits
and mantissa:26 bit
in data structure.
typedef union {
struct {
#ifdef _BIG_ENDIAN
unsigned int mant: 26; /* -33,554,432 to +33,554,431 */
unsigned int exp: 6; /* 10^-32 to 10^+31 */
#else
unsigned int exp: 6; /* 10^-32 to 10^+31 */
unsigned int mant: 26; /* -33,554,432 to +33,554,431 */
#endif
} part;
unsigned long comp;
} DMKS;
As followed by programs logic in client side:
A client(microcontroller) get data from a server and set a value to
unsigned long comp
Call
M_to_u()
which convertsDMKS
value tomicro value
M_to_u()
looks like below:long M_to_u(DMKS dmks_val) { register unsigned int exp; long retval; UARTprintf("before x= %x\n", (dmks_val.comp));//First print of comp retval = (long) (dmks_val.part.mant); UARTprintf("retval x= %x\n", (long) (dmks_val.part.mant));//second print of mantissa if (retval & 0xfe000000) { retval |= 0xfe000000; } exp = dmks_val.part.exp; UARTprintf("exp = %d\n", (long) (dmks_val.part.exp));//Third print of exponent //UARTprintf("exp x= %x\n", (long) (dmks_val.part.exp)); switch(exp) { case 58: retval /= 1000000L; break; case 59: retval /= 100000L; break; case 60: retval /= 10000L; break; case 61: retval /= 1000L; break; case 62: retval /= 100L; break; case 63: retval /= 10L; break; case 0: break; case 1: retval *= 10L; break; case 2: retval *= 100L; break; case 3: retval *= 1000L; break; case 4: retval *= 10000L; break; default: break; } return(retval);
}
Print out values
-IEEE 754 standard specifies a binary32 format example
-customized floating point in this codes = 11110100001001000000111101
mantissa exponent
+---+---+---+---+---+---+---+----+
| 11110100001001000000 | 111101 |
+---+---+---+---+---+---+---+----+
1st UARTPrint::
before x= 3d0903d
, which is pure value ofcomp
in union sent from server,and bin format =11110100001001000000111101
2nd print for mantissa fields::
retval x= f4240
which is extracted from bit fields of mantissa, and bin format =11110100001001000000
3rd print::
exp = 61
which is extracted from bit fields of exponent, and bin format =111101
Questions are:
Unlike IEEE 754 standard specifies a binary32 (exp bits:23~30, and mantissa bits:0 ~22)
, customized bit fields are used for floating point in this codes.
1. How do things work? - This codes use exp bits:0~5 and mantissa bits:6~30, so client/server need to manipulate order of bits?
2. Why is it divided by 1000L?
- print out showing exp value = 61, so get into case 61:
, but how exponent 61
connects to divided by 1000L
?
Thanks -Jin