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.
22 #include <math.h> /* need isnan() and isinf() (will be changed)*/
23 /* undef some reused constants defined by math.h */
37 typedef uint32_t UINT32;
39 #ifdef HAVE_LONG_DOUBLE
40 #ifdef WORDS_BIGENDIAN
47 volatile long double d;
56 volatile long double d;
60 #ifdef WORDS_BIGENDIAN
97 #define CLEAR_BUFFER(buffer) memset(buffer, 0, CALC_BUFFER_SIZE)
99 /* because variable sized structs are impossible, the internal
100 * value is represented as a pseudo-struct char array, addressed
103 * char sign; // 0 for positive, 1 for negative
104 * char exp[VALUE_SIZE];
105 * char mant[VALUE_SIZE];
109 #define _sign(a) (((char*)a)[SIGN_POS])
110 #define _exp(a) (&((char*)a)[EXPONENT_POS])
111 #define _mant(a) (&((char*)a)[MANTISSA_POS])
112 #define _desc(a) (*(descriptor_t *)&((char*)a)[DESCRIPTOR_POS])
114 #define _save_result(x) memcpy((x), sc_get_buffer(), VALUE_SIZE)
115 #define _shift_right(x, y, b) sc_shr((x), (y), VALUE_SIZE*4, 0, (b))
116 #define _shift_left(x, y, b) sc_shl((x), (y), VALUE_SIZE*4, 0, (b))
118 #define FC_DEFINE1(code) char* fc_##code(const void *a, void *result) \
120 return _calc((const char*)a, NULL, FC_##code, (char*)result); \
123 #define FC_DEFINE2(code) char* fc_##code(const void *a, const void *b, void *result) \
125 return _calc((const char*)a, (const char*)b, FC_##code, (char*)result); \
128 #define FUNC_PTR(code) fc_##code
131 # define DEBUGPRINTF(x) printf x
133 # define DEBUGPRINTF(x) ((void)0)
136 #if FLTCALC_TRACE_CALC
137 # define TRACEPRINTF(x) printf x
139 # define TRACEPRINTF(x) ((void)0)
142 static char *calc_buffer = NULL;
144 static fc_rounding_mode_t ROUNDING_MODE;
146 static int CALC_BUFFER_SIZE;
147 static int VALUE_SIZE;
149 static int EXPONENT_POS;
150 static int MANTISSA_POS;
151 static int DESCRIPTOR_POS;
153 static int max_precision;
158 static void _fail_char(const char *str, unsigned int len, int pos)
161 printf("ERROR: Unexpected character '%c'\n", *(str + pos));
163 printf("ERROR: Unexpected end of string\n");
164 while (len-- && *str) printf("%c", *str++); printf("\n");
165 while (pos--) printf(" "); printf("^\n");
166 /* the front end has to to check constant strings */
171 /* pack machine-like */
172 static char* _pack(const char *int_float, char *packed)
178 temp = alloca(VALUE_SIZE);
179 shift_val = alloca(VALUE_SIZE);
181 switch (_desc(int_float).class) {
183 val_buffer = alloca(CALC_BUFFER_SIZE);
184 fc_get_qnan(_desc(int_float).exponent_size, _desc(int_float).mantissa_size, val_buffer);
185 int_float = val_buffer;
189 val_buffer = alloca(CALC_BUFFER_SIZE);
190 fc_get_plusinf(_desc(int_float).exponent_size, _desc(int_float).mantissa_size, val_buffer);
191 _sign(val_buffer) = _sign(int_float);
192 int_float = val_buffer;
199 sc_val_from_ulong(_sign(int_float), temp);
201 sc_val_from_ulong(_desc(int_float).exponent_size + _desc(int_float).mantissa_size, NULL);
202 _shift_left(temp, sc_get_buffer(), packed);
204 /* extract exponent */
205 sc_val_from_ulong(_desc(int_float).mantissa_size, shift_val);
207 _shift_left(_exp(int_float), shift_val, temp);
209 sc_or(temp, packed, packed);
211 /* extract mantissa */
212 /* remove 2 rounding bits */
213 sc_val_from_ulong(2, shift_val);
214 _shift_right(_mant(int_float), shift_val, temp);
216 /* remove leading 1 (or 0 if denormalized) */
217 sc_max_from_bits(_desc(int_float).mantissa_size, 0, shift_val); /* all mantissa bits are 1's */
218 sc_and(temp, shift_val, temp);
221 sc_or(temp, packed, packed);
226 char* _normalize(const char *in_val, char *out_val, int sticky)
229 char lsb, guard, round, round_dir = 0;
232 temp = alloca(VALUE_SIZE);
234 /* +2: save two rounding bits at the end */
235 hsb = 2 + _desc(in_val).mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
237 if (in_val != out_val)
239 _sign(out_val) = _sign(in_val);
240 memcpy(&_desc(out_val), &_desc(in_val), sizeof(descriptor_t));
243 _desc(out_val).class = NORMAL;
245 /* mantissa all zeroes, so zero exponent (because of explicit one)*/
246 if (hsb == 2 + _desc(in_val).mantissa_size)
248 sc_val_from_ulong(0, _exp(out_val));
252 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
256 sc_val_from_ulong(-hsb-1, temp);
258 _shift_right(_mant(in_val), temp, _mant(out_val));
260 /* remember if some bits were shifted away */
261 if (!sticky) sticky = sc_had_carry();
263 sc_add(_exp(in_val), temp, _exp(out_val));
268 sc_val_from_ulong(hsb+1, temp);
270 _shift_left(_mant(in_val), temp, _mant(out_val));
272 sc_sub(_exp(in_val), temp, _exp(out_val));
275 /* check for exponent underflow */
276 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
277 DEBUGPRINTF(("Exponent underflow!\n"));
278 /* exponent underflow */
279 /* shift the mantissa right to have a zero exponent */
280 sc_val_from_ulong(1, temp);
281 sc_sub(temp, _exp(out_val), NULL);
283 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
284 if (!sticky) sticky = sc_had_carry();
285 /* denormalized means exponent of zero */
286 sc_val_from_ulong(0, _exp(out_val));
288 _desc(out_val).class = SUBNORMAL;
291 /* perform rounding by adding a value that clears the guard bit and the round bit
292 * and either causes a carry to round up or not */
293 /* get the last 3 bits of the value */
294 lsb = sc_sub_bits(_mant(out_val), _desc(out_val).mantissa_size + 2, 0) & 0x7;
295 guard = (lsb&0x2)>>1;
298 switch (ROUNDING_MODE)
301 /* round to nearest representable value, if in doubt choose the version
303 round_dir = guard && (sticky || round || lsb>>2);
306 /* if positive: round to one if the exact value is bigger, else to zero */
307 round_dir = (!_sign(out_val) && (guard || round || sticky));
310 /* if negative: round to one if the exact value is bigger, else to zero */
311 round_dir = (_sign(out_val) && (guard || round || sticky));
314 /* always round to 0 (chopping mode) */
318 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"));
322 guard = (round^guard)<<1;
323 lsb = !(round || guard)<<2 | guard | round;
327 lsb = -((guard<<1) | round);
330 /* add the rounded value */
332 sc_val_from_long(lsb, temp);
333 sc_add(_mant(out_val), temp, _mant(out_val));
336 /* could have rounded down to zero */
337 if (sc_is_zero(_mant(out_val)) && (_desc(out_val).class == SUBNORMAL))
338 _desc(out_val).class = ZERO;
340 /* check for rounding overflow */
341 hsb = 2 + _desc(out_val).mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
342 if ((_desc(out_val).class != SUBNORMAL) && (hsb < -1))
344 sc_val_from_ulong(1, temp);
345 _shift_right(_mant(out_val), temp, _mant(out_val));
347 sc_add(_exp(out_val), temp, _exp(out_val));
349 else if ((_desc(out_val).class == SUBNORMAL) && (hsb == -1))
351 /* overflow caused the matissa to be normal again,
352 * so adapt the exponent accordingly */
353 sc_val_from_ulong(1, temp);
354 sc_add(_exp(out_val), temp, _exp(out_val));
356 _desc(out_val).class = NORMAL;
358 /* no further rounding is needed, because rounding overflow means
359 * the carry of the original rounding was propagated all the way
360 * up to the bit left of the radix point. This implies the bits
361 * to the right are all zeros (rounding is +1) */
363 /* check for exponent overflow */
364 sc_val_from_ulong((1 << _desc(out_val).exponent_size) - 1, temp);
365 if (sc_comp(_exp(out_val), temp) != -1) {
366 DEBUGPRINTF(("Exponent overflow!\n"));
367 /* exponent overflow, reaction depends on rounding method:
369 * mode | sign of value | result
370 *--------------------------------------------------------------
371 * TO_NEAREST | + | +inf
373 *--------------------------------------------------------------
374 * TO_POSITIVE | + | +inf
375 * | - | smallest representable value
376 *--------------------------------------------------------------
377 * TO_NEAGTIVE | + | largest representable value
379 *--------------------------------------------------------------
380 * TO_ZERO | + | largest representable value
381 * | - | smallest representable value
382 *--------------------------------------------------------------*/
383 if (_sign(out_val) == 0)
385 /* value is positive */
386 switch (ROUNDING_MODE) {
389 _desc(out_val).class = INF;
394 fc_get_max(_desc(out_val).exponent_size, _desc(out_val).mantissa_size, out_val);
397 /* value is negative */
398 switch (ROUNDING_MODE) {
401 _desc(out_val).class = INF;
406 fc_get_min(_desc(out_val).exponent_size, _desc(out_val).mantissa_size, out_val);
415 * calculate a + b, where a is the value with the bigger exponent
417 static char* _add(const char* a, const char* b, char* result)
425 if (_desc(a).class == NAN) {
426 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
429 if (_desc(b).class == NAN) {
430 if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
434 /* make sure result has a descriptor */
435 if (result != a && result != b)
436 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
438 /* determine if this is an addition or subtraction */
439 sign = _sign(a) ^ _sign(b);
441 /* produce nan on inf - inf */
442 if (sign && (_desc(a).class == INF) && (_desc(b).class == INF))
443 return fc_get_qnan(_desc(a).exponent_size, _desc(b).mantissa_size, result);
445 temp = alloca(VALUE_SIZE);
446 exp_diff = alloca(VALUE_SIZE);
448 /* get exponent difference */
449 sc_sub(_exp(a), _exp(b), exp_diff);
451 /* initially set sign to be the sign of a, special treatment of subtraction
452 * when exponents are equal is required though.
453 * Also special care about the sign is needed when the mantissas are equal
455 if (sign && sc_val_to_long(exp_diff) == 0) {
456 switch (sc_comp(_mant(a), _mant(b))) {
458 if (_sign(a)) _sign(result) = 1; /* abs(a) is bigger and a is negative */
459 else _sign(result) = 0;
462 if (ROUNDING_MODE == FC_TONEGATIVE)
468 if (_sign(b)) _sign(result) = 1; /* abs(b) is bigger and b is negative */
469 else _sign(result) = 0;
472 /* can't be reached */
476 _sign(result) = _sign(a);
479 /* sign has been taken care of, check for special cases */
480 if (_desc(a).class == ZERO) {
481 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
484 if (_desc(b).class == ZERO) {
485 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
489 if (_desc(a).class == INF) {
490 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
493 if (_desc(b).class == INF) {
494 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-SIGN_POS-1);
498 /* shift the smaller value to the right to align the radix point */
499 /* subnormals have their radix point shifted to the right,
500 * take care of this first */
501 if ((_desc(b).class == SUBNORMAL) && (_desc(a).class != SUBNORMAL))
503 sc_val_from_ulong(1, temp);
504 sc_sub(exp_diff, temp, exp_diff);
507 _shift_right(_mant(b), exp_diff, temp);
508 sticky = sc_had_carry();
512 /* if subtracting a little more than the represented value or adding a little
513 * more than the represented value to a negative value this, in addition to the
514 * still set sticky bit, takes account of the 'little more' */
515 char *temp1 = alloca(CALC_BUFFER_SIZE);
516 sc_val_from_ulong(1, temp1);
517 sc_add(temp, temp1, temp);
521 if (sc_comp(_mant(a), temp) == -1)
522 sc_sub(temp, _mant(a), _mant(result));
524 sc_sub(_mant(a), temp, _mant(result));
526 sc_add(_mant(a), temp, _mant(result));
529 /* _normalize expects a 'normal' radix point, adding two subnormals
530 * results in a subnormal radix point -> shifting before normalizing */
531 if ((_desc(a).class == SUBNORMAL) && (_desc(b).class == SUBNORMAL))
533 sc_val_from_ulong(1, NULL);
534 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
537 /* resulting exponent is the bigger one */
538 memmove(_exp(result), _exp(a), VALUE_SIZE);
540 return _normalize(result, result, sticky);
543 static char* _mul(const char* a, const char* b, char* result)
547 if (_desc(a).class == NAN) {
548 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
551 if (_desc(b).class == NAN) {
552 if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
556 temp = alloca(VALUE_SIZE);
558 if (result != a && result != b)
559 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
561 _sign(result) = _sign(a) ^ _sign(b);
563 /* produce nan on 0 * inf */
564 if (_desc(a).class == ZERO) {
565 if (_desc(b).class == INF)
566 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
568 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
571 if (_desc(b).class == ZERO) {
572 if (_desc(a).class == INF)
573 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
575 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-1);
579 if (_desc(a).class == INF) {
580 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
583 if (_desc(b).class == INF) {
584 if (b != result) memcpy(result+SIGN_POS+1, b+SIGN_POS+1, CALC_BUFFER_SIZE-1);
588 /* exp = exp(a) + exp(b) - excess */
589 sc_add(_exp(a), _exp(b), _exp(result));
591 sc_val_from_ulong((1<<_desc(a).exponent_size)/2-1, temp);
592 sc_sub(_exp(result), temp, _exp(result));
594 /* mixed normal, subnormal values introduce an error of 1, correct it */
595 if ((_desc(a).class == SUBNORMAL) ^ (_desc(b).class == SUBNORMAL))
597 sc_val_from_ulong(1, temp);
598 sc_add(_exp(result), temp, _exp(result));
601 sc_mul(_mant(a), _mant(b), _mant(result));
603 /* realign result: after a multiplication the digits right of the radix
604 * point are the sum of the factors' digits after the radix point. As all
605 * values are normalized they both have the same amount of these digits,
606 * which has to be restored by proper shifting
607 * +2 because of the two rounding bits */
608 sc_val_from_ulong(2 + _desc(result).mantissa_size, temp);
610 _shift_right(_mant(result), temp, _mant(result));
612 return _normalize(result, result, sc_had_carry());
615 static char* _div(const char* a, const char* b, char* result)
617 char *temp, *dividend;
619 if (_desc(a).class == NAN) {
620 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
623 if (_desc(b).class == NAN) {
624 if (b != result) memcpy(result, b, CALC_BUFFER_SIZE);
628 temp = alloca(VALUE_SIZE);
629 dividend = alloca(VALUE_SIZE);
631 if (result != a && result != b)
632 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
634 _sign(result) = _sign(a) ^ _sign(b);
636 /* produce nan on 0/0 and inf/inf */
637 if (_desc(a).class == ZERO) {
638 if (_desc(b).class == ZERO)
640 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
643 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
647 if (_desc(b).class == INF) {
648 if (_desc(a).class == INF)
650 fc_get_qnan(_desc(a).exponent_size, _desc(a).mantissa_size, result);
653 sc_val_from_ulong(0, NULL);
654 _save_result(_exp(result));
655 _save_result(_mant(result));
656 _desc(result).class = ZERO;
661 if (_desc(a).class == INF) {
663 if (a != result) memcpy(result+SIGN_POS+1, a+SIGN_POS+1, CALC_BUFFER_SIZE-1);
666 if (_desc(b).class == ZERO) {
667 /* division by zero */
669 fc_get_minusinf(_desc(a).exponent_size, _desc(a).mantissa_size, result);
671 fc_get_plusinf(_desc(a).exponent_size, _desc(a).mantissa_size, result);
675 /* exp = exp(a) - exp(b) + excess - 1*/
676 sc_sub(_exp(a), _exp(b), _exp(result));
677 sc_val_from_ulong((1 << _desc(a).exponent_size)/2-2, temp);
678 sc_add(_exp(result), temp, _exp(result));
680 /* mixed normal, subnormal values introduce an error of 1, correct it */
681 if ((_desc(a).class == SUBNORMAL) ^ (_desc(b).class == SUBNORMAL))
683 sc_val_from_ulong(1, temp);
684 sc_add(_exp(result), temp, _exp(result));
687 /* mant(res) = mant(a) / 1/2mant(b) */
688 /* to gain more bits of precision in the result the dividend could be
689 * shifted left, as this operation does not loose bits. This would not
690 * fit into the integer precision, but due to the rounding bits (which
691 * are always zero because the values are all normalized) the divisor
692 * can be shifted right instead to achieve the same result */
693 sc_val_from_ulong(2 + _desc(result).mantissa_size, temp);
695 _shift_left(_mant(a), temp, dividend);
698 char *divisor = alloca(CALC_BUFFER_SIZE);
699 sc_val_from_ulong(1, divisor);
700 _shift_right(_mant(b), divisor, divisor);
701 sc_div(dividend, divisor, _mant(result));
704 return _normalize(result, result, sc_had_carry());
707 void _power_of_ten(int exp, descriptor_t *desc, char *result)
715 /* set new descriptor (else result is supposed to already have one) */
717 memcpy(&_desc(result), desc, sizeof(descriptor_t));
719 build = alloca(VALUE_SIZE);
720 temp = alloca(VALUE_SIZE);
722 sc_val_from_ulong((1 << _desc(result).exponent_size)/2-1, _exp(result));
726 /* temp is value of ten now */
727 sc_val_from_ulong(10, NULL);
730 for (exp--; exp > 0; exp--) {
732 sc_mul(build, temp, NULL);
736 /* temp is amount of leftshift needed to put the value left of the radix point */
737 sc_val_from_ulong(_desc(result).mantissa_size + 2, temp);
739 _shift_left(build, temp, _mant(result));
741 _normalize(result, result, 0);
745 static char* _trunc(const char *a, char *result)
747 /* when exponent == 0 all bits left of the radix point
748 * are the integral part of the value. For 15bit exp_size
749 * this would require a leftshift of max. 16383 bits which
751 * But it is enough to ensure that no bit right of the radix
752 * point remains set. This restricts the interesting
753 * exponents to the interval [0, mant_size-1].
754 * Outside this interval the truncated value is either 0 or
755 * it is are already truncated */
757 int exp_bias, exp_val;
760 temp = alloca(VALUE_SIZE);
763 memcpy(&_desc(result), &_desc(a), sizeof(descriptor_t));
765 exp_bias = (1<<_desc(a).exponent_size)/2-1;
766 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
769 sc_val_from_ulong(0, NULL);
770 _save_result(_exp(result));
771 _save_result(_mant(result));
772 _desc(result).class = ZERO;
777 if (exp_val > _desc(a).mantissa_size) {
779 memcpy(result, a, CALC_BUFFER_SIZE);
784 /* set up a proper mask to delete all bits right of the
785 * radix point if the mantissa had been shifted until exp == 0 */
786 sc_max_from_bits(1 + exp_val, 0, temp);
787 sc_val_from_long(_desc(a).mantissa_size - exp_val + 2, NULL);
788 _shift_left(temp, sc_get_buffer(), temp);
790 /* and the mask and return the result */
791 sc_and(_mant(a), temp, _mant(result));
793 if (a != result) memcpy(_exp(result), _exp(a), VALUE_SIZE);
799 * This does value sanity checking(or should do it), sets up any prerequisites,
800 * calls the proper internal functions, clears up and returns
802 char* _calc(const char *a, const char *b, int opcode, char *result)
805 #ifdef FLTCALC_TRACE_CALC
808 buffer = alloca(100);
811 if (result == NULL) result = calc_buffer;
813 TRACEPRINTF(("%s ", fc_print(a, buffer, 100, FC_PACKED)));
817 /* make the value with the bigger exponent the first one */
818 TRACEPRINTF(("+ %s ", fc_print(b, buffer, 100, FC_PACKED)));
819 if (sc_comp(_exp(a), _exp(b)) == -1)
825 TRACEPRINTF(("- %s ", fc_print(b, buffer, 100, FC_PACKED)));
826 temp = alloca(CALC_BUFFER_SIZE);
827 memcpy(temp, b, CALC_BUFFER_SIZE);
828 _sign(temp) = !_sign(b);
829 if (sc_comp(_exp(a), _exp(temp)) == -1)
830 _add(temp, a, result);
832 _add(a, temp, result);
835 TRACEPRINTF(("* %s ", fc_print(b, buffer, 100, FC_PACKED)));
839 TRACEPRINTF(("/ %s ", fc_print(b, buffer, 100, FC_PACKED)));
843 TRACEPRINTF(("negated "));
844 if (a != result) memcpy(result, a, CALC_BUFFER_SIZE);
845 _sign(result) = !_sign(a);
854 TRACEPRINTF(("= %s\n", fc_print(result, buffer, 100, FC_PACKED)));
859 * functions defined in fltcalc.h
861 const void *fc_get_buffer(void)
866 const int fc_get_buffer_length(void)
868 return CALC_BUFFER_SIZE;
871 char* fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, char *result)
884 int exp_int, hsb, state;
889 char *mant_str, *exp_val, *power_val;
891 if (result == NULL) result = calc_buffer;
893 exp_val = alloca(VALUE_SIZE);
894 power_val = alloca(CALC_BUFFER_SIZE);
895 mant_str = alloca((len)?(len):(strlen(str)));
897 _desc(result).exponent_size = exp_size;
898 _desc(result).mantissa_size = mant_size;
899 _desc(result).class = NORMAL;
906 while (len == 0 || str-old_str < len)
923 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
930 state = RIGHT_OF_DOT;
941 _fail_char(old_str, len, str - old_str);
947 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
948 mant_str[pos++] = *(str++);
952 state = RIGHT_OF_DOT;
963 mant_str[pos] = '\0';
967 _fail_char(old_str, len, str - old_str);
973 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
974 mant_str[pos++] = *(str++);
985 mant_str[pos] = '\0';
989 _fail_char(old_str, len, str - old_str);
999 if (*(str-1) != 'e' && *(str-1) != 'E') _fail_char(old_str, len, str - old_str);
1003 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
1004 mant_str[pos] = '\0';
1011 _fail_char(old_str, len, str - old_str);
1017 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
1022 case '\0': goto done;
1025 _fail_char(old_str, len, str - old_str);
1031 sc_val_from_str(mant_str, strlen(mant_str), _mant(result));
1033 /* shift to put value left of radix point */
1034 sc_val_from_ulong(mant_size + 2, exp_val);
1036 _shift_left(_mant(result), exp_val, _mant(result));
1038 sc_val_from_ulong((1 << exp_size)/2-1, _exp(result));
1040 _normalize(result, result, 0);
1042 if (state == EXPONENT) {
1043 exp_int -= atoi(str-pos);
1046 _power_of_ten(exp_int, &_desc(result), power_val);
1048 _div(result, power_val, result);
1053 /* XXX excuse of an implementation to make things work */
1055 #ifdef HAVE_LONG_DOUBLE
1056 val = strtold(str, NULL);
1058 val = strtod(str, NULL);
1061 DEBUGPRINTF(("val_from_str(%s)\n", str));
1062 return fc_val_from_float(val, exp_size, mant_size, result);
1066 char* fc_val_from_float(LLDBL l, char exp_size, char mant_size, char* result)
1069 int bias_res, bias_val, mant_val;
1071 UINT32 sign, exponent, mantissa0, mantissa1;
1074 bias_res = ((1<<exp_size)/2-1);
1076 #ifdef HAVE_LONG_DOUBLE
1079 sign = (srcval.val.high & 0x00008000) != 0;
1080 exponent = (srcval.val.high & 0x00007FFF) ;
1081 mantissa0 = srcval.val.mid;
1082 mantissa1 = srcval.val.low;
1083 #else /* no long double */
1086 sign = (srcval.val.high & 0x80000000) != 0;
1087 exponent = (srcval.val.high & 0x7FF00000) >> 20;
1088 mantissa0 = srcval.val.high & 0x000FFFFF;
1089 mantissa1 = srcval.val.low;
1092 #ifdef HAVE_LONG_DOUBLE
1093 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));
1094 DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1096 TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
1097 DEBUGPRINTF(("(%d-%.3X-%.5X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1100 if (result == NULL) result = calc_buffer;
1101 temp = alloca(VALUE_SIZE);
1103 _desc(result).exponent_size = exp_size;
1104 _desc(result).mantissa_size = mant_size;
1107 _sign(result) = sign;
1109 /* sign and flag suffice to identify nan or inf, no exponent/mantissa
1110 * encoding is needed. the function can return immediately in these cases */
1112 _desc(result).class = NAN;
1113 TRACEPRINTF(("val_from_float resulted in NAN\n"));
1116 else if (isinf(l)) {
1117 _desc(result).class = INF;
1118 TRACEPRINTF(("val_from_float resulted in %sINF\n", (_sign(result)==1)?"-":""));
1122 /* build exponent, because input and output exponent and mantissa sizes may differ
1123 * this looks more complicated than it is: unbiased input exponent + output bias,
1124 * minus the mantissa difference which is added again later when the output float
1125 * becomes normalized */
1126 #ifdef HAVE_EXPLICIT_ONE
1127 sc_val_from_long((exponent-bias_val+bias_res)-(mant_val-mant_size-1), _exp(result));
1129 sc_val_from_long((exponent-bias_val+bias_res)-(mant_val-mant_size), _exp(result));
1132 /* build mantissa representation */
1133 #ifndef HAVE_EXPLICIT_ONE
1136 /* insert the hidden bit */
1137 sc_val_from_ulong(1, temp);
1138 sc_val_from_ulong(mant_val + 2, NULL);
1139 _shift_left(temp, sc_get_buffer(), NULL);
1144 sc_val_from_ulong(0, NULL);
1147 _save_result(_mant(result));
1149 /* bits from the upper word */
1150 sc_val_from_ulong(mantissa0, temp);
1151 sc_val_from_ulong(34, NULL);
1152 _shift_left(temp, sc_get_buffer(), temp);
1153 sc_or(_mant(result), temp, _mant(result));
1155 /* bits from the lower word */
1156 sc_val_from_ulong(mantissa1, temp);
1157 sc_val_from_ulong(2, NULL);
1158 _shift_left(temp, sc_get_buffer(), temp);
1159 sc_or(_mant(result), temp, _mant(result));
1161 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
1162 * origin one to the left */
1165 sc_val_from_ulong(1, NULL);
1166 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1169 _normalize(result, result, 0);
1171 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, CALC_BUFFER_SIZE, FC_PACKED)));
1176 LLDBL fc_val_to_float(const void *val)
1190 #ifdef HAVE_LONG_DOUBLE
1191 char result_exponent = 15;
1192 char result_mantissa = 64;
1194 char result_exponent = 11;
1195 char result_mantissa = 52;
1198 temp = alloca(CALC_BUFFER_SIZE);
1199 #ifdef HAVE_EXPLICIT_ONE
1200 value = fc_cast(val, result_exponent, result_mantissa-1, temp);
1202 value = fc_cast(val, result_exponent, result_mantissa, temp);
1205 sign = _sign(value);
1207 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
1208 * lead to wrong results */
1209 exponent = sc_val_to_long(_exp(value)) ;
1211 sc_val_from_ulong(2, NULL);
1212 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1217 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1218 mantissa1 |= sc_sub_bits(_mant(value), result_mantissa, byte_offset) << (byte_offset<<3);
1220 for (; (byte_offset<<3) < result_mantissa; byte_offset++)
1221 mantissa0 |= sc_sub_bits(_mant(value), result_mantissa, byte_offset) << ((byte_offset-4)<<3);
1223 #ifndef HAVE_LONG_DOUBLE
1224 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1227 #ifdef HAVE_LONG_DOUBLE
1228 buildval.val.high = sign << 15;
1229 buildval.val.high |= exponent;
1230 buildval.val.mid = mantissa0;
1231 buildval.val.low = mantissa1;
1232 #else /* no long double */
1233 buildval.val.high = sign << 31;
1234 buildval.val.high |= exponent << 20;
1235 buildval.val.high |= mantissa0;
1236 buildval.val.low = mantissa1;
1239 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1243 char* fc_cast(const void *val, char exp_size, char mant_size, char *result)
1245 const char *value = (const char*) val;
1247 int exp_offset, val_bias, res_bias;
1249 if (result == NULL) result = calc_buffer;
1250 temp = alloca(VALUE_SIZE);
1252 if (_desc(value).exponent_size == exp_size && _desc(value).mantissa_size == mant_size)
1254 if (value != result) memcpy(result, value, CALC_BUFFER_SIZE);
1258 /* set the descriptor of the new value */
1259 _desc(result).exponent_size = exp_size;
1260 _desc(result).mantissa_size = mant_size;
1261 _desc(result).class = _desc(value).class;
1263 _sign(result) = _sign(value);
1265 /* when the mantissa sizes differ normalizing has to shift to align it.
1266 * this would change the exponent, which is unwanted. So calculate this
1267 * offset and add it */
1268 val_bias = (1<<_desc(value).exponent_size)/2-1;
1269 res_bias = (1<<exp_size)/2-1;
1271 exp_offset = (res_bias - val_bias) - (_desc(value).mantissa_size - mant_size);
1272 sc_val_from_long(exp_offset, temp);
1273 sc_add(_exp(value), temp, _exp(result));
1275 /* _normalize expects normalized radix point */
1276 if (_desc(val).class == SUBNORMAL) {
1277 sc_val_from_ulong(1, NULL);
1278 _shift_left(_mant(val), sc_get_buffer(), _mant(result));
1279 } else if (value != result) {
1280 memcpy(_mant(result), _mant(value), VALUE_SIZE);
1282 memmove(_mant(result), _mant(value), VALUE_SIZE);
1285 _normalize(result, result, 0);
1286 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, VALUE_SIZE, FC_PACKED)));
1290 char* fc_get_max(unsigned int exponent_size, unsigned int mantissa_size, char* result)
1292 if (result == NULL) result = calc_buffer;
1294 _desc(result).exponent_size = exponent_size;
1295 _desc(result).mantissa_size = mantissa_size;
1296 _desc(result).class = NORMAL;
1300 sc_val_from_ulong((1<<exponent_size) - 2, _exp(result));
1302 sc_max_from_bits(mantissa_size + 1, 0, _mant(result));
1303 sc_val_from_ulong(2, NULL);
1304 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1309 char* fc_get_min(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1311 if (result == NULL) result = calc_buffer;
1313 fc_get_max(exponent_size, mantissa_size, result);
1319 char* fc_get_snan(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1321 if (result == NULL) result = calc_buffer;
1323 _desc(result).exponent_size = exponent_size;
1324 _desc(result).mantissa_size = mantissa_size;
1325 _desc(result).class = NAN;
1329 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1331 /* signalling nan has non-zero mantissa with msb not set */
1332 sc_val_from_ulong(1, _mant(result));
1337 char* fc_get_qnan(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1339 if (result == NULL) result = calc_buffer;
1341 _desc(result).exponent_size = exponent_size;
1342 _desc(result).mantissa_size = mantissa_size;
1343 _desc(result).class = NAN;
1347 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1349 /* quiet nan has the msb of the mantissa set, so shift one there */
1350 sc_val_from_ulong(1, _mant(result));
1351 /* mantissa_size >+< 1 because of two extra rounding bits */
1352 sc_val_from_ulong(mantissa_size + 1, NULL);
1353 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1358 char* fc_get_plusinf(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1360 if (result == NULL) result = calc_buffer;
1362 _desc(result).exponent_size = exponent_size;
1363 _desc(result).mantissa_size = mantissa_size;
1364 _desc(result).class = NORMAL;
1368 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1370 sc_val_from_ulong(0, _mant(result));
1375 char* fc_get_minusinf(unsigned int exponent_size, unsigned int mantissa_size, char *result)
1377 if (result == NULL) result = calc_buffer;
1379 fc_get_plusinf(exponent_size, mantissa_size, result);
1385 int fc_comp(const void *a, const void *b)
1387 const char *val_a = (const char*)a;
1388 const char *val_b = (const char*)b;
1391 if (_desc(val_a).class == NAN || _desc(val_b).class == NAN) return 2;
1392 /* zero is equal independent of sign */
1393 if ((_desc(val_a).class == ZERO) && (_desc(val_b).class == ZERO)) return 0;
1394 /* different signs make compare easy */
1395 if (_sign(val_a) != _sign(val_b)) return (_sign(val_a)==0)?(1):(-1);
1396 /* both infinity means equality */
1397 if ((_desc(val_a).class == INF) && (_desc(val_b).class == INF)) return 0;
1398 /* infinity is bigger than the rest */
1399 if (_desc(val_a).class == INF) return _sign(val_a)?(-1):(1);
1400 if (_desc(val_b).class == INF) return _sign(val_b)?(1):(-1);
1402 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1408 return sc_comp(_mant(val_a), _mant(val_b));
1414 int fc_is_zero(const void *a)
1416 return _desc((const char*)a).class == ZERO;
1419 int fc_is_negative(const void *a)
1421 return _sign((const char*)a);
1424 int fc_is_inf(const void *a)
1426 return _desc(a).class == INF;
1429 int fc_is_nan(const void *a)
1431 return _desc(a).class == NAN;
1434 int fc_is_subnormal(const void *a)
1436 return _desc(a).class == SUBNORMAL;
1439 char *fc_print(const void *a, char *buf, int buflen, unsigned base)
1444 val = (const char*)a;
1446 mul_1 = alloca(CALC_BUFFER_SIZE);
1450 switch (_desc(val).class) {
1452 if (buflen >= 8+_sign(val)) sprintf(buf, "%sINFINITY", _sign(val)?"-":"");
1453 else snprintf(buf, buflen, "%sINF", _sign(val)?"-":NULL);
1456 snprintf(buf, buflen, "NAN");
1459 snprintf(buf, buflen, "0.0");
1462 /* XXX to be implemented */
1463 #ifdef HAVE_LONG_DOUBLE
1464 /* XXX 30 is arbitrary */
1465 snprintf(buf, buflen, "%.30LE", fc_val_to_float(val));
1467 snprintf(buf, buflen, "%.18E", fc_val_to_float(val));
1473 switch (_desc(val).class) {
1475 if (buflen >= 8+_sign(val)) sprintf(buf, "%sINFINITY", _sign(val)?"-":"");
1476 else snprintf(buf, buflen, "%sINF", _sign(val)?"-":NULL);
1479 snprintf(buf, buflen, "NAN");
1482 snprintf(buf, buflen, "0.0");
1485 #ifdef HAVE_LONG_DOUBLE
1486 snprintf(buf, buflen, "%LA", fc_val_to_float(val));
1488 snprintf(buf, buflen, "%A", fc_val_to_float(val));
1495 snprintf(buf, buflen, "%s", sc_print(_pack(val, mul_1), VALUE_SIZE*4, SC_HEX));
1501 unsigned char fc_sub_bits(const void *value, unsigned num_bits, unsigned byte_ofs)
1503 /* this is used to cache the packed version of the value */
1504 static char *pack = NULL;
1506 if (pack == NULL) pack = malloc(VALUE_SIZE);
1509 _pack((const char*)value, pack);
1511 return sc_sub_bits(pack, num_bits, byte_ofs);
1514 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode)
1516 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1517 ROUNDING_MODE = mode;
1519 return ROUNDING_MODE;
1522 fc_rounding_mode_t fc_get_rounding_mode(void)
1524 return ROUNDING_MODE;
1527 void init_fltcalc(int precision)
1529 if (calc_buffer == NULL) {
1530 /* does nothing if already init */
1531 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1533 init_strcalc(precision + 4);
1535 /* needs additionally two bits to round, a bit as explicit 1., and one for
1536 * addition overflow */
1537 max_precision = sc_get_precision() - 4;
1538 if (max_precision < precision)
1539 printf("WARING: not enough precision available, using %d\n", max_precision);
1541 ROUNDING_MODE = FC_TONEAREST;
1542 VALUE_SIZE = sc_get_buffer_length();
1544 EXPONENT_POS = SIGN_POS + sizeof(char);
1545 MANTISSA_POS = EXPONENT_POS + VALUE_SIZE;
1546 DESCRIPTOR_POS = MANTISSA_POS + VALUE_SIZE;
1547 CALC_BUFFER_SIZE = DESCRIPTOR_POS + sizeof(descriptor_t);
1549 calc_buffer = malloc(CALC_BUFFER_SIZE);
1550 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));
1551 #ifdef HAVE_LONG_DOUBLE
1552 DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
1554 DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
1556 #ifdef WORDS_BIGENDIAN
1557 DEBUGPRINTF(("\tWord order is big endian\n\n"));
1559 DEBUGPRINTF(("\tWord order is little endian\n\n"));
1564 /* definition of interface functions */