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>
53 /** The number of extra precision rounding bits */
54 #define ROUNDING_BITS 2
56 typedef uint32_t UINT32;
58 #ifdef HAVE_LONG_DOUBLE
59 #ifdef WORDS_BIGENDIAN
66 volatile long double d;
75 volatile long double d;
79 #ifdef WORDS_BIGENDIAN
98 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
100 /* our floating point value */
102 ieee_descriptor_t desc;
104 char value[1]; /* exp[value_size] + mant[value_size] */
107 #define _exp(a) &((a)->value[0])
108 #define _mant(a) &((a)->value[value_size])
110 #define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
111 #define _shift_right(x, y, res) sc_shr((x), (y), value_size*4, 0, (res))
112 #define _shift_left(x, y, res) sc_shl((x), (y), value_size*4, 0, (res))
116 # define DEBUGPRINTF(x) printf x
118 # define DEBUGPRINTF(x) ((void)0)
121 #ifdef FLTCALC_TRACE_CALC
122 # define TRACEPRINTF(x) printf x
124 # define TRACEPRINTF(x) ((void)0)
127 /** The immediate precision. */
128 static unsigned immediate_prec = 0;
130 /** A temporal buffer. */
131 static fp_value *calc_buffer = NULL;
133 /** Current rounding mode.*/
134 static fc_rounding_mode_t rounding_mode;
136 static int calc_buffer_size;
137 static int value_size;
138 static int max_precision;
141 static int fc_exact = 1;
144 static void fail_char(const char *str, unsigned int len, int pos) {
146 printf("ERROR: Unexpected character '%c'\n", *(str + pos));
148 printf("ERROR: Unexpected end of string\n");
149 while (len-- && *str) printf("%c", *str++); printf("\n");
150 while (pos--) printf(" "); printf("^\n");
151 /* the front end has to to check constant strings */
156 /** pack machine-like */
157 static void *pack(const fp_value *int_float, void *packed) {
160 fp_value *val_buffer;
163 temp = alloca(value_size);
164 shift_val = alloca(value_size);
166 switch ((value_class_t)int_float->desc.clss) {
168 val_buffer = alloca(calc_buffer_size);
169 fc_get_qnan(&int_float->desc, val_buffer);
170 int_float = val_buffer;
174 val_buffer = alloca(calc_buffer_size);
175 fc_get_plusinf(&int_float->desc, val_buffer);
176 val_buffer->sign = int_float->sign;
177 int_float = val_buffer;
183 assert(int_float->desc.explicit_one <= 1);
185 /* pack sign: move it to the left after exponent AND mantissa */
186 sc_val_from_ulong(int_float->sign, temp);
188 pos = int_float->desc.exponent_size + int_float->desc.mantissa_size + int_float->desc.explicit_one;
189 sc_val_from_ulong(pos, NULL);
190 _shift_left(temp, sc_get_buffer(), packed);
192 /* pack exponent: move it to the left after mantissa */
193 pos = int_float->desc.mantissa_size + int_float->desc.explicit_one;
194 sc_val_from_ulong(pos, shift_val);
195 _shift_left(_exp(int_float), shift_val, temp);
197 /* combine sign|exponent */
198 sc_or(temp, packed, packed);
200 /* extract mantissa */
201 /* remove rounding bits */
202 sc_val_from_ulong(ROUNDING_BITS, shift_val);
203 _shift_right(_mant(int_float), shift_val, temp);
205 /* remove leading 1 (or 0 if denormalized) */
206 sc_max_from_bits(pos, 0, shift_val); /* all mantissa bits are 1's */
207 sc_and(temp, shift_val, temp);
209 /* combine sign|exponent|mantissa */
210 sc_or(temp, packed, packed);
216 * Normalize a fp_value.
218 * @return non-zero if result is exact
220 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky) {
223 char lsb, guard, round, round_dir = 0;
224 char *temp = alloca(value_size);
226 /* save rounding bits at the end */
227 hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
229 if (in_val != out_val) {
230 out_val->sign = in_val->sign;
231 memcpy(&out_val->desc, &in_val->desc, sizeof(out_val->desc));
234 out_val->desc.clss = NORMAL;
236 /* mantissa all zeros, so zero exponent (because of explicit one) */
237 if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
238 sc_val_from_ulong(0, _exp(out_val));
242 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
245 sc_val_from_ulong(-hsb-1, temp);
247 _shift_right(_mant(in_val), temp, _mant(out_val));
249 /* remember if some bits were shifted away */
250 if (sc_had_carry()) {
254 sc_add(_exp(in_val), temp, _exp(out_val));
255 } else if (hsb > -1) {
257 sc_val_from_ulong(hsb+1, temp);
259 _shift_left(_mant(in_val), temp, _mant(out_val));
261 sc_sub(_exp(in_val), temp, _exp(out_val));
264 /* check for exponent underflow */
265 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
266 DEBUGPRINTF(("Exponent underflow!\n"));
267 /* exponent underflow */
268 /* shift the mantissa right to have a zero exponent */
269 sc_val_from_ulong(1, temp);
270 sc_sub(temp, _exp(out_val), NULL);
272 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
273 if (sc_had_carry()) {
277 /* denormalized means exponent of zero */
278 sc_val_from_ulong(0, _exp(out_val));
280 out_val->desc.clss = SUBNORMAL;
283 /* perform rounding by adding a value that clears the guard bit and the round bit
284 * and either causes a carry to round up or not */
285 /* get the last 3 bits of the value */
286 lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
287 guard = (lsb&0x2)>>1;
290 switch (rounding_mode) {
292 /* round to nearest representable value, if in doubt choose the version
294 round_dir = guard && (sticky || round || lsb>>2);
297 /* if positive: round to one if the exact value is bigger, else to zero */
298 round_dir = (!out_val->sign && (guard || round || sticky));
301 /* if negative: round to one if the exact value is bigger, else to zero */
302 round_dir = (out_val->sign && (guard || round || sticky));
305 /* always round to 0 (chopping mode) */
309 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"));
311 if (round_dir == 1) {
312 guard = (round^guard)<<1;
313 lsb = !(round || guard)<<2 | guard | round;
315 lsb = -((guard<<1) | round);
318 /* add the rounded value */
320 sc_val_from_long(lsb, temp);
321 sc_add(_mant(out_val), temp, _mant(out_val));
325 /* could have rounded down to zero */
326 if (sc_is_zero(_mant(out_val)) && (out_val->desc.clss == SUBNORMAL))
327 out_val->desc.clss = ZERO;
329 /* check for rounding overflow */
330 hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
331 if ((out_val->desc.clss != SUBNORMAL) && (hsb < -1)) {
332 sc_val_from_ulong(1, temp);
333 _shift_right(_mant(out_val), temp, _mant(out_val));
334 if (exact && sc_had_carry())
336 sc_add(_exp(out_val), temp, _exp(out_val));
337 } else if ((out_val->desc.clss == SUBNORMAL) && (hsb == -1)) {
338 /* overflow caused the mantissa to be normal again,
339 * so adapt the exponent accordingly */
340 sc_val_from_ulong(1, temp);
341 sc_add(_exp(out_val), temp, _exp(out_val));
343 out_val->desc.clss = NORMAL;
345 /* no further rounding is needed, because rounding overflow means
346 * the carry of the original rounding was propagated all the way
347 * up to the bit left of the radix point. This implies the bits
348 * to the right are all zeros (rounding is +1) */
350 /* check for exponent overflow */
351 sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
352 if (sc_comp(_exp(out_val), temp) != -1) {
353 DEBUGPRINTF(("Exponent overflow!\n"));
354 /* exponent overflow, reaction depends on rounding method:
356 * mode | sign of value | result
357 *--------------------------------------------------------------
358 * TO_NEAREST | + | +inf
360 *--------------------------------------------------------------
361 * TO_POSITIVE | + | +inf
362 * | - | smallest representable value
363 *--------------------------------------------------------------
364 * TO_NEAGTIVE | + | largest representable value
366 *--------------------------------------------------------------
367 * TO_ZERO | + | largest representable value
368 * | - | smallest representable value
369 *--------------------------------------------------------------*/
370 if (out_val->sign == 0) {
371 /* value is positive */
372 switch (rounding_mode) {
375 out_val->desc.clss = INF;
380 fc_get_max(&out_val->desc, out_val);
383 /* value is negative */
384 switch (rounding_mode) {
387 out_val->desc.clss = INF;
392 fc_get_min(&out_val->desc, out_val);
400 * Operations involving NaN's must return NaN.
401 * They are NOT exact.
403 #define handle_NAN(a, b, result) \
405 if (a->desc.clss == NAN) { \
406 if (a != result) memcpy(result, a, calc_buffer_size); \
410 if (b->desc.clss == NAN) { \
411 if (b != result) memcpy(result, b, calc_buffer_size); \
419 * calculate a + b, where a is the value with the bigger exponent
421 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result) {
430 handle_NAN(a, b, result);
432 /* make sure result has a descriptor */
433 if (result != a && result != b)
434 result->desc = a->desc;
436 /* determine if this is an addition or subtraction */
437 sign = a->sign ^ b->sign;
439 /* produce NaN on inf - inf */
440 if (sign && (a->desc.clss == INF) && (b->desc.clss == INF)) {
442 fc_get_qnan(&a->desc, result);
446 temp = alloca(value_size);
447 exp_diff = alloca(value_size);
449 /* get exponent difference */
450 sc_sub(_exp(a), _exp(b), exp_diff);
452 /* initially set sign to be the sign of a, special treatment of subtraction
453 * when exponents are equal is required though.
454 * Also special care about the sign is needed when the mantissas are equal
456 if (sign && sc_val_to_long(exp_diff) == 0) {
457 switch (sc_comp(_mant(a), _mant(b))) {
459 res_sign = a->sign; /* abs(a) is bigger and a is negative */
462 res_sign = (rounding_mode == FC_TONEGATIVE);
465 res_sign = b->sign; /* abs(b) is bigger and b is negative */
468 /* can't be reached */
475 result->sign = res_sign;
477 /* sign has been taken care of, check for special cases */
478 if (a->desc.clss == ZERO || b->desc.clss == INF) {
480 memcpy(result, b, calc_buffer_size);
481 fc_exact = b->desc.clss == NORMAL;
482 result->sign = res_sign;
485 if (b->desc.clss == ZERO || a->desc.clss == INF) {
487 memcpy(result, a, calc_buffer_size);
488 fc_exact = a->desc.clss == NORMAL;
489 result->sign = res_sign;
493 /* shift the smaller value to the right to align the radix point */
494 /* subnormals have their radix point shifted to the right,
495 * take care of this first */
496 if ((b->desc.clss == SUBNORMAL) && (a->desc.clss != SUBNORMAL)) {
497 sc_val_from_ulong(1, temp);
498 sc_sub(exp_diff, temp, exp_diff);
501 _shift_right(_mant(b), exp_diff, temp);
502 sticky = sc_had_carry();
505 if (sticky && sign) {
506 /* if subtracting a little more than the represented value or adding a little
507 * more than the represented value to a negative value this, in addition to the
508 * still set sticky bit, takes account of the 'little more' */
509 char *temp1 = alloca(calc_buffer_size);
510 sc_val_from_ulong(1, temp1);
511 sc_add(temp, temp1, temp);
515 if (sc_comp(_mant(a), temp) == -1)
516 sc_sub(temp, _mant(a), _mant(result));
518 sc_sub(_mant(a), temp, _mant(result));
520 sc_add(_mant(a), temp, _mant(result));
523 /* _normalize expects a 'normal' radix point, adding two subnormals
524 * results in a subnormal radix point -> shifting before normalizing */
525 if ((a->desc.clss == SUBNORMAL) && (b->desc.clss == SUBNORMAL)) {
526 sc_val_from_ulong(1, NULL);
527 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
530 /* resulting exponent is the bigger one */
531 memmove(_exp(result), _exp(a), value_size);
533 fc_exact &= normalize(result, result, sticky);
539 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result) {
546 handle_NAN(a, b, result);
548 temp = alloca(value_size);
550 if (result != a && result != b)
551 result->desc = a->desc;
553 result->sign = res_sign = a->sign ^ b->sign;
555 /* produce NaN on 0 * inf */
556 if (a->desc.clss == ZERO) {
557 if (b->desc.clss == INF) {
558 fc_get_qnan(&a->desc, result);
562 memcpy(result, a, calc_buffer_size);
563 result->sign = res_sign;
567 if (b->desc.clss == ZERO) {
568 if (a->desc.clss == INF) {
569 fc_get_qnan(&a->desc, result);
573 memcpy(result, b, calc_buffer_size);
574 result->sign = res_sign;
579 if (a->desc.clss == INF) {
582 memcpy(result, a, calc_buffer_size);
583 result->sign = res_sign;
586 if (b->desc.clss == INF) {
589 memcpy(result, b, calc_buffer_size);
590 result->sign = res_sign;
594 /* exp = exp(a) + exp(b) - excess */
595 sc_add(_exp(a), _exp(b), _exp(result));
597 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
598 sc_sub(_exp(result), temp, _exp(result));
600 /* mixed normal, subnormal values introduce an error of 1, correct it */
601 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
602 sc_val_from_ulong(1, temp);
603 sc_add(_exp(result), temp, _exp(result));
606 sc_mul(_mant(a), _mant(b), _mant(result));
608 /* realign result: after a multiplication the digits right of the radix
609 * point are the sum of the factors' digits after the radix point. As all
610 * values are normalized they both have the same amount of these digits,
611 * which has to be restored by proper shifting
612 * because of the rounding bits */
613 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
615 _shift_right(_mant(result), temp, _mant(result));
616 sticky = sc_had_carry();
619 fc_exact &= normalize(result, result, sticky);
625 static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result) {
627 char *temp, *dividend;
632 handle_NAN(a, b, result);
634 temp = alloca(value_size);
635 dividend = alloca(value_size);
637 if (result != a && result != b)
638 result->desc = a->desc;
640 result->sign = res_sign = a->sign ^ b->sign;
642 /* produce NAN on 0/0 and inf/inf */
643 if (a->desc.clss == ZERO) {
644 if (b->desc.clss == ZERO) {
646 fc_get_qnan(&a->desc, result);
651 memcpy(result, a, calc_buffer_size);
652 result->sign = res_sign;
657 if (b->desc.clss == INF) {
659 if (a->desc.clss == INF) {
661 fc_get_qnan(&a->desc, result);
664 sc_val_from_ulong(0, NULL);
665 _save_result(_exp(result));
666 _save_result(_mant(result));
667 result->desc.clss = ZERO;
672 if (a->desc.clss == INF) {
676 memcpy(result, a, calc_buffer_size);
677 result->sign = res_sign;
680 if (b->desc.clss == ZERO) {
682 /* division by zero */
684 fc_get_minusinf(&a->desc, result);
686 fc_get_plusinf(&a->desc, result);
690 /* exp = exp(a) - exp(b) + excess - 1*/
691 sc_sub(_exp(a), _exp(b), _exp(result));
692 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 2, temp);
693 sc_add(_exp(result), temp, _exp(result));
695 /* mixed normal, subnormal values introduce an error of 1, correct it */
696 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
697 sc_val_from_ulong(1, temp);
698 sc_add(_exp(result), temp, _exp(result));
701 /* mant(res) = mant(a) / 1/2mant(b) */
702 /* to gain more bits of precision in the result the dividend could be
703 * shifted left, as this operation does not loose bits. This would not
704 * fit into the integer precision, but due to the rounding bits (which
705 * are always zero because the values are all normalized) the divisor
706 * can be shifted right instead to achieve the same result */
707 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
709 _shift_left(_mant(a), temp, dividend);
712 char *divisor = alloca(calc_buffer_size);
713 sc_val_from_ulong(1, divisor);
714 _shift_right(_mant(b), divisor, divisor);
715 sc_div(dividend, divisor, _mant(result));
716 sticky = sc_had_carry();
720 fc_exact &= normalize(result, result, sticky);
724 static void _power_of_ten(int exp, ieee_descriptor_t *desc, char *result) {
731 /* set new descriptor (else result is supposed to already have one) */
733 result->desc = *desc;
735 build = alloca(value_size);
736 temp = alloca(value_size);
738 sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
741 /* temp is value of ten now */
742 sc_val_from_ulong(10, NULL);
745 for (exp--; exp > 0; exp--) {
747 sc_mul(build, temp, NULL);
751 /* temp is amount of left shift needed to put the value left of the radix point */
752 sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
754 _shift_left(build, temp, _mant(result));
756 _normalize(result, result, 0);
762 * Truncate the fractional part away.
764 * This does not clip to any integer range.
766 static void _trunc(const fp_value *a, fp_value *result) {
768 * When exponent == 0 all bits left of the radix point
769 * are the integral part of the value. For 15bit exp_size
770 * this would require a left shift of max. 16383 bits which
772 * But it is enough to ensure that no bit right of the radix
773 * point remains set. This restricts the interesting
774 * exponents to the interval [0, mant_size-1].
775 * Outside this interval the truncated value is either 0 or
776 * it does not have fractional parts.
779 int exp_bias, exp_val;
782 /* fixme: can be exact */
785 temp = alloca(value_size);
788 result->desc = a->desc;
790 exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
791 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
794 sc_val_from_ulong(0, NULL);
795 _save_result(_exp(result));
796 _save_result(_mant(result));
797 result->desc.clss = ZERO;
802 if (exp_val > a->desc.mantissa_size) {
804 memcpy(result, a, calc_buffer_size);
809 /* set up a proper mask to delete all bits right of the
810 * radix point if the mantissa had been shifted until exp == 0 */
811 sc_max_from_bits(1 + exp_val, 0, temp);
812 sc_val_from_long(a->desc.mantissa_size - exp_val + 2, NULL);
813 _shift_left(temp, sc_get_buffer(), temp);
815 /* and the mask and return the result */
816 sc_and(_mant(a), temp, _mant(result));
818 if (a != result) memcpy(_exp(result), _exp(a), value_size);
822 * functions defined in fltcalc.h
824 const void *fc_get_buffer(void) {
828 int fc_get_buffer_length(void) {
829 return calc_buffer_size;
832 void *fc_val_from_str(const char *str, unsigned int len, const ieee_descriptor_t *desc, void *result) {
844 int exp_int, hsb, state;
849 char *mant_str, *exp_val, *power_val;
852 if (result == NULL) result = calc_buffer;
854 exp_val = alloca(value_size);
855 power_val = alloca(calc_buffer_size);
856 mant_str = alloca((len)?(len):(strlen(str)));
858 result->desc.exponent_size = desc->exponent_size;
859 result->desc.mantissa_size = desc->mantissa_size;
860 result->desc.explicit_one = desc->explicit_one;
861 result->desc.clss = NORMAL;
868 while (len == 0 || str-old_str < len) {
884 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
891 state = RIGHT_OF_DOT;
902 fail_char(old_str, len, str - old_str);
908 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
909 mant_str[pos++] = *(str++);
913 state = RIGHT_OF_DOT;
924 mant_str[pos] = '\0';
928 fail_char(old_str, len, str - old_str);
934 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
935 mant_str[pos++] = *(str++);
946 mant_str[pos] = '\0';
950 fail_char(old_str, len, str - old_str);
960 if (*(str-1) != 'e' && *(str-1) != 'E') fail_char(old_str, len, str - old_str);
964 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
965 mant_str[pos] = '\0';
972 fail_char(old_str, len, str - old_str);
978 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
983 case '\0': goto done;
986 fail_char(old_str, len, str - old_str);
989 } /* switch(state) */
992 sc_val_from_str(mant_str, strlen(mant_str), _mant(result));
994 /* shift to put value left of radix point */
995 sc_val_from_ulong(mant_size + ROUNDING_BITS, exp_val);
997 _shift_left(_mant(result), exp_val, _mant(result));
999 sc_val_from_ulong((1 << (exp_size - 1)) - 1, _exp(result));
1001 _normalize(result, result, 0);
1003 if (state == EXPONENT) {
1004 exp_int -= atoi(str-pos);
1007 _power_of_ten(exp_int, &result->desc, power_val);
1009 _fdiv(result, power_val, result);
1013 /* XXX excuse of an implementation to make things work */
1015 fp_value *tmp = alloca(calc_buffer_size);
1016 ieee_descriptor_t tmp_desc;
1019 #ifdef HAVE_LONG_DOUBLE
1020 val = strtold(str, NULL);
1021 DEBUGPRINTF(("val_from_str(%s)\n", str));
1022 tmp_desc.exponent_size = 15;
1023 tmp_desc.mantissa_size = 63;
1024 tmp_desc.explicit_one = 1;
1025 tmp_desc.clss = NORMAL;
1026 fc_val_from_ieee754(val, &tmp_desc, tmp);
1028 val = strtod(str, NULL);
1029 DEBUGPRINTF(("val_from_str(%s)\n", str));
1030 tmp_desc.exponent_size = 11;
1031 tmp_desc.mantissa_size = 52;
1032 tmp_desc.explicit_one = 0;
1033 tmp_desc.clss = NORMAL;
1034 fc_val_from_ieee754(val, &tmp_desc, tmp);
1035 #endif /* HAVE_LONG_DOUBLE */
1036 return fc_cast(tmp, desc, result);
1040 fp_value *fc_val_from_ieee754(LLDBL l, const ieee_descriptor_t *desc, fp_value *result) {
1042 int bias_res, bias_val, mant_val;
1044 UINT32 sign, 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);
1232 /* set the descriptor of the new value */
1233 result->desc.exponent_size = desc->exponent_size;
1234 result->desc.mantissa_size = desc->mantissa_size;
1235 result->desc.explicit_one = desc->explicit_one;
1236 result->desc.clss = value->desc.clss;
1238 result->sign = value->sign;
1240 /* when the mantissa sizes differ normalizing has to shift to align it.
1241 * this would change the exponent, which is unwanted. So calculate this
1242 * offset and add it */
1243 val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1244 res_bias = (1 << (desc->exponent_size - 1)) - 1;
1246 exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - desc->mantissa_size);
1247 sc_val_from_long(exp_offset, temp);
1248 sc_add(_exp(value), temp, _exp(result));
1250 /* _normalize expects normalized radix point */
1251 if (value->desc.clss == SUBNORMAL) {
1252 sc_val_from_ulong(1, NULL);
1253 _shift_left(_mant(value), sc_get_buffer(), _mant(result));
1254 } else if (value != result) {
1255 memcpy(_mant(result), _mant(value), value_size);
1257 memmove(_mant(result), _mant(value), value_size);
1260 normalize(result, result, 0);
1261 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1265 fp_value *fc_get_max(const ieee_descriptor_t *desc, fp_value *result) {
1266 if (result == NULL) result = calc_buffer;
1268 result->desc.exponent_size = desc->exponent_size;
1269 result->desc.mantissa_size = desc->mantissa_size;
1270 result->desc.explicit_one = desc->explicit_one;
1271 result->desc.clss = NORMAL;
1275 sc_val_from_ulong((1 << desc->exponent_size) - 2, _exp(result));
1277 sc_max_from_bits(desc->mantissa_size + 1, 0, _mant(result));
1278 sc_val_from_ulong(ROUNDING_BITS, NULL);
1279 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1284 fp_value *fc_get_min(const ieee_descriptor_t *desc, fp_value *result) {
1285 if (result == NULL) result = calc_buffer;
1287 fc_get_max(desc, result);
1293 fp_value *fc_get_snan(const ieee_descriptor_t *desc, fp_value *result) {
1294 if (result == NULL) result = calc_buffer;
1296 result->desc.exponent_size = desc->exponent_size;
1297 result->desc.mantissa_size = desc->mantissa_size;
1298 result->desc.explicit_one = desc->explicit_one;
1299 result->desc.clss = NAN;
1303 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1305 /* signaling NaN has non-zero mantissa with msb not set */
1306 sc_val_from_ulong(1, _mant(result));
1311 fp_value *fc_get_qnan(const ieee_descriptor_t *desc, fp_value *result) {
1312 if (result == NULL) result = calc_buffer;
1314 result->desc.exponent_size = desc->exponent_size;
1315 result->desc.mantissa_size = desc->mantissa_size;
1316 result->desc.explicit_one = desc->explicit_one;
1317 result->desc.clss = NAN;
1321 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1323 /* quiet NaN has the msb of the mantissa set, so shift one there */
1324 sc_val_from_ulong(1, _mant(result));
1325 /* mantissa_size >+< 1 because of two extra rounding bits */
1326 sc_val_from_ulong(desc->mantissa_size + 1, NULL);
1327 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1332 fp_value *fc_get_plusinf(const ieee_descriptor_t *desc, fp_value *result) {
1333 if (result == NULL) result = calc_buffer;
1335 result->desc.exponent_size = desc->exponent_size;
1336 result->desc.mantissa_size = desc->mantissa_size;
1337 result->desc.explicit_one = desc->explicit_one;
1338 result->desc.clss = INF;
1342 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1344 sc_val_from_ulong(0, _mant(result));
1349 fp_value *fc_get_minusinf(const ieee_descriptor_t *desc, fp_value *result) {
1350 if (result == NULL) result = calc_buffer;
1352 fc_get_plusinf(desc, result);
1358 int fc_comp(const fp_value *val_a, const fp_value *val_b) {
1362 * shortcut: if both values are identical, they are either
1363 * Unordered if NaN or equal
1366 return val_a->desc.clss == NAN ? 2 : 0;
1368 /* unordered if one is a NaN */
1369 if (val_a->desc.clss == NAN || val_b->desc.clss == NAN)
1372 /* zero is equal independent of sign */
1373 if ((val_a->desc.clss == ZERO) && (val_b->desc.clss == ZERO))
1376 /* different signs make compare easy */
1377 if (val_a->sign != val_b->sign)
1378 return (val_a->sign == 0) ? (1) : (-1);
1380 mul = val_a->sign ? -1 : 1;
1382 /* both infinity means equality */
1383 if ((val_a->desc.clss == INF) && (val_b->desc.clss == INF))
1386 /* infinity is bigger than the rest */
1387 if (val_a->desc.clss == INF)
1389 if (val_b->desc.clss == INF)
1392 /* check first exponent, that mantissa if equal */
1393 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1399 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1405 int fc_is_zero(const fp_value *a) {
1406 return a->desc.clss == ZERO;
1409 int fc_is_negative(const fp_value *a) {
1413 int fc_is_inf(const fp_value *a) {
1414 return a->desc.clss == INF;
1417 int fc_is_nan(const fp_value *a) {
1418 return a->desc.clss == NAN;
1421 int fc_is_subnormal(const fp_value *a) {
1422 return a->desc.clss == SUBNORMAL;
1425 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base) {
1429 mul_1 = alloca(calc_buffer_size);
1433 switch ((value_class_t)val->desc.clss) {
1435 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1438 snprintf(buf, buflen, "NaN");
1441 snprintf(buf, buflen, "0.0");
1444 flt_val = fc_val_to_ieee754(val);
1445 #ifdef HAVE_LONG_DOUBLE
1446 /* XXX 30 is arbitrary */
1447 snprintf(buf, buflen, "%.30LE", flt_val);
1449 snprintf(buf, buflen, "%.18E", flt_val);
1455 switch ((value_class_t)val->desc.clss) {
1457 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1460 snprintf(buf, buflen, "NAN");
1463 snprintf(buf, buflen, "0.0");
1466 flt_val = fc_val_to_ieee754(val);
1467 #ifdef HAVE_LONG_DOUBLE
1468 snprintf(buf, buflen, "%LA", flt_val);
1470 snprintf(buf, buflen, "%A", flt_val);
1477 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1478 buf[buflen - 1] = '\0';
1484 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs) {
1485 /* this is used to cache the packed version of the value */
1486 static char *packed_value = NULL;
1488 if (packed_value == NULL) packed_value = XMALLOCN(char, value_size);
1491 pack(value, packed_value);
1493 return sc_sub_bits(packed_value, num_bits, byte_ofs);
1496 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1497 int fc_zero_mantissa(const fp_value *value) {
1498 return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1501 /* Returns the exponent of a value. */
1502 int fc_get_exponent(const fp_value *value) {
1503 int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1504 return sc_val_to_long(_exp(value)) - exp_bias;
1507 /* Return non-zero if a given value can be converted lossless into another precision */
1508 int fc_can_lossless_conv_to(const fp_value *value, const ieee_descriptor_t *desc) {
1512 /* handle some special cases first */
1513 switch (value->desc.clss) {
1522 /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
1523 exp_bias = (1 << (desc->exponent_size - 1)) - 1;
1524 v = fc_get_exponent(value) + exp_bias;
1525 if (0 < v && v < (1 << desc->exponent_size) - 1) {
1526 /* exponent can be encoded, now check the mantissa */
1527 v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
1528 return v < desc->mantissa_size;
1534 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode) {
1535 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1536 rounding_mode = mode;
1538 return rounding_mode;
1541 fc_rounding_mode_t fc_get_rounding_mode(void) {
1542 return rounding_mode;
1545 void init_fltcalc(int precision) {
1546 if (calc_buffer == NULL) {
1547 /* does nothing if already init */
1548 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1550 init_strcalc(precision + 2 + ROUNDING_BITS);
1552 /* needs additionally rounding bits, one bit as explicit 1., and one for
1553 * addition overflow */
1554 max_precision = sc_get_precision() - (2 + ROUNDING_BITS);
1555 if (max_precision < precision)
1556 printf("WARNING: not enough precision available, using %d\n", max_precision);
1558 rounding_mode = FC_TONEAREST;
1559 value_size = sc_get_buffer_length();
1560 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1562 calc_buffer = xmalloc(calc_buffer_size);
1563 memset(calc_buffer, 0, calc_buffer_size);
1564 DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
1565 #ifdef HAVE_LONG_DOUBLE
1566 DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
1568 DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
1570 #ifdef WORDS_BIGENDIAN
1571 DEBUGPRINTF(("\tWord order is big endian\n\n"));
1573 DEBUGPRINTF(("\tWord order is little endian\n\n"));
1578 void finish_fltcalc (void) {
1579 free(calc_buffer); calc_buffer = NULL;
1582 #ifdef FLTCALC_TRACE_CALC
1583 static char buffer[100];
1586 /* definition of interface functions */
1587 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result) {
1588 if (result == NULL) result = calc_buffer;
1590 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1591 TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1593 /* make the value with the bigger exponent the first one */
1594 if (sc_comp(_exp(a), _exp(b)) == -1)
1595 _fadd(b, a, result);
1597 _fadd(a, b, result);
1599 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1603 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result) {
1606 if (result == NULL) result = calc_buffer;
1608 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1609 TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1611 temp = alloca(calc_buffer_size);
1612 memcpy(temp, b, calc_buffer_size);
1613 temp->sign = !b->sign;
1614 if (sc_comp(_exp(a), _exp(temp)) == -1)
1615 _fadd(temp, a, result);
1617 _fadd(a, temp, result);
1619 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1623 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result) {
1624 if (result == NULL) result = calc_buffer;
1626 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1627 TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1629 _fmul(a, b, result);
1631 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1635 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result) {
1636 if (result == NULL) result = calc_buffer;
1638 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1639 TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1641 _fdiv(a, b, result);
1643 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1647 fp_value *fc_neg(const fp_value *a, fp_value *result) {
1648 if (result == NULL) result = calc_buffer;
1650 TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1653 memcpy(result, a, calc_buffer_size);
1654 result->sign = !a->sign;
1656 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1660 fp_value *fc_int(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)));
1664 TRACEPRINTF(("truncated to integer "));
1668 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1672 fp_value *fc_rnd(const fp_value *a, fp_value *result) {
1673 if (result == NULL) result = calc_buffer;
1676 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1677 TRACEPRINTF(("rounded to integer "));
1679 assert(!"fc_rnd() not yet implemented");
1681 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1686 * convert a floating point value into an sc value ...
1688 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode) {
1689 if (a->desc.clss == NORMAL) {
1690 int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
1691 int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
1694 if (a->sign && !mode_is_signed(dst_mode)) {
1695 /* FIXME: for now we cannot convert this */
1699 assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
1700 shift = exp_val - (a->desc.mantissa_size + ROUNDING_BITS);
1703 sc_shlI(_mant(a), shift, 64, 0, result);
1705 sc_shrI(_mant(a), -shift, 64, 0, result);
1708 /* check for overflow */
1709 highest = sc_get_highest_set_bit(result);
1711 if (mode_is_signed(dst_mode)) {
1712 if (highest == sc_get_lowest_set_bit(result)) {
1713 /* need extra test for MIN_INT */
1714 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1715 /* FIXME: handle overflow */
1719 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1720 /* FIXME: handle overflow */
1725 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1726 /* FIXME: handle overflow */
1732 sc_neg(result, result);
1736 else if (a->desc.clss == ZERO) {
1744 unsigned fc_set_immediate_precision(unsigned bits) {
1745 unsigned old = immediate_prec;
1747 immediate_prec = bits;
1751 int fc_is_exact(void) {