2 * Authors: Matthias Heil
8 #include <math.h> /* need isnan() and isinf() (will be changed)*/
9 /* undef some reused constants defined by math.h */
21 typedef uint32_t UINT32;
23 #ifdef HAVE_LONG_DOUBLE
24 #ifdef WORDS_BIGENDIAN
31 volatile long double d;
40 volatile long double d;
44 #ifdef WORDS_BIGENDIAN
81 #define CLEAR_BUFFER(buffer) memset(buffer, 0, CALC_BUFFER_SIZE)
83 /* because variable sized structs are impossible, the internal
84 * value is represented as a pseudo-struct char array, addressed
87 * char sign; // 0 for positive, 1 for negative
88 * char exp[VALUE_SIZE];
89 * char mant[VALUE_SIZE];
93 #define _sign(a) (((char*)a)[SIGN_POS])
94 #define _exp(a) (&((char*)a)[EXPONENT_POS])
95 #define _mant(a) (&((char*)a)[MANTISSA_POS])
96 #define _desc(a) (*(descriptor_t *)&((char*)a)[DESCRIPTOR_POS])
98 #define _save_result(x) memcpy((x), sc_get_buffer(), VALUE_SIZE)
99 #define _shift_right(x, y, b) sc_shr((x), (y), VALUE_SIZE*4, 0, (b))
100 #define _shift_left(x, y, b) sc_shl((x), (y), VALUE_SIZE*4, 0, (b))
102 #define FC_DEFINE1(code) char* fc_##code(const void *a, void *result) \
104 return _calc((const char*)a, NULL, FC_##code, (char*)result); \
107 #define FC_DEFINE2(code) char* fc_##code(const void *a, const void *b, void *result) \
109 return _calc((const char*)a, (const char*)b, FC_##code, (char*)result); \
112 #define FUNC_PTR(code) fc_##code
115 # define DEBUGPRINTF(x) printf x
117 # define DEBUGPRINTF(x) ((void)0)
120 #if FLTCALC_TRACE_CALC
121 # define TRACEPRINTF(x) printf x
123 # define TRACEPRINTF(x) ((void)0)
126 static char *calc_buffer = NULL;
128 static fc_rounding_mode_t ROUNDING_MODE;
130 static int CALC_BUFFER_SIZE;
131 static int VALUE_SIZE;
133 static int EXPONENT_POS;
134 static int MANTISSA_POS;
135 static int DESCRIPTOR_POS;
137 static int max_precision;
142 static void _fail_char(const char *str, unsigned int len, int pos)
145 printf("ERROR: Unexpected character '%c'\n", *(str + pos));
147 printf("ERROR: Unexpected end of string\n");
148 while (len-- && *str) printf("%c", *str++); printf("\n");
149 while (pos--) printf(" "); printf("^\n");
150 /* the front end has to to check constant strings */
155 /* pack machine-like */
156 static char* _pack(const char *int_float, char *packed)
162 temp = alloca(VALUE_SIZE);
163 shift_val = alloca(VALUE_SIZE);
165 switch (_desc(int_float).class) {
167 val_buffer = alloca(CALC_BUFFER_SIZE);
168 fc_get_qnan(_desc(int_float).exponent_size, _desc(int_float).mantissa_size, val_buffer);
169 int_float = val_buffer;
173 val_buffer = alloca(CALC_BUFFER_SIZE);
174 fc_get_plusinf(_desc(int_float).exponent_size, _desc(int_float).mantissa_size, val_buffer);
175 _sign(val_buffer) = _sign(int_float);
176 int_float = val_buffer;
183 sc_val_from_ulong(_sign(int_float), temp);
185 sc_val_from_ulong(_desc(int_float).exponent_size + _desc(int_float).mantissa_size, NULL);
186 _shift_left(temp, sc_get_buffer(), packed);
188 /* extract exponent */
189 sc_val_from_ulong(_desc(int_float).mantissa_size, shift_val);
191 _shift_left(_exp(int_float), shift_val, temp);
193 sc_or(temp, packed, packed);
195 /* extract mantissa */
196 /* remove 2 rounding bits */
197 sc_val_from_ulong(2, shift_val);
198 _shift_right(_mant(int_float), shift_val, temp);
200 /* remove leading 1 (or 0 if denormalized) */
201 sc_max_from_bits(_desc(int_float).mantissa_size, 0, shift_val); /* all mantissa bits are 1's */
202 sc_and(temp, shift_val, temp);
205 sc_or(temp, packed, packed);
210 char* _normalize(const char *in_val, char *out_val, int sticky)
213 char lsb, guard, round, round_dir = 0;
216 temp = alloca(VALUE_SIZE);
218 /* +2: save two rounding bits at the end */
219 hsb = 2 + _desc(in_val).mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
221 if (in_val != out_val)
223 _sign(out_val) = _sign(in_val);
224 memcpy(&_desc(out_val), &_desc(in_val), sizeof(descriptor_t));
227 _desc(out_val).class = NORMAL;
229 /* mantissa all zeroes, so zero exponent (because of explicit one)*/
230 if (hsb == 2 + _desc(in_val).mantissa_size)
232 sc_val_from_ulong(0, _exp(out_val));
236 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
240 sc_val_from_ulong(-hsb-1, temp);
242 _shift_right(_mant(in_val), temp, _mant(out_val));
244 /* remember if some bits were shifted away */
245 if (!sticky) sticky = sc_had_carry();
247 sc_add(_exp(in_val), temp, _exp(out_val));
252 sc_val_from_ulong(hsb+1, temp);
254 _shift_left(_mant(in_val), temp, _mant(out_val));
256 sc_sub(_exp(in_val), temp, _exp(out_val));
259 /* check for exponent underflow */
260 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
261 DEBUGPRINTF(("Exponent underflow!\n"));
262 /* exponent underflow */
263 /* shift the mantissa right to have a zero exponent */
264 sc_val_from_ulong(1, temp);
265 sc_sub(temp, _exp(out_val), NULL);
267 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
268 if (!sticky) sticky = sc_had_carry();
269 /* denormalized means exponent of zero */
270 sc_val_from_ulong(0, _exp(out_val));
272 _desc(out_val).class = SUBNORMAL;
275 /* perform rounding by adding a value that clears the guard bit and the round bit
276 * and either causes a carry to round up or not */
277 /* get the last 3 bits of the value */
278 lsb = sc_sub_bits(_mant(out_val), _desc(out_val).mantissa_size + 2, 0) & 0x7;
279 guard = (lsb&0x2)>>1;
282 switch (ROUNDING_MODE)
285 /* round to nearest representable value, if in doubt choose the version
287 round_dir = guard && (sticky || round || lsb>>2);
290 /* if positive: round to one if the exact value is bigger, else to zero */
291 round_dir = (!_sign(out_val) && (guard || round || sticky));
294 /* if negative: round to one if the exact value is bigger, else to zero */
295 round_dir = (_sign(out_val) && (guard || round || sticky));
298 /* always round to 0 (chopping mode) */
302 DEBUGPRINTF(("Rounding (s%d, l%d, g%d, r%d, s%d) %s\n", _sign(out_val), lsb>>2, guard, round, sticky, (round_dir)?"up":"down"));
306 guard = (round^guard)<<1;
307 lsb = !(round || guard)<<2 | guard | round;
311 lsb = -((guard<<1) | round);
314 /* add the rounded value */
316 sc_val_from_long(lsb, temp);
317 sc_add(_mant(out_val), temp, _mant(out_val));
320 /* could have rounded down to zero */
321 if (sc_is_zero(_mant(out_val)) && (_desc(out_val).class == SUBNORMAL))
322 _desc(out_val).class = ZERO;
324 /* check for rounding overflow */
325 hsb = 2 + _desc(out_val).mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
326 if ((_desc(out_val).class != SUBNORMAL) && (hsb < -1))
328 sc_val_from_ulong(1, temp);
329 _shift_right(_mant(out_val), temp, _mant(out_val));
331 sc_add(_exp(out_val), temp, _exp(out_val));
333 else if ((_desc(out_val).class == SUBNORMAL) && (hsb == -1))
335 /* overflow caused the matissa to be normal again,
336 * so adapt the exponent accordingly */
337 sc_val_from_ulong(1, temp);
338 sc_add(_exp(out_val), temp, _exp(out_val));
340 _desc(out_val).class = NORMAL;
342 /* no further rounding is needed, because rounding overflow means
343 * the carry of the original rounding was propagated all the way
344 * up to the bit left of the radix point. This implies the bits
345 * to the right are all zeros (rounding is +1) */
347 /* check for exponent overflow */
348 sc_val_from_ulong((1 << _desc(out_val).exponent_size) - 1, temp);
349 if (sc_comp(_exp(out_val), temp) != -1) {
350 DEBUGPRINTF(("Exponent overflow!\n"));
351 /* exponent overflow, reaction depends on rounding method:
353 * mode | sign of value | result
354 *--------------------------------------------------------------
355 * TO_NEAREST | + | +inf
357 *--------------------------------------------------------------
358 * TO_POSITIVE | + | +inf
359 * | - | smallest representable value
360 *--------------------------------------------------------------
361 * TO_NEAGTIVE | + | largest representable value
363 *--------------------------------------------------------------
364 * TO_ZERO | + | largest representable value
365 * | - | smallest representable value
366 *--------------------------------------------------------------*/
367 if (_sign(out_val) == 0)
369 /* value is positive */
370 switch (ROUNDING_MODE) {
373 _desc(out_val).class = INF;
378 fc_get_max(_desc(out_val).exponent_size, _desc(out_val).mantissa_size, out_val);
381 /* value is negative */
382 switch (ROUNDING_MODE) {
385 _desc(out_val).class = INF;
390 fc_get_min(_desc(out_val).exponent_size, _desc(out_val).mantissa_size, out_val);
399 * calculate a + b, where a is the value with the bigger exponent
401 static char* _add(const char* a, const char* b, char* result)
409 if (_desc(a).class == NAN) {
410 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
413 if (_desc(b).class == NAN) {
414 if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
418 /* make sure result has a descriptor */
419 if (result != a && result != b)
420 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
422 /* determine if this is an addition or subtraction */
423 sign = _sign(a) ^ _sign(b);
425 /* produce nan on inf - inf */
426 if (sign && (_desc(a).class == INF) && (_desc(b).class == INF))
427 return fc_get_qnan(_desc(a).exponent_size, _desc(b).mantissa_size, result);
429 temp = alloca(VALUE_SIZE);
430 exp_diff = alloca(VALUE_SIZE);
432 /* get exponent difference */
433 sc_sub(_exp(a), _exp(b), exp_diff);
435 /* initially set sign to be the sign of a, special treatment of subtraction
436 * when exponents are equal is required though.
437 * Also special care about the sign is needed when the mantissas are equal
439 if (sign && sc_val_to_long(exp_diff) == 0) {
440 switch (sc_comp(_mant(a), _mant(b))) {
442 if (_sign(a)) _sign(result) = 1; /* abs(a) is bigger and a is negative */
443 else _sign(result) = 0;
446 if (ROUNDING_MODE == FC_TONEGATIVE)
452 if (_sign(b)) _sign(result) = 1; /* abs(b) is bigger and b is negative */
453 else _sign(result) = 0;
456 /* can't be reached */
460 _sign(result) = _sign(a);
463 /* sign has been taken care of, check for special cases */
464 if (_desc(a).class == ZERO) {
465 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
468 if (_desc(b).class == ZERO) {
469 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
473 if (_desc(a).class == INF) {
474 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
477 if (_desc(b).class == INF) {
478 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
482 /* shift the smaller value to the right to align the radix point */
483 /* subnormals have their radix point shifted to the right,
484 * take care of this first */
485 if ((_desc(b).class == SUBNORMAL) && (_desc(a).class != SUBNORMAL))
487 sc_val_from_ulong(1, temp);
488 sc_sub(exp_diff, temp, exp_diff);
491 _shift_right(_mant(b), exp_diff, temp);
492 sticky = sc_had_carry();
496 /* if subtracting a little more than the represented value or adding a little
497 * more than the represented value to a negative value this, in addition to the
498 * still set sticky bit, takes account of the 'little more' */
499 char *temp1 = alloca(CALC_BUFFER_SIZE);
500 sc_val_from_ulong(1, temp1);
501 sc_add(temp, temp1, temp);
505 if (sc_comp(_mant(a), temp) == -1)
506 sc_sub(temp, _mant(a), _mant(result));
508 sc_sub(_mant(a), temp, _mant(result));
510 sc_add(_mant(a), temp, _mant(result));
513 /* _normalize expects a 'normal' radix point, adding two subnormals
514 * results in a subnormal radix point -> shifting before normalizing */
515 if ((_desc(a).class == SUBNORMAL) && (_desc(b).class == SUBNORMAL))
517 sc_val_from_ulong(1, NULL);
518 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
521 /* resulting exponent is the bigger one */
522 memmove(_exp(result), _exp(a), VALUE_SIZE);
524 return _normalize(result, result, sticky);
527 static char* _mul(const char* a, const char* b, char* result)
531 if (_desc(a).class == NAN) {
532 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
535 if (_desc(b).class == NAN) {
536 if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
540 temp = alloca(VALUE_SIZE);
542 if (result != a && result != b)
543 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
545 _sign(result) = _sign(a) ^ _sign(b);
547 /* produce nan on 0 * inf */
548 if (_desc(a).class == ZERO) {
549 if (_desc(b).class == INF)
550 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
552 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
555 if (_desc(b).class == ZERO) {
556 if (_desc(a).class == INF)
557 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
559 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-1);
563 if (_desc(a).class == INF) {
564 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
567 if (_desc(b).class == INF) {
568 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-1);
572 /* exp = exp(a) + exp(b) - excess */
573 sc_add(_exp(a), _exp(b), _exp(result));
575 sc_val_from_ulong((1<<_desc(a).exponent_size)/2-1, temp);
576 sc_sub(_exp(result), temp, _exp(result));
578 /* mixed normal, subnormal values introduce an error of 1, correct it */
579 if ((_desc(a).class == SUBNORMAL) ^ (_desc(b).class == SUBNORMAL))
581 sc_val_from_ulong(1, temp);
582 sc_add(_exp(result), temp, _exp(result));
585 sc_mul(_mant(a), _mant(b), _mant(result));
587 /* realign result: after a multiplication the digits right of the radix
588 * point are the sum of the factors' digits after the radix point. As all
589 * values are normalized they both have the same amount of these digits,
590 * which has to be restored by proper shifting
591 * +2 because of the two rounding bits */
592 sc_val_from_ulong(2 + _desc(result).mantissa_size, temp);
594 _shift_right(_mant(result), temp, _mant(result));
596 return _normalize(result, result, sc_had_carry());
599 static char* _div(const char* a, const char* b, char* result)
601 char *temp, *dividend;
603 if (_desc(a).class == NAN) {
604 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
607 if (_desc(b).class == NAN) {
608 if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
612 temp = alloca(VALUE_SIZE);
613 dividend = alloca(VALUE_SIZE);
615 if (result != a && result != b)
616 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
618 _sign(result) = _sign(a) ^ _sign(b);
620 /* produce nan on 0/0 and inf/inf */
621 if (_desc(a).class == ZERO) {
622 if (_desc(b).class == ZERO)
624 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
627 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
631 if (_desc(b).class == INF) {
632 if (_desc(a).class == INF)
634 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
637 sc_val_from_ulong(0, NULL);
638 _save_result(_exp(result));
639 _save_result(_mant(result));
640 _desc(result).class = ZERO;
645 if (_desc(a).class == INF) {
647 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
650 if (_desc(b).class == ZERO) {
651 /* division by zero */
653 fc_get_minusinf(_desc(a).exponent_size, _desc(a).mantissa_size, result);
655 fc_get_plusinf(_desc(a).exponent_size, _desc(a).mantissa_size, result);
659 /* exp = exp(a) - exp(b) + excess - 1*/
660 sc_sub(_exp(a), _exp(b), _exp(result));
661 sc_val_from_ulong((1 << _desc(a).exponent_size)/2-2, temp);
662 sc_add(_exp(result), temp, _exp(result));
664 /* mixed normal, subnormal values introduce an error of 1, correct it */
665 if ((_desc(a).class == SUBNORMAL) ^ (_desc(b).class == SUBNORMAL))
667 sc_val_from_ulong(1, temp);
668 sc_add(_exp(result), temp, _exp(result));
671 /* mant(res) = mant(a) / 1/2mant(b) */
672 /* to gain more bits of precision in the result the dividend could be
673 * shifted left, as this operation does not loose bits. This would not
674 * fit into the integer precision, but due to the rounding bits (which
675 * are always zero because the values are all normalized) the divisor
676 * can be shifted right instead to achieve the same result */
677 sc_val_from_ulong(2 + _desc(result).mantissa_size, temp);
679 _shift_left(_mant(a), temp, dividend);
682 char *divisor = alloca(CALC_BUFFER_SIZE);
683 sc_val_from_ulong(1, divisor);
684 _shift_right(_mant(b), divisor, divisor);
685 sc_div(dividend, divisor, _mant(result));
688 return _normalize(result, result, sc_had_carry());
691 void _power_of_ten(int exp, descriptor_t *desc, char *result)
699 /* set new descriptor (else result is supposed to already have one) */
701 memcpy(&_desc(result), desc, sizeof(descriptor_t));
703 build = alloca(VALUE_SIZE);
704 temp = alloca(VALUE_SIZE);
706 sc_val_from_ulong((1 << _desc(result).exponent_size)/2-1, _exp(result));
710 /* temp is value of ten now */
711 sc_val_from_ulong(10, NULL);
714 for (exp--; exp > 0; exp--) {
716 sc_mul(build, temp, NULL);
720 /* temp is amount of leftshift needed to put the value left of the radix point */
721 sc_val_from_ulong(_desc(result).mantissa_size + 2, temp);
723 _shift_left(build, temp, _mant(result));
725 _normalize(result, result, 0);
729 static char* _trunc(const char *a, char *result)
731 /* when exponent == 0 all bits left of the radix point
732 * are the integral part of the value. For 15bit exp_size
733 * this would require a leftshift of max. 16383 bits which
735 * But it is enough to ensure that no bit right of the radix
736 * point remains set. This restricts the interesting
737 * exponents to the interval [0, mant_size-1].
738 * Outside this interval the truncated value is either 0 or
739 * it is are already truncated */
741 int exp_bias, exp_val;
744 temp = alloca(VALUE_SIZE);
747 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
749 exp_bias = (1<<_desc(a).exponent_size)/2-1;
750 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
753 sc_val_from_ulong(0, NULL);
754 _save_result(_exp(result));
755 _save_result(_mant(result));
756 _desc(result).class = ZERO;
761 if (exp_val > _desc(a).mantissa_size) {
763 memcpy(result, a, CALC_BUFFER_SIZE);
768 /* set up a proper mask to delete all bits right of the
769 * radix point if the mantissa had been shifted until exp == 0 */
770 sc_max_from_bits(1 + exp_val, 0, temp);
771 sc_val_from_long(_desc(a).mantissa_size - exp_val + 2, NULL);
772 _shift_left(temp, sc_get_buffer(), temp);
774 /* and the mask and return the result */
775 sc_and(_mant(a), temp, _mant(result));
777 if (a != result) memcpy(_exp(result), _exp(a), VALUE_SIZE);
783 * This does value sanity checking(or should do it), sets up any prerequisites,
784 * calls the proper internal functions, clears up and returns
786 char* _calc(const char *a, const char *b, int opcode, char *result)
789 #ifdef FLTCALC_TRACE_CALC
792 buffer = alloca(100);
795 if (result == NULL) result = calc_buffer;
797 TRACEPRINTF(("%s ", fc_print(a, buffer, 100, FC_PACKED)));
801 /* make the value with the bigger exponent the first one */
802 TRACEPRINTF(("+ %s ", fc_print(b, buffer, 100, FC_PACKED)));
803 if (sc_comp(_exp(a), _exp(b)) == -1)
809 TRACEPRINTF(("- %s ", fc_print(b, buffer, 100, FC_PACKED)));
810 temp = alloca(CALC_BUFFER_SIZE);
811 memcpy(temp, b, CALC_BUFFER_SIZE);
812 _sign(temp) = !_sign(b);
813 if (sc_comp(_exp(a), _exp(temp)) == -1)
814 _add(temp, a, result);
816 _add(a, temp, result);
819 TRACEPRINTF(("* %s ", fc_print(b, buffer, 100, FC_PACKED)));
823 TRACEPRINTF(("/ %s ", fc_print(b, buffer, 100, FC_PACKED)));
827 TRACEPRINTF(("negated "));
828 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
829 _sign(result) = !_sign(a);
838 TRACEPRINTF(("= %s\n", fc_print(result, buffer, 100, FC_PACKED)));
843 * functions defined in fltcalc.h
845 const void *fc_get_buffer(void)
850 const int fc_get_buffer_length(void)
852 return CALC_BUFFER_SIZE;
855 char* fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, char *result)
868 int exp_int, hsb, state;
873 char *mant_str, *exp_val, *power_val;
875 if (result == NULL) result = calc_buffer;
877 exp_val = alloca(VALUE_SIZE);
878 power_val = alloca(CALC_BUFFER_SIZE);
879 mant_str = alloca((len)?(len):(strlen(str)));
881 _desc(result).exponent_size = exp_size;
882 _desc(result).mantissa_size = mant_size;
883 _desc(result).class = NORMAL;
890 while (len == 0 || str-old_str < len)
907 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
914 state = RIGHT_OF_DOT;
925 _fail_char(old_str, len, str - old_str);
931 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
932 mant_str[pos++] = *(str++);
936 state = RIGHT_OF_DOT;
947 mant_str[pos] = '\0';
951 _fail_char(old_str, len, str - old_str);
957 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
958 mant_str[pos++] = *(str++);
969 mant_str[pos] = '\0';
973 _fail_char(old_str, len, str - old_str);
983 if (*(str-1) != 'e' && *(str-1) != 'E') _fail_char(old_str, len, str - old_str);
987 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
988 mant_str[pos] = '\0';
995 _fail_char(old_str, len, str - old_str);
1001 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
1006 case '\0': goto done;
1009 _fail_char(old_str, len, str - old_str);
1015 sc_val_from_str(mant_str, strlen(mant_str), _mant(result));
1017 /* shift to put value left of radix point */
1018 sc_val_from_ulong(mant_size + 2, exp_val);
1020 _shift_left(_mant(result), exp_val, _mant(result));
1022 sc_val_from_ulong((1 << exp_size)/2-1, _exp(result));
1024 _normalize(result, result, 0);
1026 if (state == EXPONENT) {
1027 exp_int -= atoi(str-pos);
1030 _power_of_ten(exp_int, &_desc(result), power_val);
1032 _div(result, power_val, result);
1037 /* XXX excuse of an implementation to make things work */
1039 #ifdef HAVE_LONG_DOUBLE
1040 val = strtold(str, NULL);
1042 val = strtod(str, NULL);
1045 DEBUGPRINTF(("val_from_str(%s)\n", str));
1046 return fc_val_from_float(val, exp_size, mant_size, result);
1050 char* fc_val_from_float(LLDBL l, char exp_size, char mant_size, char* result)
1053 int bias_res, bias_val, mant_val;
1057 bias_res = ((1<<exp_size)/2-1);
1059 #ifdef HAVE_LONG_DOUBLE
1062 UINT32 sign = (srcval.val.high & 0x00008000) != 0;
1063 UINT32 exponent = (srcval.val.high & 0x00007FFF) ;
1064 UINT32 mantissa0 = srcval.val.mid;
1065 UINT32 mantissa1 = srcval.val.low;
1066 #else /* no long double */
1069 UINT32 sign = (srcval.val.high & 0x80000000) != 0;
1070 UINT32 exponent = (srcval.val.high & 0x7FF00000) >> 20;
1071 UINT32 mantissa0 = srcval.val.high & 0x000FFFFF;
1072 UINT32 mantissa1 = srcval.val.low;
1075 #ifdef HAVE_LONG_DOUBLE
1076 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));
1077 DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1079 TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
1080 DEBUGPRINTF(("(%d-%.3X-%.5X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1083 if (result == NULL) result = calc_buffer;
1084 temp = alloca(VALUE_SIZE);
1086 _desc(result).exponent_size = exp_size;
1087 _desc(result).mantissa_size = mant_size;
1090 _sign(result) = sign;
1092 /* sign and flag suffice to identify nan or inf, no exponent/mantissa
1093 * encoding is needed. the function can return immediately in these cases */
1095 _desc(result).class = NAN;
1096 TRACEPRINTF(("val_from_float resulted in NAN\n"));
1099 else if (isinf(l)) {
1100 _desc(result).class = INF;
1101 TRACEPRINTF(("val_from_float resulted in %sINF\n", (_sign(result)==1)?"-":""));
1105 /* build exponent, because input and output exponent and mantissa sizes may differ
1106 * this looks more complicated than it is: unbiased input exponent + output bias,
1107 * minus the mantissa difference which is added again later when the output float
1108 * becomes normalized */
1109 #ifdef HAVE_EXPLICIT_ONE
1110 sc_val_from_long((exponent-bias_val+bias_res)-(mant_val-mant_size-1), _exp(result));
1112 sc_val_from_long((exponent-bias_val+bias_res)-(mant_val-mant_size), _exp(result));
1115 /* build mantissa representation */
1116 #ifndef HAVE_EXPLICIT_ONE
1119 /* insert the hidden bit */
1120 sc_val_from_ulong(1, temp);
1121 sc_val_from_ulong(mant_val + 2, NULL);
1122 _shift_left(temp, sc_get_buffer(), NULL);
1127 sc_val_from_ulong(0, NULL);
1130 _save_result(_mant(result));
1132 /* bits from the upper word */
1133 sc_val_from_ulong(mantissa0, temp);
1134 sc_val_from_ulong(34, NULL);
1135 _shift_left(temp, sc_get_buffer(), temp);
1136 sc_or(_mant(result), temp, _mant(result));
1138 /* bits from the lower word */
1139 sc_val_from_ulong(mantissa1, temp);
1140 sc_val_from_ulong(2, NULL);
1141 _shift_left(temp, sc_get_buffer(), temp);
1142 sc_or(_mant(result), temp, _mant(result));
1144 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
1145 * origin one to the left */
1148 sc_val_from_ulong(1, NULL);
1149 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1152 _normalize(result, result, 0);
1154 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, CALC_BUFFER_SIZE, FC_PACKED)));
1159 LLDBL fc_val_to_float(const void *val)
1173 #ifdef HAVE_LONG_DOUBLE
1174 char result_exponent = 15;
1175 char result_mantissa = 64;
1177 char result_exponent = 11;
1178 char result_mantissa = 52;
1181 temp = alloca(CALC_BUFFER_SIZE);
1182 #ifdef HAVE_EXPLICIT_ONE
1183 value = fc_cast(val, result_exponent, result_mantissa-1, temp);
1185 value = fc_cast(val, result_exponent, result_mantissa, temp);
1188 sign = _sign(value);
1190 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
1191 * lead to wrong results */
1192 exponent = sc_val_to_long(_exp(value)) ;
1194 sc_val_from_ulong(2, NULL);
1195 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1200 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1201 mantissa1 |= sc_sub_bits(_mant(value), result_mantissa, byte_offset) << (byte_offset<<3);
1203 for (; (byte_offset<<3) < result_mantissa; byte_offset++)
1204 mantissa0 |= sc_sub_bits(_mant(value), result_mantissa, byte_offset) << ((byte_offset-4)<<3);
1206 #ifndef HAVE_LONG_DOUBLE
1207 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1210 #ifdef HAVE_LONG_DOUBLE
1211 buildval.val.high = sign << 15;
1212 buildval.val.high |= exponent;
1213 buildval.val.mid = mantissa0;
1214 buildval.val.low = mantissa1;
1215 #else /* no long double */
1216 buildval.val.high = sign << 31;
1217 buildval.val.high |= exponent << 20;
1218 buildval.val.high |= mantissa0;
1219 buildval.val.low = mantissa1;
1222 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1226 char* fc_cast(const void *val, char exp_size, char mant_size, char *result)
1228 const char *value = (const char*) val;
1230 int exp_offset, val_bias, res_bias;
1232 if (result == NULL) result = calc_buffer;
1233 temp = alloca(VALUE_SIZE);
1235 if (_desc(value).exponent_size == exp_size && _desc(value).mantissa_size == mant_size)
1237 if (value != result) memcpy(result, value, CALC_BUFFER_SIZE);
1241 /* set the descriptor of the new value */
1242 _desc(result).exponent_size = exp_size;
1243 _desc(result).mantissa_size = mant_size;
1244 _desc(result).class = _desc(value).class;
1246 _sign(result) = _sign(value);
1248 /* when the mantissa sizes differ normalizing has to shift to align it.
1249 * this would change the exponent, which is unwanted. So calculate this
1250 * offset and add it */
1251 val_bias = (1<<_desc(value).exponent_size)/2-1;
1252 res_bias = (1<<exp_size)/2-1;
1254 exp_offset = (res_bias - val_bias) - (_desc(value).mantissa_size - mant_size);
1255 sc_val_from_long(exp_offset, temp);
1256 sc_add(_exp(value), temp, _exp(result));
1258 /* _normalize expects normalized radix point */
1259 if (_desc(val).class == SUBNORMAL) {
1260 sc_val_from_ulong(1, NULL);
1261 _shift_left(_mant(val), sc_get_buffer(), _mant(result));
1262 } else if (value != result) {
1263 memcpy(_mant(result), _mant(value), VALUE_SIZE);
1265 memmove(_mant(result), _mant(value), VALUE_SIZE);
1268 _normalize(result, result, 0);
1269 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, VALUE_SIZE, FC_PACKED)));
1273 char* fc_get_max(unsigned int exponent_size, unsigned int mantissa_size, char* result)
1275 if (result == NULL) result = calc_buffer;
1277 _desc(result).exponent_size = exponent_size;
1278 _desc(result).mantissa_size = mantissa_size;
1279 _desc(result).class = NORMAL;
1283 sc_val_from_ulong((1<<exponent_size) - 2, _exp(result));
1285 sc_max_from_bits(mantissa_size + 1, 0, _mant(result));
1286 sc_val_from_ulong(2, NULL);
1287 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1292 char* fc_get_min(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1294 if (result == NULL) result = calc_buffer;
1296 fc_get_max(exponent_size, mantissa_size, result);
1302 char* fc_get_snan(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1304 if (result == NULL) result = calc_buffer;
1306 _desc(result).exponent_size = exponent_size;
1307 _desc(result).mantissa_size = mantissa_size;
1308 _desc(result).class = NAN;
1312 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1314 /* signalling nan has non-zero mantissa with msb not set */
1315 sc_val_from_ulong(1, _mant(result));
1320 char* fc_get_qnan(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1322 if (result == NULL) result = calc_buffer;
1324 _desc(result).exponent_size = exponent_size;
1325 _desc(result).mantissa_size = mantissa_size;
1326 _desc(result).class = NAN;
1330 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1332 /* quiet nan has the msb of the mantissa set, so shift one there */
1333 sc_val_from_ulong(1, _mant(result));
1334 /* mantissa_size >+< 1 because of two extra rounding bits */
1335 sc_val_from_ulong(mantissa_size + 1, NULL);
1336 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1341 char* fc_get_plusinf(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1343 if (result == NULL) result = calc_buffer;
1345 _desc(result).exponent_size = exponent_size;
1346 _desc(result).mantissa_size = mantissa_size;
1347 _desc(result).class = NORMAL;
1351 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1353 sc_val_from_ulong(0, _mant(result));
1358 char* fc_get_minusinf(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1360 if (result == NULL) result = calc_buffer;
1362 fc_get_plusinf(exponent_size, mantissa_size, result);
1368 int fc_comp(const void *a, const void *b)
1370 const char *val_a = (const char*)a;
1371 const char *val_b = (const char*)b;
1374 if (_desc(val_a).class == NAN || _desc(val_b).class == NAN) return 2;
1375 /* zero is equal independent of sign */
1376 if ((_desc(val_a).class == ZERO) && (_desc(val_b).class == ZERO)) return 0;
1377 /* different signs make compare easy */
1378 if (_sign(val_a) != _sign(val_b)) return (_sign(val_a)==0)?(1):(-1);
1379 /* both infinity means equality */
1380 if ((_desc(val_a).class == INF) && (_desc(val_b).class == INF)) return 0;
1381 /* infinity is bigger than the rest */
1382 if (_desc(val_a).class == INF) return _sign(val_a)?(-1):(1);
1383 if (_desc(val_b).class == INF) return _sign(val_b)?(1):(-1);
1385 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1391 return sc_comp(_mant(val_a), _mant(val_b));
1397 int fc_is_zero(const void *a)
1399 return _desc((const char*)a).class == ZERO;
1402 int fc_is_negative(const void *a)
1404 return _sign((const char*)a);
1407 int fc_is_inf(const void *a)
1409 return _desc(a).class == INF;
1412 int fc_is_nan(const void *a)
1414 return _desc(a).class == NAN;
1417 int fc_is_subnormal(const void *a)
1419 return _desc(a).class == SUBNORMAL;
1422 char *fc_print(const void *a, char *buf, int buflen, unsigned base)
1427 val = (const char*)a;
1429 mul_1 = alloca(CALC_BUFFER_SIZE);
1433 switch (_desc(val).class) {
1435 if (buflen >= 8+_sign(val)) sprintf(buf, "%sINFINITY", _sign(val)?"-":"");
1436 else snprintf(buf, buflen, "%sINF", _sign(val)?"-":NULL);
1439 snprintf(buf, buflen, "NAN");
1442 snprintf(buf, buflen, "0.0");
1445 /* XXX to be implemented */
1446 #ifdef HAVE_LONG_DOUBLE
1447 /* XXX 30 is arbitrary */
1448 snprintf(buf, buflen, "%.30LE", fc_val_to_float(val));
1450 snprintf(buf, buflen, "%.18E", fc_val_to_float(val));
1456 switch (_desc(val).class) {
1458 if (buflen >= 8+_sign(val)) sprintf(buf, "%sINFINITY", _sign(val)?"-":"");
1459 else snprintf(buf, buflen, "%sINF", _sign(val)?"-":NULL);
1462 snprintf(buf, buflen, "NAN");
1465 snprintf(buf, buflen, "0.0");
1468 #ifdef HAVE_LONG_DOUBLE
1469 snprintf(buf, buflen, "%LA", fc_val_to_float(val));
1471 snprintf(buf, buflen, "%A", fc_val_to_float(val));
1478 snprintf(buf, buflen, "%s", sc_print(_pack(val, mul_1), VALUE_SIZE*4, SC_HEX));
1484 unsigned char fc_sub_bits(const void *value, unsigned num_bits, unsigned byte_ofs)
1486 /* this is used to cache the packed version of the value */
1487 static char *pack = NULL;
1489 if (pack == NULL) pack = malloc(VALUE_SIZE);
1492 _pack((const char*)value, pack);
1494 return sc_sub_bits(pack, num_bits, byte_ofs);
1497 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode)
1499 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1500 ROUNDING_MODE = mode;
1502 return ROUNDING_MODE;
1505 fc_rounding_mode_t fc_get_rounding_mode(void)
1507 return ROUNDING_MODE;
1510 void init_fltcalc(int precision)
1512 if (calc_buffer == NULL) {
1513 /* does nothing if already init */
1514 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1516 init_strcalc(precision + 4);
1518 /* needs additionally two bits to round, a bit as explicit 1., and one for
1519 * addition overflow */
1520 max_precision = sc_get_precision() - 4;
1521 if (max_precision < precision)
1522 printf("WARING: not enough precision available, using %d\n", max_precision);
1524 ROUNDING_MODE = FC_TONEAREST;
1525 VALUE_SIZE = sc_get_buffer_length();
1527 EXPONENT_POS = SIGN_POS + sizeof(char);
1528 MANTISSA_POS = EXPONENT_POS + VALUE_SIZE;
1529 DESCRIPTOR_POS = MANTISSA_POS + VALUE_SIZE;
1530 CALC_BUFFER_SIZE = DESCRIPTOR_POS + sizeof(descriptor_t);
1532 calc_buffer = malloc(CALC_BUFFER_SIZE);
1533 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));
1534 #ifdef HAVE_LONG_DOUBLE
1535 DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
1537 DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
1539 #ifdef WORDS_BIGENDIAN
1540 DEBUGPRINTF(("\tWord order is big endian\n\n"));
1542 DEBUGPRINTF(("\tWord order is little endian\n\n"));
1547 /* definition of interface functions */