Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mangle.h File Reference

Go to the source code of this file.

Macros

#define BITMASK(bits)   ( (1<<bits)-1 )
 
#define SIGN_MASK   0x80000000
 
#define EXP_MASK   0x7f800000
 
#define SIGNIF_MASK   0x007fffff
 
#define EXP_BITS   8
 
#define SIGNIF_BITS   23
 

Functions

unsigned int float_to_uint32 (float value, char exponent, char significand, char denorm)
 
float uint32_to_float (unsigned int value, char exponent, char significand, char denorm)
 

Macro Definition Documentation

#define BITMASK (   bits)    ( (1<<bits)-1 )

Definition at line 22 of file mangle.h.

Referenced by float_to_uint32(), and uint32_to_float().

#define EXP_BITS   8

Definition at line 26 of file mangle.h.

Referenced by float_to_uint32().

#define EXP_MASK   0x7f800000

Definition at line 24 of file mangle.h.

Referenced by float_to_uint32().

#define SIGN_MASK   0x80000000

Definition at line 23 of file mangle.h.

Referenced by float_to_uint32().

#define SIGNIF_BITS   23

Definition at line 27 of file mangle.h.

Referenced by float_to_uint32(), and uint32_to_float().

#define SIGNIF_MASK   0x007fffff

Definition at line 25 of file mangle.h.

Referenced by float_to_uint32().

Function Documentation

unsigned int float_to_uint32 ( float  value,
char  exponent,
char  significand,
char  denorm 
)

Definition at line 54 of file mangle.cpp.

References BITMASK, EXP_BITS, EXP_MASK, type_conv::fl_val, SIGN_MASK, SIGNIF_BITS, SIGNIF_MASK, and type_conv::u32_val.

55 {
56  union type_conv tmp;
57  unsigned int newval, tmpsig;
58  int tmpexp;
59 
60  tmp.fl_val = value;
61  //set the sign bit
62  newval = SIGN_MASK&tmp.u32_val;
63  newval >>= EXP_BITS+SIGNIF_BITS-(exponent+significand);
64  //get the exponent and remove its offset
65  tmpexp = (EXP_MASK&tmp.u32_val)>>SIGNIF_BITS;
66  tmpexp -= 127;
67  //get the significand but leave one extra bit for use while performing
68  //rounding later
69  tmpsig = (SIGNIF_MASK&tmp.u32_val)>>(SIGNIF_BITS-significand-1);
70  //if this will be a normalized number, perform rounding
71  if (tmpexp > denorm && tmpsig&1) {
72  tmpsig++;
73  if ( tmpsig&( 1<<(significand+1) ) ) {
74  tmpexp++;
75  tmpsig -= 1<<(significand+1);
76  }
77  }
78  //remove the bit saved for rounding
79  tmpsig >>= 1;
80  if (tmpexp == denorm) {
81  if (tmpsig&1) //more rounding fun
82  tmpsig++;
83  if ( tmpsig&(1<<significand) ) {
84  tmpexp++;
85  tmpsig -= 1<<significand;
86  tmpexp = 1;
87  } else {
88  tmpsig |= 1<<significand;
89  tmpsig >>= 1;
90  tmpexp = 0;
91  }
92  } else if (tmpexp < denorm) {
93  tmpsig |= 1<<significand;
94  tmpsig >>= denorm-tmpexp;
95  if (tmpsig&1)
96  tmpsig++;
97  tmpsig >>= 1;
98  tmpexp = 0;
99  //if the number is too big, clamp it to the maximum represented value
100  } else if (tmpexp >= (1<<exponent)+denorm) {
101  tmpexp = BITMASK( exponent );
102  tmpsig = BITMASK( significand );
103  } else {
104  tmpexp -= denorm;
105  }
106  newval |= (tmpexp<<significand)|tmpsig;
107  return newval;
108 }
float uint32_to_float ( unsigned int  value,
char  exponent,
char  significand,
char  denorm 
)

Definition at line 110 of file mangle.cpp.

References BITMASK, type_conv::fl_val, SIGNIF_BITS, and type_conv::u32_val.

111 {
112  union type_conv newval;
113  unsigned int tmpsig;
114  int tmpexp;
115 
116  newval.u32_val = ( 1<<(exponent+significand) )&value;
117  newval.u32_val <<= 31-(exponent+significand);
118  tmpsig = value&BITMASK( significand );
119  tmpexp = value&(BITMASK( exponent )<<significand);
120  tmpexp >>= significand;
121  //if the number should be 0 or -0, go ahead and return
122  if (tmpexp == 0 && tmpsig == 0) {
123  return newval.fl_val;
124  //if a denormalized value, rebuild to normalized form
125  } else if (tmpexp == 0) {
126  tmpsig <<= 1;
127  while ( !( tmpsig&(1<<significand) ) ) {
128  tmpexp -= 1;
129  tmpsig <<= 1;
130  }
131  tmpsig -= 1<<significand;
132  }
133  tmpexp += 127+denorm;
134  tmpsig <<= SIGNIF_BITS-significand;
135 
136  newval.u32_val |= tmpexp<<SIGNIF_BITS;
137  newval.u32_val |= tmpsig;
138 
139  return newval.fl_val;
140 }