added routine to read flag
[libfirm] / ir / tv / fltcalc.c
index cca8bf8..18f0bf1 100644 (file)
  */
 
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+
 #include "fltcalc.h"
 #include "strcalc.h"
 
 #  undef NAN
 #endif
 
+#ifdef HAVE_INTTYPES_H
 #include <inttypes.h>
+#endif
+
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
-#ifdef linux
+#ifdef HAVE_ALLOCA_H
 # include <alloca.h>
-#else
-# ifdef __APPLE__
-# include <stdlib.h>
-# endif
+#endif
+#ifdef HAVE_MALLOC_H
+# include <malloc.h>
 #endif
 
 typedef uint32_t UINT32;
@@ -99,7 +106,7 @@ typedef struct {
  * value is represented as a pseudo-struct char array, addressed
  * by macros
  * struct {
- *   char sign;             // 0 for positive, 1 for negative
+ *   char sign;             //  0 for positive, 1 for negative
  *   char exp[VALUE_SIZE];
  *   char mant[VALUE_SIZE];
  *   descriptor_t desc;
@@ -451,7 +458,7 @@ static char* _add(const char* a, const char* b, char* result)
    * when exponents are equal is required though.
    * Also special care about the sign is needed when the mantissas are equal
    * (+/- 0 ?) */
-  if (sign && sc_val_to_long(exp_diff) == 0) {
+  if (sign && sc_val_to_long(exp_diff, VALUE_SIZE >> 2, 1) == 0) {
     switch (sc_comp(_mant(a), _mant(b))) {
       case 1:  /* a > b */
         if (_sign(a)) _sign(result) = 1;  /* abs(a) is bigger and a is negative */
@@ -762,7 +769,7 @@ static char* _trunc(const char *a, char *result)
     memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
 
   exp_bias = (1<<_desc(a).exponent_size)/2-1;
-  exp_val = sc_val_to_long(_exp(a)) - exp_bias;
+  exp_val = sc_val_to_long(_exp(a), VALUE_SIZE >> 2, 1) - exp_bias;
 
   if (exp_val < 0) {
     sc_val_from_ulong(0, NULL);
@@ -1024,7 +1031,7 @@ char* fc_val_from_str(const char *str, unsigned int len, char exp_size, char man
             _fail_char(old_str, len, str - old_str);
         }
     }
-  } // switch(state)
+  } /*  switch(state) */
 
 done:
   sc_val_from_str(mant_str, strlen(mant_str), _mant(result));
@@ -1089,7 +1096,7 @@ char* fc_val_from_float(LLDBL l, char exp_size, char mant_size, char* result)
 #endif
 
 #ifdef HAVE_LONG_DOUBLE
-  TRACEPRINTF(("val_from_float(%.8X%.8X%.8X)\n", ((int*)&l)[2], ((int*)&l)[1], ((int*)&l)[0]));//srcval.val.high, srcval.val.mid, srcval.val.low));
+  TRACEPRINTF(("val_from_float(%.8X%.8X%.8X)\n", ((int*)&l)[2], ((int*)&l)[1], ((int*)&l)[0]));/* srcval.val.high, srcval.val.mid, srcval.val.low)); */
   DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
 #else
   TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
@@ -1205,7 +1212,7 @@ LLDBL fc_val_to_float(const void *val)
 
   /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
    * lead to wrong results */
-  exponent = sc_val_to_long(_exp(value)) ;
+  exponent = sc_val_to_long(_exp(value), VALUE_SIZE >> 2, 1) ;
 
   sc_val_from_ulong(2, NULL);
   _shift_right(_mant(value), sc_get_buffer(), _mant(value));
@@ -1560,6 +1567,10 @@ void init_fltcalc(int precision)
   }
 }
 
+void finish_fltcalc (void) {
+  free(calc_buffer); calc_buffer = NULL;
+}
+
 /* definition of interface functions */
 FC_DEFINE2(add)
 FC_DEFINE2(sub)