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>
51 /** The number of extra precision rounding bits */
52 #define ROUNDING_BITS 2
54 typedef uint32_t UINT32;
56 #ifdef HAVE_LONG_DOUBLE
57 #ifdef WORDS_BIGENDIAN
64 volatile long double d;
73 volatile long double d;
77 #ifdef WORDS_BIGENDIAN
96 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
98 /* our floating point value */
100 ieee_descriptor_t desc;
102 char value[1]; /* exp[value_size] + mant[value_size] */
105 #define _exp(a) &((a)->value[0])
106 #define _mant(a) &((a)->value[value_size])
108 #define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
109 #define _shift_right(x, y, res) sc_shr((x), (y), value_size*4, 0, (res))
110 #define _shift_left(x, y, res) sc_shl((x), (y), value_size*4, 0, (res))
114 # define DEBUGPRINTF(x) printf x
116 # define DEBUGPRINTF(x) ((void)0)
119 #ifdef FLTCALC_TRACE_CALC
120 # define TRACEPRINTF(x) printf x
122 # define TRACEPRINTF(x) ((void)0)
125 /** The immediate precision. */
126 static unsigned immediate_prec = 0;
128 /** A temporal buffer. */
129 static fp_value *calc_buffer = NULL;
131 /** Current rounding mode.*/
132 static fc_rounding_mode_t rounding_mode;
134 static int calc_buffer_size;
135 static int value_size;
136 static int max_precision;
139 static int fc_exact = 1;
142 static void fail_char(const char *str, unsigned int len, int pos) {
144 printf("ERROR: Unexpected character '%c'\n", *(str + pos));
146 printf("ERROR: Unexpected end of string\n");
147 while (len-- && *str) printf("%c", *str++); printf("\n");
148 while (pos--) printf(" "); printf("^\n");
149 /* the front end has to to check constant strings */
154 /** pack machine-like */
155 static void *pack(const fp_value *int_float, void *packed) {
158 fp_value *val_buffer;
161 temp = alloca(value_size);
162 shift_val = alloca(value_size);
164 switch ((value_class_t)int_float->desc.clss) {
166 val_buffer = alloca(calc_buffer_size);
167 fc_get_qnan(&int_float->desc, val_buffer);
168 int_float = val_buffer;
172 val_buffer = alloca(calc_buffer_size);
173 fc_get_plusinf(&int_float->desc, val_buffer);
174 val_buffer->sign = int_float->sign;
175 int_float = val_buffer;
181 assert(int_float->desc.explicit_one <= 1);
183 /* pack sign: move it to the left after exponent AND mantissa */
184 sc_val_from_ulong(int_float->sign, temp);
186 pos = int_float->desc.exponent_size + int_float->desc.mantissa_size + int_float->desc.explicit_one;
187 sc_val_from_ulong(pos, NULL);
188 _shift_left(temp, sc_get_buffer(), packed);
190 /* pack exponent: move it to the left after mantissa */
191 pos = int_float->desc.mantissa_size + int_float->desc.explicit_one;
192 sc_val_from_ulong(pos, shift_val);
193 _shift_left(_exp(int_float), shift_val, temp);
195 /* combine sign|exponent */
196 sc_or(temp, packed, packed);
198 /* extract mantissa */
199 /* remove rounding bits */
200 sc_val_from_ulong(ROUNDING_BITS, shift_val);
201 _shift_right(_mant(int_float), shift_val, temp);
203 /* remove leading 1 (or 0 if denormalized) */
204 sc_max_from_bits(pos, 0, shift_val); /* all mantissa bits are 1's */
205 sc_and(temp, shift_val, temp);
207 /* combine sign|exponent|mantissa */
208 sc_or(temp, packed, packed);
214 * Normalize a fp_value.
216 * @return non-zero if result is exact
218 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky) {
221 char lsb, guard, round, round_dir = 0;
222 char *temp = alloca(value_size);
224 /* save rounding bits at the end */
225 hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
227 if (in_val != out_val) {
228 out_val->sign = in_val->sign;
229 memcpy(&out_val->desc, &in_val->desc, sizeof(out_val->desc));
232 out_val->desc.clss = NORMAL;
234 /* mantissa all zeros, so zero exponent (because of explicit one) */
235 if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
236 sc_val_from_ulong(0, _exp(out_val));
240 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
243 sc_val_from_ulong(-hsb-1, temp);
245 _shift_right(_mant(in_val), temp, _mant(out_val));
247 /* remember if some bits were shifted away */
248 if (sc_had_carry()) {
252 sc_add(_exp(in_val), temp, _exp(out_val));
253 } else if (hsb > -1) {
255 sc_val_from_ulong(hsb+1, temp);
257 _shift_left(_mant(in_val), temp, _mant(out_val));
259 sc_sub(_exp(in_val), temp, _exp(out_val));
262 /* check for exponent underflow */
263 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
264 DEBUGPRINTF(("Exponent underflow!\n"));
265 /* exponent underflow */
266 /* shift the mantissa right to have a zero exponent */
267 sc_val_from_ulong(1, temp);
268 sc_sub(temp, _exp(out_val), NULL);
270 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
271 if (sc_had_carry()) {
275 /* denormalized means exponent of zero */
276 sc_val_from_ulong(0, _exp(out_val));
278 out_val->desc.clss = SUBNORMAL;
281 /* perform rounding by adding a value that clears the guard bit and the round bit
282 * and either causes a carry to round up or not */
283 /* get the last 3 bits of the value */
284 lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
285 guard = (lsb&0x2)>>1;
288 switch (rounding_mode) {
290 /* round to nearest representable value, if in doubt choose the version
292 round_dir = guard && (sticky || round || lsb>>2);
295 /* if positive: round to one if the exact value is bigger, else to zero */
296 round_dir = (!out_val->sign && (guard || round || sticky));
299 /* if negative: round to one if the exact value is bigger, else to zero */
300 round_dir = (out_val->sign && (guard || round || sticky));
303 /* always round to 0 (chopping mode) */
307 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"));
309 if (round_dir == 1) {
310 guard = (round^guard)<<1;
311 lsb = !(round || guard)<<2 | guard | round;
313 lsb = -((guard<<1) | round);
316 /* add the rounded value */
318 sc_val_from_long(lsb, temp);
319 sc_add(_mant(out_val), temp, _mant(out_val));
323 /* could have rounded down to zero */
324 if (sc_is_zero(_mant(out_val)) && (out_val->desc.clss == SUBNORMAL))
325 out_val->desc.clss = ZERO;
327 /* check for rounding overflow */
328 hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
329 if ((out_val->desc.clss != SUBNORMAL) && (hsb < -1)) {
330 sc_val_from_ulong(1, temp);
331 _shift_right(_mant(out_val), temp, _mant(out_val));
332 if (exact && sc_had_carry())
334 sc_add(_exp(out_val), temp, _exp(out_val));
335 } else if ((out_val->desc.clss == SUBNORMAL) && (hsb == -1)) {
336 /* overflow caused the mantissa to be normal again,
337 * so adapt the exponent accordingly */
338 sc_val_from_ulong(1, temp);
339 sc_add(_exp(out_val), temp, _exp(out_val));
341 out_val->desc.clss = NORMAL;
343 /* no further rounding is needed, because rounding overflow means
344 * the carry of the original rounding was propagated all the way
345 * up to the bit left of the radix point. This implies the bits
346 * to the right are all zeros (rounding is +1) */
348 /* check for exponent overflow */
349 sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
350 if (sc_comp(_exp(out_val), temp) != -1) {
351 DEBUGPRINTF(("Exponent overflow!\n"));
352 /* exponent overflow, reaction depends on rounding method:
354 * mode | sign of value | result
355 *--------------------------------------------------------------
356 * TO_NEAREST | + | +inf
358 *--------------------------------------------------------------
359 * TO_POSITIVE | + | +inf
360 * | - | smallest representable value
361 *--------------------------------------------------------------
362 * TO_NEAGTIVE | + | largest representable value
364 *--------------------------------------------------------------
365 * TO_ZERO | + | largest representable value
366 * | - | smallest representable value
367 *--------------------------------------------------------------*/
368 if (out_val->sign == 0) {
369 /* value is positive */
370 switch (rounding_mode) {
373 out_val->desc.clss = INF;
378 fc_get_max(&out_val->desc, out_val);
381 /* value is negative */
382 switch (rounding_mode) {
385 out_val->desc.clss = INF;
390 fc_get_min(&out_val->desc, out_val);
398 * Operations involving NaN's must return NaN.
399 * They are NOT exact.
401 #define handle_NAN(a, b, result) \
403 if (a->desc.clss == NAN) { \
404 if (a != result) memcpy(result, a, calc_buffer_size); \
408 if (b->desc.clss == NAN) { \
409 if (b != result) memcpy(result, b, calc_buffer_size); \
417 * calculate a + b, where a is the value with the bigger exponent
419 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result) {
428 handle_NAN(a, b, result);
430 /* make sure result has a descriptor */
431 if (result != a && result != b)
432 result->desc = a->desc;
434 /* determine if this is an addition or subtraction */
435 sign = a->sign ^ b->sign;
437 /* produce NaN on inf - inf */
438 if (sign && (a->desc.clss == INF) && (b->desc.clss == INF)) {
440 fc_get_qnan(&a->desc, result);
444 temp = alloca(value_size);
445 exp_diff = alloca(value_size);
447 /* get exponent difference */
448 sc_sub(_exp(a), _exp(b), exp_diff);
450 /* initially set sign to be the sign of a, special treatment of subtraction
451 * when exponents are equal is required though.
452 * Also special care about the sign is needed when the mantissas are equal
454 if (sign && sc_val_to_long(exp_diff) == 0) {
455 switch (sc_comp(_mant(a), _mant(b))) {
457 res_sign = a->sign; /* abs(a) is bigger and a is negative */
460 res_sign = (rounding_mode == FC_TONEGATIVE);
463 res_sign = b->sign; /* abs(b) is bigger and b is negative */
466 /* can't be reached */
473 result->sign = res_sign;
475 /* sign has been taken care of, check for special cases */
476 if (a->desc.clss == ZERO || b->desc.clss == INF) {
478 memcpy(result, b, calc_buffer_size);
479 fc_exact = b->desc.clss == NORMAL;
480 result->sign = res_sign;
483 if (b->desc.clss == ZERO || a->desc.clss == INF) {
485 memcpy(result, a, calc_buffer_size);
486 fc_exact = a->desc.clss == NORMAL;
487 result->sign = res_sign;
491 /* shift the smaller value to the right to align the radix point */
492 /* subnormals have their radix point shifted to the right,
493 * take care of this first */
494 if ((b->desc.clss == SUBNORMAL) && (a->desc.clss != SUBNORMAL)) {
495 sc_val_from_ulong(1, temp);
496 sc_sub(exp_diff, temp, exp_diff);
499 _shift_right(_mant(b), exp_diff, temp);
500 sticky = sc_had_carry();
503 if (sticky && sign) {
504 /* if subtracting a little more than the represented value or adding a little
505 * more than the represented value to a negative value this, in addition to the
506 * still set sticky bit, takes account of the 'little more' */
507 char *temp1 = alloca(calc_buffer_size);
508 sc_val_from_ulong(1, temp1);
509 sc_add(temp, temp1, temp);
513 if (sc_comp(_mant(a), temp) == -1)
514 sc_sub(temp, _mant(a), _mant(result));
516 sc_sub(_mant(a), temp, _mant(result));
518 sc_add(_mant(a), temp, _mant(result));
521 /* _normalize expects a 'normal' radix point, adding two subnormals
522 * results in a subnormal radix point -> shifting before normalizing */
523 if ((a->desc.clss == SUBNORMAL) && (b->desc.clss == SUBNORMAL)) {
524 sc_val_from_ulong(1, NULL);
525 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
528 /* resulting exponent is the bigger one */
529 memmove(_exp(result), _exp(a), value_size);
531 fc_exact &= normalize(result, result, sticky);
537 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result) {
544 handle_NAN(a, b, result);
546 temp = alloca(value_size);
548 if (result != a && result != b)
549 result->desc = a->desc;
551 result->sign = res_sign = a->sign ^ b->sign;
553 /* produce NaN on 0 * inf */
554 if (a->desc.clss == ZERO) {
555 if (b->desc.clss == INF) {
556 fc_get_qnan(&a->desc, result);
560 memcpy(result, a, calc_buffer_size);
561 result->sign = res_sign;
565 if (b->desc.clss == ZERO) {
566 if (a->desc.clss == INF) {
567 fc_get_qnan(&a->desc, result);
571 memcpy(result, b, calc_buffer_size);
572 result->sign = res_sign;
577 if (a->desc.clss == INF) {
580 memcpy(result, a, calc_buffer_size);
581 result->sign = res_sign;
584 if (b->desc.clss == INF) {
587 memcpy(result, b, calc_buffer_size);
588 result->sign = res_sign;
592 /* exp = exp(a) + exp(b) - excess */
593 sc_add(_exp(a), _exp(b), _exp(result));
595 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
596 sc_sub(_exp(result), temp, _exp(result));
598 /* mixed normal, subnormal values introduce an error of 1, correct it */
599 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
600 sc_val_from_ulong(1, temp);
601 sc_add(_exp(result), temp, _exp(result));
604 sc_mul(_mant(a), _mant(b), _mant(result));
606 /* realign result: after a multiplication the digits right of the radix
607 * point are the sum of the factors' digits after the radix point. As all
608 * values are normalized they both have the same amount of these digits,
609 * which has to be restored by proper shifting
610 * because of the rounding bits */
611 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
613 _shift_right(_mant(result), temp, _mant(result));
614 sticky = sc_had_carry();
617 fc_exact &= normalize(result, result, sticky);
623 static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result) {
625 char *temp, *dividend;
630 handle_NAN(a, b, result);
632 temp = alloca(value_size);
633 dividend = alloca(value_size);
635 if (result != a && result != b)
636 result->desc = a->desc;
638 result->sign = res_sign = a->sign ^ b->sign;
640 /* produce NAN on 0/0 and inf/inf */
641 if (a->desc.clss == ZERO) {
642 if (b->desc.clss == ZERO) {
644 fc_get_qnan(&a->desc, result);
649 memcpy(result, a, calc_buffer_size);
650 result->sign = res_sign;
655 if (b->desc.clss == INF) {
657 if (a->desc.clss == INF) {
659 fc_get_qnan(&a->desc, result);
662 sc_val_from_ulong(0, NULL);
663 _save_result(_exp(result));
664 _save_result(_mant(result));
665 result->desc.clss = ZERO;
670 if (a->desc.clss == INF) {
674 memcpy(result, a, calc_buffer_size);
675 result->sign = res_sign;
678 if (b->desc.clss == ZERO) {
680 /* division by zero */
682 fc_get_minusinf(&a->desc, result);
684 fc_get_plusinf(&a->desc, result);
688 /* exp = exp(a) - exp(b) + excess - 1*/
689 sc_sub(_exp(a), _exp(b), _exp(result));
690 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 2, temp);
691 sc_add(_exp(result), temp, _exp(result));
693 /* mixed normal, subnormal values introduce an error of 1, correct it */
694 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
695 sc_val_from_ulong(1, temp);
696 sc_add(_exp(result), temp, _exp(result));
699 /* mant(res) = mant(a) / 1/2mant(b) */
700 /* to gain more bits of precision in the result the dividend could be
701 * shifted left, as this operation does not loose bits. This would not
702 * fit into the integer precision, but due to the rounding bits (which
703 * are always zero because the values are all normalized) the divisor
704 * can be shifted right instead to achieve the same result */
705 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
707 _shift_left(_mant(a), temp, dividend);
710 char *divisor = alloca(calc_buffer_size);
711 sc_val_from_ulong(1, divisor);
712 _shift_right(_mant(b), divisor, divisor);
713 sc_div(dividend, divisor, _mant(result));
714 sticky = sc_had_carry();
718 fc_exact &= normalize(result, result, sticky);
722 static void _power_of_ten(int exp, ieee_descriptor_t *desc, char *result) {
729 /* set new descriptor (else result is supposed to already have one) */
731 result->desc = *desc;
733 build = alloca(value_size);
734 temp = alloca(value_size);
736 sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
739 /* temp is value of ten now */
740 sc_val_from_ulong(10, NULL);
743 for (exp--; exp > 0; exp--) {
745 sc_mul(build, temp, NULL);
749 /* temp is amount of left shift needed to put the value left of the radix point */
750 sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
752 _shift_left(build, temp, _mant(result));
754 _normalize(result, result, 0);
760 * Truncate the fractional part away.
762 * This does not clip to any integer range.
764 static void _trunc(const fp_value *a, fp_value *result) {
766 * When exponent == 0 all bits left of the radix point
767 * are the integral part of the value. For 15bit exp_size
768 * this would require a left shift of max. 16383 bits which
770 * But it is enough to ensure that no bit right of the radix
771 * point remains set. This restricts the interesting
772 * exponents to the interval [0, mant_size-1].
773 * Outside this interval the truncated value is either 0 or
774 * it does not have fractional parts.
777 int exp_bias, exp_val;
780 /* fixme: can be exact */
783 temp = alloca(value_size);
786 result->desc = a->desc;
788 exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
789 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
792 sc_val_from_ulong(0, NULL);
793 _save_result(_exp(result));
794 _save_result(_mant(result));
795 result->desc.clss = ZERO;
800 if (exp_val > a->desc.mantissa_size) {
802 memcpy(result, a, calc_buffer_size);
807 /* set up a proper mask to delete all bits right of the
808 * radix point if the mantissa had been shifted until exp == 0 */
809 sc_max_from_bits(1 + exp_val, 0, temp);
810 sc_val_from_long(a->desc.mantissa_size - exp_val + 2, NULL);
811 _shift_left(temp, sc_get_buffer(), temp);
813 /* and the mask and return the result */
814 sc_and(_mant(a), temp, _mant(result));
816 if (a != result) memcpy(_exp(result), _exp(a), value_size);
820 * functions defined in fltcalc.h
822 const void *fc_get_buffer(void) {
826 int fc_get_buffer_length(void) {
827 return calc_buffer_size;
830 void *fc_val_from_str(const char *str, unsigned int len, const ieee_descriptor_t *desc, void *result) {
842 int exp_int, hsb, state;
847 char *mant_str, *exp_val, *power_val;
850 if (result == NULL) result = calc_buffer;
852 exp_val = alloca(value_size);
853 power_val = alloca(calc_buffer_size);
854 mant_str = alloca((len)?(len):(strlen(str)));
856 result->desc.exponent_size = desc->exponent_size;
857 result->desc.mantissa_size = desc->mantissa_size;
858 result->desc.explicit_one = desc->explicit_one;
859 result->desc.clss = NORMAL;
866 while (len == 0 || str-old_str < len) {
882 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
889 state = RIGHT_OF_DOT;
900 fail_char(old_str, len, str - old_str);
906 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
907 mant_str[pos++] = *(str++);
911 state = RIGHT_OF_DOT;
922 mant_str[pos] = '\0';
926 fail_char(old_str, len, str - old_str);
932 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
933 mant_str[pos++] = *(str++);
944 mant_str[pos] = '\0';
948 fail_char(old_str, len, str - old_str);
958 if (*(str-1) != 'e' && *(str-1) != 'E') fail_char(old_str, len, str - old_str);
962 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
963 mant_str[pos] = '\0';
970 fail_char(old_str, len, str - old_str);
976 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
981 case '\0': goto done;
984 fail_char(old_str, len, str - old_str);
987 } /* switch(state) */
990 sc_val_from_str(mant_str, strlen(mant_str), _mant(result));
992 /* shift to put value left of radix point */
993 sc_val_from_ulong(mant_size + ROUNDING_BITS, exp_val);
995 _shift_left(_mant(result), exp_val, _mant(result));
997 sc_val_from_ulong((1 << (exp_size - 1)) - 1, _exp(result));
999 _normalize(result, result, 0);
1001 if (state == EXPONENT) {
1002 exp_int -= atoi(str-pos);
1005 _power_of_ten(exp_int, &result->desc, power_val);
1007 _fdiv(result, power_val, result);
1011 /* XXX excuse of an implementation to make things work */
1013 fp_value *tmp = alloca(calc_buffer_size);
1014 ieee_descriptor_t tmp_desc;
1017 #ifdef HAVE_LONG_DOUBLE
1018 val = strtold(str, NULL);
1019 DEBUGPRINTF(("val_from_str(%s)\n", str));
1020 tmp_desc.exponent_size = 15;
1021 tmp_desc.mantissa_size = 63;
1022 tmp_desc.explicit_one = 1;
1023 tmp_desc.clss = NORMAL;
1024 fc_val_from_ieee754(val, &tmp_desc, tmp);
1026 val = strtod(str, NULL);
1027 DEBUGPRINTF(("val_from_str(%s)\n", str));
1028 tmp_desc.exponent_size = 11;
1029 tmp_desc.mantissa_size = 52;
1030 tmp_desc.explicit_one = 0;
1031 tmp_desc.clss = NORMAL;
1032 fc_val_from_ieee754(val, &tmp_desc, tmp);
1033 #endif /* HAVE_LONG_DOUBLE */
1034 return fc_cast(tmp, desc, result);
1038 fp_value *fc_val_from_ieee754(LLDBL l, const ieee_descriptor_t *desc, fp_value *result) {
1040 int bias_res, bias_val, mant_val;
1042 UINT32 sign, exponent, mantissa0, mantissa1;
1045 bias_res = ((1 << (desc->exponent_size - 1)) - 1);
1047 #ifdef HAVE_LONG_DOUBLE
1050 sign = (srcval.val.high & 0x00008000) != 0;
1051 exponent = (srcval.val.high & 0x00007FFF) ;
1052 mantissa0 = srcval.val.mid;
1053 mantissa1 = srcval.val.low;
1054 #else /* no long double */
1057 sign = (srcval.val.high & 0x80000000) != 0;
1058 exponent = (srcval.val.high & 0x7FF00000) >> 20;
1059 mantissa0 = srcval.val.high & 0x000FFFFF;
1060 mantissa1 = srcval.val.low;
1063 #ifdef HAVE_LONG_DOUBLE
1064 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)); */
1065 DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1067 TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
1068 DEBUGPRINTF(("(%d-%.3X-%.5X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1071 if (result == NULL) result = calc_buffer;
1072 temp = alloca(value_size);
1074 /* CLEAR the buffer, else some bits might be uninitialized */
1075 memset(result, 0, fc_get_buffer_length());
1077 result->desc.exponent_size = desc->exponent_size;
1078 result->desc.mantissa_size = desc->mantissa_size;
1079 result->desc.explicit_one = desc->explicit_one;
1082 result->sign = sign;
1084 /* sign and flag suffice to identify NaN or inf, no exponent/mantissa
1085 * encoding is needed. the function can return immediately in these cases */
1087 result->desc.clss = NAN;
1088 TRACEPRINTF(("val_from_float resulted in NAN\n"));
1091 else if (isinf(l)) {
1092 result->desc.clss = INF;
1093 TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
1097 /* build exponent, because input and output exponent and mantissa sizes may differ
1098 * this looks more complicated than it is: unbiased input exponent + output bias,
1099 * minus the mantissa difference which is added again later when the output float
1100 * becomes normalized */
1101 sc_val_from_long((exponent - bias_val + bias_res) - (mant_val - desc->mantissa_size), _exp(result));
1103 /* build mantissa representation */
1104 if (exponent != 0) {
1105 /* insert the hidden bit */
1106 sc_val_from_ulong(1, temp);
1107 sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
1108 _shift_left(temp, sc_get_buffer(), NULL);
1111 sc_val_from_ulong(0, NULL);
1114 _save_result(_mant(result));
1116 /* bits from the upper word */
1117 sc_val_from_ulong(mantissa0, temp);
1118 sc_val_from_ulong(34, NULL);
1119 _shift_left(temp, sc_get_buffer(), temp);
1120 sc_or(_mant(result), temp, _mant(result));
1122 /* bits from the lower word */
1123 sc_val_from_ulong(mantissa1, temp);
1124 sc_val_from_ulong(ROUNDING_BITS, NULL);
1125 _shift_left(temp, sc_get_buffer(), temp);
1126 sc_or(_mant(result), temp, _mant(result));
1128 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
1129 * origin one to the left */
1130 if (exponent == 0) {
1131 sc_val_from_ulong(1, NULL);
1132 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1135 normalize(result, result, 0);
1137 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
1142 LLDBL fc_val_to_ieee754(const fp_value *val) {
1144 fp_value *temp = NULL;
1154 ieee_descriptor_t desc;
1155 unsigned mantissa_size;
1157 #ifdef HAVE_LONG_DOUBLE
1158 desc.exponent_size = 15;
1159 desc.mantissa_size = 63;
1160 desc.explicit_one = 1;
1163 desc.exponent_size = 11;
1164 desc.mantissa_size = 52;
1165 desc.explicit_one = 0;
1168 mantissa_size = desc.mantissa_size + desc.explicit_one;
1170 temp = alloca(calc_buffer_size);
1171 value = fc_cast(val, &desc, temp);
1175 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
1176 * lead to wrong results */
1177 exponent = sc_val_to_long(_exp(value)) ;
1179 sc_val_from_ulong(ROUNDING_BITS, NULL);
1180 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1185 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1186 mantissa1 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << (byte_offset << 3);
1188 for (; (byte_offset<<3) < desc.mantissa_size; byte_offset++)
1189 mantissa0 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << ((byte_offset - 4) << 3);
1191 #ifdef HAVE_LONG_DOUBLE
1192 buildval.val.high = sign << 15;
1193 buildval.val.high |= exponent;
1194 buildval.val.mid = mantissa0;
1195 buildval.val.low = mantissa1;
1196 #else /* no long double */
1197 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1198 buildval.val.high = sign << 31;
1199 buildval.val.high |= exponent << 20;
1200 buildval.val.high |= mantissa0;
1201 buildval.val.low = mantissa1;
1204 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1208 fp_value *fc_cast(const fp_value *value, const ieee_descriptor_t *desc, fp_value *result) {
1210 int exp_offset, val_bias, res_bias;
1212 if (result == NULL) result = calc_buffer;
1213 temp = alloca(value_size);
1215 if (value->desc.exponent_size == desc->exponent_size &&
1216 value->desc.mantissa_size == desc->mantissa_size &&
1217 value->desc.explicit_one == desc->explicit_one) {
1218 if (value != result)
1219 memcpy(result, value, calc_buffer_size);
1223 if (value->desc.clss == NAN) {
1224 if (sc_get_highest_set_bit(_mant(value)) == value->desc.mantissa_size + 1)
1225 return fc_get_qnan(desc, result);
1227 return fc_get_snan(desc, result);
1230 /* set the descriptor of the new value */
1231 result->desc.exponent_size = desc->exponent_size;
1232 result->desc.mantissa_size = desc->mantissa_size;
1233 result->desc.explicit_one = desc->explicit_one;
1234 result->desc.clss = value->desc.clss;
1236 result->sign = value->sign;
1238 /* when the mantissa sizes differ normalizing has to shift to align it.
1239 * this would change the exponent, which is unwanted. So calculate this
1240 * offset and add it */
1241 val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1242 res_bias = (1 << (desc->exponent_size - 1)) - 1;
1244 exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - desc->mantissa_size);
1245 sc_val_from_long(exp_offset, temp);
1246 sc_add(_exp(value), temp, _exp(result));
1248 /* _normalize expects normalized radix point */
1249 if (value->desc.clss == SUBNORMAL) {
1250 sc_val_from_ulong(1, NULL);
1251 _shift_left(_mant(value), sc_get_buffer(), _mant(result));
1252 } else if (value != result) {
1253 memcpy(_mant(result), _mant(value), value_size);
1255 memmove(_mant(result), _mant(value), value_size);
1258 normalize(result, result, 0);
1259 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1263 fp_value *fc_get_max(const ieee_descriptor_t *desc, fp_value *result) {
1264 if (result == NULL) result = calc_buffer;
1266 result->desc.exponent_size = desc->exponent_size;
1267 result->desc.mantissa_size = desc->mantissa_size;
1268 result->desc.explicit_one = desc->explicit_one;
1269 result->desc.clss = NORMAL;
1273 sc_val_from_ulong((1 << desc->exponent_size) - 2, _exp(result));
1275 sc_max_from_bits(desc->mantissa_size + 1, 0, _mant(result));
1276 sc_val_from_ulong(ROUNDING_BITS, NULL);
1277 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1282 fp_value *fc_get_min(const ieee_descriptor_t *desc, fp_value *result) {
1283 if (result == NULL) result = calc_buffer;
1285 fc_get_max(desc, result);
1291 fp_value *fc_get_snan(const ieee_descriptor_t *desc, fp_value *result) {
1292 if (result == NULL) result = calc_buffer;
1294 result->desc.exponent_size = desc->exponent_size;
1295 result->desc.mantissa_size = desc->mantissa_size;
1296 result->desc.explicit_one = desc->explicit_one;
1297 result->desc.clss = NAN;
1301 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1303 /* signaling NaN has non-zero mantissa with msb not set */
1304 sc_val_from_ulong(1, _mant(result));
1309 fp_value *fc_get_qnan(const ieee_descriptor_t *desc, fp_value *result) {
1310 if (result == NULL) result = calc_buffer;
1312 result->desc.exponent_size = desc->exponent_size;
1313 result->desc.mantissa_size = desc->mantissa_size;
1314 result->desc.explicit_one = desc->explicit_one;
1315 result->desc.clss = NAN;
1319 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1321 /* quiet NaN has the msb of the mantissa set, so shift one there */
1322 sc_val_from_ulong(1, _mant(result));
1323 /* mantissa_size >+< 1 because of two extra rounding bits */
1324 sc_val_from_ulong(desc->mantissa_size + 1, NULL);
1325 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1330 fp_value *fc_get_plusinf(const ieee_descriptor_t *desc, fp_value *result) {
1331 if (result == NULL) result = calc_buffer;
1333 result->desc.exponent_size = desc->exponent_size;
1334 result->desc.mantissa_size = desc->mantissa_size;
1335 result->desc.explicit_one = desc->explicit_one;
1336 result->desc.clss = INF;
1340 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1342 sc_val_from_ulong(0, _mant(result));
1347 fp_value *fc_get_minusinf(const ieee_descriptor_t *desc, fp_value *result) {
1348 if (result == NULL) result = calc_buffer;
1350 fc_get_plusinf(desc, result);
1356 int fc_comp(const fp_value *val_a, const fp_value *val_b) {
1360 * shortcut: if both values are identical, they are either
1361 * Unordered if NaN or equal
1364 return val_a->desc.clss == NAN ? 2 : 0;
1366 /* unordered if one is a NaN */
1367 if (val_a->desc.clss == NAN || val_b->desc.clss == NAN)
1370 /* zero is equal independent of sign */
1371 if ((val_a->desc.clss == ZERO) && (val_b->desc.clss == ZERO))
1374 /* different signs make compare easy */
1375 if (val_a->sign != val_b->sign)
1376 return (val_a->sign == 0) ? (1) : (-1);
1378 mul = val_a->sign ? -1 : 1;
1380 /* both infinity means equality */
1381 if ((val_a->desc.clss == INF) && (val_b->desc.clss == INF))
1384 /* infinity is bigger than the rest */
1385 if (val_a->desc.clss == INF)
1387 if (val_b->desc.clss == INF)
1390 /* check first exponent, that mantissa if equal */
1391 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1397 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1403 int fc_is_zero(const fp_value *a) {
1404 return a->desc.clss == ZERO;
1407 int fc_is_negative(const fp_value *a) {
1411 int fc_is_inf(const fp_value *a) {
1412 return a->desc.clss == INF;
1415 int fc_is_nan(const fp_value *a) {
1416 return a->desc.clss == NAN;
1419 int fc_is_subnormal(const fp_value *a) {
1420 return a->desc.clss == SUBNORMAL;
1423 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base) {
1427 mul_1 = alloca(calc_buffer_size);
1431 switch ((value_class_t)val->desc.clss) {
1433 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1436 snprintf(buf, buflen, "NaN");
1439 snprintf(buf, buflen, "0.0");
1442 flt_val = fc_val_to_ieee754(val);
1443 #ifdef HAVE_LONG_DOUBLE
1444 /* XXX 30 is arbitrary */
1445 snprintf(buf, buflen, "%.30LE", flt_val);
1447 snprintf(buf, buflen, "%.18E", flt_val);
1453 switch ((value_class_t)val->desc.clss) {
1455 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1458 snprintf(buf, buflen, "NAN");
1461 snprintf(buf, buflen, "0.0");
1464 flt_val = fc_val_to_ieee754(val);
1465 #ifdef HAVE_LONG_DOUBLE
1466 snprintf(buf, buflen, "%LA", flt_val);
1468 snprintf(buf, buflen, "%A", flt_val);
1475 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1476 buf[buflen - 1] = '\0';
1482 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs) {
1483 /* this is used to cache the packed version of the value */
1484 static char *packed_value = NULL;
1486 if (packed_value == NULL) packed_value = XMALLOCN(char, value_size);
1489 pack(value, packed_value);
1491 return sc_sub_bits(packed_value, num_bits, byte_ofs);
1494 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1495 int fc_zero_mantissa(const fp_value *value) {
1496 return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1499 /* Returns the exponent of a value. */
1500 int fc_get_exponent(const fp_value *value) {
1501 int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1502 return sc_val_to_long(_exp(value)) - exp_bias;
1505 /* Return non-zero if a given value can be converted lossless into another precision */
1506 int fc_can_lossless_conv_to(const fp_value *value, const ieee_descriptor_t *desc) {
1510 /* handle some special cases first */
1511 switch (value->desc.clss) {
1520 /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
1521 exp_bias = (1 << (desc->exponent_size - 1)) - 1;
1522 v = fc_get_exponent(value) + exp_bias;
1523 if (0 < v && v < (1 << desc->exponent_size) - 1) {
1524 /* exponent can be encoded, now check the mantissa */
1525 v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
1526 return v < desc->mantissa_size;
1532 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode) {
1533 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1534 rounding_mode = mode;
1536 return rounding_mode;
1539 fc_rounding_mode_t fc_get_rounding_mode(void) {
1540 return rounding_mode;
1543 void init_fltcalc(int precision) {
1544 if (calc_buffer == NULL) {
1545 /* does nothing if already init */
1546 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1548 init_strcalc(precision + 2 + ROUNDING_BITS);
1550 /* needs additionally rounding bits, one bit as explicit 1., and one for
1551 * addition overflow */
1552 max_precision = sc_get_precision() - (2 + ROUNDING_BITS);
1553 if (max_precision < precision)
1554 printf("WARNING: not enough precision available, using %d\n", max_precision);
1556 rounding_mode = FC_TONEAREST;
1557 value_size = sc_get_buffer_length();
1558 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1560 calc_buffer = xmalloc(calc_buffer_size);
1561 memset(calc_buffer, 0, calc_buffer_size);
1562 DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
1563 #ifdef HAVE_LONG_DOUBLE
1564 DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
1566 DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
1568 #ifdef WORDS_BIGENDIAN
1569 DEBUGPRINTF(("\tWord order is big endian\n\n"));
1571 DEBUGPRINTF(("\tWord order is little endian\n\n"));
1576 void finish_fltcalc (void) {
1577 free(calc_buffer); calc_buffer = NULL;
1580 #ifdef FLTCALC_TRACE_CALC
1581 static char buffer[100];
1584 /* definition of interface functions */
1585 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result) {
1586 if (result == NULL) result = calc_buffer;
1588 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1589 TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1591 /* make the value with the bigger exponent the first one */
1592 if (sc_comp(_exp(a), _exp(b)) == -1)
1593 _fadd(b, a, result);
1595 _fadd(a, b, result);
1597 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1601 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result) {
1604 if (result == NULL) result = calc_buffer;
1606 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1607 TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1609 temp = alloca(calc_buffer_size);
1610 memcpy(temp, b, calc_buffer_size);
1611 temp->sign = !b->sign;
1612 if (sc_comp(_exp(a), _exp(temp)) == -1)
1613 _fadd(temp, a, result);
1615 _fadd(a, temp, result);
1617 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1621 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result) {
1622 if (result == NULL) result = calc_buffer;
1624 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1625 TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1627 _fmul(a, b, result);
1629 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1633 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result) {
1634 if (result == NULL) result = calc_buffer;
1636 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1637 TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1639 _fdiv(a, b, result);
1641 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1645 fp_value *fc_neg(const fp_value *a, fp_value *result) {
1646 if (result == NULL) result = calc_buffer;
1648 TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1651 memcpy(result, a, calc_buffer_size);
1652 result->sign = !a->sign;
1654 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1658 fp_value *fc_int(const fp_value *a, fp_value *result) {
1659 if (result == NULL) result = calc_buffer;
1661 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1662 TRACEPRINTF(("truncated to integer "));
1666 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1670 fp_value *fc_rnd(const fp_value *a, fp_value *result) {
1671 if (result == NULL) result = calc_buffer;
1674 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1675 TRACEPRINTF(("rounded to integer "));
1677 assert(!"fc_rnd() not yet implemented");
1679 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1684 * convert a floating point value into an sc value ...
1686 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode) {
1687 if (a->desc.clss == NORMAL) {
1688 int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
1689 int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
1692 if (a->sign && !mode_is_signed(dst_mode)) {
1693 /* FIXME: for now we cannot convert this */
1697 assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
1698 shift = exp_val - (a->desc.mantissa_size + ROUNDING_BITS);
1701 sc_shlI(_mant(a), shift, 64, 0, result);
1703 sc_shrI(_mant(a), -shift, 64, 0, result);
1706 /* check for overflow */
1707 highest = sc_get_highest_set_bit(result);
1709 if (mode_is_signed(dst_mode)) {
1710 if (highest == sc_get_lowest_set_bit(result)) {
1711 /* need extra test for MIN_INT */
1712 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1713 /* FIXME: handle overflow */
1717 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1718 /* FIXME: handle overflow */
1723 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1724 /* FIXME: handle overflow */
1730 sc_neg(result, result);
1734 else if (a->desc.clss == ZERO) {
1742 unsigned fc_set_immediate_precision(unsigned bits) {
1743 unsigned old = immediate_prec;
1745 immediate_prec = bits;
1749 int fc_is_exact(void) {