3 * File name: ir/tv/fltcalc.c
9 * Copyright: (c) 2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 #include <math.h> /* need isnan() and isinf() (will be changed)*/
18 /* undef some reused constants defined by math.h */
36 typedef uint32_t UINT32;
38 #ifdef HAVE_LONG_DOUBLE
39 #ifdef WORDS_BIGENDIAN
46 volatile long double d;
55 volatile long double d;
59 #ifdef WORDS_BIGENDIAN
96 #define CLEAR_BUFFER(buffer) memset(buffer, 0, CALC_BUFFER_SIZE)
98 /* because variable sized structs are impossible, the internal
99 * value is represented as a pseudo-struct char array, addressed
102 * char sign; // 0 for positive, 1 for negative
103 * char exp[VALUE_SIZE];
104 * char mant[VALUE_SIZE];
108 #define _sign(a) (((char*)a)[SIGN_POS])
109 #define _exp(a) (&((char*)a)[EXPONENT_POS])
110 #define _mant(a) (&((char*)a)[MANTISSA_POS])
111 #define _desc(a) (*(descriptor_t *)&((char*)a)[DESCRIPTOR_POS])
113 #define _save_result(x) memcpy((x), sc_get_buffer(), VALUE_SIZE)
114 #define _shift_right(x, y, b) sc_shr((x), (y), VALUE_SIZE*4, 0, (b))
115 #define _shift_left(x, y, b) sc_shl((x), (y), VALUE_SIZE*4, 0, (b))
117 #define FC_DEFINE1(code) char* fc_##code(const void *a, void *result) \
119 return _calc((const char*)a, NULL, FC_##code, (char*)result); \
122 #define FC_DEFINE2(code) char* fc_##code(const void *a, const void *b, void *result) \
124 return _calc((const char*)a, (const char*)b, FC_##code, (char*)result); \
127 #define FUNC_PTR(code) fc_##code
130 # define DEBUGPRINTF(x) printf x
132 # define DEBUGPRINTF(x) ((void)0)
135 #if FLTCALC_TRACE_CALC
136 # define TRACEPRINTF(x) printf x
138 # define TRACEPRINTF(x) ((void)0)
141 static char *calc_buffer = NULL;
143 static fc_rounding_mode_t ROUNDING_MODE;
145 static int CALC_BUFFER_SIZE;
146 static int VALUE_SIZE;
148 static int EXPONENT_POS;
149 static int MANTISSA_POS;
150 static int DESCRIPTOR_POS;
152 static int max_precision;
157 static void _fail_char(const char *str, unsigned int len, int pos)
160 printf("ERROR: Unexpected character '%c'\n", *(str + pos));
162 printf("ERROR: Unexpected end of string\n");
163 while (len-- && *str) printf("%c", *str++); printf("\n");
164 while (pos--) printf(" "); printf("^\n");
165 /* the front end has to to check constant strings */
170 /* pack machine-like */
171 static char* _pack(const char *int_float, char *packed)
177 temp = alloca(VALUE_SIZE);
178 shift_val = alloca(VALUE_SIZE);
180 switch (_desc(int_float).class) {
182 val_buffer = alloca(CALC_BUFFER_SIZE);
183 fc_get_qnan(_desc(int_float).exponent_size, _desc(int_float).mantissa_size, val_buffer);
184 int_float = val_buffer;
188 val_buffer = alloca(CALC_BUFFER_SIZE);
189 fc_get_plusinf(_desc(int_float).exponent_size, _desc(int_float).mantissa_size, val_buffer);
190 _sign(val_buffer) = _sign(int_float);
191 int_float = val_buffer;
198 sc_val_from_ulong(_sign(int_float), temp);
200 sc_val_from_ulong(_desc(int_float).exponent_size + _desc(int_float).mantissa_size, NULL);
201 _shift_left(temp, sc_get_buffer(), packed);
203 /* extract exponent */
204 sc_val_from_ulong(_desc(int_float).mantissa_size, shift_val);
206 _shift_left(_exp(int_float), shift_val, temp);
208 sc_or(temp, packed, packed);
210 /* extract mantissa */
211 /* remove 2 rounding bits */
212 sc_val_from_ulong(2, shift_val);
213 _shift_right(_mant(int_float), shift_val, temp);
215 /* remove leading 1 (or 0 if denormalized) */
216 sc_max_from_bits(_desc(int_float).mantissa_size, 0, shift_val); /* all mantissa bits are 1's */
217 sc_and(temp, shift_val, temp);
220 sc_or(temp, packed, packed);
225 char* _normalize(const char *in_val, char *out_val, int sticky)
228 char lsb, guard, round, round_dir = 0;
231 temp = alloca(VALUE_SIZE);
233 /* +2: save two rounding bits at the end */
234 hsb = 2 + _desc(in_val).mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
236 if (in_val != out_val)
238 _sign(out_val) = _sign(in_val);
239 memcpy(&_desc(out_val), &_desc(in_val), sizeof(descriptor_t));
242 _desc(out_val).class = NORMAL;
244 /* mantissa all zeroes, so zero exponent (because of explicit one)*/
245 if (hsb == 2 + _desc(in_val).mantissa_size)
247 sc_val_from_ulong(0, _exp(out_val));
251 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
255 sc_val_from_ulong(-hsb-1, temp);
257 _shift_right(_mant(in_val), temp, _mant(out_val));
259 /* remember if some bits were shifted away */
260 if (!sticky) sticky = sc_had_carry();
262 sc_add(_exp(in_val), temp, _exp(out_val));
267 sc_val_from_ulong(hsb+1, temp);
269 _shift_left(_mant(in_val), temp, _mant(out_val));
271 sc_sub(_exp(in_val), temp, _exp(out_val));
274 /* check for exponent underflow */
275 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
276 DEBUGPRINTF(("Exponent underflow!\n"));
277 /* exponent underflow */
278 /* shift the mantissa right to have a zero exponent */
279 sc_val_from_ulong(1, temp);
280 sc_sub(temp, _exp(out_val), NULL);
282 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
283 if (!sticky) sticky = sc_had_carry();
284 /* denormalized means exponent of zero */
285 sc_val_from_ulong(0, _exp(out_val));
287 _desc(out_val).class = SUBNORMAL;
290 /* perform rounding by adding a value that clears the guard bit and the round bit
291 * and either causes a carry to round up or not */
292 /* get the last 3 bits of the value */
293 lsb = sc_sub_bits(_mant(out_val), _desc(out_val).mantissa_size + 2, 0) & 0x7;
294 guard = (lsb&0x2)>>1;
297 switch (ROUNDING_MODE)
300 /* round to nearest representable value, if in doubt choose the version
302 round_dir = guard && (sticky || round || lsb>>2);
305 /* if positive: round to one if the exact value is bigger, else to zero */
306 round_dir = (!_sign(out_val) && (guard || round || sticky));
309 /* if negative: round to one if the exact value is bigger, else to zero */
310 round_dir = (_sign(out_val) && (guard || round || sticky));
313 /* always round to 0 (chopping mode) */
317 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"));
321 guard = (round^guard)<<1;
322 lsb = !(round || guard)<<2 | guard | round;
326 lsb = -((guard<<1) | round);
329 /* add the rounded value */
331 sc_val_from_long(lsb, temp);
332 sc_add(_mant(out_val), temp, _mant(out_val));
335 /* could have rounded down to zero */
336 if (sc_is_zero(_mant(out_val)) && (_desc(out_val).class == SUBNORMAL))
337 _desc(out_val).class = ZERO;
339 /* check for rounding overflow */
340 hsb = 2 + _desc(out_val).mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
341 if ((_desc(out_val).class != SUBNORMAL) && (hsb < -1))
343 sc_val_from_ulong(1, temp);
344 _shift_right(_mant(out_val), temp, _mant(out_val));
346 sc_add(_exp(out_val), temp, _exp(out_val));
348 else if ((_desc(out_val).class == SUBNORMAL) && (hsb == -1))
350 /* overflow caused the matissa to be normal again,
351 * so adapt the exponent accordingly */
352 sc_val_from_ulong(1, temp);
353 sc_add(_exp(out_val), temp, _exp(out_val));
355 _desc(out_val).class = NORMAL;
357 /* no further rounding is needed, because rounding overflow means
358 * the carry of the original rounding was propagated all the way
359 * up to the bit left of the radix point. This implies the bits
360 * to the right are all zeros (rounding is +1) */
362 /* check for exponent overflow */
363 sc_val_from_ulong((1 << _desc(out_val).exponent_size) - 1, temp);
364 if (sc_comp(_exp(out_val), temp) != -1) {
365 DEBUGPRINTF(("Exponent overflow!\n"));
366 /* exponent overflow, reaction depends on rounding method:
368 * mode | sign of value | result
369 *--------------------------------------------------------------
370 * TO_NEAREST | + | +inf
372 *--------------------------------------------------------------
373 * TO_POSITIVE | + | +inf
374 * | - | smallest representable value
375 *--------------------------------------------------------------
376 * TO_NEAGTIVE | + | largest representable value
378 *--------------------------------------------------------------
379 * TO_ZERO | + | largest representable value
380 * | - | smallest representable value
381 *--------------------------------------------------------------*/
382 if (_sign(out_val) == 0)
384 /* value is positive */
385 switch (ROUNDING_MODE) {
388 _desc(out_val).class = INF;
393 fc_get_max(_desc(out_val).exponent_size, _desc(out_val).mantissa_size, out_val);
396 /* value is negative */
397 switch (ROUNDING_MODE) {
400 _desc(out_val).class = INF;
405 fc_get_min(_desc(out_val).exponent_size, _desc(out_val).mantissa_size, out_val);
414 * calculate a + b, where a is the value with the bigger exponent
416 static char* _add(const char* a, const char* b, char* result)
424 if (_desc(a).class == NAN) {
425 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
428 if (_desc(b).class == NAN) {
429 if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
433 /* make sure result has a descriptor */
434 if (result != a && result != b)
435 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
437 /* determine if this is an addition or subtraction */
438 sign = _sign(a) ^ _sign(b);
440 /* produce nan on inf - inf */
441 if (sign && (_desc(a).class == INF) && (_desc(b).class == INF))
442 return fc_get_qnan(_desc(a).exponent_size, _desc(b).mantissa_size, result);
444 temp = alloca(VALUE_SIZE);
445 exp_diff = alloca(VALUE_SIZE);
447 /* get exponent difference */
448 sc_sub(_exp(a), _exp(b), exp_diff);
450 /* initially set sign to be the sign of a, special treatment of subtraction
451 * when exponents are equal is required though.
452 * Also special care about the sign is needed when the mantissas are equal
454 if (sign && sc_val_to_long(exp_diff) == 0) {
455 switch (sc_comp(_mant(a), _mant(b))) {
457 if (_sign(a)) _sign(result) = 1; /* abs(a) is bigger and a is negative */
458 else _sign(result) = 0;
461 if (ROUNDING_MODE == FC_TONEGATIVE)
467 if (_sign(b)) _sign(result) = 1; /* abs(b) is bigger and b is negative */
468 else _sign(result) = 0;
471 /* can't be reached */
475 _sign(result) = _sign(a);
478 /* sign has been taken care of, check for special cases */
479 if (_desc(a).class == ZERO) {
480 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
483 if (_desc(b).class == ZERO) {
484 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
488 if (_desc(a).class == INF) {
489 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
492 if (_desc(b).class == INF) {
493 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
497 /* shift the smaller value to the right to align the radix point */
498 /* subnormals have their radix point shifted to the right,
499 * take care of this first */
500 if ((_desc(b).class == SUBNORMAL) && (_desc(a).class != SUBNORMAL))
502 sc_val_from_ulong(1, temp);
503 sc_sub(exp_diff, temp, exp_diff);
506 _shift_right(_mant(b), exp_diff, temp);
507 sticky = sc_had_carry();
511 /* if subtracting a little more than the represented value or adding a little
512 * more than the represented value to a negative value this, in addition to the
513 * still set sticky bit, takes account of the 'little more' */
514 char *temp1 = alloca(CALC_BUFFER_SIZE);
515 sc_val_from_ulong(1, temp1);
516 sc_add(temp, temp1, temp);
520 if (sc_comp(_mant(a), temp) == -1)
521 sc_sub(temp, _mant(a), _mant(result));
523 sc_sub(_mant(a), temp, _mant(result));
525 sc_add(_mant(a), temp, _mant(result));
528 /* _normalize expects a 'normal' radix point, adding two subnormals
529 * results in a subnormal radix point -> shifting before normalizing */
530 if ((_desc(a).class == SUBNORMAL) && (_desc(b).class == SUBNORMAL))
532 sc_val_from_ulong(1, NULL);
533 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
536 /* resulting exponent is the bigger one */
537 memmove(_exp(result), _exp(a), VALUE_SIZE);
539 return _normalize(result, result, sticky);
542 static char* _mul(const char* a, const char* b, char* result)
546 if (_desc(a).class == NAN) {
547 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
550 if (_desc(b).class == NAN) {
551 if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
555 temp = alloca(VALUE_SIZE);
557 if (result != a && result != b)
558 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
560 _sign(result) = _sign(a) ^ _sign(b);
562 /* produce nan on 0 * inf */
563 if (_desc(a).class == ZERO) {
564 if (_desc(b).class == INF)
565 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
567 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
570 if (_desc(b).class == ZERO) {
571 if (_desc(a).class == INF)
572 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
574 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-1);
578 if (_desc(a).class == INF) {
579 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
582 if (_desc(b).class == INF) {
583 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-1);
587 /* exp = exp(a) + exp(b) - excess */
588 sc_add(_exp(a), _exp(b), _exp(result));
590 sc_val_from_ulong((1<<_desc(a).exponent_size)/2-1, temp);
591 sc_sub(_exp(result), temp, _exp(result));
593 /* mixed normal, subnormal values introduce an error of 1, correct it */
594 if ((_desc(a).class == SUBNORMAL) ^ (_desc(b).class == SUBNORMAL))
596 sc_val_from_ulong(1, temp);
597 sc_add(_exp(result), temp, _exp(result));
600 sc_mul(_mant(a), _mant(b), _mant(result));
602 /* realign result: after a multiplication the digits right of the radix
603 * point are the sum of the factors' digits after the radix point. As all
604 * values are normalized they both have the same amount of these digits,
605 * which has to be restored by proper shifting
606 * +2 because of the two rounding bits */
607 sc_val_from_ulong(2 + _desc(result).mantissa_size, temp);
609 _shift_right(_mant(result), temp, _mant(result));
611 return _normalize(result, result, sc_had_carry());
614 static char* _div(const char* a, const char* b, char* result)
616 char *temp, *dividend;
618 if (_desc(a).class == NAN) {
619 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
622 if (_desc(b).class == NAN) {
623 if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
627 temp = alloca(VALUE_SIZE);
628 dividend = alloca(VALUE_SIZE);
630 if (result != a && result != b)
631 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
633 _sign(result) = _sign(a) ^ _sign(b);
635 /* produce nan on 0/0 and inf/inf */
636 if (_desc(a).class == ZERO) {
637 if (_desc(b).class == ZERO)
639 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
642 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
646 if (_desc(b).class == INF) {
647 if (_desc(a).class == INF)
649 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
652 sc_val_from_ulong(0, NULL);
653 _save_result(_exp(result));
654 _save_result(_mant(result));
655 _desc(result).class = ZERO;
660 if (_desc(a).class == INF) {
662 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
665 if (_desc(b).class == ZERO) {
666 /* division by zero */
668 fc_get_minusinf(_desc(a).exponent_size, _desc(a).mantissa_size, result);
670 fc_get_plusinf(_desc(a).exponent_size, _desc(a).mantissa_size, result);
674 /* exp = exp(a) - exp(b) + excess - 1*/
675 sc_sub(_exp(a), _exp(b), _exp(result));
676 sc_val_from_ulong((1 << _desc(a).exponent_size)/2-2, temp);
677 sc_add(_exp(result), temp, _exp(result));
679 /* mixed normal, subnormal values introduce an error of 1, correct it */
680 if ((_desc(a).class == SUBNORMAL) ^ (_desc(b).class == SUBNORMAL))
682 sc_val_from_ulong(1, temp);
683 sc_add(_exp(result), temp, _exp(result));
686 /* mant(res) = mant(a) / 1/2mant(b) */
687 /* to gain more bits of precision in the result the dividend could be
688 * shifted left, as this operation does not loose bits. This would not
689 * fit into the integer precision, but due to the rounding bits (which
690 * are always zero because the values are all normalized) the divisor
691 * can be shifted right instead to achieve the same result */
692 sc_val_from_ulong(2 + _desc(result).mantissa_size, temp);
694 _shift_left(_mant(a), temp, dividend);
697 char *divisor = alloca(CALC_BUFFER_SIZE);
698 sc_val_from_ulong(1, divisor);
699 _shift_right(_mant(b), divisor, divisor);
700 sc_div(dividend, divisor, _mant(result));
703 return _normalize(result, result, sc_had_carry());
706 void _power_of_ten(int exp, descriptor_t *desc, char *result)
714 /* set new descriptor (else result is supposed to already have one) */
716 memcpy(&_desc(result), desc, sizeof(descriptor_t));
718 build = alloca(VALUE_SIZE);
719 temp = alloca(VALUE_SIZE);
721 sc_val_from_ulong((1 << _desc(result).exponent_size)/2-1, _exp(result));
725 /* temp is value of ten now */
726 sc_val_from_ulong(10, NULL);
729 for (exp--; exp > 0; exp--) {
731 sc_mul(build, temp, NULL);
735 /* temp is amount of leftshift needed to put the value left of the radix point */
736 sc_val_from_ulong(_desc(result).mantissa_size + 2, temp);
738 _shift_left(build, temp, _mant(result));
740 _normalize(result, result, 0);
744 static char* _trunc(const char *a, char *result)
746 /* when exponent == 0 all bits left of the radix point
747 * are the integral part of the value. For 15bit exp_size
748 * this would require a leftshift of max. 16383 bits which
750 * But it is enough to ensure that no bit right of the radix
751 * point remains set. This restricts the interesting
752 * exponents to the interval [0, mant_size-1].
753 * Outside this interval the truncated value is either 0 or
754 * it is are already truncated */
756 int exp_bias, exp_val;
759 temp = alloca(VALUE_SIZE);
762 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
764 exp_bias = (1<<_desc(a).exponent_size)/2-1;
765 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
768 sc_val_from_ulong(0, NULL);
769 _save_result(_exp(result));
770 _save_result(_mant(result));
771 _desc(result).class = ZERO;
776 if (exp_val > _desc(a).mantissa_size) {
778 memcpy(result, a, CALC_BUFFER_SIZE);
783 /* set up a proper mask to delete all bits right of the
784 * radix point if the mantissa had been shifted until exp == 0 */
785 sc_max_from_bits(1 + exp_val, 0, temp);
786 sc_val_from_long(_desc(a).mantissa_size - exp_val + 2, NULL);
787 _shift_left(temp, sc_get_buffer(), temp);
789 /* and the mask and return the result */
790 sc_and(_mant(a), temp, _mant(result));
792 if (a != result) memcpy(_exp(result), _exp(a), VALUE_SIZE);
798 * This does value sanity checking(or should do it), sets up any prerequisites,
799 * calls the proper internal functions, clears up and returns
801 char* _calc(const char *a, const char *b, int opcode, char *result)
804 #ifdef FLTCALC_TRACE_CALC
807 buffer = alloca(100);
810 if (result == NULL) result = calc_buffer;
812 TRACEPRINTF(("%s ", fc_print(a, buffer, 100, FC_PACKED)));
816 /* make the value with the bigger exponent the first one */
817 TRACEPRINTF(("+ %s ", fc_print(b, buffer, 100, FC_PACKED)));
818 if (sc_comp(_exp(a), _exp(b)) == -1)
824 TRACEPRINTF(("- %s ", fc_print(b, buffer, 100, FC_PACKED)));
825 temp = alloca(CALC_BUFFER_SIZE);
826 memcpy(temp, b, CALC_BUFFER_SIZE);
827 _sign(temp) = !_sign(b);
828 if (sc_comp(_exp(a), _exp(temp)) == -1)
829 _add(temp, a, result);
831 _add(a, temp, result);
834 TRACEPRINTF(("* %s ", fc_print(b, buffer, 100, FC_PACKED)));
838 TRACEPRINTF(("/ %s ", fc_print(b, buffer, 100, FC_PACKED)));
842 TRACEPRINTF(("negated "));
843 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
844 _sign(result) = !_sign(a);
853 TRACEPRINTF(("= %s\n", fc_print(result, buffer, 100, FC_PACKED)));
858 * functions defined in fltcalc.h
860 const void *fc_get_buffer(void)
865 const int fc_get_buffer_length(void)
867 return CALC_BUFFER_SIZE;
870 char* fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, char *result)
883 int exp_int, hsb, state;
888 char *mant_str, *exp_val, *power_val;
890 if (result == NULL) result = calc_buffer;
892 exp_val = alloca(VALUE_SIZE);
893 power_val = alloca(CALC_BUFFER_SIZE);
894 mant_str = alloca((len)?(len):(strlen(str)));
896 _desc(result).exponent_size = exp_size;
897 _desc(result).mantissa_size = mant_size;
898 _desc(result).class = NORMAL;
905 while (len == 0 || str-old_str < len)
922 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
929 state = RIGHT_OF_DOT;
940 _fail_char(old_str, len, str - old_str);
946 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
947 mant_str[pos++] = *(str++);
951 state = RIGHT_OF_DOT;
962 mant_str[pos] = '\0';
966 _fail_char(old_str, len, str - old_str);
972 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
973 mant_str[pos++] = *(str++);
984 mant_str[pos] = '\0';
988 _fail_char(old_str, len, str - old_str);
998 if (*(str-1) != 'e' && *(str-1) != 'E') _fail_char(old_str, len, str - old_str);
1002 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
1003 mant_str[pos] = '\0';
1010 _fail_char(old_str, len, str - old_str);
1016 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
1021 case '\0': goto done;
1024 _fail_char(old_str, len, str - old_str);
1030 sc_val_from_str(mant_str, strlen(mant_str), _mant(result));
1032 /* shift to put value left of radix point */
1033 sc_val_from_ulong(mant_size + 2, exp_val);
1035 _shift_left(_mant(result), exp_val, _mant(result));
1037 sc_val_from_ulong((1 << exp_size)/2-1, _exp(result));
1039 _normalize(result, result, 0);
1041 if (state == EXPONENT) {
1042 exp_int -= atoi(str-pos);
1045 _power_of_ten(exp_int, &_desc(result), power_val);
1047 _div(result, power_val, result);
1052 /* XXX excuse of an implementation to make things work */
1054 #ifdef HAVE_LONG_DOUBLE
1055 val = strtold(str, NULL);
1057 val = strtod(str, NULL);
1060 DEBUGPRINTF(("val_from_str(%s)\n", str));
1061 return fc_val_from_float(val, exp_size, mant_size, result);
1065 char* fc_val_from_float(LLDBL l, char exp_size, char mant_size, char* result)
1068 int bias_res, bias_val, mant_val;
1070 UINT32 sign, exponent, mantissa0, mantissa1;
1073 bias_res = ((1<<exp_size)/2-1);
1075 #ifdef HAVE_LONG_DOUBLE
1078 sign = (srcval.val.high & 0x00008000) != 0;
1079 exponent = (srcval.val.high & 0x00007FFF) ;
1080 mantissa0 = srcval.val.mid;
1081 mantissa1 = srcval.val.low;
1082 #else /* no long double */
1085 sign = (srcval.val.high & 0x80000000) != 0;
1086 exponent = (srcval.val.high & 0x7FF00000) >> 20;
1087 mantissa0 = srcval.val.high & 0x000FFFFF;
1088 mantissa1 = srcval.val.low;
1091 #ifdef HAVE_LONG_DOUBLE
1092 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));
1093 DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1095 TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
1096 DEBUGPRINTF(("(%d-%.3X-%.5X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1099 if (result == NULL) result = calc_buffer;
1100 temp = alloca(VALUE_SIZE);
1102 _desc(result).exponent_size = exp_size;
1103 _desc(result).mantissa_size = mant_size;
1106 _sign(result) = sign;
1108 /* sign and flag suffice to identify nan or inf, no exponent/mantissa
1109 * encoding is needed. the function can return immediately in these cases */
1111 _desc(result).class = NAN;
1112 TRACEPRINTF(("val_from_float resulted in NAN\n"));
1115 else if (isinf(l)) {
1116 _desc(result).class = INF;
1117 TRACEPRINTF(("val_from_float resulted in %sINF\n", (_sign(result)==1)?"-":""));
1121 /* build exponent, because input and output exponent and mantissa sizes may differ
1122 * this looks more complicated than it is: unbiased input exponent + output bias,
1123 * minus the mantissa difference which is added again later when the output float
1124 * becomes normalized */
1125 #ifdef HAVE_EXPLICIT_ONE
1126 sc_val_from_long((exponent-bias_val+bias_res)-(mant_val-mant_size-1), _exp(result));
1128 sc_val_from_long((exponent-bias_val+bias_res)-(mant_val-mant_size), _exp(result));
1131 /* build mantissa representation */
1132 #ifndef HAVE_EXPLICIT_ONE
1135 /* insert the hidden bit */
1136 sc_val_from_ulong(1, temp);
1137 sc_val_from_ulong(mant_val + 2, NULL);
1138 _shift_left(temp, sc_get_buffer(), NULL);
1143 sc_val_from_ulong(0, NULL);
1146 _save_result(_mant(result));
1148 /* bits from the upper word */
1149 sc_val_from_ulong(mantissa0, temp);
1150 sc_val_from_ulong(34, NULL);
1151 _shift_left(temp, sc_get_buffer(), temp);
1152 sc_or(_mant(result), temp, _mant(result));
1154 /* bits from the lower word */
1155 sc_val_from_ulong(mantissa1, temp);
1156 sc_val_from_ulong(2, NULL);
1157 _shift_left(temp, sc_get_buffer(), temp);
1158 sc_or(_mant(result), temp, _mant(result));
1160 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
1161 * origin one to the left */
1164 sc_val_from_ulong(1, NULL);
1165 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1168 _normalize(result, result, 0);
1170 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, CALC_BUFFER_SIZE, FC_PACKED)));
1175 LLDBL fc_val_to_float(const void *val)
1189 #ifdef HAVE_LONG_DOUBLE
1190 char result_exponent = 15;
1191 char result_mantissa = 64;
1193 char result_exponent = 11;
1194 char result_mantissa = 52;
1197 temp = alloca(CALC_BUFFER_SIZE);
1198 #ifdef HAVE_EXPLICIT_ONE
1199 value = fc_cast(val, result_exponent, result_mantissa-1, temp);
1201 value = fc_cast(val, result_exponent, result_mantissa, temp);
1204 sign = _sign(value);
1206 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
1207 * lead to wrong results */
1208 exponent = sc_val_to_long(_exp(value)) ;
1210 sc_val_from_ulong(2, NULL);
1211 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1216 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1217 mantissa1 |= sc_sub_bits(_mant(value), result_mantissa, byte_offset) << (byte_offset<<3);
1219 for (; (byte_offset<<3) < result_mantissa; byte_offset++)
1220 mantissa0 |= sc_sub_bits(_mant(value), result_mantissa, byte_offset) << ((byte_offset-4)<<3);
1222 #ifndef HAVE_LONG_DOUBLE
1223 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1226 #ifdef HAVE_LONG_DOUBLE
1227 buildval.val.high = sign << 15;
1228 buildval.val.high |= exponent;
1229 buildval.val.mid = mantissa0;
1230 buildval.val.low = mantissa1;
1231 #else /* no long double */
1232 buildval.val.high = sign << 31;
1233 buildval.val.high |= exponent << 20;
1234 buildval.val.high |= mantissa0;
1235 buildval.val.low = mantissa1;
1238 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1242 char* fc_cast(const void *val, char exp_size, char mant_size, char *result)
1244 const char *value = (const char*) val;
1246 int exp_offset, val_bias, res_bias;
1248 if (result == NULL) result = calc_buffer;
1249 temp = alloca(VALUE_SIZE);
1251 if (_desc(value).exponent_size == exp_size && _desc(value).mantissa_size == mant_size)
1253 if (value != result) memcpy(result, value, CALC_BUFFER_SIZE);
1257 /* set the descriptor of the new value */
1258 _desc(result).exponent_size = exp_size;
1259 _desc(result).mantissa_size = mant_size;
1260 _desc(result).class = _desc(value).class;
1262 _sign(result) = _sign(value);
1264 /* when the mantissa sizes differ normalizing has to shift to align it.
1265 * this would change the exponent, which is unwanted. So calculate this
1266 * offset and add it */
1267 val_bias = (1<<_desc(value).exponent_size)/2-1;
1268 res_bias = (1<<exp_size)/2-1;
1270 exp_offset = (res_bias - val_bias) - (_desc(value).mantissa_size - mant_size);
1271 sc_val_from_long(exp_offset, temp);
1272 sc_add(_exp(value), temp, _exp(result));
1274 /* _normalize expects normalized radix point */
1275 if (_desc(val).class == SUBNORMAL) {
1276 sc_val_from_ulong(1, NULL);
1277 _shift_left(_mant(val), sc_get_buffer(), _mant(result));
1278 } else if (value != result) {
1279 memcpy(_mant(result), _mant(value), VALUE_SIZE);
1281 memmove(_mant(result), _mant(value), VALUE_SIZE);
1284 _normalize(result, result, 0);
1285 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, VALUE_SIZE, FC_PACKED)));
1289 char* fc_get_max(unsigned int exponent_size, unsigned int mantissa_size, char* result)
1291 if (result == NULL) result = calc_buffer;
1293 _desc(result).exponent_size = exponent_size;
1294 _desc(result).mantissa_size = mantissa_size;
1295 _desc(result).class = NORMAL;
1299 sc_val_from_ulong((1<<exponent_size) - 2, _exp(result));
1301 sc_max_from_bits(mantissa_size + 1, 0, _mant(result));
1302 sc_val_from_ulong(2, NULL);
1303 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1308 char* fc_get_min(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1310 if (result == NULL) result = calc_buffer;
1312 fc_get_max(exponent_size, mantissa_size, result);
1318 char* fc_get_snan(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1320 if (result == NULL) result = calc_buffer;
1322 _desc(result).exponent_size = exponent_size;
1323 _desc(result).mantissa_size = mantissa_size;
1324 _desc(result).class = NAN;
1328 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1330 /* signalling nan has non-zero mantissa with msb not set */
1331 sc_val_from_ulong(1, _mant(result));
1336 char* fc_get_qnan(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1338 if (result == NULL) result = calc_buffer;
1340 _desc(result).exponent_size = exponent_size;
1341 _desc(result).mantissa_size = mantissa_size;
1342 _desc(result).class = NAN;
1346 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1348 /* quiet nan has the msb of the mantissa set, so shift one there */
1349 sc_val_from_ulong(1, _mant(result));
1350 /* mantissa_size >+< 1 because of two extra rounding bits */
1351 sc_val_from_ulong(mantissa_size + 1, NULL);
1352 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1357 char* fc_get_plusinf(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1359 if (result == NULL) result = calc_buffer;
1361 _desc(result).exponent_size = exponent_size;
1362 _desc(result).mantissa_size = mantissa_size;
1363 _desc(result).class = NORMAL;
1367 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1369 sc_val_from_ulong(0, _mant(result));
1374 char* fc_get_minusinf(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1376 if (result == NULL) result = calc_buffer;
1378 fc_get_plusinf(exponent_size, mantissa_size, result);
1384 int fc_comp(const void *a, const void *b)
1386 const char *val_a = (const char*)a;
1387 const char *val_b = (const char*)b;
1390 if (_desc(val_a).class == NAN || _desc(val_b).class == NAN) return 2;
1391 /* zero is equal independent of sign */
1392 if ((_desc(val_a).class == ZERO) && (_desc(val_b).class == ZERO)) return 0;
1393 /* different signs make compare easy */
1394 if (_sign(val_a) != _sign(val_b)) return (_sign(val_a)==0)?(1):(-1);
1395 /* both infinity means equality */
1396 if ((_desc(val_a).class == INF) && (_desc(val_b).class == INF)) return 0;
1397 /* infinity is bigger than the rest */
1398 if (_desc(val_a).class == INF) return _sign(val_a)?(-1):(1);
1399 if (_desc(val_b).class == INF) return _sign(val_b)?(1):(-1);
1401 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1407 return sc_comp(_mant(val_a), _mant(val_b));
1413 int fc_is_zero(const void *a)
1415 return _desc((const char*)a).class == ZERO;
1418 int fc_is_negative(const void *a)
1420 return _sign((const char*)a);
1423 int fc_is_inf(const void *a)
1425 return _desc(a).class == INF;
1428 int fc_is_nan(const void *a)
1430 return _desc(a).class == NAN;
1433 int fc_is_subnormal(const void *a)
1435 return _desc(a).class == SUBNORMAL;
1438 char *fc_print(const void *a, char *buf, int buflen, unsigned base)
1443 val = (const char*)a;
1445 mul_1 = alloca(CALC_BUFFER_SIZE);
1449 switch (_desc(val).class) {
1451 if (buflen >= 8+_sign(val)) sprintf(buf, "%sINFINITY", _sign(val)?"-":"");
1452 else snprintf(buf, buflen, "%sINF", _sign(val)?"-":NULL);
1455 snprintf(buf, buflen, "NAN");
1458 snprintf(buf, buflen, "0.0");
1461 /* XXX to be implemented */
1462 #ifdef HAVE_LONG_DOUBLE
1463 /* XXX 30 is arbitrary */
1464 snprintf(buf, buflen, "%.30LE", fc_val_to_float(val));
1466 snprintf(buf, buflen, "%.18E", fc_val_to_float(val));
1472 switch (_desc(val).class) {
1474 if (buflen >= 8+_sign(val)) sprintf(buf, "%sINFINITY", _sign(val)?"-":"");
1475 else snprintf(buf, buflen, "%sINF", _sign(val)?"-":NULL);
1478 snprintf(buf, buflen, "NAN");
1481 snprintf(buf, buflen, "0.0");
1484 #ifdef HAVE_LONG_DOUBLE
1485 snprintf(buf, buflen, "%LA", fc_val_to_float(val));
1487 snprintf(buf, buflen, "%A", fc_val_to_float(val));
1494 snprintf(buf, buflen, "%s", sc_print(_pack(val, mul_1), VALUE_SIZE*4, SC_HEX));
1500 unsigned char fc_sub_bits(const void *value, unsigned num_bits, unsigned byte_ofs)
1502 /* this is used to cache the packed version of the value */
1503 static char *pack = NULL;
1505 if (pack == NULL) pack = malloc(VALUE_SIZE);
1508 _pack((const char*)value, pack);
1510 return sc_sub_bits(pack, num_bits, byte_ofs);
1513 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode)
1515 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1516 ROUNDING_MODE = mode;
1518 return ROUNDING_MODE;
1521 fc_rounding_mode_t fc_get_rounding_mode(void)
1523 return ROUNDING_MODE;
1526 void init_fltcalc(int precision)
1528 if (calc_buffer == NULL) {
1529 /* does nothing if already init */
1530 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1532 init_strcalc(precision + 4);
1534 /* needs additionally two bits to round, a bit as explicit 1., and one for
1535 * addition overflow */
1536 max_precision = sc_get_precision() - 4;
1537 if (max_precision < precision)
1538 printf("WARING: not enough precision available, using %d\n", max_precision);
1540 ROUNDING_MODE = FC_TONEAREST;
1541 VALUE_SIZE = sc_get_buffer_length();
1543 EXPONENT_POS = SIGN_POS + sizeof(char);
1544 MANTISSA_POS = EXPONENT_POS + VALUE_SIZE;
1545 DESCRIPTOR_POS = MANTISSA_POS + VALUE_SIZE;
1546 CALC_BUFFER_SIZE = DESCRIPTOR_POS + sizeof(descriptor_t);
1548 calc_buffer = malloc(CALC_BUFFER_SIZE);
1549 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));
1550 #ifdef HAVE_LONG_DOUBLE
1551 DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
1553 DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
1555 #ifdef WORDS_BIGENDIAN
1556 DEBUGPRINTF(("\tWord order is big endian\n\n"));
1558 DEBUGPRINTF(("\tWord order is little endian\n\n"));
1563 /* definition of interface functions */