+const void *fc_get_buffer(void) {
+ return calc_buffer;
+}
+
+int fc_get_buffer_length(void) {
+ return calc_buffer_size;
+}
+
+void *fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, void *result) {
+#if 0
+ enum {
+ START,
+ LEFT_OF_DOT,
+ RIGHT_OF_DOT,
+ EXP_START,
+ EXPONENT,
+ END
+ };
+
+ char exp_sign;
+ int exp_int, hsb, state;
+
+ const char *old_str;
+
+ int pos;
+ char *mant_str, *exp_val, *power_val;
+
+ (void) len;
+ if (result == NULL) result = calc_buffer;
+
+ exp_val = alloca(value_size);
+ power_val = alloca(calc_buffer_size);
+ mant_str = alloca((len)?(len):(strlen(str)));
+
+ result->desc.exponent_size = exp_size;
+ result->desc.mantissa_size = mant_size;
+ result->desc.clss = NORMAL;
+
+ old_str = str;
+ pos = 0;
+ exp_int = 0;
+ state = START;
+
+ while (len == 0 || str-old_str < len) {
+ switch (state) {
+ case START:
+ switch (*str) {
+ case '+':
+ result->sign = 0;
+ state = LEFT_OF_DOT;
+ str++;
+ break;
+
+ case '-':
+ result->sign = 1;
+ state = LEFT_OF_DOT;
+ str++;
+ break;
+
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
+ result->sign = 0;
+ state = LEFT_OF_DOT;
+ break;
+
+ case '.':
+ result->sign = 0;
+ state = RIGHT_OF_DOT;
+ str++;
+ break;
+
+ case 'n':
+ case 'N':
+ case 'i':
+ case 'I':
+ break;
+
+ default:
+ fail_char(old_str, len, str - old_str);
+ }
+ break;
+
+ case LEFT_OF_DOT:
+ switch (*str) {
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
+ mant_str[pos++] = *(str++);
+ break;
+
+ case '.':
+ state = RIGHT_OF_DOT;
+ str++;
+ break;
+
+ case 'e':
+ case 'E':
+ state = EXP_START;
+ str++;
+ break;
+
+ case '\0':
+ mant_str[pos] = '\0';
+ goto done;
+
+ default:
+ fail_char(old_str, len, str - old_str);
+ }
+ break;
+
+ case RIGHT_OF_DOT:
+ switch (*str) {
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
+ mant_str[pos++] = *(str++);
+ exp_int++;
+ break;
+
+ case 'e':
+ case 'E':
+ state = EXP_START;
+ str++;
+ break;
+
+ case '\0':
+ mant_str[pos] = '\0';
+ goto done;
+
+ default:
+ fail_char(old_str, len, str - old_str);
+ }
+ break;
+
+ case EXP_START:
+ switch (*str) {
+ case '-':
+ exp_sign = 1;
+ /* fall through */
+ case '+':
+ if (*(str-1) != 'e' && *(str-1) != 'E') fail_char(old_str, len, str - old_str);
+ str++;
+ break;
+
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
+ mant_str[pos] = '\0';
+ pos = 1;
+ str++;
+ state = EXPONENT;
+ break;
+
+ default:
+ fail_char(old_str, len, str - old_str);
+ }
+ break;
+
+ case EXPONENT:
+ switch (*str) {
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
+ pos++;
+ str++;
+ break;
+
+ case '\0': goto done;
+
+ default:
+ fail_char(old_str, len, str - old_str);
+ }
+ }
+ } /* switch(state) */
+
+done:
+ sc_val_from_str(mant_str, strlen(mant_str), _mant(result));
+
+ /* shift to put value left of radix point */
+ sc_val_from_ulong(mant_size + ROUNDING_BITS, exp_val);
+
+ _shift_left(_mant(result), exp_val, _mant(result));
+
+ sc_val_from_ulong((1 << (exp_size - 1)) - 1, _exp(result));
+
+ _normalize(result, result, 0);
+
+ if (state == EXPONENT) {
+ exp_int -= atoi(str-pos);
+ }
+
+ _power_of_ten(exp_int, &result->desc, power_val);
+
+ _fdiv(result, power_val, result);
+
+ return result;
+#else
+ /* XXX excuse of an implementation to make things work */
+ LLDBL val;
+ fp_value *tmp = alloca(calc_buffer_size);
+ (void) len;
+
+#ifdef HAVE_LONG_DOUBLE
+ val = strtold(str, NULL);
+ DEBUGPRINTF(("val_from_str(%s)\n", str));
+ fc_val_from_ieee754(val, 15, 64, tmp);
+#else
+ val = strtod(str, NULL);
+ DEBUGPRINTF(("val_from_str(%s)\n", str));
+ fc_val_from_ieee754(val, 11, 52, tmp);
+#endif /* HAVE_LONG_DOUBLE */
+ return fc_cast(tmp, exp_size, mant_size, result);
+#endif
+}
+
+fp_value *fc_val_from_ieee754(LLDBL l, char exp_size, char mant_size, fp_value *result) {
+ char *temp;
+ int bias_res, bias_val, mant_val;
+ value_t srcval;
+ UINT32 sign, exponent, mantissa0, mantissa1;
+
+ srcval.d = l;
+ bias_res = ((1 << (exp_size - 1)) - 1);
+
+#ifdef HAVE_LONG_DOUBLE
+ mant_val = 64;
+ bias_val = 0x3fff;
+ sign = (srcval.val.high & 0x00008000) != 0;
+ exponent = (srcval.val.high & 0x00007FFF) ;
+ mantissa0 = srcval.val.mid;
+ mantissa1 = srcval.val.low;
+#else /* no long double */
+ mant_val = 52;
+ bias_val = 0x3ff;
+ sign = (srcval.val.high & 0x80000000) != 0;
+ exponent = (srcval.val.high & 0x7FF00000) >> 20;
+ mantissa0 = srcval.val.high & 0x000FFFFF;
+ mantissa1 = srcval.val.low;
+#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)); */
+ DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
+#else
+ TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
+ DEBUGPRINTF(("(%d-%.3X-%.5X%.8X)\n", sign, exponent, mantissa0, mantissa1));
+#endif
+
+ if (result == NULL) result = calc_buffer;
+ temp = alloca(value_size);
+
+ /* CLEAR the buffer, else some bits might be uninitialised */
+ memset(result, 0, fc_get_buffer_length());
+
+ result->desc.exponent_size = exp_size;
+ result->desc.mantissa_size = mant_size;
+
+ /* extract sign */
+ result->sign = sign;
+
+ /* sign and flag suffice to identify nan or inf, no exponent/mantissa
+ * encoding is needed. the function can return immediately in these cases */
+ if (isnan(l)) {
+ result->desc.clss = NAN;
+ TRACEPRINTF(("val_from_float resulted in NAN\n"));
+ return result;
+ }
+ else if (isinf(l)) {
+ result->desc.clss = INF;
+ TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
+ return result;
+ }
+
+ /* build exponent, because input and output exponent and mantissa sizes may differ
+ * this looks more complicated than it is: unbiased input exponent + output bias,
+ * minus the mantissa difference which is added again later when the output float
+ * becomes normalized */
+#ifdef HAVE_EXPLICIT_ONE
+ sc_val_from_long((exponent-bias_val+bias_res)-(mant_val-mant_size-1), _exp(result));
+#else
+ sc_val_from_long((exponent-bias_val+bias_res)-(mant_val-mant_size), _exp(result));
+#endif
+
+ /* build mantissa representation */
+#ifndef HAVE_EXPLICIT_ONE
+ if (exponent != 0) {
+ /* insert the hidden bit */
+ sc_val_from_ulong(1, temp);
+ sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
+ _shift_left(temp, sc_get_buffer(), NULL);
+ }
+ else
+#endif
+ {
+ sc_val_from_ulong(0, NULL);
+ }
+
+ _save_result(_mant(result));
+
+ /* bits from the upper word */
+ sc_val_from_ulong(mantissa0, temp);
+ sc_val_from_ulong(34, NULL);
+ _shift_left(temp, sc_get_buffer(), temp);
+ sc_or(_mant(result), temp, _mant(result));
+
+ /* bits from the lower word */
+ sc_val_from_ulong(mantissa1, temp);
+ sc_val_from_ulong(ROUNDING_BITS, NULL);
+ _shift_left(temp, sc_get_buffer(), temp);
+ sc_or(_mant(result), temp, _mant(result));
+
+ /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
+ * origin one to the left */
+ if (exponent == 0) {
+ sc_val_from_ulong(1, NULL);
+ _shift_left(_mant(result), sc_get_buffer(), _mant(result));
+ }
+
+ normalize(result, result, 0);
+
+ TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
+
+ return result;
+}
+
+LLDBL fc_val_to_ieee754(const fp_value *val) {
+ fp_value *value;
+ fp_value *temp = NULL;
+
+ int byte_offset;
+
+ UINT32 sign;
+ UINT32 exponent;
+ UINT32 mantissa0;
+ UINT32 mantissa1;
+
+ value_t buildval;
+
+#ifdef HAVE_LONG_DOUBLE
+ char result_exponent = 15;
+ char result_mantissa = 64;
+#else
+ char result_exponent = 11;
+ char result_mantissa = 52;
+#endif
+
+ temp = alloca(calc_buffer_size);
+#ifdef HAVE_EXPLICIT_ONE
+ value = fc_cast(val, result_exponent, result_mantissa-1, temp);
+#else
+ value = fc_cast(val, result_exponent, result_mantissa, temp);
+#endif
+
+ sign = value->sign;
+
+ /* @@@ 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)) ;
+
+ sc_val_from_ulong(ROUNDING_BITS, NULL);
+ _shift_right(_mant(value), sc_get_buffer(), _mant(value));
+
+ mantissa0 = 0;
+ mantissa1 = 0;
+
+ for (byte_offset = 0; byte_offset < 4; byte_offset++)
+ mantissa1 |= sc_sub_bits(_mant(value), result_mantissa, byte_offset) << (byte_offset<<3);
+
+ for (; (byte_offset<<3) < result_mantissa; byte_offset++)
+ mantissa0 |= sc_sub_bits(_mant(value), result_mantissa, byte_offset) << ((byte_offset-4)<<3);
+
+#ifdef HAVE_LONG_DOUBLE
+ buildval.val.high = sign << 15;
+ buildval.val.high |= exponent;
+ buildval.val.mid = mantissa0;
+ buildval.val.low = mantissa1;
+#else /* no long double */
+ mantissa0 &= 0x000FFFFF; /* get rid of garbage */
+ buildval.val.high = sign << 31;
+ buildval.val.high |= exponent << 20;
+ buildval.val.high |= mantissa0;
+ buildval.val.low = mantissa1;
+#endif
+
+ TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
+ return buildval.d;
+}
+
+fp_value *fc_cast(const fp_value *value, char exp_size, char mant_size, fp_value *result) {
+ char *temp;
+ int exp_offset, val_bias, res_bias;
+
+ if (result == NULL) result = calc_buffer;
+ temp = alloca(value_size);
+
+ if (value->desc.exponent_size == exp_size && value->desc.mantissa_size == mant_size) {
+ if (value != result)
+ memcpy(result, value, calc_buffer_size);
+ return result;
+ }
+
+ if (value->desc.clss == NAN) {
+ if (sc_get_highest_set_bit(_mant(value)) == value->desc.mantissa_size + 1)
+ return fc_get_qnan(exp_size, mant_size, result);
+ else
+ return fc_get_snan(exp_size, mant_size, result);
+ }
+
+ /* set the descriptor of the new value */
+ result->desc.exponent_size = exp_size;
+ result->desc.mantissa_size = mant_size;
+ result->desc.clss = value->desc.clss;
+
+ result->sign = value->sign;
+
+ /* when the mantissa sizes differ normalizing has to shift to align it.
+ * this would change the exponent, which is unwanted. So calculate this
+ * offset and add it */
+ val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
+ res_bias = (1 << (exp_size - 1)) - 1;
+
+ exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - mant_size);
+ sc_val_from_long(exp_offset, temp);
+ sc_add(_exp(value), temp, _exp(result));
+
+ /* _normalize expects normalized radix point */
+ if (value->desc.clss == SUBNORMAL) {
+ sc_val_from_ulong(1, NULL);
+ _shift_left(_mant(value), sc_get_buffer(), _mant(result));
+ } else if (value != result) {
+ memcpy(_mant(result), _mant(value), value_size);
+ } else {
+ memmove(_mant(result), _mant(value), value_size);
+ }
+
+ normalize(result, result, 0);
+ TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
+ return result;
+}
+
+fp_value *fc_get_max(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ result->desc.exponent_size = exponent_size;
+ result->desc.mantissa_size = mantissa_size;
+ result->desc.clss = NORMAL;
+
+ result->sign = 0;
+
+ sc_val_from_ulong((1<<exponent_size) - 2, _exp(result));
+
+ sc_max_from_bits(mantissa_size + 1, 0, _mant(result));
+ sc_val_from_ulong(ROUNDING_BITS, NULL);
+ _shift_left(_mant(result), sc_get_buffer(), _mant(result));
+
+ return result;
+}
+
+fp_value *fc_get_min(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ fc_get_max(exponent_size, mantissa_size, result);
+ result->sign = 1;
+
+ return result;
+}
+
+fp_value *fc_get_snan(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ result->desc.exponent_size = exponent_size;
+ result->desc.mantissa_size = mantissa_size;
+ result->desc.clss = NAN;
+
+ result->sign = 0;
+
+ sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
+
+ /* signaling NaN has non-zero mantissa with msb not set */
+ sc_val_from_ulong(1, _mant(result));
+
+ return result;
+}
+
+fp_value *fc_get_qnan(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ result->desc.exponent_size = exponent_size;
+ result->desc.mantissa_size = mantissa_size;
+ result->desc.clss = NAN;
+
+ result->sign = 0;
+
+ sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
+
+ /* quiet NaN has the msb of the mantissa set, so shift one there */
+ sc_val_from_ulong(1, _mant(result));
+ /* mantissa_size >+< 1 because of two extra rounding bits */
+ sc_val_from_ulong(mantissa_size + 1, NULL);
+ _shift_left(_mant(result), sc_get_buffer(), _mant(result));
+
+ return result;
+}
+
+fp_value *fc_get_plusinf(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ result->desc.exponent_size = exponent_size;
+ result->desc.mantissa_size = mantissa_size;
+ result->desc.clss = NORMAL;
+
+ result->sign = 0;
+
+ sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
+
+ sc_val_from_ulong(0, _mant(result));
+
+ return result;
+}
+
+fp_value *fc_get_minusinf(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ fc_get_plusinf(exponent_size, mantissa_size, result);
+ result->sign = 1;
+
+ return result;
+}
+
+int fc_comp(const fp_value *val_a, const fp_value *val_b) {
+ int mul = 1;
+
+ /*
+ * shortcut: if both values are identical, they are either
+ * Unordered if NaN or equal
+ */
+ if (val_a == val_b)
+ return val_a->desc.clss == NAN ? 2 : 0;
+
+ /* unordered if one is a NaN */
+ if (val_a->desc.clss == NAN || val_b->desc.clss == NAN)
+ return 2;
+
+ /* zero is equal independent of sign */
+ if ((val_a->desc.clss == ZERO) && (val_b->desc.clss == ZERO))
+ return 0;
+
+ /* different signs make compare easy */
+ if (val_a->sign != val_b->sign)
+ return (val_a->sign == 0) ? (1) : (-1);
+
+ mul = val_a->sign ? -1 : 1;
+
+ /* both infinity means equality */
+ if ((val_a->desc.clss == INF) && (val_b->desc.clss == INF))
+ return 0;
+
+ /* infinity is bigger than the rest */
+ if (val_a->desc.clss == INF)
+ return 1 * mul;
+ if (val_b->desc.clss == INF)
+ return -1 * mul;
+
+ /* check first exponent, that mantissa if equal */
+ switch (sc_comp(_exp(val_a), _exp(val_b))) {
+ case -1:
+ return -1 * mul;
+ case 1:
+ return 1 * mul;
+ case 0:
+ return sc_comp(_mant(val_a), _mant(val_b)) * mul;
+ default:
+ return 2;
+ }
+}
+
+int fc_is_zero(const fp_value *a) {
+ return a->desc.clss == ZERO;
+}
+
+int fc_is_negative(const fp_value *a) {
+ return a->sign;
+}
+
+int fc_is_inf(const fp_value *a) {
+ return a->desc.clss == INF;
+}
+
+int fc_is_nan(const fp_value *a) {
+ return a->desc.clss == NAN;
+}
+
+int fc_is_subnormal(const fp_value *a) {
+ return a->desc.clss == SUBNORMAL;
+}
+
+char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base) {
+ char *mul_1;
+
+ mul_1 = alloca(calc_buffer_size);
+
+ switch (base) {
+ case FC_DEC:
+ switch (val->desc.clss) {
+ case INF:
+ if (buflen >= 8 + val->sign) sprintf(buf, "%sINFINITY", val->sign ? "-":"");
+ else snprintf(buf, buflen, "%sINF", val->sign ? "-":NULL);
+ break;
+ case NAN:
+ snprintf(buf, buflen, "NAN");
+ break;
+ case ZERO:
+ snprintf(buf, buflen, "0.0");
+ break;
+ default:
+ /* XXX to be implemented */
+#ifdef HAVE_LONG_DOUBLE
+ /* XXX 30 is arbitrary */
+ snprintf(buf, buflen, "%.30LE", fc_val_to_ieee754(val));
+#else
+ snprintf(buf, buflen, "%.18E", fc_val_to_ieee754(val));
+#endif
+ }
+ break;
+
+ case FC_HEX:
+ switch (val->desc.clss) {
+ case INF:
+ if (buflen >= 8+val->sign) sprintf(buf, "%sINFINITY", val->sign?"-":"");
+ else snprintf(buf, buflen, "%sINF", val->sign?"-":NULL);
+ break;
+ case NAN:
+ snprintf(buf, buflen, "NAN");
+ break;
+ case ZERO:
+ snprintf(buf, buflen, "0.0");
+ break;
+ default:
+#ifdef HAVE_LONG_DOUBLE
+ snprintf(buf, buflen, "%LA", fc_val_to_ieee754(val));
+#else
+ snprintf(buf, buflen, "%A", fc_val_to_ieee754(val));
+#endif
+ }
+ break;
+
+ case FC_PACKED:
+ default:
+ snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
+ buf[buflen - 1] = '\0';
+ break;
+ }
+ return buf;
+}
+
+unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs) {
+ /* this is used to cache the packed version of the value */
+ static char *packed_value = NULL;
+
+ if (packed_value == NULL) packed_value = xmalloc(value_size);
+
+ if (value != NULL)
+ pack(value, packed_value);
+
+ return sc_sub_bits(packed_value, num_bits, byte_ofs);
+}
+
+/* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
+int fc_zero_mantissa(const fp_value *value) {
+ return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
+}
+
+/* Returns the exponent of a value. */
+int fc_get_exponent(const fp_value *value) {
+ int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
+ return sc_val_to_long(_exp(value)) - exp_bias;
+}
+
+/* Return non-zero if a given value can be converted lossless into another precision */
+int fc_can_lossless_conv_to(const fp_value *value, char exp_size, char mant_size) {
+ int v;
+ int exp_bias;
+
+ /* handle some special cases first */
+ switch (value->desc.clss) {
+ case ZERO:
+ case INF:
+ case NAN:
+ return 1;
+ default:
+ break;
+ }
+
+ /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
+ exp_bias = (1 << (exp_size - 1)) - 1;
+ v = fc_get_exponent(value) + exp_bias;
+ if (0 < v && v < (1 << exp_size) - 1) {
+ /* check the mantissa */
+ v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
+ return v < mant_size;
+ }
+ return 0;
+}
+
+
+fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode) {
+ if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
+ rounding_mode = mode;
+
+ return rounding_mode;
+}
+
+fc_rounding_mode_t fc_get_rounding_mode(void) {
+ return rounding_mode;
+}
+
+void init_fltcalc(int precision) {
+ if (calc_buffer == NULL) {
+ /* does nothing if already init */
+ if (precision == 0) precision = FC_DEFAULT_PRECISION;
+
+ init_strcalc(precision + 4);
+
+ /* needs additionally two bits to round, a bit as explicit 1., and one for
+ * addition overflow */
+ max_precision = sc_get_precision() - 4;
+ if (max_precision < precision)
+ printf("WARNING: not enough precision available, using %d\n", max_precision);
+
+ rounding_mode = FC_TONEAREST;
+ value_size = sc_get_buffer_length();
+ calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
+
+ calc_buffer = xmalloc(calc_buffer_size);
+ memset(calc_buffer, 0, calc_buffer_size);
+ DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
+#ifdef HAVE_LONG_DOUBLE
+ DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
+#else
+ DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
+#endif
+#ifdef WORDS_BIGENDIAN
+ DEBUGPRINTF(("\tWord order is big endian\n\n"));
+#else
+ DEBUGPRINTF(("\tWord order is little endian\n\n"));
+#endif
+ }
+}
+
+void finish_fltcalc (void) {
+ free(calc_buffer); calc_buffer = NULL;
+}
+
+#ifdef FLTCALC_TRACE_CALC
+static char buffer[100];
+#endif
+
+/* definition of interface functions */
+fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
+ TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
+
+ /* make the value with the bigger exponent the first one */
+ if (sc_comp(_exp(a), _exp(b)) == -1)
+ _fadd(b, a, result);
+ else
+ _fadd(a, b, result);
+
+ TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
+ return result;
+}
+
+fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result) {
+ fp_value *temp;
+
+ if (result == NULL) result = calc_buffer;
+
+ TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
+ TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
+
+ temp = alloca(calc_buffer_size);
+ memcpy(temp, b, calc_buffer_size);
+ temp->sign = !b->sign;
+ if (sc_comp(_exp(a), _exp(temp)) == -1)
+ _fadd(temp, a, result);
+ else
+ _fadd(a, temp, result);
+
+ TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
+ return result;
+}
+
+fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
+ TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
+
+ _fmul(a, b, result);
+
+ TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
+ return result;
+}
+
+fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
+ TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
+
+ _fdiv(a, b, result);
+
+ TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
+ return result;
+}
+
+fp_value *fc_neg(const fp_value *a, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
+
+ if (a != result)
+ memcpy(result, a, calc_buffer_size);
+ result->sign = !a->sign;
+
+ TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
+ return result;
+}
+
+fp_value *fc_int(const fp_value *a, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
+ TRACEPRINTF(("truncated to integer "));
+
+ _trunc(a, result);
+
+ TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
+ return result;
+}
+
+fp_value *fc_rnd(const fp_value *a, fp_value *result) {
+ if (result == NULL) result = calc_buffer;
+
+ (void) a;
+ TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
+ TRACEPRINTF(("rounded to integer "));
+
+ assert(!"fc_rnd() not yet implemented");
+
+ TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
+ return result;
+}
+
+/*
+ * convert a floating point value into an sc value ...
+ */
+int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode) {
+ if (a->desc.clss == NORMAL) {
+ int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
+ int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
+ int shift, highest;
+
+ if (a->sign && !mode_is_signed(dst_mode)) {
+ /* FIXME: for now we cannot convert this */
+ return 0;
+ }
+
+ assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
+ shift = exp_val - (a->desc.mantissa_size + ROUNDING_BITS);
+
+ if (shift > 0) {
+ sc_shlI(_mant(a), shift, 64, 0, result);
+ } else {
+ sc_shrI(_mant(a), -shift, 64, 0, result);
+ }
+
+ /* check for overflow */
+ highest = sc_get_highest_set_bit(result);
+
+ if (mode_is_signed(dst_mode)) {
+ if (highest == sc_get_lowest_set_bit(result)) {
+ /* need extra test for MIN_INT */
+ if (highest >= (int) get_mode_size_bits(dst_mode)) {
+ /* FIXME: handle overflow */
+ return 0;
+ }
+ } else {
+ if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
+ /* FIXME: handle overflow */
+ return 0;
+ }
+ }
+ } else {
+ if (highest >= (int) get_mode_size_bits(dst_mode)) {
+ /* FIXME: handle overflow */
+ return 0;
+ }
+ }
+
+ if (a->sign)
+ sc_neg(result, result);
+
+ return 1;
+ }
+ else if (a->desc.clss == ZERO) {
+ sc_zero(result);
+ return 1;
+ }
+ return 0;
+}
+
+
+unsigned fc_set_immediate_precision(unsigned bits) {
+ unsigned old = immediate_prec;
+
+ immediate_prec = bits;
+ return old;
+}
+
+int fc_is_exact(void) {
+ return fc_exact;