+
+ TRACEPRINTF(("= %s\n", fc_print(result, buffer, 100, FC_PACKED)));
+ return result;
+}
+
+/********
+ * functions defined in fltcalc.h
+ ********/
+const void *fc_get_buffer(void)
+{
+ return calc_buffer;
+}
+
+int fc_get_buffer_length(void)
+{
+ return calc_buffer_size;
+}
+
+char* fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, char *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;
+
+ if (result == NULL) result = calc_buffer;
+
+ exp_val = alloca(value_size);
+ power_val = alloca(calc_buffer_size);
+ mant_str = alloca((len)?(len):(strlen(str)));
+
+ _desc(result).exponent_size = exp_size;
+ _desc(result).mantissa_size = mant_size;
+ _desc(result).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 '+':
+ _sign(result) = 0;
+ state = LEFT_OF_DOT;
+ str++;
+ break;
+
+ case '-':
+ _sign(result) = 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':
+ _sign(result) = 0;
+ state = LEFT_OF_DOT;
+ break;
+
+ case '.':
+ _sign(result) = 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 + 2, exp_val);
+
+ _shift_left(_mant(result), exp_val, _mant(result));
+
+ sc_val_from_ulong((1 << exp_size)/2-1, _exp(result));
+
+ _normalize(result, result, 0);
+
+ if (state == EXPONENT) {
+ exp_int -= atoi(str-pos);
+ }
+
+ _power_of_ten(exp_int, &_desc(result), power_val);
+
+ _fdiv(result, power_val, result);
+
+ return result;
+#else
+
+ /* XXX excuse of an implementation to make things work */
+ LLDBL val;
+#ifdef HAVE_LONG_DOUBLE
+ val = strtold(str, NULL);
+#else
+ val = strtod(str, NULL);
+#endif
+
+ DEBUGPRINTF(("val_from_str(%s)\n", str));
+ return fc_val_from_float(val, exp_size, mant_size, result);
+#endif
+}
+
+char* fc_val_from_float(LLDBL l, char exp_size, char mant_size, char* 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)/2-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);
+
+ _desc(result).exponent_size = exp_size;
+ _desc(result).mantissa_size = mant_size;
+
+ /* extract sign */
+ _sign(result) = 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)) {
+ _desc(result).clss = NAN;
+ TRACEPRINTF(("val_from_float resulted in NAN\n"));
+ return result;
+ }
+ else if (isinf(l)) {
+ _desc(result).clss = INF;
+ TRACEPRINTF(("val_from_float resulted in %sINF\n", (_sign(result)==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 + 2, 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(2, 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_float(const void *val)
+{
+ const char *value;
+ char *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 = _sign(value);
+
+ /* @@@ 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(2, 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;
+}
+
+char* fc_cast(const void *val, char exp_size, char mant_size, char *result)
+{
+ const char *value = (const char*) val;
+ char *temp;
+ int exp_offset, val_bias, res_bias;
+
+ if (result == NULL) result = calc_buffer;
+ temp = alloca(value_size);
+
+ if (_desc(value).exponent_size == exp_size && _desc(value).mantissa_size == mant_size)
+ {
+ if (value != result) memcpy(result, value, calc_buffer_size);
+ return result;
+ }
+
+ /* set the descriptor of the new value */
+ _desc(result).exponent_size = exp_size;
+ _desc(result).mantissa_size = mant_size;
+ _desc(result).clss = _desc(value).clss;
+
+ _sign(result) = _sign(value);
+
+ /* 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<<_desc(value).exponent_size)/2-1;
+ res_bias = (1<<exp_size)/2-1;
+
+ exp_offset = (res_bias - val_bias) - (_desc(value).mantissa_size - mant_size);
+ sc_val_from_long(exp_offset, temp);
+ sc_add(_exp(value), temp, _exp(result));
+
+ /* _normalize expects normalized radix point */
+ if (_desc(val).clss == SUBNORMAL) {
+ sc_val_from_ulong(1, NULL);
+ _shift_left(_mant(val), 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;
+}
+
+char* fc_get_max(unsigned int exponent_size, unsigned int mantissa_size, char* result)
+{
+ if (result == NULL) result = calc_buffer;
+
+ _desc(result).exponent_size = exponent_size;
+ _desc(result).mantissa_size = mantissa_size;
+ _desc(result).clss = NORMAL;
+
+ _sign(result) = 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(2, NULL);
+ _shift_left(_mant(result), sc_get_buffer(), _mant(result));
+
+ return result;
+}
+
+char* fc_get_min(unsigned int exponent_size, unsigned int mantissa_size, char *result)
+{
+ if (result == NULL) result = calc_buffer;
+
+ fc_get_max(exponent_size, mantissa_size, result);
+ _sign(result) = 1;
+
+ return result;
+}
+
+char* fc_get_snan(unsigned int exponent_size, unsigned int mantissa_size, char *result)
+{
+ if (result == NULL) result = calc_buffer;
+
+ _desc(result).exponent_size = exponent_size;
+ _desc(result).mantissa_size = mantissa_size;
+ _desc(result).clss = NAN;
+
+ _sign(result) = 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;
+}
+
+char* fc_get_qnan(unsigned int exponent_size, unsigned int mantissa_size, char *result)
+{
+ if (result == NULL) result = calc_buffer;
+
+ _desc(result).exponent_size = exponent_size;
+ _desc(result).mantissa_size = mantissa_size;
+ _desc(result).clss = NAN;
+
+ _sign(result) = 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;
+}
+
+char* fc_get_plusinf(unsigned int exponent_size, unsigned int mantissa_size, char *result)
+{
+ if (result == NULL) result = calc_buffer;
+
+ _desc(result).exponent_size = exponent_size;
+ _desc(result).mantissa_size = mantissa_size;
+ _desc(result).clss = NORMAL;
+
+ _sign(result) = 0;
+
+ sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
+
+ sc_val_from_ulong(0, _mant(result));
+
+ return result;
+}
+
+char* fc_get_minusinf(unsigned int exponent_size, unsigned int mantissa_size, char *result)
+{
+ if (result == NULL) result = calc_buffer;
+
+ fc_get_plusinf(exponent_size, mantissa_size, result);
+ _sign(result) = 1;
+
+ return result;