Some of you will already know this but I'm working on a function for a compiler I'm making, this function is supposed to read both integers and floating numbers based on text provided. I was struggling to identify what variables I needed where and how to handle them, I then thought to do a small project/file with fixed data to verify those details, thing is I've now hit a roadblock and struggling to come up with ideas of how to fix it, I think my problem lies in how I calculate the mantissa (parameter man) but it could also be the exponent (parametet exp) or both, I started with the floating part as 0 to check the exponent so I don't think it's that though. Here's what I have so far:
I'll take a look at some bugs mentioned of mitsy before taking a break until I think of something or someone else manages to think of something (and posts it)Code:#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <float.h>
typedef unsigned char uchar;
typedef unsigned long long ullong;
#define TYPE float
typedef union LDBL {
TYPE fpn;
uchar hex[sizeof(TYPE)];
struct {
ulong man : 23;
ulong exp : 8;
ulong sig : 1;
};
} LDBL;
void print( char *text, LDBL val ) {
printf("%s",text);
for ( size_t i = 0; i < sizeof(TYPE); ++i ) {
printf( " %02X", val.hex[i] );
}
}
#define NUM 2
#define FPN 0
int main() {
ulong num = 3, fpn = 0, exp = 0;
ulong bit = 1, pos = 0;
LDBL test = {0}, mine = {0};
test.fpn = fpn;
test.fpn /= 10;
test.fpn += num;
printf("%E\n", test.fpn);
for ( pos = 0; bit; bit <<= 1 ) {
mine.man |= bit;
if ( num & bit ) mine.exp = pos;
++pos;
if ( pos == FLT_MAX_EXP ) break;
}
mine.exp += FLT_MAX_EXP;
if ( exp )
mine.exp = (mine.exp == (FLT_MAX_EXP) * 2) ? -1 : mine.exp + exp;
num *= 10;
bit = 1;
pos = 0;
if ( mine.exp != (FLT_MAX_EXP + exp) )
for ( ; !(bit & num); bit <<= 1, ++pos );
for ( ; pos < 23; ++pos ) {
fpn <<= 1;
mine.man <<= 1;
if ( !fpn ) break;
mine.man |= (fpn & bit);
}
print( "test", test );
putchar('\n');
print( "mine", mine );
putchar('\n');
printf( "%08X %08X\n", test.exp, mine.exp );
return 0;
}
Edit: Oh and here are my results:
Code:gcc -Wall -o "test_fpn" "test_fpn.c" && "./test_fpn"
3.000000E+00
test 00 00 40 40
mine FE FF FF 40
00000080 00000081
Compilation finished successfully.