# include <malloc.h>
#endif
+#include "xmalloc.h"
+
typedef uint32_t UINT32;
#ifdef HAVE_LONG_DOUBLE
value_class_t class;
} descriptor_t;
-#define CLEAR_BUFFER(buffer) memset(buffer, 0, CALC_BUFFER_SIZE)
+#define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
/* because variable sized structs are impossible, the internal
* value is represented as a pseudo-struct char array, addressed
* by macros
* struct {
* char sign; // 0 for positive, 1 for negative
- * char exp[VALUE_SIZE];
- * char mant[VALUE_SIZE];
+ * char exp[value_size];
+ * char mant[value_size];
* descriptor_t desc;
* };
*/
#define _mant(a) (&((char*)a)[MANTISSA_POS])
#define _desc(a) (*(descriptor_t *)&((char*)a)[DESCRIPTOR_POS])
-#define _save_result(x) memcpy((x), sc_get_buffer(), VALUE_SIZE)
-#define _shift_right(x, y, b) sc_shr((x), (y), VALUE_SIZE*4, 0, (b))
-#define _shift_left(x, y, b) sc_shl((x), (y), VALUE_SIZE*4, 0, (b))
+#define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
+#define _shift_right(x, y, b) sc_shr((x), (y), value_size*4, 0, (b))
+#define _shift_left(x, y, b) sc_shl((x), (y), value_size*4, 0, (b))
#define FC_DEFINE1(code) char* fc_##code(const void *a, void *result) \
{ \
static char *calc_buffer = NULL;
-static fc_rounding_mode_t ROUNDING_MODE;
+static fc_rounding_mode_t rounding_mode;
-static int CALC_BUFFER_SIZE;
-static int VALUE_SIZE;
+static int calc_buffer_size;
+static int value_size;
static int SIGN_POS;
static int EXPONENT_POS;
static int MANTISSA_POS;
char *temp;
char *val_buffer;
- temp = alloca(VALUE_SIZE);
- shift_val = alloca(VALUE_SIZE);
+ temp = alloca(value_size);
+ shift_val = alloca(value_size);
switch (_desc(int_float).class) {
case NAN:
- val_buffer = alloca(CALC_BUFFER_SIZE);
+ val_buffer = alloca(calc_buffer_size);
fc_get_qnan(_desc(int_float).exponent_size, _desc(int_float).mantissa_size, val_buffer);
int_float = val_buffer;
break;
case INF:
- val_buffer = alloca(CALC_BUFFER_SIZE);
+ val_buffer = alloca(calc_buffer_size);
fc_get_plusinf(_desc(int_float).exponent_size, _desc(int_float).mantissa_size, val_buffer);
_sign(val_buffer) = _sign(int_float);
int_float = val_buffer;
char lsb, guard, round, round_dir = 0;
char *temp;
- temp = alloca(VALUE_SIZE);
+ temp = alloca(value_size);
/* +2: save two rounding bits at the end */
hsb = 2 + _desc(in_val).mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
_desc(out_val).class = NORMAL;
- /* mantissa all zeroes, so zero exponent (because of explicit one)*/
+ /* mantissa all zeros, so zero exponent (because of explicit one)*/
if (hsb == 2 + _desc(in_val).mantissa_size)
{
sc_val_from_ulong(0, _exp(out_val));
guard = (lsb&0x2)>>1;
round = lsb&0x1;
- switch (ROUNDING_MODE)
+ switch (rounding_mode)
{
case FC_TONEAREST:
/* round to nearest representable value, if in doubt choose the version
if (_sign(out_val) == 0)
{
/* value is positive */
- switch (ROUNDING_MODE) {
+ switch (rounding_mode) {
case FC_TONEAREST:
case FC_TOPOSITIVE:
_desc(out_val).class = INF;
}
} else {
/* value is negative */
- switch (ROUNDING_MODE) {
+ switch (rounding_mode) {
case FC_TONEAREST:
case FC_TONEGATIVE:
_desc(out_val).class = INF;
char sticky;
if (_desc(a).class == NAN) {
- if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
+ if (a != result) memcpy(result, a, calc_buffer_size);
return result;
}
if (_desc(b).class == NAN) {
- if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
+ if (b != result) memcpy(result, b, calc_buffer_size);
return result;
}
if (sign && (_desc(a).class == INF) && (_desc(b).class == INF))
return fc_get_qnan(_desc(a).exponent_size, _desc(b).mantissa_size, result);
- temp = alloca(VALUE_SIZE);
- exp_diff = alloca(VALUE_SIZE);
+ temp = alloca(value_size);
+ exp_diff = alloca(value_size);
/* get exponent difference */
sc_sub(_exp(a), _exp(b), exp_diff);
else _sign(result) = 0;
break;
case 0: /* a == b */
- if (ROUNDING_MODE == FC_TONEGATIVE)
+ if (rounding_mode == FC_TONEGATIVE)
_sign(result) = 1;
else
_sign(result) = 0;
/* sign has been taken care of, check for special cases */
if (_desc(a).class == ZERO) {
- if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
+ if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, calc_buffer_size-SIGN_POS-1);
return result;
}
if (_desc(b).class == ZERO) {
- if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
+ if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, calc_buffer_size-SIGN_POS-1);
return result;
}
if (_desc(a).class == INF) {
- if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
+ if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, calc_buffer_size-SIGN_POS-1);
return result;
}
if (_desc(b).class == INF) {
- if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
+ if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, calc_buffer_size-SIGN_POS-1);
return result;
}
/* if subtracting a little more than the represented value or adding a little
* more than the represented value to a negative value this, in addition to the
* still set sticky bit, takes account of the 'little more' */
- char *temp1 = alloca(CALC_BUFFER_SIZE);
+ char *temp1 = alloca(calc_buffer_size);
sc_val_from_ulong(1, temp1);
sc_add(temp, temp1, temp);
}
}
/* resulting exponent is the bigger one */
- memmove(_exp(result), _exp(a), VALUE_SIZE);
+ memmove(_exp(result), _exp(a), value_size);
return _normalize(result, result, sticky);
}
char *temp;
if (_desc(a).class == NAN) {
- if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
+ if (a != result) memcpy(result, a, calc_buffer_size);
return result;
}
if (_desc(b).class == NAN) {
- if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
+ if (b != result) memcpy(result, b, calc_buffer_size);
return result;
}
- temp = alloca(VALUE_SIZE);
+ temp = alloca(value_size);
if (result != a && result != b)
memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
if (_desc(b).class == INF)
fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
else
- if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
+ if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, calc_buffer_size-1);
return result;
}
if (_desc(b).class == ZERO) {
if (_desc(a).class == INF)
fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
else
- if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-1);
+ if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, calc_buffer_size-1);
return result;
}
if (_desc(a).class == INF) {
- if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
+ if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, calc_buffer_size-1);
return result;
}
if (_desc(b).class == INF) {
- if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-1);
+ if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, calc_buffer_size-1);
return result;
}
char *temp, *dividend;
if (_desc(a).class == NAN) {
- if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
+ if (a != result) memcpy(result, a, calc_buffer_size);
return result;
}
if (_desc(b).class == NAN) {
- if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
+ if (b != result) memcpy(result, b, calc_buffer_size);
return result;
}
- temp = alloca(VALUE_SIZE);
- dividend = alloca(VALUE_SIZE);
+ temp = alloca(value_size);
+ dividend = alloca(value_size);
if (result != a && result != b)
memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
else
/* 0/x -> a */
- if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
+ if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, calc_buffer_size-1);
return result;
}
if (_desc(a).class == INF) {
/* inf/x -> inf */
- if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
+ if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, calc_buffer_size-1);
return result;
}
if (_desc(b).class == ZERO) {
_shift_left(_mant(a), temp, dividend);
{
- char *divisor = alloca(CALC_BUFFER_SIZE);
+ char *divisor = alloca(calc_buffer_size);
sc_val_from_ulong(1, divisor);
_shift_right(_mant(b), divisor, divisor);
sc_div(dividend, divisor, _mant(result));
if (desc != NULL)
memcpy(&_desc(result), desc, sizeof(descriptor_t));
- build = alloca(VALUE_SIZE);
- temp = alloca(VALUE_SIZE);
+ build = alloca(value_size);
+ temp = alloca(value_size);
sc_val_from_ulong((1 << _desc(result).exponent_size)/2-1, _exp(result));
int exp_bias, exp_val;
char *temp;
- temp = alloca(VALUE_SIZE);
+ temp = alloca(value_size);
if (a != result)
memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
if (exp_val > _desc(a).mantissa_size) {
if (a != result)
- memcpy(result, a, CALC_BUFFER_SIZE);
+ memcpy(result, a, calc_buffer_size);
return result;
}
/* and the mask and return the result */
sc_and(_mant(a), temp, _mant(result));
- if (a != result) memcpy(_exp(result), _exp(a), VALUE_SIZE);
+ if (a != result) memcpy(_exp(result), _exp(a), value_size);
return result;
}
break;
case FC_sub:
TRACEPRINTF(("- %s ", fc_print(b, buffer, 100, FC_PACKED)));
- temp = alloca(CALC_BUFFER_SIZE);
- memcpy(temp, b, CALC_BUFFER_SIZE);
+ temp = alloca(calc_buffer_size);
+ memcpy(temp, b, calc_buffer_size);
_sign(temp) = !_sign(b);
if (sc_comp(_exp(a), _exp(temp)) == -1)
_add(temp, a, result);
break;
case FC_neg:
TRACEPRINTF(("negated "));
- if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
+ if (a != result) memcpy(result, a, calc_buffer_size);
_sign(result) = !_sign(a);
break;
case FC_int:
const int fc_get_buffer_length(void)
{
- return CALC_BUFFER_SIZE;
+ return calc_buffer_size;
}
char* fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, char *result)
if (result == NULL) result = calc_buffer;
- exp_val = alloca(VALUE_SIZE);
- power_val = alloca(CALC_BUFFER_SIZE);
+ exp_val = alloca(value_size);
+ power_val = alloca(calc_buffer_size);
mant_str = alloca((len)?(len):(strlen(str)));
_desc(result).exponent_size = exp_size;
#endif
if (result == NULL) result = calc_buffer;
- temp = alloca(VALUE_SIZE);
+ temp = alloca(value_size);
_desc(result).exponent_size = exp_size;
_desc(result).mantissa_size = mant_size;
_normalize(result, result, 0);
- TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, CALC_BUFFER_SIZE, FC_PACKED)));
+ TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
return result;
}
char result_mantissa = 52;
#endif
- temp = alloca(CALC_BUFFER_SIZE);
+ temp = alloca(calc_buffer_size);
#ifdef HAVE_EXPLICIT_ONE
value = fc_cast(val, result_exponent, result_mantissa-1, temp);
#else
int exp_offset, val_bias, res_bias;
if (result == NULL) result = calc_buffer;
- temp = alloca(VALUE_SIZE);
+ 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);
+ if (value != result) memcpy(result, value, calc_buffer_size);
return result;
}
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);
+ memcpy(_mant(result), _mant(value), value_size);
} else {
- memmove(_mant(result), _mant(value), VALUE_SIZE);
+ 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)));
+ TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
return result;
}
val = (const char*)a;
- mul_1 = alloca(CALC_BUFFER_SIZE);
+ mul_1 = alloca(calc_buffer_size);
switch (base) {
case FC_DEC:
case FC_PACKED:
default:
- snprintf(buf, buflen, "%s", sc_print(_pack(val, mul_1), VALUE_SIZE*4, SC_HEX));
+ snprintf(buf, buflen, "%s", sc_print(_pack(val, mul_1), value_size*4, SC_HEX));
break;
}
return buf;
/* this is used to cache the packed version of the value */
static char *pack = NULL;
- if (pack == NULL) pack = malloc(VALUE_SIZE);
+ if (pack == NULL) pack = xmalloc(value_size);
if (value != NULL)
_pack((const char*)value, pack);
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;
+ rounding_mode = mode;
- return ROUNDING_MODE;
+ return rounding_mode;
}
fc_rounding_mode_t fc_get_rounding_mode(void)
{
- return ROUNDING_MODE;
+ return rounding_mode;
}
void init_fltcalc(int precision)
if (max_precision < precision)
printf("WARING: not enough precision available, using %d\n", max_precision);
- ROUNDING_MODE = FC_TONEAREST;
- VALUE_SIZE = sc_get_buffer_length();
+ rounding_mode = FC_TONEAREST;
+ value_size = sc_get_buffer_length();
SIGN_POS = 0;
EXPONENT_POS = SIGN_POS + sizeof(char);
- MANTISSA_POS = EXPONENT_POS + VALUE_SIZE;
- DESCRIPTOR_POS = MANTISSA_POS + VALUE_SIZE;
- CALC_BUFFER_SIZE = DESCRIPTOR_POS + sizeof(descriptor_t);
+ MANTISSA_POS = EXPONENT_POS + value_size;
+ DESCRIPTOR_POS = MANTISSA_POS + value_size;
+ calc_buffer_size = DESCRIPTOR_POS + sizeof(descriptor_t);
- calc_buffer = malloc(CALC_BUFFER_SIZE);
- DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\n\tSIGN_POS = %d\n\tEXPONENT_POS = %d\n\tMANTISSA_POS = %d\n\tDESCRIPTOR_POS = %d\n\tCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", VALUE_SIZE, SIGN_POS, EXPONENT_POS, MANTISSA_POS, DESCRIPTOR_POS, CALC_BUFFER_SIZE, calc_buffer));
+ calc_buffer = xmalloc(calc_buffer_size);
+ DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\n\tSIGN_POS = %d\n\tEXPONENT_POS = %d\n\tMANTISSA_POS = %d\n\tDESCRIPTOR_POS = %d\n\tCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, SIGN_POS, EXPONENT_POS, MANTISSA_POS, DESCRIPTOR_POS, calc_buffer_size, calc_buffer));
#ifdef HAVE_LONG_DOUBLE
DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
#else