2 * Copyright (C) 1995-2010 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
34 /* undef some reused constants defined by math.h */
47 #if !defined(HAVE_LONG_DOUBLE) || defined(__CYGWIN__)
48 /* No strtold on windows and no replacement yet */
49 #define strtold(s, e) strtod(s, e)
52 /** The number of extra precision rounding bits */
53 #define ROUNDING_BITS 2
55 typedef uint32_t UINT32;
57 #ifdef HAVE_LONG_DOUBLE
58 #ifdef WORDS_BIGENDIAN
65 volatile long double d;
74 volatile long double d;
78 #ifdef WORDS_BIGENDIAN
97 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
99 /* our floating point value */
101 ieee_descriptor_t desc;
103 char value[1]; /* exp[value_size] + mant[value_size] */
106 #define _exp(a) &((a)->value[0])
107 #define _mant(a) &((a)->value[value_size])
109 #define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
110 #define _shift_right(x, y, res) sc_shr((x), (y), value_size*4, 0, (res))
111 #define _shift_left(x, y, res) sc_shl((x), (y), value_size*4, 0, (res))
115 # define DEBUGPRINTF(x) printf x
117 # define DEBUGPRINTF(x) ((void)0)
120 #ifdef FLTCALC_TRACE_CALC
121 # define TRACEPRINTF(x) printf x
123 # define TRACEPRINTF(x) ((void)0)
126 /** The immediate precision. */
127 static unsigned immediate_prec = 0;
129 /** A temporal buffer. */
130 static fp_value *calc_buffer = NULL;
132 /** Current rounding mode.*/
133 static fc_rounding_mode_t rounding_mode;
135 static int calc_buffer_size;
136 static int value_size;
137 static int max_precision;
140 static int fc_exact = 1;
143 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)
161 fp_value *val_buffer;
164 temp = alloca(value_size);
165 shift_val = alloca(value_size);
167 switch ((value_class_t)int_float->desc.clss) {
169 val_buffer = alloca(calc_buffer_size);
170 fc_get_qnan(&int_float->desc, val_buffer);
171 int_float = val_buffer;
175 val_buffer = alloca(calc_buffer_size);
176 fc_get_plusinf(&int_float->desc, val_buffer);
177 val_buffer->sign = int_float->sign;
178 int_float = val_buffer;
184 assert(int_float->desc.explicit_one <= 1);
186 /* pack sign: move it to the left after exponent AND mantissa */
187 sc_val_from_ulong(int_float->sign, temp);
189 pos = int_float->desc.exponent_size + int_float->desc.mantissa_size + int_float->desc.explicit_one;
190 sc_val_from_ulong(pos, NULL);
191 _shift_left(temp, sc_get_buffer(), packed);
193 /* pack exponent: move it to the left after mantissa */
194 pos = int_float->desc.mantissa_size + int_float->desc.explicit_one;
195 sc_val_from_ulong(pos, shift_val);
196 _shift_left(_exp(int_float), shift_val, temp);
198 /* combine sign|exponent */
199 sc_or(temp, packed, packed);
201 /* extract mantissa */
202 /* remove rounding bits */
203 sc_val_from_ulong(ROUNDING_BITS, shift_val);
204 _shift_right(_mant(int_float), shift_val, temp);
206 /* remove leading 1 (or 0 if denormalized) */
207 sc_max_from_bits(pos, 0, shift_val); /* all mantissa bits are 1's */
208 sc_and(temp, shift_val, temp);
210 /* combine sign|exponent|mantissa */
211 sc_or(temp, packed, packed);
217 * Normalize a fp_value.
219 * @return non-zero if result is exact
221 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky)
225 char lsb, guard, round, round_dir = 0;
226 char *temp = alloca(value_size);
228 /* save rounding bits at the end */
229 hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
231 if (in_val != out_val) {
232 out_val->sign = in_val->sign;
233 memcpy(&out_val->desc, &in_val->desc, sizeof(out_val->desc));
236 out_val->desc.clss = NORMAL;
238 /* mantissa all zeros, so zero exponent (because of explicit one) */
239 if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
240 sc_val_from_ulong(0, _exp(out_val));
244 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
247 sc_val_from_ulong(-hsb-1, temp);
249 _shift_right(_mant(in_val), temp, _mant(out_val));
251 /* remember if some bits were shifted away */
252 if (sc_had_carry()) {
256 sc_add(_exp(in_val), temp, _exp(out_val));
257 } else if (hsb > -1) {
259 sc_val_from_ulong(hsb+1, temp);
261 _shift_left(_mant(in_val), temp, _mant(out_val));
263 sc_sub(_exp(in_val), temp, _exp(out_val));
266 /* check for exponent underflow */
267 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
268 DEBUGPRINTF(("Exponent underflow!\n"));
269 /* exponent underflow */
270 /* shift the mantissa right to have a zero exponent */
271 sc_val_from_ulong(1, temp);
272 sc_sub(temp, _exp(out_val), NULL);
274 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
275 if (sc_had_carry()) {
279 /* denormalized means exponent of zero */
280 sc_val_from_ulong(0, _exp(out_val));
282 out_val->desc.clss = SUBNORMAL;
285 /* perform rounding by adding a value that clears the guard bit and the round bit
286 * and either causes a carry to round up or not */
287 /* get the last 3 bits of the value */
288 lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
289 guard = (lsb&0x2)>>1;
292 switch (rounding_mode) {
294 /* round to nearest representable value, if in doubt choose the version
296 round_dir = guard && (sticky || round || lsb>>2);
299 /* if positive: round to one if the exact value is bigger, else to zero */
300 round_dir = (!out_val->sign && (guard || round || sticky));
303 /* if negative: round to one if the exact value is bigger, else to zero */
304 round_dir = (out_val->sign && (guard || round || sticky));
307 /* always round to 0 (chopping mode) */
311 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"));
313 if (round_dir == 1) {
314 guard = (round^guard)<<1;
315 lsb = !(round || guard)<<2 | guard | round;
317 lsb = -((guard<<1) | round);
320 /* add the rounded value */
322 sc_val_from_long(lsb, temp);
323 sc_add(_mant(out_val), temp, _mant(out_val));
327 /* could have rounded down to zero */
328 if (sc_is_zero(_mant(out_val)) && (out_val->desc.clss == SUBNORMAL))
329 out_val->desc.clss = ZERO;
331 /* check for rounding overflow */
332 hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
333 if ((out_val->desc.clss != SUBNORMAL) && (hsb < -1)) {
334 sc_val_from_ulong(1, temp);
335 _shift_right(_mant(out_val), temp, _mant(out_val));
336 if (exact && sc_had_carry())
338 sc_add(_exp(out_val), temp, _exp(out_val));
339 } else if ((out_val->desc.clss == SUBNORMAL) && (hsb == -1)) {
340 /* overflow caused the mantissa to be normal again,
341 * so adapt the exponent accordingly */
342 sc_val_from_ulong(1, temp);
343 sc_add(_exp(out_val), temp, _exp(out_val));
345 out_val->desc.clss = NORMAL;
347 /* no further rounding is needed, because rounding overflow means
348 * the carry of the original rounding was propagated all the way
349 * up to the bit left of the radix point. This implies the bits
350 * to the right are all zeros (rounding is +1) */
352 /* check for exponent overflow */
353 sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
354 if (sc_comp(_exp(out_val), temp) != -1) {
355 DEBUGPRINTF(("Exponent overflow!\n"));
356 /* exponent overflow, reaction depends on rounding method:
358 * mode | sign of value | result
359 *--------------------------------------------------------------
360 * TO_NEAREST | + | +inf
362 *--------------------------------------------------------------
363 * TO_POSITIVE | + | +inf
364 * | - | smallest representable value
365 *--------------------------------------------------------------
366 * TO_NEAGTIVE | + | largest representable value
368 *--------------------------------------------------------------
369 * TO_ZERO | + | largest representable value
370 * | - | smallest representable value
371 *--------------------------------------------------------------*/
372 if (out_val->sign == 0) {
373 /* value is positive */
374 switch (rounding_mode) {
377 out_val->desc.clss = INF;
382 fc_get_max(&out_val->desc, out_val);
385 /* value is negative */
386 switch (rounding_mode) {
389 out_val->desc.clss = INF;
394 fc_get_min(&out_val->desc, out_val);
402 * Operations involving NaN's must return NaN.
403 * They are NOT exact.
405 #define handle_NAN(a, b, result) \
407 if (a->desc.clss == NAN) { \
408 if (a != result) memcpy(result, a, calc_buffer_size); \
412 if (b->desc.clss == NAN) { \
413 if (b != result) memcpy(result, b, calc_buffer_size); \
421 * calculate a + b, where a is the value with the bigger exponent
423 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result)
433 handle_NAN(a, b, result);
435 /* make sure result has a descriptor */
436 if (result != a && result != b)
437 result->desc = a->desc;
439 /* determine if this is an addition or subtraction */
440 sign = a->sign ^ b->sign;
442 /* produce NaN on inf - inf */
443 if (sign && (a->desc.clss == INF) && (b->desc.clss == INF)) {
445 fc_get_qnan(&a->desc, result);
449 temp = alloca(value_size);
450 exp_diff = alloca(value_size);
452 /* get exponent difference */
453 sc_sub(_exp(a), _exp(b), exp_diff);
455 /* initially set sign to be the sign of a, special treatment of subtraction
456 * when exponents are equal is required though.
457 * Also special care about the sign is needed when the mantissas are equal
459 if (sign && sc_val_to_long(exp_diff) == 0) {
460 switch (sc_comp(_mant(a), _mant(b))) {
462 res_sign = a->sign; /* abs(a) is bigger and a is negative */
465 res_sign = (rounding_mode == FC_TONEGATIVE);
468 res_sign = b->sign; /* abs(b) is bigger and b is negative */
471 /* can't be reached */
478 result->sign = res_sign;
480 /* sign has been taken care of, check for special cases */
481 if (a->desc.clss == ZERO || b->desc.clss == INF) {
483 memcpy(result, b, calc_buffer_size);
484 fc_exact = b->desc.clss == NORMAL;
485 result->sign = res_sign;
488 if (b->desc.clss == ZERO || a->desc.clss == INF) {
490 memcpy(result, a, calc_buffer_size);
491 fc_exact = a->desc.clss == NORMAL;
492 result->sign = res_sign;
496 /* shift the smaller value to the right to align the radix point */
497 /* subnormals have their radix point shifted to the right,
498 * take care of this first */
499 if ((b->desc.clss == SUBNORMAL) && (a->desc.clss != SUBNORMAL)) {
500 sc_val_from_ulong(1, temp);
501 sc_sub(exp_diff, temp, exp_diff);
504 _shift_right(_mant(b), exp_diff, temp);
505 sticky = sc_had_carry();
508 if (sticky && sign) {
509 /* if subtracting a little more than the represented value or adding a little
510 * more than the represented value to a negative value this, in addition to the
511 * still set sticky bit, takes account of the 'little more' */
512 char *temp1 = alloca(calc_buffer_size);
513 sc_val_from_ulong(1, temp1);
514 sc_add(temp, temp1, temp);
518 if (sc_comp(_mant(a), temp) == -1)
519 sc_sub(temp, _mant(a), _mant(result));
521 sc_sub(_mant(a), temp, _mant(result));
523 sc_add(_mant(a), temp, _mant(result));
526 /* _normalize expects a 'normal' radix point, adding two subnormals
527 * results in a subnormal radix point -> shifting before normalizing */
528 if ((a->desc.clss == SUBNORMAL) && (b->desc.clss == SUBNORMAL)) {
529 sc_val_from_ulong(1, NULL);
530 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
533 /* resulting exponent is the bigger one */
534 memmove(_exp(result), _exp(a), value_size);
536 fc_exact &= normalize(result, result, sticky);
542 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result)
550 handle_NAN(a, b, result);
552 temp = alloca(value_size);
554 if (result != a && result != b)
555 result->desc = a->desc;
557 result->sign = res_sign = a->sign ^ b->sign;
559 /* produce NaN on 0 * inf */
560 if (a->desc.clss == ZERO) {
561 if (b->desc.clss == INF) {
562 fc_get_qnan(&a->desc, result);
566 memcpy(result, a, calc_buffer_size);
567 result->sign = res_sign;
571 if (b->desc.clss == ZERO) {
572 if (a->desc.clss == INF) {
573 fc_get_qnan(&a->desc, result);
577 memcpy(result, b, calc_buffer_size);
578 result->sign = res_sign;
583 if (a->desc.clss == INF) {
586 memcpy(result, a, calc_buffer_size);
587 result->sign = res_sign;
590 if (b->desc.clss == INF) {
593 memcpy(result, b, calc_buffer_size);
594 result->sign = res_sign;
598 /* exp = exp(a) + exp(b) - excess */
599 sc_add(_exp(a), _exp(b), _exp(result));
601 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
602 sc_sub(_exp(result), temp, _exp(result));
604 /* mixed normal, subnormal values introduce an error of 1, correct it */
605 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
606 sc_val_from_ulong(1, temp);
607 sc_add(_exp(result), temp, _exp(result));
610 sc_mul(_mant(a), _mant(b), _mant(result));
612 /* realign result: after a multiplication the digits right of the radix
613 * point are the sum of the factors' digits after the radix point. As all
614 * values are normalized they both have the same amount of these digits,
615 * which has to be restored by proper shifting
616 * because of the rounding bits */
617 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
619 _shift_right(_mant(result), temp, _mant(result));
620 sticky = sc_had_carry();
623 fc_exact &= normalize(result, result, sticky);
629 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, result);
656 memcpy(result, a, calc_buffer_size);
657 result->sign = res_sign;
662 if (b->desc.clss == INF) {
664 if (a->desc.clss == INF) {
666 fc_get_qnan(&a->desc, result);
669 sc_val_from_ulong(0, NULL);
670 _save_result(_exp(result));
671 _save_result(_mant(result));
672 result->desc.clss = ZERO;
677 if (a->desc.clss == INF) {
681 memcpy(result, a, calc_buffer_size);
682 result->sign = res_sign;
685 if (b->desc.clss == ZERO) {
687 /* division by zero */
689 fc_get_minusinf(&a->desc, result);
691 fc_get_plusinf(&a->desc, result);
695 /* exp = exp(a) - exp(b) + excess - 1*/
696 sc_sub(_exp(a), _exp(b), _exp(result));
697 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 2, temp);
698 sc_add(_exp(result), temp, _exp(result));
700 /* mixed normal, subnormal values introduce an error of 1, correct it */
701 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
702 sc_val_from_ulong(1, temp);
703 sc_add(_exp(result), temp, _exp(result));
706 /* mant(res) = mant(a) / 1/2mant(b) */
707 /* to gain more bits of precision in the result the dividend could be
708 * shifted left, as this operation does not loose bits. This would not
709 * fit into the integer precision, but due to the rounding bits (which
710 * are always zero because the values are all normalized) the divisor
711 * can be shifted right instead to achieve the same result */
712 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
714 _shift_left(_mant(a), temp, dividend);
717 char *divisor = alloca(calc_buffer_size);
718 sc_val_from_ulong(1, divisor);
719 _shift_right(_mant(b), divisor, divisor);
720 sc_div(dividend, divisor, _mant(result));
721 sticky = sc_had_carry();
725 fc_exact &= normalize(result, result, sticky);
729 static void _power_of_ten(int exp, ieee_descriptor_t *desc, char *result)
737 /* set new descriptor (else result is supposed to already have one) */
739 result->desc = *desc;
741 build = alloca(value_size);
742 temp = alloca(value_size);
744 sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
747 /* temp is value of ten now */
748 sc_val_from_ulong(10, NULL);
751 for (exp--; exp > 0; exp--) {
753 sc_mul(build, temp, NULL);
757 /* temp is amount of left shift needed to put the value left of the radix point */
758 sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
760 _shift_left(build, temp, _mant(result));
762 _normalize(result, result, 0);
768 * Truncate the fractional part away.
770 * This does not clip to any integer range.
772 static void _trunc(const fp_value *a, fp_value *result)
775 * When exponent == 0 all bits left of the radix point
776 * are the integral part of the value. For 15bit exp_size
777 * this would require a left shift of max. 16383 bits which
779 * But it is enough to ensure that no bit right of the radix
780 * point remains set. This restricts the interesting
781 * exponents to the interval [0, mant_size-1].
782 * Outside this interval the truncated value is either 0 or
783 * it does not have fractional parts.
786 int exp_bias, exp_val;
789 /* fixme: can be exact */
792 temp = alloca(value_size);
795 result->desc = a->desc;
797 exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
798 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
801 sc_val_from_ulong(0, NULL);
802 _save_result(_exp(result));
803 _save_result(_mant(result));
804 result->desc.clss = ZERO;
809 if (exp_val > a->desc.mantissa_size) {
811 memcpy(result, a, calc_buffer_size);
816 /* set up a proper mask to delete all bits right of the
817 * radix point if the mantissa had been shifted until exp == 0 */
818 sc_max_from_bits(1 + exp_val, 0, temp);
819 sc_val_from_long(a->desc.mantissa_size - exp_val + 2, NULL);
820 _shift_left(temp, sc_get_buffer(), temp);
822 /* and the mask and return the result */
823 sc_and(_mant(a), temp, _mant(result));
826 memcpy(_exp(result), _exp(a), value_size);
827 result->sign = a->sign;
832 * functions defined in fltcalc.h
834 const void *fc_get_buffer(void)
839 int fc_get_buffer_length(void)
841 return calc_buffer_size;
844 void *fc_val_from_str(const char *str, unsigned int len, const ieee_descriptor_t *desc, void *result)
848 /* XXX excuse of an implementation to make things work */
850 fp_value *tmp = alloca(calc_buffer_size);
851 ieee_descriptor_t tmp_desc;
853 buffer = alloca(len+1);
854 memcpy(buffer, str, len);
856 val = strtold(buffer, NULL);
858 DEBUGPRINTF(("val_from_str(%s)\n", str));
859 tmp_desc.exponent_size = 15;
860 tmp_desc.mantissa_size = 63;
861 tmp_desc.explicit_one = 1;
862 tmp_desc.clss = NORMAL;
863 fc_val_from_ieee754(val, &tmp_desc, tmp);
865 return fc_cast(tmp, desc, result);
868 fp_value *fc_val_from_ieee754(LLDBL l, const ieee_descriptor_t *desc, fp_value *result)
871 int bias_res, bias_val, mant_val;
874 UINT32 exponent, mantissa0, mantissa1;
877 bias_res = ((1 << (desc->exponent_size - 1)) - 1);
879 #ifdef HAVE_LONG_DOUBLE
882 sign = (srcval.val.high & 0x00008000) != 0;
883 exponent = (srcval.val.high & 0x00007FFF) ;
884 mantissa0 = srcval.val.mid;
885 mantissa1 = srcval.val.low;
886 #else /* no long double */
889 sign = (srcval.val.high & 0x80000000) != 0;
890 exponent = (srcval.val.high & 0x7FF00000) >> 20;
891 mantissa0 = srcval.val.high & 0x000FFFFF;
892 mantissa1 = srcval.val.low;
895 #ifdef HAVE_LONG_DOUBLE
896 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)); */
897 DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
899 TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
900 DEBUGPRINTF(("(%d-%.3X-%.5X%.8X)\n", sign, exponent, mantissa0, mantissa1));
903 if (result == NULL) result = calc_buffer;
904 temp = alloca(value_size);
906 /* CLEAR the buffer, else some bits might be uninitialized */
907 memset(result, 0, fc_get_buffer_length());
909 result->desc.exponent_size = desc->exponent_size;
910 result->desc.mantissa_size = desc->mantissa_size;
911 result->desc.explicit_one = desc->explicit_one;
916 /* sign and flag suffice to identify NaN or inf, no exponent/mantissa
917 * encoding is needed. the function can return immediately in these cases */
919 result->desc.clss = NAN;
920 TRACEPRINTF(("val_from_float resulted in NAN\n"));
924 result->desc.clss = INF;
925 TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
929 /* build exponent, because input and output exponent and mantissa sizes may differ
930 * this looks more complicated than it is: unbiased input exponent + output bias,
931 * minus the mantissa difference which is added again later when the output float
932 * becomes normalized */
933 sc_val_from_long((exponent - bias_val + bias_res) - (mant_val - desc->mantissa_size), _exp(result));
935 /* build mantissa representation */
937 /* insert the hidden bit */
938 sc_val_from_ulong(1, temp);
939 sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
940 _shift_left(temp, sc_get_buffer(), NULL);
943 sc_val_from_ulong(0, NULL);
946 _save_result(_mant(result));
948 /* bits from the upper word */
949 sc_val_from_ulong(mantissa0, temp);
950 sc_val_from_ulong(34, NULL);
951 _shift_left(temp, sc_get_buffer(), temp);
952 sc_or(_mant(result), temp, _mant(result));
954 /* bits from the lower word */
955 sc_val_from_ulong(mantissa1, temp);
956 sc_val_from_ulong(ROUNDING_BITS, NULL);
957 _shift_left(temp, sc_get_buffer(), temp);
958 sc_or(_mant(result), temp, _mant(result));
960 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
961 * origin one to the left */
963 sc_val_from_ulong(1, NULL);
964 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
967 normalize(result, result, 0);
969 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
974 LLDBL fc_val_to_ieee754(const fp_value *val)
977 fp_value *temp = NULL;
987 ieee_descriptor_t desc;
988 unsigned mantissa_size;
990 #ifdef HAVE_LONG_DOUBLE
991 desc.exponent_size = 15;
992 desc.mantissa_size = 63;
993 desc.explicit_one = 1;
996 desc.exponent_size = 11;
997 desc.mantissa_size = 52;
998 desc.explicit_one = 0;
1001 mantissa_size = desc.mantissa_size + desc.explicit_one;
1003 temp = alloca(calc_buffer_size);
1004 value = fc_cast(val, &desc, temp);
1008 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
1009 * lead to wrong results */
1010 exponent = sc_val_to_long(_exp(value)) ;
1012 sc_val_from_ulong(ROUNDING_BITS, NULL);
1013 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1018 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1019 mantissa1 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << (byte_offset << 3);
1021 for (; (byte_offset<<3) < desc.mantissa_size; byte_offset++)
1022 mantissa0 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << ((byte_offset - 4) << 3);
1024 #ifdef HAVE_LONG_DOUBLE
1025 buildval.val.high = sign << 15;
1026 buildval.val.high |= exponent;
1027 buildval.val.mid = mantissa0;
1028 buildval.val.low = mantissa1;
1029 #else /* no long double */
1030 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1031 buildval.val.high = sign << 31;
1032 buildval.val.high |= exponent << 20;
1033 buildval.val.high |= mantissa0;
1034 buildval.val.low = mantissa1;
1037 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1041 fp_value *fc_cast(const fp_value *value, const ieee_descriptor_t *desc, fp_value *result)
1044 int exp_offset, val_bias, res_bias;
1046 if (result == NULL) result = calc_buffer;
1047 temp = alloca(value_size);
1049 if (value->desc.exponent_size == desc->exponent_size &&
1050 value->desc.mantissa_size == desc->mantissa_size &&
1051 value->desc.explicit_one == desc->explicit_one) {
1052 if (value != result)
1053 memcpy(result, value, calc_buffer_size);
1057 if (value->desc.clss == NAN) {
1058 if (sc_get_highest_set_bit(_mant(value)) == value->desc.mantissa_size + 1)
1059 return fc_get_qnan(desc, result);
1061 return fc_get_snan(desc, result);
1063 else if (value->desc.clss == INF) {
1064 if (value->sign == 0)
1065 return fc_get_plusinf(desc, result);
1067 return fc_get_minusinf(desc, result);
1070 /* set the descriptor of the new value */
1071 result->desc.exponent_size = desc->exponent_size;
1072 result->desc.mantissa_size = desc->mantissa_size;
1073 result->desc.explicit_one = desc->explicit_one;
1074 result->desc.clss = value->desc.clss;
1076 result->sign = value->sign;
1078 /* when the mantissa sizes differ normalizing has to shift to align it.
1079 * this would change the exponent, which is unwanted. So calculate this
1080 * offset and add it */
1081 val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1082 res_bias = (1 << (desc->exponent_size - 1)) - 1;
1084 exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - desc->mantissa_size);
1085 sc_val_from_long(exp_offset, temp);
1086 sc_add(_exp(value), temp, _exp(result));
1088 /* _normalize expects normalized radix point */
1089 if (value->desc.clss == SUBNORMAL) {
1090 sc_val_from_ulong(1, NULL);
1091 _shift_left(_mant(value), sc_get_buffer(), _mant(result));
1092 } else if (value != result) {
1093 memcpy(_mant(result), _mant(value), value_size);
1095 memmove(_mant(result), _mant(value), value_size);
1098 normalize(result, result, 0);
1099 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1103 fp_value *fc_get_max(const ieee_descriptor_t *desc, fp_value *result)
1105 if (result == NULL) result = calc_buffer;
1107 result->desc.exponent_size = desc->exponent_size;
1108 result->desc.mantissa_size = desc->mantissa_size;
1109 result->desc.explicit_one = desc->explicit_one;
1110 result->desc.clss = NORMAL;
1114 sc_val_from_ulong((1 << desc->exponent_size) - 2, _exp(result));
1116 sc_max_from_bits(desc->mantissa_size + 1, 0, _mant(result));
1117 sc_val_from_ulong(ROUNDING_BITS, NULL);
1118 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1123 fp_value *fc_get_min(const ieee_descriptor_t *desc, fp_value *result)
1125 if (result == NULL) result = calc_buffer;
1127 fc_get_max(desc, result);
1133 fp_value *fc_get_snan(const ieee_descriptor_t *desc, fp_value *result)
1135 if (result == NULL) result = calc_buffer;
1137 result->desc.exponent_size = desc->exponent_size;
1138 result->desc.mantissa_size = desc->mantissa_size;
1139 result->desc.explicit_one = desc->explicit_one;
1140 result->desc.clss = NAN;
1144 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1146 /* signaling NaN has non-zero mantissa with msb not set */
1147 sc_val_from_ulong(1, _mant(result));
1152 fp_value *fc_get_qnan(const ieee_descriptor_t *desc, fp_value *result)
1154 if (result == NULL) result = calc_buffer;
1156 result->desc.exponent_size = desc->exponent_size;
1157 result->desc.mantissa_size = desc->mantissa_size;
1158 result->desc.explicit_one = desc->explicit_one;
1159 result->desc.clss = NAN;
1163 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1165 /* quiet NaN has the msb of the mantissa set, so shift one there */
1166 sc_val_from_ulong(1, _mant(result));
1167 /* mantissa_size >+< 1 because of two extra rounding bits */
1168 sc_val_from_ulong(desc->mantissa_size + 1, NULL);
1169 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1174 fp_value *fc_get_plusinf(const ieee_descriptor_t *desc, fp_value *result)
1178 if (result == NULL) result = calc_buffer;
1180 result->desc.exponent_size = desc->exponent_size;
1181 result->desc.mantissa_size = desc->mantissa_size;
1182 result->desc.explicit_one = desc->explicit_one;
1183 result->desc.clss = INF;
1187 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1189 mant = _mant(result);
1190 sc_val_from_ulong(0, mant);
1191 if (desc->explicit_one) {
1192 sc_set_bit_at(mant, result->desc.mantissa_size + ROUNDING_BITS);
1198 fp_value *fc_get_minusinf(const ieee_descriptor_t *desc, fp_value *result)
1200 if (result == NULL) result = calc_buffer;
1202 fc_get_plusinf(desc, result);
1208 int fc_comp(const fp_value *val_a, const fp_value *val_b)
1213 * shortcut: if both values are identical, they are either
1214 * Unordered if NaN or equal
1217 return val_a->desc.clss == NAN ? 2 : 0;
1219 /* unordered if one is a NaN */
1220 if (val_a->desc.clss == NAN || val_b->desc.clss == NAN)
1223 /* zero is equal independent of sign */
1224 if ((val_a->desc.clss == ZERO) && (val_b->desc.clss == ZERO))
1227 /* different signs make compare easy */
1228 if (val_a->sign != val_b->sign)
1229 return (val_a->sign == 0) ? (1) : (-1);
1231 mul = val_a->sign ? -1 : 1;
1233 /* both infinity means equality */
1234 if ((val_a->desc.clss == INF) && (val_b->desc.clss == INF))
1237 /* infinity is bigger than the rest */
1238 if (val_a->desc.clss == INF)
1240 if (val_b->desc.clss == INF)
1243 /* check first exponent, that mantissa if equal */
1244 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1250 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1256 int fc_is_zero(const fp_value *a)
1258 return a->desc.clss == ZERO;
1261 int fc_is_negative(const fp_value *a)
1266 int fc_is_inf(const fp_value *a)
1268 return a->desc.clss == INF;
1271 int fc_is_nan(const fp_value *a)
1273 return a->desc.clss == NAN;
1276 int fc_is_subnormal(const fp_value *a)
1278 return a->desc.clss == SUBNORMAL;
1281 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base)
1286 mul_1 = alloca(calc_buffer_size);
1290 switch ((value_class_t)val->desc.clss) {
1292 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1295 snprintf(buf, buflen, "NaN");
1298 snprintf(buf, buflen, "0.0");
1301 flt_val = fc_val_to_ieee754(val);
1302 #ifdef HAVE_LONG_DOUBLE
1303 /* XXX 30 is arbitrary */
1304 snprintf(buf, buflen, "%.30LE", flt_val);
1306 snprintf(buf, buflen, "%.18E", flt_val);
1312 switch ((value_class_t)val->desc.clss) {
1314 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1317 snprintf(buf, buflen, "NAN");
1320 snprintf(buf, buflen, "0.0");
1323 flt_val = fc_val_to_ieee754(val);
1324 #ifdef HAVE_LONG_DOUBLE
1325 snprintf(buf, buflen, "%LA", flt_val);
1327 snprintf(buf, buflen, "%A", flt_val);
1334 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1335 buf[buflen - 1] = '\0';
1341 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs)
1343 /* this is used to cache the packed version of the value */
1344 static char *packed_value = NULL;
1346 if (packed_value == NULL) packed_value = XMALLOCN(char, value_size);
1349 pack(value, packed_value);
1351 return sc_sub_bits(packed_value, num_bits, byte_ofs);
1354 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1355 int fc_zero_mantissa(const fp_value *value)
1357 return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1360 /* Returns the exponent of a value. */
1361 int fc_get_exponent(const fp_value *value)
1363 int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1364 return sc_val_to_long(_exp(value)) - exp_bias;
1367 /* Return non-zero if a given value can be converted lossless into another precision */
1368 int fc_can_lossless_conv_to(const fp_value *value, const ieee_descriptor_t *desc)
1373 /* handle some special cases first */
1374 switch (value->desc.clss) {
1383 /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
1384 exp_bias = (1 << (desc->exponent_size - 1)) - 1;
1385 v = fc_get_exponent(value) + exp_bias;
1386 if (0 < v && v < (1 << desc->exponent_size) - 1) {
1387 /* exponent can be encoded, now check the mantissa */
1388 v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
1389 return v <= desc->mantissa_size;
1395 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode)
1397 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1398 rounding_mode = mode;
1400 return rounding_mode;
1403 fc_rounding_mode_t fc_get_rounding_mode(void)
1405 return rounding_mode;
1408 void init_fltcalc(int precision)
1410 if (calc_buffer == NULL) {
1411 /* does nothing if already init */
1412 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1414 init_strcalc(precision + 2 + ROUNDING_BITS);
1416 /* needs additionally rounding bits, one bit as explicit 1., and one for
1417 * addition overflow */
1418 max_precision = sc_get_precision() - (2 + ROUNDING_BITS);
1419 if (max_precision < precision)
1420 printf("WARNING: not enough precision available, using %d\n", max_precision);
1422 rounding_mode = FC_TONEAREST;
1423 value_size = sc_get_buffer_length();
1424 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1426 calc_buffer = xmalloc(calc_buffer_size);
1427 memset(calc_buffer, 0, calc_buffer_size);
1428 DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
1429 #ifdef HAVE_LONG_DOUBLE
1430 DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
1432 DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
1434 #ifdef WORDS_BIGENDIAN
1435 DEBUGPRINTF(("\tWord order is big endian\n\n"));
1437 DEBUGPRINTF(("\tWord order is little endian\n\n"));
1442 void finish_fltcalc (void)
1444 free(calc_buffer); calc_buffer = NULL;
1447 #ifdef FLTCALC_TRACE_CALC
1448 static char buffer[100];
1451 /* definition of interface functions */
1452 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result)
1454 if (result == NULL) result = calc_buffer;
1456 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1457 TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1459 /* make the value with the bigger exponent the first one */
1460 if (sc_comp(_exp(a), _exp(b)) == -1)
1461 _fadd(b, a, result);
1463 _fadd(a, b, result);
1465 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1469 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result)
1473 if (result == NULL) result = calc_buffer;
1475 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1476 TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1478 temp = alloca(calc_buffer_size);
1479 memcpy(temp, b, calc_buffer_size);
1480 temp->sign = !b->sign;
1481 if (sc_comp(_exp(a), _exp(temp)) == -1)
1482 _fadd(temp, a, result);
1484 _fadd(a, temp, result);
1486 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1490 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result)
1492 if (result == NULL) result = calc_buffer;
1494 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1495 TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1497 _fmul(a, b, result);
1499 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1503 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result)
1505 if (result == NULL) result = calc_buffer;
1507 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1508 TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1510 _fdiv(a, b, result);
1512 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1516 fp_value *fc_neg(const fp_value *a, fp_value *result)
1518 if (result == NULL) result = calc_buffer;
1520 TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1523 memcpy(result, a, calc_buffer_size);
1524 result->sign = !a->sign;
1526 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1530 fp_value *fc_int(const fp_value *a, fp_value *result)
1532 if (result == NULL) result = calc_buffer;
1534 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1535 TRACEPRINTF(("truncated to integer "));
1539 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1543 fp_value *fc_rnd(const fp_value *a, fp_value *result)
1545 if (result == NULL) result = calc_buffer;
1548 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1549 TRACEPRINTF(("rounded to integer "));
1551 panic("fc_rnd() not yet implemented");
1555 * convert a floating point value into an sc value ...
1557 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode)
1559 if (a->desc.clss == NORMAL) {
1560 int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
1561 int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
1566 if (a->sign && !mode_is_signed(dst_mode)) {
1567 /* FIXME: for now we cannot convert this */
1571 tgt_bits = get_mode_size_bits(dst_mode);
1572 if (mode_is_signed(dst_mode))
1575 assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
1576 mantissa_size = a->desc.mantissa_size + ROUNDING_BITS;
1577 shift = exp_val - mantissa_size;
1579 if (tgt_bits < mantissa_size + 1)
1580 tgt_bits = mantissa_size + 1;
1582 sc_shlI(_mant(a), shift, tgt_bits, 0, result);
1584 sc_shrI(_mant(a), -shift, tgt_bits, 0, result);
1587 /* check for overflow */
1588 highest = sc_get_highest_set_bit(result);
1590 if (mode_is_signed(dst_mode)) {
1591 if (highest == sc_get_lowest_set_bit(result)) {
1592 /* need extra test for MIN_INT */
1593 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1594 /* FIXME: handle overflow */
1598 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1599 /* FIXME: handle overflow */
1604 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1605 /* FIXME: handle overflow */
1611 sc_neg(result, result);
1615 else if (a->desc.clss == ZERO) {
1623 unsigned fc_set_immediate_precision(unsigned bits)
1625 unsigned old = immediate_prec;
1627 immediate_prec = bits;
1631 int fc_is_exact(void)