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.cpp File Reference
#include "mangle.h"

Go to the source code of this file.

Classes

union  type_conv
 

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)
 

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 }