2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief tarval floating point calculations
24 * @author Mathias Heil
33 #include <math.h> /* need isnan() and isinf() (will be changed)*/
34 /* undef some reused constants defined by math.h */
39 #ifdef HAVE_INTTYPES_H
40 # include <inttypes.h>
49 /** The number of extra precision rounding bits */
50 #define ROUNDING_BITS 2
52 typedef uint32_t UINT32;
54 #ifdef HAVE_LONG_DOUBLE
55 #ifdef WORDS_BIGENDIAN
62 volatile long double d;
71 volatile long double d;
75 #ifdef WORDS_BIGENDIAN
94 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
96 /* our floating point value */
98 ieee_descriptor_t desc;
100 char value[1]; /* exp[value_size] + mant[value_size] */
103 #define _exp(a) &((a)->value[0])
104 #define _mant(a) &((a)->value[value_size])
106 #define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
107 #define _shift_right(x, y, res) sc_shr((x), (y), value_size*4, 0, (res))
108 #define _shift_left(x, y, res) sc_shl((x), (y), value_size*4, 0, (res))
112 # define DEBUGPRINTF(x) printf x
114 # define DEBUGPRINTF(x) ((void)0)
117 #ifdef FLTCALC_TRACE_CALC
118 # define TRACEPRINTF(x) printf x
120 # define TRACEPRINTF(x) ((void)0)
123 /** The immediate precision. */
124 static unsigned immediate_prec = 0;
126 /** A temporal buffer. */
127 static fp_value *calc_buffer = NULL;
129 /** Current rounding mode.*/
130 static fc_rounding_mode_t rounding_mode;
132 static int calc_buffer_size;
133 static int value_size;
134 static int max_precision;
137 static int fc_exact = 1;
140 static void fail_char(const char *str, unsigned int len, int pos) {
142 printf("ERROR: Unexpected character '%c'\n", *(str + pos));
144 printf("ERROR: Unexpected end of string\n");
145 while (len-- && *str) printf("%c", *str++); printf("\n");
146 while (pos--) printf(" "); printf("^\n");
147 /* the front end has to to check constant strings */
152 /** pack machine-like */
153 static void *pack(const fp_value *int_float, void *packed) {
156 fp_value *val_buffer;
159 temp = alloca(value_size);
160 shift_val = alloca(value_size);
162 switch ((value_class_t)int_float->desc.clss) {
164 val_buffer = alloca(calc_buffer_size);
165 fc_get_qnan(&int_float->desc, val_buffer);
166 int_float = val_buffer;
170 val_buffer = alloca(calc_buffer_size);
171 fc_get_plusinf(&int_float->desc, val_buffer);
172 val_buffer->sign = int_float->sign;
173 int_float = val_buffer;
179 assert(int_float->desc.explicit_one <= 1);
181 /* pack sign: move it to the left after exponent AND mantissa */
182 sc_val_from_ulong(int_float->sign, temp);
184 pos = int_float->desc.exponent_size + int_float->desc.mantissa_size + int_float->desc.explicit_one;
185 sc_val_from_ulong(pos, NULL);
186 _shift_left(temp, sc_get_buffer(), packed);
188 /* pack exponent: move it to the left after mantissa */
189 pos = int_float->desc.mantissa_size + int_float->desc.explicit_one;
190 sc_val_from_ulong(pos, shift_val);
191 _shift_left(_exp(int_float), shift_val, temp);
193 /* combine sign|exponent */
194 sc_or(temp, packed, packed);
196 /* extract mantissa */
197 /* remove rounding bits */
198 sc_val_from_ulong(ROUNDING_BITS, shift_val);
199 _shift_right(_mant(int_float), shift_val, temp);
201 /* remove leading 1 (or 0 if denormalized) */
202 sc_max_from_bits(pos, 0, shift_val); /* all mantissa bits are 1's */
203 sc_and(temp, shift_val, temp);
205 /* combine sign|exponent|mantissa */
206 sc_or(temp, packed, packed);
212 * Normalize a fp_value.
214 * @return non-zero if result is exact
216 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky) {
219 char lsb, guard, round, round_dir = 0;
220 char *temp = alloca(value_size);
222 /* save rounding bits at the end */
223 hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
225 if (in_val != out_val) {
226 out_val->sign = in_val->sign;
227 memcpy(&out_val->desc, &in_val->desc, sizeof(out_val->desc));
230 out_val->desc.clss = NORMAL;
232 /* mantissa all zeros, so zero exponent (because of explicit one) */
233 if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
234 sc_val_from_ulong(0, _exp(out_val));
238 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
241 sc_val_from_ulong(-hsb-1, temp);
243 _shift_right(_mant(in_val), temp, _mant(out_val));
245 /* remember if some bits were shifted away */
246 if (sc_had_carry()) {
250 sc_add(_exp(in_val), temp, _exp(out_val));
251 } else if (hsb > -1) {
253 sc_val_from_ulong(hsb+1, temp);
255 _shift_left(_mant(in_val), temp, _mant(out_val));
257 sc_sub(_exp(in_val), temp, _exp(out_val));
260 /* check for exponent underflow */
261 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
262 DEBUGPRINTF(("Exponent underflow!\n"));
263 /* exponent underflow */
264 /* shift the mantissa right to have a zero exponent */
265 sc_val_from_ulong(1, temp);
266 sc_sub(temp, _exp(out_val), NULL);
268 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
269 if (sc_had_carry()) {
273 /* denormalized means exponent of zero */
274 sc_val_from_ulong(0, _exp(out_val));
276 out_val->desc.clss = SUBNORMAL;
279 /* perform rounding by adding a value that clears the guard bit and the round bit
280 * and either causes a carry to round up or not */
281 /* get the last 3 bits of the value */
282 lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
283 guard = (lsb&0x2)>>1;
286 switch (rounding_mode) {
288 /* round to nearest representable value, if in doubt choose the version
290 round_dir = guard && (sticky || round || lsb>>2);
293 /* if positive: round to one if the exact value is bigger, else to zero */
294 round_dir = (!out_val->sign && (guard || round || sticky));
297 /* if negative: round to one if the exact value is bigger, else to zero */
298 round_dir = (out_val->sign && (guard || round || sticky));
301 /* always round to 0 (chopping mode) */
305 DEBUGPRINTF(("Rounding (s%d, l%d, g%d, r%d, s%d) %s\n", out_val->sign, lsb>>2, guard, round, sticky, (round_dir)?"up":"down"));
307 if (round_dir == 1) {
308 guard = (round^guard)<<1;
309 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));
321 /* could have rounded down to zero */
322 if (sc_is_zero(_mant(out_val)) && (out_val->desc.clss == SUBNORMAL))
323 out_val->desc.clss = ZERO;
325 /* check for rounding overflow */
326 hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
327 if ((out_val->desc.clss != SUBNORMAL) && (hsb < -1)) {
328 sc_val_from_ulong(1, temp);
329 _shift_right(_mant(out_val), temp, _mant(out_val));
330 if (exact && sc_had_carry())
332 sc_add(_exp(out_val), temp, _exp(out_val));
333 } else if ((out_val->desc.clss == SUBNORMAL) && (hsb == -1)) {
334 /* overflow caused the mantissa to be normal again,
335 * so adapt the exponent accordingly */
336 sc_val_from_ulong(1, temp);
337 sc_add(_exp(out_val), temp, _exp(out_val));
339 out_val->desc.clss = NORMAL;
341 /* no further rounding is needed, because rounding overflow means
342 * the carry of the original rounding was propagated all the way
343 * up to the bit left of the radix point. This implies the bits
344 * to the right are all zeros (rounding is +1) */
346 /* check for exponent overflow */
347 sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
348 if (sc_comp(_exp(out_val), temp) != -1) {
349 DEBUGPRINTF(("Exponent overflow!\n"));
350 /* exponent overflow, reaction depends on rounding method:
352 * mode | sign of value | result
353 *--------------------------------------------------------------
354 * TO_NEAREST | + | +inf
356 *--------------------------------------------------------------
357 * TO_POSITIVE | + | +inf
358 * | - | smallest representable value
359 *--------------------------------------------------------------
360 * TO_NEAGTIVE | + | largest representable value
362 *--------------------------------------------------------------
363 * TO_ZERO | + | largest representable value
364 * | - | smallest representable value
365 *--------------------------------------------------------------*/
366 if (out_val->sign == 0) {
367 /* value is positive */
368 switch (rounding_mode) {
371 out_val->desc.clss = INF;
376 fc_get_max(&out_val->desc, out_val);
379 /* value is negative */
380 switch (rounding_mode) {
383 out_val->desc.clss = INF;
388 fc_get_min(&out_val->desc, out_val);
396 * Operations involving NaN's must return NaN.
397 * They are NOT exact.
399 #define handle_NAN(a, b, result) \
401 if (a->desc.clss == NAN) { \
402 if (a != result) memcpy(result, a, calc_buffer_size); \
406 if (b->desc.clss == NAN) { \
407 if (b != result) memcpy(result, b, calc_buffer_size); \
415 * calculate a + b, where a is the value with the bigger exponent
417 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result) {
426 handle_NAN(a, b, result);
428 /* make sure result has a descriptor */
429 if (result != a && result != b)
430 result->desc = a->desc;
432 /* determine if this is an addition or subtraction */
433 sign = a->sign ^ b->sign;
435 /* produce NaN on inf - inf */
436 if (sign && (a->desc.clss == INF) && (b->desc.clss == INF)) {
438 fc_get_qnan(&a->desc, result);
442 temp = alloca(value_size);
443 exp_diff = alloca(value_size);
445 /* get exponent difference */
446 sc_sub(_exp(a), _exp(b), exp_diff);
448 /* initially set sign to be the sign of a, special treatment of subtraction
449 * when exponents are equal is required though.
450 * Also special care about the sign is needed when the mantissas are equal
452 if (sign && sc_val_to_long(exp_diff) == 0) {
453 switch (sc_comp(_mant(a), _mant(b))) {
455 res_sign = a->sign; /* abs(a) is bigger and a is negative */
458 res_sign = (rounding_mode == FC_TONEGATIVE);
461 res_sign = b->sign; /* abs(b) is bigger and b is negative */
464 /* can't be reached */
471 result->sign = res_sign;
473 /* sign has been taken care of, check for special cases */
474 if (a->desc.clss == ZERO || b->desc.clss == INF) {
476 memcpy(result, b, calc_buffer_size);
477 fc_exact = b->desc.clss == NORMAL;
478 result->sign = res_sign;
481 if (b->desc.clss == ZERO || a->desc.clss == INF) {
483 memcpy(result, a, calc_buffer_size);
484 fc_exact = a->desc.clss == NORMAL;
485 result->sign = res_sign;
489 /* shift the smaller value to the right to align the radix point */
490 /* subnormals have their radix point shifted to the right,
491 * take care of this first */
492 if ((b->desc.clss == SUBNORMAL) && (a->desc.clss != SUBNORMAL)) {
493 sc_val_from_ulong(1, temp);
494 sc_sub(exp_diff, temp, exp_diff);
497 _shift_right(_mant(b), exp_diff, temp);
498 sticky = sc_had_carry();
501 if (sticky && sign) {
502 /* if subtracting a little more than the represented value or adding a little
503 * more than the represented value to a negative value this, in addition to the
504 * still set sticky bit, takes account of the 'little more' */
505 char *temp1 = alloca(calc_buffer_size);
506 sc_val_from_ulong(1, temp1);
507 sc_add(temp, temp1, temp);
511 if (sc_comp(_mant(a), temp) == -1)
512 sc_sub(temp, _mant(a), _mant(result));
514 sc_sub(_mant(a), temp, _mant(result));
516 sc_add(_mant(a), temp, _mant(result));
519 /* _normalize expects a 'normal' radix point, adding two subnormals
520 * results in a subnormal radix point -> shifting before normalizing */
521 if ((a->desc.clss == SUBNORMAL) && (b->desc.clss == SUBNORMAL)) {
522 sc_val_from_ulong(1, NULL);
523 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
526 /* resulting exponent is the bigger one */
527 memmove(_exp(result), _exp(a), value_size);
529 fc_exact &= normalize(result, result, sticky);
535 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result) {
542 handle_NAN(a, b, result);
544 temp = alloca(value_size);
546 if (result != a && result != b)
547 result->desc = a->desc;
549 result->sign = res_sign = a->sign ^ b->sign;
551 /* produce NaN on 0 * inf */
552 if (a->desc.clss == ZERO) {
553 if (b->desc.clss == INF) {
554 fc_get_qnan(&a->desc, result);
558 memcpy(result, a, calc_buffer_size);
559 result->sign = res_sign;
563 if (b->desc.clss == ZERO) {
564 if (a->desc.clss == INF) {
565 fc_get_qnan(&a->desc, result);
569 memcpy(result, b, calc_buffer_size);
570 result->sign = res_sign;
575 if (a->desc.clss == INF) {
578 memcpy(result, a, calc_buffer_size);
579 result->sign = res_sign;
582 if (b->desc.clss == INF) {
585 memcpy(result, b, calc_buffer_size);
586 result->sign = res_sign;
590 /* exp = exp(a) + exp(b) - excess */
591 sc_add(_exp(a), _exp(b), _exp(result));
593 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
594 sc_sub(_exp(result), temp, _exp(result));
596 /* mixed normal, subnormal values introduce an error of 1, correct it */
597 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
598 sc_val_from_ulong(1, temp);
599 sc_add(_exp(result), temp, _exp(result));
602 sc_mul(_mant(a), _mant(b), _mant(result));
604 /* realign result: after a multiplication the digits right of the radix
605 * point are the sum of the factors' digits after the radix point. As all
606 * values are normalized they both have the same amount of these digits,
607 * which has to be restored by proper shifting
608 * because of the rounding bits */
609 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
611 _shift_right(_mant(result), temp, _mant(result));
612 sticky = sc_had_carry();
615 fc_exact &= normalize(result, result, sticky);
621 static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result) {
623 char *temp, *dividend;
628 handle_NAN(a, b, result);
630 temp = alloca(value_size);
631 dividend = alloca(value_size);
633 if (result != a && result != b)
634 result->desc = a->desc;
636 result->sign = res_sign = a->sign ^ b->sign;
638 /* produce NAN on 0/0 and inf/inf */
639 if (a->desc.clss == ZERO) {
640 if (b->desc.clss == ZERO) {
642 fc_get_qnan(&a->desc, result);
647 memcpy(result, a, calc_buffer_size);
648 result->sign = res_sign;
653 if (b->desc.clss == INF) {
655 if (a->desc.clss == INF) {
657 fc_get_qnan(&a->desc, result);
660 sc_val_from_ulong(0, NULL);
661 _save_result(_exp(result));
662 _save_result(_mant(result));
663 result->desc.clss = ZERO;
668 if (a->desc.clss == INF) {
672 memcpy(result, a, calc_buffer_size);
673 result->sign = res_sign;
676 if (b->desc.clss == ZERO) {
678 /* division by zero */
680 fc_get_minusinf(&a->desc, result);
682 fc_get_plusinf(&a->desc, result);
686 /* exp = exp(a) - exp(b) + excess - 1*/
687 sc_sub(_exp(a), _exp(b), _exp(result));
688 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 2, temp);
689 sc_add(_exp(result), temp, _exp(result));
691 /* mixed normal, subnormal values introduce an error of 1, correct it */
692 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
693 sc_val_from_ulong(1, temp);
694 sc_add(_exp(result), temp, _exp(result));
697 /* mant(res) = mant(a) / 1/2mant(b) */
698 /* to gain more bits of precision in the result the dividend could be
699 * shifted left, as this operation does not loose bits. This would not
700 * fit into the integer precision, but due to the rounding bits (which
701 * are always zero because the values are all normalized) the divisor
702 * can be shifted right instead to achieve the same result */
703 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
705 _shift_left(_mant(a), temp, dividend);
708 char *divisor = alloca(calc_buffer_size);
709 sc_val_from_ulong(1, divisor);
710 _shift_right(_mant(b), divisor, divisor);
711 sc_div(dividend, divisor, _mant(result));
712 sticky = sc_had_carry();
716 fc_exact &= normalize(result, result, sticky);
720 static void _power_of_ten(int exp, ieee_descriptor_t *desc, char *result) {
727 /* set new descriptor (else result is supposed to already have one) */
729 result->desc = *desc;
731 build = alloca(value_size);
732 temp = alloca(value_size);
734 sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
737 /* temp is value of ten now */
738 sc_val_from_ulong(10, NULL);
741 for (exp--; exp > 0; exp--) {
743 sc_mul(build, temp, NULL);
747 /* temp is amount of left shift needed to put the value left of the radix point */
748 sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
750 _shift_left(build, temp, _mant(result));
752 _normalize(result, result, 0);
758 * Truncate the fractional part away.
760 * This does not clip to any integer range.
762 static void _trunc(const fp_value *a, fp_value *result) {
764 * When exponent == 0 all bits left of the radix point
765 * are the integral part of the value. For 15bit exp_size
766 * this would require a left shift of max. 16383 bits which
768 * But it is enough to ensure that no bit right of the radix
769 * point remains set. This restricts the interesting
770 * exponents to the interval [0, mant_size-1].
771 * Outside this interval the truncated value is either 0 or
772 * it does not have fractional parts.
775 int exp_bias, exp_val;
778 /* fixme: can be exact */
781 temp = alloca(value_size);
784 result->desc = a->desc;
786 exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
787 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
790 sc_val_from_ulong(0, NULL);
791 _save_result(_exp(result));
792 _save_result(_mant(result));
793 result->desc.clss = ZERO;
798 if (exp_val > a->desc.mantissa_size) {
800 memcpy(result, a, calc_buffer_size);
805 /* set up a proper mask to delete all bits right of the
806 * radix point if the mantissa had been shifted until exp == 0 */
807 sc_max_from_bits(1 + exp_val, 0, temp);
808 sc_val_from_long(a->desc.mantissa_size - exp_val + 2, NULL);
809 _shift_left(temp, sc_get_buffer(), temp);
811 /* and the mask and return the result */
812 sc_and(_mant(a), temp, _mant(result));
815 memcpy(_exp(result), _exp(a), value_size);
816 result->sign = a->sign;
821 * functions defined in fltcalc.h
823 const void *fc_get_buffer(void) {
827 int fc_get_buffer_length(void) {
828 return calc_buffer_size;
831 void *fc_val_from_str(const char *str, unsigned int len, const ieee_descriptor_t *desc, void *result) {
843 int exp_int, hsb, state;
848 char *mant_str, *exp_val, *power_val;
851 if (result == NULL) result = calc_buffer;
853 exp_val = alloca(value_size);
854 power_val = alloca(calc_buffer_size);
855 mant_str = alloca((len)?(len):(strlen(str)));
857 result->desc.exponent_size = desc->exponent_size;
858 result->desc.mantissa_size = desc->mantissa_size;
859 result->desc.explicit_one = desc->explicit_one;
860 result->desc.clss = NORMAL;
867 while (len == 0 || str-old_str < len) {
883 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
890 state = RIGHT_OF_DOT;
901 fail_char(old_str, len, str - old_str);
907 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
908 mant_str[pos++] = *(str++);
912 state = RIGHT_OF_DOT;
923 mant_str[pos] = '\0';
927 fail_char(old_str, len, str - old_str);
933 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
934 mant_str[pos++] = *(str++);
945 mant_str[pos] = '\0';
949 fail_char(old_str, len, str - old_str);
959 if (*(str-1) != 'e' && *(str-1) != 'E') fail_char(old_str, len, str - old_str);
963 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
964 mant_str[pos] = '\0';
971 fail_char(old_str, len, str - old_str);
977 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
982 case '\0': goto done;
985 fail_char(old_str, len, str - old_str);
988 } /* switch(state) */
991 sc_val_from_str(mant_str, strlen(mant_str), _mant(result));
993 /* shift to put value left of radix point */
994 sc_val_from_ulong(mant_size + ROUNDING_BITS, exp_val);
996 _shift_left(_mant(result), exp_val, _mant(result));
998 sc_val_from_ulong((1 << (exp_size - 1)) - 1, _exp(result));
1000 _normalize(result, result, 0);
1002 if (state == EXPONENT) {
1003 exp_int -= atoi(str-pos);
1006 _power_of_ten(exp_int, &result->desc, power_val);
1008 _fdiv(result, power_val, result);
1012 /* XXX excuse of an implementation to make things work */
1014 fp_value *tmp = alloca(calc_buffer_size);
1015 ieee_descriptor_t tmp_desc;
1018 #if defined(HAVE_LONG_DOUBLE) && !defined(__CYGWIN__)
1019 val = strtold(str, NULL);
1020 DEBUGPRINTF(("val_from_str(%s)\n", str));
1021 tmp_desc.exponent_size = 15;
1022 tmp_desc.mantissa_size = 63;
1023 tmp_desc.explicit_one = 1;
1024 tmp_desc.clss = NORMAL;
1025 fc_val_from_ieee754(val, &tmp_desc, tmp);
1027 val = strtod(str, NULL);
1028 DEBUGPRINTF(("val_from_str(%s)\n", str));
1029 tmp_desc.exponent_size = 11;
1030 tmp_desc.mantissa_size = 52;
1031 tmp_desc.explicit_one = 0;
1032 tmp_desc.clss = NORMAL;
1033 fc_val_from_ieee754(val, &tmp_desc, tmp);
1034 #endif /* HAVE_LONG_DOUBLE */
1035 return fc_cast(tmp, desc, result);
1039 fp_value *fc_val_from_ieee754(LLDBL l, const ieee_descriptor_t *desc, fp_value *result) {
1041 int bias_res, bias_val, mant_val;
1044 UINT32 exponent, mantissa0, mantissa1;
1047 bias_res = ((1 << (desc->exponent_size - 1)) - 1);
1049 #ifdef HAVE_LONG_DOUBLE
1052 sign = (srcval.val.high & 0x00008000) != 0;
1053 exponent = (srcval.val.high & 0x00007FFF) ;
1054 mantissa0 = srcval.val.mid;
1055 mantissa1 = srcval.val.low;
1056 #else /* no long double */
1059 sign = (srcval.val.high & 0x80000000) != 0;
1060 exponent = (srcval.val.high & 0x7FF00000) >> 20;
1061 mantissa0 = srcval.val.high & 0x000FFFFF;
1062 mantissa1 = srcval.val.low;
1065 #ifdef HAVE_LONG_DOUBLE
1066 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)); */
1067 DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1069 TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
1070 DEBUGPRINTF(("(%d-%.3X-%.5X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1073 if (result == NULL) result = calc_buffer;
1074 temp = alloca(value_size);
1076 /* CLEAR the buffer, else some bits might be uninitialized */
1077 memset(result, 0, fc_get_buffer_length());
1079 result->desc.exponent_size = desc->exponent_size;
1080 result->desc.mantissa_size = desc->mantissa_size;
1081 result->desc.explicit_one = desc->explicit_one;
1084 result->sign = sign;
1086 /* sign and flag suffice to identify NaN or inf, no exponent/mantissa
1087 * encoding is needed. the function can return immediately in these cases */
1089 result->desc.clss = NAN;
1090 TRACEPRINTF(("val_from_float resulted in NAN\n"));
1093 else if (isinf(l)) {
1094 result->desc.clss = INF;
1095 TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
1099 /* build exponent, because input and output exponent and mantissa sizes may differ
1100 * this looks more complicated than it is: unbiased input exponent + output bias,
1101 * minus the mantissa difference which is added again later when the output float
1102 * becomes normalized */
1103 sc_val_from_long((exponent - bias_val + bias_res) - (mant_val - desc->mantissa_size), _exp(result));
1105 /* build mantissa representation */
1106 if (exponent != 0) {
1107 /* insert the hidden bit */
1108 sc_val_from_ulong(1, temp);
1109 sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
1110 _shift_left(temp, sc_get_buffer(), NULL);
1113 sc_val_from_ulong(0, NULL);
1116 _save_result(_mant(result));
1118 /* bits from the upper word */
1119 sc_val_from_ulong(mantissa0, temp);
1120 sc_val_from_ulong(34, NULL);
1121 _shift_left(temp, sc_get_buffer(), temp);
1122 sc_or(_mant(result), temp, _mant(result));
1124 /* bits from the lower word */
1125 sc_val_from_ulong(mantissa1, temp);
1126 sc_val_from_ulong(ROUNDING_BITS, NULL);
1127 _shift_left(temp, sc_get_buffer(), temp);
1128 sc_or(_mant(result), temp, _mant(result));
1130 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
1131 * origin one to the left */
1132 if (exponent == 0) {
1133 sc_val_from_ulong(1, NULL);
1134 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1137 normalize(result, result, 0);
1139 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
1144 LLDBL fc_val_to_ieee754(const fp_value *val) {
1146 fp_value *temp = NULL;
1156 ieee_descriptor_t desc;
1157 unsigned mantissa_size;
1159 #ifdef HAVE_LONG_DOUBLE
1160 desc.exponent_size = 15;
1161 desc.mantissa_size = 63;
1162 desc.explicit_one = 1;
1165 desc.exponent_size = 11;
1166 desc.mantissa_size = 52;
1167 desc.explicit_one = 0;
1170 mantissa_size = desc.mantissa_size + desc.explicit_one;
1172 temp = alloca(calc_buffer_size);
1173 value = fc_cast(val, &desc, temp);
1177 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
1178 * lead to wrong results */
1179 exponent = sc_val_to_long(_exp(value)) ;
1181 sc_val_from_ulong(ROUNDING_BITS, NULL);
1182 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1187 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1188 mantissa1 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << (byte_offset << 3);
1190 for (; (byte_offset<<3) < desc.mantissa_size; byte_offset++)
1191 mantissa0 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << ((byte_offset - 4) << 3);
1193 #ifdef HAVE_LONG_DOUBLE
1194 buildval.val.high = sign << 15;
1195 buildval.val.high |= exponent;
1196 buildval.val.mid = mantissa0;
1197 buildval.val.low = mantissa1;
1198 #else /* no long double */
1199 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1200 buildval.val.high = sign << 31;
1201 buildval.val.high |= exponent << 20;
1202 buildval.val.high |= mantissa0;
1203 buildval.val.low = mantissa1;
1206 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1210 fp_value *fc_cast(const fp_value *value, const ieee_descriptor_t *desc, fp_value *result) {
1212 int exp_offset, val_bias, res_bias;
1214 if (result == NULL) result = calc_buffer;
1215 temp = alloca(value_size);
1217 if (value->desc.exponent_size == desc->exponent_size &&
1218 value->desc.mantissa_size == desc->mantissa_size &&
1219 value->desc.explicit_one == desc->explicit_one) {
1220 if (value != result)
1221 memcpy(result, value, calc_buffer_size);
1225 if (value->desc.clss == NAN) {
1226 if (sc_get_highest_set_bit(_mant(value)) == value->desc.mantissa_size + 1)
1227 return fc_get_qnan(desc, result);
1229 return fc_get_snan(desc, result);
1231 else if(value->desc.clss == INF) {
1232 if (value->sign == 0)
1233 return fc_get_plusinf(desc, result);
1235 return fc_get_minusinf(desc, result);
1238 /* set the descriptor of the new value */
1239 result->desc.exponent_size = desc->exponent_size;
1240 result->desc.mantissa_size = desc->mantissa_size;
1241 result->desc.explicit_one = desc->explicit_one;
1242 result->desc.clss = value->desc.clss;
1244 result->sign = value->sign;
1246 /* when the mantissa sizes differ normalizing has to shift to align it.
1247 * this would change the exponent, which is unwanted. So calculate this
1248 * offset and add it */
1249 val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1250 res_bias = (1 << (desc->exponent_size - 1)) - 1;
1252 exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - desc->mantissa_size);
1253 sc_val_from_long(exp_offset, temp);
1254 sc_add(_exp(value), temp, _exp(result));
1256 /* _normalize expects normalized radix point */
1257 if (value->desc.clss == SUBNORMAL) {
1258 sc_val_from_ulong(1, NULL);
1259 _shift_left(_mant(value), sc_get_buffer(), _mant(result));
1260 } else if (value != result) {
1261 memcpy(_mant(result), _mant(value), value_size);
1263 memmove(_mant(result), _mant(value), value_size);
1266 normalize(result, result, 0);
1267 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1271 fp_value *fc_get_max(const ieee_descriptor_t *desc, fp_value *result) {
1272 if (result == NULL) result = calc_buffer;
1274 result->desc.exponent_size = desc->exponent_size;
1275 result->desc.mantissa_size = desc->mantissa_size;
1276 result->desc.explicit_one = desc->explicit_one;
1277 result->desc.clss = NORMAL;
1281 sc_val_from_ulong((1 << desc->exponent_size) - 2, _exp(result));
1283 sc_max_from_bits(desc->mantissa_size + 1, 0, _mant(result));
1284 sc_val_from_ulong(ROUNDING_BITS, NULL);
1285 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1290 fp_value *fc_get_min(const ieee_descriptor_t *desc, fp_value *result) {
1291 if (result == NULL) result = calc_buffer;
1293 fc_get_max(desc, result);
1299 fp_value *fc_get_snan(const ieee_descriptor_t *desc, fp_value *result) {
1300 if (result == NULL) result = calc_buffer;
1302 result->desc.exponent_size = desc->exponent_size;
1303 result->desc.mantissa_size = desc->mantissa_size;
1304 result->desc.explicit_one = desc->explicit_one;
1305 result->desc.clss = NAN;
1309 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1311 /* signaling NaN has non-zero mantissa with msb not set */
1312 sc_val_from_ulong(1, _mant(result));
1317 fp_value *fc_get_qnan(const ieee_descriptor_t *desc, fp_value *result) {
1318 if (result == NULL) result = calc_buffer;
1320 result->desc.exponent_size = desc->exponent_size;
1321 result->desc.mantissa_size = desc->mantissa_size;
1322 result->desc.explicit_one = desc->explicit_one;
1323 result->desc.clss = NAN;
1327 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1329 /* quiet NaN has the msb of the mantissa set, so shift one there */
1330 sc_val_from_ulong(1, _mant(result));
1331 /* mantissa_size >+< 1 because of two extra rounding bits */
1332 sc_val_from_ulong(desc->mantissa_size + 1, NULL);
1333 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1338 fp_value *fc_get_plusinf(const ieee_descriptor_t *desc, fp_value *result)
1342 if (result == NULL) result = calc_buffer;
1344 result->desc.exponent_size = desc->exponent_size;
1345 result->desc.mantissa_size = desc->mantissa_size;
1346 result->desc.explicit_one = desc->explicit_one;
1347 result->desc.clss = INF;
1351 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1353 mant = _mant(result);
1354 sc_val_from_ulong(0, mant);
1355 if (desc->explicit_one) {
1356 sc_set_bit_at(mant, result->desc.mantissa_size + ROUNDING_BITS);
1362 fp_value *fc_get_minusinf(const ieee_descriptor_t *desc, fp_value *result) {
1363 if (result == NULL) result = calc_buffer;
1365 fc_get_plusinf(desc, result);
1371 int fc_comp(const fp_value *val_a, const fp_value *val_b) {
1375 * shortcut: if both values are identical, they are either
1376 * Unordered if NaN or equal
1379 return val_a->desc.clss == NAN ? 2 : 0;
1381 /* unordered if one is a NaN */
1382 if (val_a->desc.clss == NAN || val_b->desc.clss == NAN)
1385 /* zero is equal independent of sign */
1386 if ((val_a->desc.clss == ZERO) && (val_b->desc.clss == ZERO))
1389 /* different signs make compare easy */
1390 if (val_a->sign != val_b->sign)
1391 return (val_a->sign == 0) ? (1) : (-1);
1393 mul = val_a->sign ? -1 : 1;
1395 /* both infinity means equality */
1396 if ((val_a->desc.clss == INF) && (val_b->desc.clss == INF))
1399 /* infinity is bigger than the rest */
1400 if (val_a->desc.clss == INF)
1402 if (val_b->desc.clss == INF)
1405 /* check first exponent, that mantissa if equal */
1406 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1412 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1418 int fc_is_zero(const fp_value *a) {
1419 return a->desc.clss == ZERO;
1422 int fc_is_negative(const fp_value *a) {
1426 int fc_is_inf(const fp_value *a) {
1427 return a->desc.clss == INF;
1430 int fc_is_nan(const fp_value *a) {
1431 return a->desc.clss == NAN;
1434 int fc_is_subnormal(const fp_value *a) {
1435 return a->desc.clss == SUBNORMAL;
1438 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base) {
1442 mul_1 = alloca(calc_buffer_size);
1446 switch ((value_class_t)val->desc.clss) {
1448 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1451 snprintf(buf, buflen, "NaN");
1454 snprintf(buf, buflen, "0.0");
1457 flt_val = fc_val_to_ieee754(val);
1458 #ifdef HAVE_LONG_DOUBLE
1459 /* XXX 30 is arbitrary */
1460 snprintf(buf, buflen, "%.30LE", flt_val);
1462 snprintf(buf, buflen, "%.18E", flt_val);
1468 switch ((value_class_t)val->desc.clss) {
1470 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1473 snprintf(buf, buflen, "NAN");
1476 snprintf(buf, buflen, "0.0");
1479 flt_val = fc_val_to_ieee754(val);
1480 #ifdef HAVE_LONG_DOUBLE
1481 snprintf(buf, buflen, "%LA", flt_val);
1483 snprintf(buf, buflen, "%A", flt_val);
1490 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1491 buf[buflen - 1] = '\0';
1497 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs) {
1498 /* this is used to cache the packed version of the value */
1499 static char *packed_value = NULL;
1501 if (packed_value == NULL) packed_value = XMALLOCN(char, value_size);
1504 pack(value, packed_value);
1506 return sc_sub_bits(packed_value, num_bits, byte_ofs);
1509 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1510 int fc_zero_mantissa(const fp_value *value) {
1511 return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1514 /* Returns the exponent of a value. */
1515 int fc_get_exponent(const fp_value *value) {
1516 int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1517 return sc_val_to_long(_exp(value)) - exp_bias;
1520 /* Return non-zero if a given value can be converted lossless into another precision */
1521 int fc_can_lossless_conv_to(const fp_value *value, const ieee_descriptor_t *desc) {
1525 /* handle some special cases first */
1526 switch (value->desc.clss) {
1535 /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
1536 exp_bias = (1 << (desc->exponent_size - 1)) - 1;
1537 v = fc_get_exponent(value) + exp_bias;
1538 if (0 < v && v < (1 << desc->exponent_size) - 1) {
1539 /* exponent can be encoded, now check the mantissa */
1540 v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
1541 return v <= desc->mantissa_size;
1547 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode) {
1548 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1549 rounding_mode = mode;
1551 return rounding_mode;
1554 fc_rounding_mode_t fc_get_rounding_mode(void) {
1555 return rounding_mode;
1558 void init_fltcalc(int precision) {
1559 if (calc_buffer == NULL) {
1560 /* does nothing if already init */
1561 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1563 init_strcalc(precision + 2 + ROUNDING_BITS);
1565 /* needs additionally rounding bits, one bit as explicit 1., and one for
1566 * addition overflow */
1567 max_precision = sc_get_precision() - (2 + ROUNDING_BITS);
1568 if (max_precision < precision)
1569 printf("WARNING: not enough precision available, using %d\n", max_precision);
1571 rounding_mode = FC_TONEAREST;
1572 value_size = sc_get_buffer_length();
1573 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1575 calc_buffer = xmalloc(calc_buffer_size);
1576 memset(calc_buffer, 0, calc_buffer_size);
1577 DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
1578 #ifdef HAVE_LONG_DOUBLE
1579 DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
1581 DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
1583 #ifdef WORDS_BIGENDIAN
1584 DEBUGPRINTF(("\tWord order is big endian\n\n"));
1586 DEBUGPRINTF(("\tWord order is little endian\n\n"));
1591 void finish_fltcalc (void) {
1592 free(calc_buffer); calc_buffer = NULL;
1595 #ifdef FLTCALC_TRACE_CALC
1596 static char buffer[100];
1599 /* definition of interface functions */
1600 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result) {
1601 if (result == NULL) result = calc_buffer;
1603 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1604 TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1606 /* make the value with the bigger exponent the first one */
1607 if (sc_comp(_exp(a), _exp(b)) == -1)
1608 _fadd(b, a, result);
1610 _fadd(a, b, result);
1612 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1616 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result) {
1619 if (result == NULL) result = calc_buffer;
1621 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1622 TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1624 temp = alloca(calc_buffer_size);
1625 memcpy(temp, b, calc_buffer_size);
1626 temp->sign = !b->sign;
1627 if (sc_comp(_exp(a), _exp(temp)) == -1)
1628 _fadd(temp, a, result);
1630 _fadd(a, temp, result);
1632 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1636 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result) {
1637 if (result == NULL) result = calc_buffer;
1639 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1640 TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1642 _fmul(a, b, result);
1644 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1648 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result) {
1649 if (result == NULL) result = calc_buffer;
1651 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1652 TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1654 _fdiv(a, b, result);
1656 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1660 fp_value *fc_neg(const fp_value *a, fp_value *result) {
1661 if (result == NULL) result = calc_buffer;
1663 TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1666 memcpy(result, a, calc_buffer_size);
1667 result->sign = !a->sign;
1669 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1673 fp_value *fc_int(const fp_value *a, fp_value *result) {
1674 if (result == NULL) result = calc_buffer;
1676 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1677 TRACEPRINTF(("truncated to integer "));
1681 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1685 fp_value *fc_rnd(const fp_value *a, fp_value *result) {
1686 if (result == NULL) result = calc_buffer;
1689 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1690 TRACEPRINTF(("rounded to integer "));
1692 assert(!"fc_rnd() not yet implemented");
1694 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1699 * convert a floating point value into an sc value ...
1701 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode)
1703 if (a->desc.clss == NORMAL) {
1704 int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
1705 int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
1710 if (a->sign && !mode_is_signed(dst_mode)) {
1711 /* FIXME: for now we cannot convert this */
1715 tgt_bits = get_mode_size_bits(dst_mode);
1716 if (mode_is_signed(dst_mode))
1719 assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
1720 mantissa_size = a->desc.mantissa_size + ROUNDING_BITS;
1721 shift = exp_val - mantissa_size;
1723 if (tgt_bits < mantissa_size + 1)
1724 tgt_bits = mantissa_size + 1;
1726 sc_shlI(_mant(a), shift, tgt_bits, 0, result);
1728 sc_shrI(_mant(a), -shift, tgt_bits, 0, result);
1731 /* check for overflow */
1732 highest = sc_get_highest_set_bit(result);
1734 if (mode_is_signed(dst_mode)) {
1735 if (highest == sc_get_lowest_set_bit(result)) {
1736 /* need extra test for MIN_INT */
1737 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1738 /* FIXME: handle overflow */
1742 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1743 /* FIXME: handle overflow */
1748 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1749 /* FIXME: handle overflow */
1755 sc_neg(result, result);
1759 else if (a->desc.clss == ZERO) {
1767 unsigned fc_set_immediate_precision(unsigned bits) {
1768 unsigned old = immediate_prec;
1770 immediate_prec = bits;
1774 int fc_is_exact(void) {