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
35 #include <math.h> /* need isnan() and isinf() (will be changed)*/
36 /* undef some reused constants defined by math.h */
41 #ifdef HAVE_INTTYPES_H
42 # include <inttypes.h>
55 /** The number of extra precesion rounding bits */
56 #define ROUNDING_BITS 2
58 typedef uint32_t UINT32;
60 #ifdef HAVE_LONG_DOUBLE
61 #ifdef WORDS_BIGENDIAN
68 volatile long double d;
77 volatile long double d;
81 #ifdef WORDS_BIGENDIAN
101 * possible float states
104 NORMAL, /**< normal representation, implicit 1 */
106 SUBNORMAL, /**< denormals, implicit 0 */
108 NAN, /**< Not A Number */
111 /** A descriptor for an IEEE float value. */
113 unsigned char exponent_size; /**< size of exponent in bits */
114 unsigned char mantissa_size; /**< size of mantissa in bits */
115 value_class_t clss; /**< state of this float */
118 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
120 /* our floating point value */
124 char value[1]; /* exp[value_size] + mant[value_size] */
127 #define _exp(a) &((a)->value[0])
128 #define _mant(a) &((a)->value[value_size])
130 #define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
131 #define _shift_right(x, y, b) sc_shr((x), (y), value_size*4, 0, (b))
132 #define _shift_left(x, y, b) sc_shl((x), (y), value_size*4, 0, (b))
136 # define DEBUGPRINTF(x) printf x
138 # define DEBUGPRINTF(x) ((void)0)
141 #ifdef FLTCALC_TRACE_CALC
142 # define TRACEPRINTF(x) printf x
144 # define TRACEPRINTF(x) ((void)0)
147 /** The immediate precision. */
148 static unsigned immediate_prec = 0;
150 /** A temporal buffer. */
151 static fp_value *calc_buffer = NULL;
153 /** Current rounding mode.*/
154 static fc_rounding_mode_t rounding_mode;
156 static int calc_buffer_size;
157 static int value_size;
158 static int max_precision;
161 static int fc_exact = 1;
164 static void fail_char(const char *str, unsigned int len, int pos) {
166 printf("ERROR: Unexpected character '%c'\n", *(str + pos));
168 printf("ERROR: Unexpected end of string\n");
169 while (len-- && *str) printf("%c", *str++); printf("\n");
170 while (pos--) printf(" "); printf("^\n");
171 /* the front end has to to check constant strings */
176 /** pack machine-like */
177 static void *pack(const fp_value *int_float, void *packed) {
180 fp_value *val_buffer;
182 temp = alloca(value_size);
183 shift_val = alloca(value_size);
185 switch (int_float->desc.clss) {
187 val_buffer = alloca(calc_buffer_size);
188 fc_get_qnan(int_float->desc.exponent_size, int_float->desc.mantissa_size, val_buffer);
189 int_float = val_buffer;
193 val_buffer = alloca(calc_buffer_size);
194 fc_get_plusinf(int_float->desc.exponent_size, int_float->desc.mantissa_size, val_buffer);
195 val_buffer->sign = int_float->sign;
196 int_float = val_buffer;
203 sc_val_from_ulong(int_float->sign, temp);
205 sc_val_from_ulong(int_float->desc.exponent_size + int_float->desc.mantissa_size, NULL);
206 _shift_left(temp, sc_get_buffer(), packed);
208 /* extract exponent */
209 sc_val_from_ulong(int_float->desc.mantissa_size, shift_val);
211 _shift_left(_exp(int_float), shift_val, temp);
213 sc_or(temp, packed, packed);
215 /* extract mantissa */
216 /* remove rounding bits */
217 sc_val_from_ulong(ROUNDING_BITS, shift_val);
218 _shift_right(_mant(int_float), shift_val, temp);
220 /* remove leading 1 (or 0 if denormalized) */
221 sc_max_from_bits(int_float->desc.mantissa_size, 0, shift_val); /* all mantissa bits are 1's */
222 sc_and(temp, shift_val, temp);
225 sc_or(temp, packed, packed);
231 * Normalize a fp_value.
233 * @return non-zero if result is exact
235 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky) {
238 char lsb, guard, round, round_dir = 0;
239 char *temp = alloca(value_size);
241 /* save rounding bits at the end */
242 hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
244 if (in_val != out_val) {
245 out_val->sign = in_val->sign;
246 memcpy(&out_val->desc, &in_val->desc, sizeof(out_val->desc));
249 out_val->desc.clss = NORMAL;
251 /* mantissa all zeros, so zero exponent (because of explicit one) */
252 if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
253 sc_val_from_ulong(0, _exp(out_val));
257 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
260 sc_val_from_ulong(-hsb-1, temp);
262 _shift_right(_mant(in_val), temp, _mant(out_val));
264 /* remember if some bits were shifted away */
265 if (sc_had_carry()) {
269 sc_add(_exp(in_val), temp, _exp(out_val));
270 } else if (hsb > -1) {
272 sc_val_from_ulong(hsb+1, temp);
274 _shift_left(_mant(in_val), temp, _mant(out_val));
276 sc_sub(_exp(in_val), temp, _exp(out_val));
279 /* check for exponent underflow */
280 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
281 DEBUGPRINTF(("Exponent underflow!\n"));
282 /* exponent underflow */
283 /* shift the mantissa right to have a zero exponent */
284 sc_val_from_ulong(1, temp);
285 sc_sub(temp, _exp(out_val), NULL);
287 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
288 if (sc_had_carry()) {
292 /* denormalized means exponent of zero */
293 sc_val_from_ulong(0, _exp(out_val));
295 out_val->desc.clss = SUBNORMAL;
298 /* perform rounding by adding a value that clears the guard bit and the round bit
299 * and either causes a carry to round up or not */
300 /* get the last 3 bits of the value */
301 lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
302 guard = (lsb&0x2)>>1;
305 switch (rounding_mode) {
307 /* round to nearest representable value, if in doubt choose the version
309 round_dir = guard && (sticky || round || lsb>>2);
312 /* if positive: round to one if the exact value is bigger, else to zero */
313 round_dir = (!out_val->sign && (guard || round || sticky));
316 /* if negative: round to one if the exact value is bigger, else to zero */
317 round_dir = (out_val->sign && (guard || round || sticky));
320 /* always round to 0 (chopping mode) */
324 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"));
326 if (round_dir == 1) {
327 guard = (round^guard)<<1;
328 lsb = !(round || guard)<<2 | guard | round;
330 lsb = -((guard<<1) | round);
333 /* add the rounded value */
335 sc_val_from_long(lsb, temp);
336 sc_add(_mant(out_val), temp, _mant(out_val));
340 /* could have rounded down to zero */
341 if (sc_is_zero(_mant(out_val)) && (out_val->desc.clss == SUBNORMAL))
342 out_val->desc.clss = ZERO;
344 /* check for rounding overflow */
345 hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
346 if ((out_val->desc.clss != SUBNORMAL) && (hsb < -1)) {
347 sc_val_from_ulong(1, temp);
348 _shift_right(_mant(out_val), temp, _mant(out_val));
349 if (exact && sc_had_carry())
351 sc_add(_exp(out_val), temp, _exp(out_val));
352 } else if ((out_val->desc.clss == SUBNORMAL) && (hsb == -1)) {
353 /* overflow caused the mantissa to be normal again,
354 * so adapt the exponent accordingly */
355 sc_val_from_ulong(1, temp);
356 sc_add(_exp(out_val), temp, _exp(out_val));
358 out_val->desc.clss = NORMAL;
360 /* no further rounding is needed, because rounding overflow means
361 * the carry of the original rounding was propagated all the way
362 * up to the bit left of the radix point. This implies the bits
363 * to the right are all zeros (rounding is +1) */
365 /* check for exponent overflow */
366 sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
367 if (sc_comp(_exp(out_val), temp) != -1) {
368 DEBUGPRINTF(("Exponent overflow!\n"));
369 /* exponent overflow, reaction depends on rounding method:
371 * mode | sign of value | result
372 *--------------------------------------------------------------
373 * TO_NEAREST | + | +inf
375 *--------------------------------------------------------------
376 * TO_POSITIVE | + | +inf
377 * | - | smallest representable value
378 *--------------------------------------------------------------
379 * TO_NEAGTIVE | + | largest representable value
381 *--------------------------------------------------------------
382 * TO_ZERO | + | largest representable value
383 * | - | smallest representable value
384 *--------------------------------------------------------------*/
385 if (out_val->sign == 0) {
386 /* value is positive */
387 switch (rounding_mode) {
390 out_val->desc.clss = INF;
395 fc_get_max(out_val->desc.exponent_size, out_val->desc.mantissa_size, out_val);
398 /* value is negative */
399 switch (rounding_mode) {
402 out_val->desc.clss = INF;
407 fc_get_min(out_val->desc.exponent_size, out_val->desc.mantissa_size, out_val);
415 * Operations involving NaN's must return NaN
417 #define handle_NAN(a, b, result) \
419 if (a->desc.clss == NAN) { \
420 if (a != result) memcpy(result, a, calc_buffer_size); \
423 if (b->desc.clss == NAN) { \
424 if (b != result) memcpy(result, b, calc_buffer_size); \
431 * calculate a + b, where a is the value with the bigger exponent
433 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result) {
442 handle_NAN(a, b, result);
444 /* make sure result has a descriptor */
445 if (result != a && result != b)
446 result->desc = a->desc;
448 /* determine if this is an addition or subtraction */
449 sign = a->sign ^ b->sign;
451 /* produce NaN on inf - inf */
452 if (sign && (a->desc.clss == INF) && (b->desc.clss == INF)) {
453 fc_get_qnan(a->desc.exponent_size, b->desc.mantissa_size, result);
457 temp = alloca(value_size);
458 exp_diff = alloca(value_size);
460 /* get exponent difference */
461 sc_sub(_exp(a), _exp(b), exp_diff);
463 /* initially set sign to be the sign of a, special treatment of subtraction
464 * when exponents are equal is required though.
465 * Also special care about the sign is needed when the mantissas are equal
467 if (sign && sc_val_to_long(exp_diff) == 0) {
468 switch (sc_comp(_mant(a), _mant(b))) {
470 res_sign = a->sign; /* abs(a) is bigger and a is negative */
473 res_sign = (rounding_mode == FC_TONEGATIVE);
476 res_sign = b->sign; /* abs(b) is bigger and b is negative */
479 /* can't be reached */
486 result->sign = res_sign;
488 /* sign has been taken care of, check for special cases */
489 if (a->desc.clss == ZERO || b->desc.clss == INF) {
491 memcpy(result, b, calc_buffer_size);
492 result->sign = res_sign;
495 if (b->desc.clss == ZERO || a->desc.clss == INF) {
497 memcpy(result, a, calc_buffer_size);
498 result->sign = res_sign;
502 /* shift the smaller value to the right to align the radix point */
503 /* subnormals have their radix point shifted to the right,
504 * take care of this first */
505 if ((b->desc.clss == SUBNORMAL) && (a->desc.clss != SUBNORMAL)) {
506 sc_val_from_ulong(1, temp);
507 sc_sub(exp_diff, temp, exp_diff);
510 _shift_right(_mant(b), exp_diff, temp);
511 sticky = sc_had_carry();
514 if (sticky && sign) {
515 /* if subtracting a little more than the represented value or adding a little
516 * more than the represented value to a negative value this, in addition to the
517 * still set sticky bit, takes account of the 'little more' */
518 char *temp1 = alloca(calc_buffer_size);
519 sc_val_from_ulong(1, temp1);
520 sc_add(temp, temp1, temp);
524 if (sc_comp(_mant(a), temp) == -1)
525 sc_sub(temp, _mant(a), _mant(result));
527 sc_sub(_mant(a), temp, _mant(result));
529 sc_add(_mant(a), temp, _mant(result));
532 /* _normalize expects a 'normal' radix point, adding two subnormals
533 * results in a subnormal radix point -> shifting before normalizing */
534 if ((a->desc.clss == SUBNORMAL) && (b->desc.clss == SUBNORMAL)) {
535 sc_val_from_ulong(1, NULL);
536 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
539 /* resulting exponent is the bigger one */
540 memmove(_exp(result), _exp(a), value_size);
542 fc_exact &= normalize(result, result, sticky);
548 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result) {
555 handle_NAN(a, b, result);
557 temp = alloca(value_size);
559 if (result != a && result != b)
560 result->desc = a->desc;
562 result->sign = res_sign = a->sign ^ b->sign;
564 /* produce NaN on 0 * inf */
565 if (a->desc.clss == ZERO) {
566 if (b->desc.clss == INF)
567 fc_get_qnan(a->desc.exponent_size, a->desc.mantissa_size, result);
570 memcpy(result, a, calc_buffer_size);
571 result->sign = res_sign;
575 if (b->desc.clss == ZERO) {
576 if (a->desc.clss == INF)
577 fc_get_qnan(a->desc.exponent_size, a->desc.mantissa_size, result);
580 memcpy(result, b, calc_buffer_size);
581 result->sign = res_sign;
586 if (a->desc.clss == INF) {
588 memcpy(result, a, calc_buffer_size);
589 result->sign = res_sign;
592 if (b->desc.clss == INF) {
594 memcpy(result, b, calc_buffer_size);
595 result->sign = res_sign;
599 /* exp = exp(a) + exp(b) - excess */
600 sc_add(_exp(a), _exp(b), _exp(result));
602 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
603 sc_sub(_exp(result), temp, _exp(result));
605 /* mixed normal, subnormal values introduce an error of 1, correct it */
606 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
607 sc_val_from_ulong(1, temp);
608 sc_add(_exp(result), temp, _exp(result));
611 sc_mul(_mant(a), _mant(b), _mant(result));
613 /* realign result: after a multiplication the digits right of the radix
614 * point are the sum of the factors' digits after the radix point. As all
615 * values are normalized they both have the same amount of these digits,
616 * which has to be restored by proper shifting
617 * because of the rounding bits */
618 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
620 _shift_right(_mant(result), temp, _mant(result));
621 sticky = sc_had_carry();
624 fc_exact &= normalize(result, result, sticky);
630 static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result) {
632 char *temp, *dividend;
637 handle_NAN(a, b, result);
639 temp = alloca(value_size);
640 dividend = alloca(value_size);
642 if (result != a && result != b)
643 result->desc = a->desc;
645 result->sign = res_sign = a->sign ^ b->sign;
647 /* produce NAN on 0/0 and inf/inf */
648 if (a->desc.clss == ZERO) {
649 if (b->desc.clss == ZERO)
651 fc_get_qnan(a->desc.exponent_size, a->desc.mantissa_size, result);
655 memcpy(result, a, calc_buffer_size);
656 result->sign = res_sign;
661 if (b->desc.clss == INF) {
662 if (a->desc.clss == INF)
664 fc_get_qnan(a->desc.exponent_size, a->desc.mantissa_size, result);
667 sc_val_from_ulong(0, NULL);
668 _save_result(_exp(result));
669 _save_result(_mant(result));
670 result->desc.clss = ZERO;
675 if (a->desc.clss == INF) {
678 memcpy(result, a, calc_buffer_size);
679 result->sign = res_sign;
682 if (b->desc.clss == ZERO) {
683 /* division by zero */
685 fc_get_minusinf(a->desc.exponent_size, a->desc.mantissa_size, result);
687 fc_get_plusinf(a->desc.exponent_size, a->desc.mantissa_size, result);
691 /* exp = exp(a) - exp(b) + excess - 1*/
692 sc_sub(_exp(a), _exp(b), _exp(result));
693 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 2, temp);
694 sc_add(_exp(result), temp, _exp(result));
696 /* mixed normal, subnormal values introduce an error of 1, correct it */
697 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
698 sc_val_from_ulong(1, temp);
699 sc_add(_exp(result), temp, _exp(result));
702 /* mant(res) = mant(a) / 1/2mant(b) */
703 /* to gain more bits of precision in the result the dividend could be
704 * shifted left, as this operation does not loose bits. This would not
705 * fit into the integer precision, but due to the rounding bits (which
706 * are always zero because the values are all normalized) the divisor
707 * can be shifted right instead to achieve the same result */
708 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
710 _shift_left(_mant(a), temp, dividend);
713 char *divisor = alloca(calc_buffer_size);
714 sc_val_from_ulong(1, divisor);
715 _shift_right(_mant(b), divisor, divisor);
716 sc_div(dividend, divisor, _mant(result));
717 sticky = sc_had_carry();
721 fc_exact &= normalize(result, result, sticky);
725 static void _power_of_ten(int exp, descriptor_t *desc, char *result) {
732 /* set new descriptor (else result is supposed to already have one) */
734 result->desc = *desc;
736 build = alloca(value_size);
737 temp = alloca(value_size);
739 sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
742 /* temp is value of ten now */
743 sc_val_from_ulong(10, NULL);
746 for (exp--; exp > 0; exp--) {
748 sc_mul(build, temp, NULL);
752 /* temp is amount of left shift needed to put the value left of the radix point */
753 sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
755 _shift_left(build, temp, _mant(result));
757 _normalize(result, result, 0);
763 * Truncate the fractional part away.
765 * This does not clip to any integer range.
767 static void _trunc(const fp_value *a, fp_value *result) {
769 * When exponent == 0 all bits left of the radix point
770 * are the integral part of the value. For 15bit exp_size
771 * this would require a left shift of max. 16383 bits which
773 * But it is enough to ensure that no bit right of the radix
774 * point remains set. This restricts the interesting
775 * exponents to the interval [0, mant_size-1].
776 * Outside this interval the truncated value is either 0 or
777 * it does not have fractional parts.
780 int exp_bias, exp_val;
783 /* fixme: can be exact */
786 temp = alloca(value_size);
789 result->desc = a->desc;
791 exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
792 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
795 sc_val_from_ulong(0, NULL);
796 _save_result(_exp(result));
797 _save_result(_mant(result));
798 result->desc.clss = ZERO;
803 if (exp_val > a->desc.mantissa_size) {
805 memcpy(result, a, calc_buffer_size);
810 /* set up a proper mask to delete all bits right of the
811 * radix point if the mantissa had been shifted until exp == 0 */
812 sc_max_from_bits(1 + exp_val, 0, temp);
813 sc_val_from_long(a->desc.mantissa_size - exp_val + 2, NULL);
814 _shift_left(temp, sc_get_buffer(), temp);
816 /* and the mask and return the result */
817 sc_and(_mant(a), temp, _mant(result));
819 if (a != result) memcpy(_exp(result), _exp(a), value_size);
823 * functions defined in fltcalc.h
825 const void *fc_get_buffer(void) {
829 int fc_get_buffer_length(void) {
830 return calc_buffer_size;
833 void *fc_val_from_str(const char *str, unsigned int len, char exp_size, char mant_size, void *result) {
845 int exp_int, hsb, state;
850 char *mant_str, *exp_val, *power_val;
853 if (result == NULL) result = calc_buffer;
855 exp_val = alloca(value_size);
856 power_val = alloca(calc_buffer_size);
857 mant_str = alloca((len)?(len):(strlen(str)));
859 result->desc.exponent_size = exp_size;
860 result->desc.mantissa_size = mant_size;
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);
1018 #ifdef HAVE_LONG_DOUBLE
1019 val = strtold(str, NULL);
1020 DEBUGPRINTF(("val_from_str(%s)\n", str));
1021 fc_val_from_ieee754(val, 15, 64, tmp);
1023 val = strtod(str, NULL);
1024 DEBUGPRINTF(("val_from_str(%s)\n", str));
1025 fc_val_from_ieee754(val, 11, 52, tmp);
1026 #endif /* HAVE_LONG_DOUBLE */
1027 return fc_cast(tmp, exp_size, mant_size, result);
1031 fp_value *fc_val_from_ieee754(LLDBL l, char exp_size, char mant_size, fp_value *result) {
1033 int bias_res, bias_val, mant_val;
1035 UINT32 sign, exponent, mantissa0, mantissa1;
1038 bias_res = ((1 << (exp_size - 1)) - 1);
1040 #ifdef HAVE_LONG_DOUBLE
1043 sign = (srcval.val.high & 0x00008000) != 0;
1044 exponent = (srcval.val.high & 0x00007FFF) ;
1045 mantissa0 = srcval.val.mid;
1046 mantissa1 = srcval.val.low;
1047 #else /* no long double */
1050 sign = (srcval.val.high & 0x80000000) != 0;
1051 exponent = (srcval.val.high & 0x7FF00000) >> 20;
1052 mantissa0 = srcval.val.high & 0x000FFFFF;
1053 mantissa1 = srcval.val.low;
1056 #ifdef HAVE_LONG_DOUBLE
1057 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)); */
1058 DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1060 TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
1061 DEBUGPRINTF(("(%d-%.3X-%.5X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1064 if (result == NULL) result = calc_buffer;
1065 temp = alloca(value_size);
1067 /* CLEAR the buffer, else some bits might be uninitialised */
1068 memset(result, 0, fc_get_buffer_length());
1070 result->desc.exponent_size = exp_size;
1071 result->desc.mantissa_size = mant_size;
1074 result->sign = sign;
1076 /* sign and flag suffice to identify nan or inf, no exponent/mantissa
1077 * encoding is needed. the function can return immediately in these cases */
1079 result->desc.clss = NAN;
1080 TRACEPRINTF(("val_from_float resulted in NAN\n"));
1083 else if (isinf(l)) {
1084 result->desc.clss = INF;
1085 TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
1089 /* build exponent, because input and output exponent and mantissa sizes may differ
1090 * this looks more complicated than it is: unbiased input exponent + output bias,
1091 * minus the mantissa difference which is added again later when the output float
1092 * becomes normalized */
1093 #ifdef HAVE_EXPLICIT_ONE
1094 sc_val_from_long((exponent-bias_val+bias_res)-(mant_val-mant_size-1), _exp(result));
1096 sc_val_from_long((exponent-bias_val+bias_res)-(mant_val-mant_size), _exp(result));
1099 /* build mantissa representation */
1100 #ifndef HAVE_EXPLICIT_ONE
1101 if (exponent != 0) {
1102 /* insert the hidden bit */
1103 sc_val_from_ulong(1, temp);
1104 sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
1105 _shift_left(temp, sc_get_buffer(), NULL);
1110 sc_val_from_ulong(0, NULL);
1113 _save_result(_mant(result));
1115 /* bits from the upper word */
1116 sc_val_from_ulong(mantissa0, temp);
1117 sc_val_from_ulong(34, NULL);
1118 _shift_left(temp, sc_get_buffer(), temp);
1119 sc_or(_mant(result), temp, _mant(result));
1121 /* bits from the lower word */
1122 sc_val_from_ulong(mantissa1, temp);
1123 sc_val_from_ulong(ROUNDING_BITS, NULL);
1124 _shift_left(temp, sc_get_buffer(), temp);
1125 sc_or(_mant(result), temp, _mant(result));
1127 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
1128 * origin one to the left */
1129 if (exponent == 0) {
1130 sc_val_from_ulong(1, NULL);
1131 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1134 normalize(result, result, 0);
1136 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
1141 LLDBL fc_val_to_ieee754(const fp_value *val) {
1143 fp_value *temp = NULL;
1154 #ifdef HAVE_LONG_DOUBLE
1155 char result_exponent = 15;
1156 char result_mantissa = 64;
1158 char result_exponent = 11;
1159 char result_mantissa = 52;
1162 temp = alloca(calc_buffer_size);
1163 #ifdef HAVE_EXPLICIT_ONE
1164 value = fc_cast(val, result_exponent, result_mantissa-1, temp);
1166 value = fc_cast(val, result_exponent, result_mantissa, temp);
1171 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
1172 * lead to wrong results */
1173 exponent = sc_val_to_long(_exp(value)) ;
1175 sc_val_from_ulong(ROUNDING_BITS, NULL);
1176 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1181 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1182 mantissa1 |= sc_sub_bits(_mant(value), result_mantissa, byte_offset) << (byte_offset<<3);
1184 for (; (byte_offset<<3) < result_mantissa; byte_offset++)
1185 mantissa0 |= sc_sub_bits(_mant(value), result_mantissa, byte_offset) << ((byte_offset-4)<<3);
1187 #ifdef HAVE_LONG_DOUBLE
1188 buildval.val.high = sign << 15;
1189 buildval.val.high |= exponent;
1190 buildval.val.mid = mantissa0;
1191 buildval.val.low = mantissa1;
1192 #else /* no long double */
1193 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1194 buildval.val.high = sign << 31;
1195 buildval.val.high |= exponent << 20;
1196 buildval.val.high |= mantissa0;
1197 buildval.val.low = mantissa1;
1200 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1204 fp_value *fc_cast(const fp_value *value, char exp_size, char mant_size, fp_value *result) {
1206 int exp_offset, val_bias, res_bias;
1208 if (result == NULL) result = calc_buffer;
1209 temp = alloca(value_size);
1211 if (value->desc.exponent_size == exp_size && value->desc.mantissa_size == mant_size) {
1212 if (value != result)
1213 memcpy(result, value, calc_buffer_size);
1217 if (value->desc.clss == NAN) {
1218 if (sc_get_highest_set_bit(_mant(value)) == value->desc.mantissa_size + 1)
1219 return fc_get_qnan(exp_size, mant_size, result);
1221 return fc_get_snan(exp_size, mant_size, result);
1224 /* set the descriptor of the new value */
1225 result->desc.exponent_size = exp_size;
1226 result->desc.mantissa_size = mant_size;
1227 result->desc.clss = value->desc.clss;
1229 result->sign = value->sign;
1231 /* when the mantissa sizes differ normalizing has to shift to align it.
1232 * this would change the exponent, which is unwanted. So calculate this
1233 * offset and add it */
1234 val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1235 res_bias = (1 << (exp_size - 1)) - 1;
1237 exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - mant_size);
1238 sc_val_from_long(exp_offset, temp);
1239 sc_add(_exp(value), temp, _exp(result));
1241 /* _normalize expects normalized radix point */
1242 if (value->desc.clss == SUBNORMAL) {
1243 sc_val_from_ulong(1, NULL);
1244 _shift_left(_mant(value), sc_get_buffer(), _mant(result));
1245 } else if (value != result) {
1246 memcpy(_mant(result), _mant(value), value_size);
1248 memmove(_mant(result), _mant(value), value_size);
1251 normalize(result, result, 0);
1252 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1256 fp_value *fc_get_max(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
1257 if (result == NULL) result = calc_buffer;
1259 result->desc.exponent_size = exponent_size;
1260 result->desc.mantissa_size = mantissa_size;
1261 result->desc.clss = NORMAL;
1265 sc_val_from_ulong((1<<exponent_size) - 2, _exp(result));
1267 sc_max_from_bits(mantissa_size + 1, 0, _mant(result));
1268 sc_val_from_ulong(ROUNDING_BITS, NULL);
1269 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1274 fp_value *fc_get_min(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
1275 if (result == NULL) result = calc_buffer;
1277 fc_get_max(exponent_size, mantissa_size, result);
1283 fp_value *fc_get_snan(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
1284 if (result == NULL) result = calc_buffer;
1286 result->desc.exponent_size = exponent_size;
1287 result->desc.mantissa_size = mantissa_size;
1288 result->desc.clss = NAN;
1292 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1294 /* signaling NaN has non-zero mantissa with msb not set */
1295 sc_val_from_ulong(1, _mant(result));
1300 fp_value *fc_get_qnan(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
1301 if (result == NULL) result = calc_buffer;
1303 result->desc.exponent_size = exponent_size;
1304 result->desc.mantissa_size = mantissa_size;
1305 result->desc.clss = NAN;
1309 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1311 /* quiet NaN has the msb of the mantissa set, so shift one there */
1312 sc_val_from_ulong(1, _mant(result));
1313 /* mantissa_size >+< 1 because of two extra rounding bits */
1314 sc_val_from_ulong(mantissa_size + 1, NULL);
1315 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1320 fp_value *fc_get_plusinf(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
1321 if (result == NULL) result = calc_buffer;
1323 result->desc.exponent_size = exponent_size;
1324 result->desc.mantissa_size = mantissa_size;
1325 result->desc.clss = NORMAL;
1329 sc_val_from_ulong((1<<exponent_size)-1, _exp(result));
1331 sc_val_from_ulong(0, _mant(result));
1336 fp_value *fc_get_minusinf(unsigned int exponent_size, unsigned int mantissa_size, fp_value *result) {
1337 if (result == NULL) result = calc_buffer;
1339 fc_get_plusinf(exponent_size, mantissa_size, result);
1345 int fc_comp(const fp_value *val_a, const fp_value *val_b) {
1349 * shortcut: if both values are identical, they are either
1350 * Unordered if NaN or equal
1353 return val_a->desc.clss == NAN ? 2 : 0;
1355 /* unordered if one is a NaN */
1356 if (val_a->desc.clss == NAN || val_b->desc.clss == NAN)
1359 /* zero is equal independent of sign */
1360 if ((val_a->desc.clss == ZERO) && (val_b->desc.clss == ZERO))
1363 /* different signs make compare easy */
1364 if (val_a->sign != val_b->sign)
1365 return (val_a->sign == 0) ? (1) : (-1);
1367 mul = val_a->sign ? -1 : 1;
1369 /* both infinity means equality */
1370 if ((val_a->desc.clss == INF) && (val_b->desc.clss == INF))
1373 /* infinity is bigger than the rest */
1374 if (val_a->desc.clss == INF)
1376 if (val_b->desc.clss == INF)
1379 /* check first exponent, that mantissa if equal */
1380 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1386 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1392 int fc_is_zero(const fp_value *a) {
1393 return a->desc.clss == ZERO;
1396 int fc_is_negative(const fp_value *a) {
1400 int fc_is_inf(const fp_value *a) {
1401 return a->desc.clss == INF;
1404 int fc_is_nan(const fp_value *a) {
1405 return a->desc.clss == NAN;
1408 int fc_is_subnormal(const fp_value *a) {
1409 return a->desc.clss == SUBNORMAL;
1412 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base) {
1415 mul_1 = alloca(calc_buffer_size);
1419 switch (val->desc.clss) {
1421 if (buflen >= 8 + val->sign) sprintf(buf, "%sINFINITY", val->sign ? "-":"");
1422 else snprintf(buf, buflen, "%sINF", val->sign ? "-":NULL);
1425 snprintf(buf, buflen, "NAN");
1428 snprintf(buf, buflen, "0.0");
1431 /* XXX to be implemented */
1432 #ifdef HAVE_LONG_DOUBLE
1433 /* XXX 30 is arbitrary */
1434 snprintf(buf, buflen, "%.30LE", fc_val_to_ieee754(val));
1436 snprintf(buf, buflen, "%.18E", fc_val_to_ieee754(val));
1442 switch (val->desc.clss) {
1444 if (buflen >= 8+val->sign) sprintf(buf, "%sINFINITY", val->sign?"-":"");
1445 else snprintf(buf, buflen, "%sINF", val->sign?"-":NULL);
1448 snprintf(buf, buflen, "NAN");
1451 snprintf(buf, buflen, "0.0");
1454 #ifdef HAVE_LONG_DOUBLE
1455 snprintf(buf, buflen, "%LA", fc_val_to_ieee754(val));
1457 snprintf(buf, buflen, "%A", fc_val_to_ieee754(val));
1464 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1465 buf[buflen - 1] = '\0';
1471 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs) {
1472 /* this is used to cache the packed version of the value */
1473 static char *packed_value = NULL;
1475 if (packed_value == NULL) packed_value = xmalloc(value_size);
1478 pack(value, packed_value);
1480 return sc_sub_bits(packed_value, num_bits, byte_ofs);
1483 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1484 int fc_zero_mantissa(const fp_value *value) {
1485 return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1488 /* Returns the exponent of a value. */
1489 int fc_get_exponent(const fp_value *value) {
1490 int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1491 return sc_val_to_long(_exp(value)) - exp_bias;
1494 /* Return non-zero if a given value can be converted lossless into another precision */
1495 int fc_can_lossless_conv_to(const fp_value *value, char exp_size, char mant_size) {
1499 /* handle some special cases first */
1500 switch (value->desc.clss) {
1509 /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
1510 exp_bias = (1 << (exp_size - 1)) - 1;
1511 v = fc_get_exponent(value) + exp_bias;
1512 if (0 < v && v < (1 << exp_size) - 1) {
1513 /* check the mantissa */
1514 v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
1515 return v < mant_size;
1521 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode) {
1522 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1523 rounding_mode = mode;
1525 return rounding_mode;
1528 fc_rounding_mode_t fc_get_rounding_mode(void) {
1529 return rounding_mode;
1532 void init_fltcalc(int precision) {
1533 if (calc_buffer == NULL) {
1534 /* does nothing if already init */
1535 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1537 init_strcalc(precision + 4);
1539 /* needs additionally two bits to round, a bit as explicit 1., and one for
1540 * addition overflow */
1541 max_precision = sc_get_precision() - 4;
1542 if (max_precision < precision)
1543 printf("WARNING: not enough precision available, using %d\n", max_precision);
1545 rounding_mode = FC_TONEAREST;
1546 value_size = sc_get_buffer_length();
1547 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1549 calc_buffer = xmalloc(calc_buffer_size);
1550 memset(calc_buffer, 0, calc_buffer_size);
1551 DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
1552 #ifdef HAVE_LONG_DOUBLE
1553 DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
1555 DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
1557 #ifdef WORDS_BIGENDIAN
1558 DEBUGPRINTF(("\tWord order is big endian\n\n"));
1560 DEBUGPRINTF(("\tWord order is little endian\n\n"));
1565 void finish_fltcalc (void) {
1566 free(calc_buffer); calc_buffer = NULL;
1569 #ifdef FLTCALC_TRACE_CALC
1570 static char buffer[100];
1573 /* definition of interface functions */
1574 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result) {
1575 if (result == NULL) result = calc_buffer;
1577 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1578 TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1580 /* make the value with the bigger exponent the first one */
1581 if (sc_comp(_exp(a), _exp(b)) == -1)
1582 _fadd(b, a, result);
1584 _fadd(a, b, result);
1586 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1590 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result) {
1593 if (result == NULL) result = calc_buffer;
1595 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1596 TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1598 temp = alloca(calc_buffer_size);
1599 memcpy(temp, b, calc_buffer_size);
1600 temp->sign = !b->sign;
1601 if (sc_comp(_exp(a), _exp(temp)) == -1)
1602 _fadd(temp, a, result);
1604 _fadd(a, temp, result);
1606 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1610 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result) {
1611 if (result == NULL) result = calc_buffer;
1613 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1614 TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1616 _fmul(a, b, result);
1618 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1622 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result) {
1623 if (result == NULL) result = calc_buffer;
1625 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1626 TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1628 _fdiv(a, b, result);
1630 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1634 fp_value *fc_neg(const fp_value *a, fp_value *result) {
1635 if (result == NULL) result = calc_buffer;
1637 TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1640 memcpy(result, a, calc_buffer_size);
1641 result->sign = !a->sign;
1643 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1647 fp_value *fc_int(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)));
1651 TRACEPRINTF(("truncated to integer "));
1655 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1659 fp_value *fc_rnd(const fp_value *a, fp_value *result) {
1660 if (result == NULL) result = calc_buffer;
1663 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1664 TRACEPRINTF(("rounded to integer "));
1666 assert(!"fc_rnd() not yet implemented");
1668 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1673 * convert a floating point value into an sc value ...
1675 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode) {
1676 if (a->desc.clss == NORMAL) {
1677 int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
1678 int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
1681 if (a->sign && !mode_is_signed(dst_mode)) {
1682 /* FIXME: for now we cannot convert this */
1686 assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
1687 shift = exp_val - (a->desc.mantissa_size + ROUNDING_BITS);
1690 sc_shlI(_mant(a), shift, 64, 0, result);
1692 sc_shrI(_mant(a), -shift, 64, 0, result);
1695 /* check for overflow */
1696 highest = sc_get_highest_set_bit(result);
1698 if (mode_is_signed(dst_mode)) {
1699 if (highest == sc_get_lowest_set_bit(result)) {
1700 /* need extra test for MIN_INT */
1701 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1702 /* FIXME: handle overflow */
1706 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1707 /* FIXME: handle overflow */
1712 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1713 /* FIXME: handle overflow */
1719 sc_neg(result, result);
1723 else if (a->desc.clss == ZERO) {
1731 unsigned fc_set_immediate_precision(unsigned bits) {
1732 unsigned old = immediate_prec;
1734 immediate_prec = bits;
1738 int fc_is_exact(void) {