2 * Copyright (C) 1995-2011 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 */
49 #define isnan(x) _isnan(x)
50 static inline int isinf(double x)
52 return !_finite(x) && !_isnan(x);
54 #define strtold(s, e) strtod(s, e)
55 #define SIZEOF_LONG_DOUBLE_8
58 /** The number of extra precision rounding bits */
59 #define ROUNDING_BITS 2
63 #ifdef WORDS_BIGENDIAN
68 #ifndef SIZEOF_LONG_DOUBLE_8
71 #ifdef WORDS_BIGENDIAN
77 volatile long double d;
80 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
82 /* our floating point value */
84 ieee_descriptor_t desc;
86 char value[1]; /* exp[value_size] + mant[value_size] */
89 #define _exp(a) &((a)->value[0])
90 #define _mant(a) &((a)->value[value_size])
92 #define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
93 #define _shift_right(x, y, res) sc_shr((x), (y), value_size*4, 0, (res))
94 #define _shift_left(x, y, res) sc_shl((x), (y), value_size*4, 0, (res))
98 # define DEBUGPRINTF(x) printf x
100 # define DEBUGPRINTF(x) ((void)0)
103 #ifdef FLTCALC_TRACE_CALC
104 # define TRACEPRINTF(x) printf x
106 # define TRACEPRINTF(x) ((void)0)
109 /** The immediate precision. */
110 static unsigned immediate_prec = 0;
112 /** A temporal buffer. */
113 static fp_value *calc_buffer = NULL;
115 /** Current rounding mode.*/
116 static fc_rounding_mode_t rounding_mode;
118 static int calc_buffer_size;
119 static int value_size;
120 static int max_precision;
123 static int fc_exact = 1;
125 /** pack machine-like */
126 static void *pack(const fp_value *int_float, void *packed)
130 fp_value *val_buffer;
133 temp = (char*) alloca(value_size);
134 shift_val = (char*) alloca(value_size);
136 switch ((value_class_t)int_float->desc.clss) {
138 val_buffer = (fp_value*) alloca(calc_buffer_size);
139 fc_get_qnan(&int_float->desc, val_buffer);
140 int_float = val_buffer;
144 val_buffer = (fp_value*) alloca(calc_buffer_size);
145 fc_get_plusinf(&int_float->desc, val_buffer);
146 val_buffer->sign = int_float->sign;
147 int_float = val_buffer;
153 assert(int_float->desc.explicit_one <= 1);
155 /* pack sign: move it to the left after exponent AND mantissa */
156 sc_val_from_ulong(int_float->sign, temp);
158 pos = int_float->desc.exponent_size + int_float->desc.mantissa_size + int_float->desc.explicit_one;
159 sc_val_from_ulong(pos, NULL);
160 _shift_left(temp, sc_get_buffer(), packed);
162 /* pack exponent: move it to the left after mantissa */
163 pos = int_float->desc.mantissa_size + int_float->desc.explicit_one;
164 sc_val_from_ulong(pos, shift_val);
165 _shift_left(_exp(int_float), shift_val, temp);
167 /* combine sign|exponent */
168 sc_or(temp, packed, packed);
170 /* extract mantissa */
171 /* remove rounding bits */
172 sc_val_from_ulong(ROUNDING_BITS, shift_val);
173 _shift_right(_mant(int_float), shift_val, temp);
175 /* remove leading 1 (or 0 if denormalized) */
176 sc_max_from_bits(pos, 0, shift_val); /* all mantissa bits are 1's */
177 sc_and(temp, shift_val, temp);
179 /* combine sign|exponent|mantissa */
180 sc_or(temp, packed, packed);
186 * Normalize a fp_value.
188 * @return non-zero if result is exact
190 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky)
194 char lsb, guard, round, round_dir = 0;
195 char *temp = (char*) alloca(value_size);
197 /* save rounding bits at the end */
198 hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
200 if (in_val != out_val) {
201 out_val->sign = in_val->sign;
202 memcpy(&out_val->desc, &in_val->desc, sizeof(out_val->desc));
205 out_val->desc.clss = NORMAL;
207 /* mantissa all zeros, so zero exponent (because of explicit one) */
208 if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
209 sc_val_from_ulong(0, _exp(out_val));
213 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
216 sc_val_from_ulong(-hsb-1, temp);
218 _shift_right(_mant(in_val), temp, _mant(out_val));
220 /* remember if some bits were shifted away */
221 if (sc_had_carry()) {
225 sc_add(_exp(in_val), temp, _exp(out_val));
226 } else if (hsb > -1) {
228 sc_val_from_ulong(hsb+1, temp);
230 _shift_left(_mant(in_val), temp, _mant(out_val));
232 sc_sub(_exp(in_val), temp, _exp(out_val));
235 /* check for exponent underflow */
236 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
237 DEBUGPRINTF(("Exponent underflow!\n"));
238 /* exponent underflow */
239 /* shift the mantissa right to have a zero exponent */
240 sc_val_from_ulong(1, temp);
241 sc_sub(temp, _exp(out_val), NULL);
243 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
244 if (sc_had_carry()) {
248 /* denormalized means exponent of zero */
249 sc_val_from_ulong(0, _exp(out_val));
251 out_val->desc.clss = SUBNORMAL;
254 /* perform rounding by adding a value that clears the guard bit and the round bit
255 * and either causes a carry to round up or not */
256 /* get the last 3 bits of the value */
257 lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
258 guard = (lsb&0x2)>>1;
261 switch (rounding_mode) {
263 /* round to nearest representable value, if in doubt choose the version
265 round_dir = guard && (sticky || round || lsb>>2);
268 /* if positive: round to one if the exact value is bigger, else to zero */
269 round_dir = (!out_val->sign && (guard || round || sticky));
272 /* if negative: round to one if the exact value is bigger, else to zero */
273 round_dir = (out_val->sign && (guard || round || sticky));
276 /* always round to 0 (chopping mode) */
280 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"));
282 if (round_dir == 1) {
283 guard = (round^guard)<<1;
284 lsb = !(round || guard)<<2 | guard | round;
286 lsb = -((guard<<1) | round);
289 /* add the rounded value */
291 sc_val_from_long(lsb, temp);
292 sc_add(_mant(out_val), temp, _mant(out_val));
296 /* could have rounded down to zero */
297 if (sc_is_zero(_mant(out_val)) && (out_val->desc.clss == SUBNORMAL))
298 out_val->desc.clss = ZERO;
300 /* check for rounding overflow */
301 hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
302 if ((out_val->desc.clss != SUBNORMAL) && (hsb < -1)) {
303 sc_val_from_ulong(1, temp);
304 _shift_right(_mant(out_val), temp, _mant(out_val));
305 if (exact && sc_had_carry())
307 sc_add(_exp(out_val), temp, _exp(out_val));
308 } else if ((out_val->desc.clss == SUBNORMAL) && (hsb == -1)) {
309 /* overflow caused the mantissa to be normal again,
310 * so adapt the exponent accordingly */
311 sc_val_from_ulong(1, temp);
312 sc_add(_exp(out_val), temp, _exp(out_val));
314 out_val->desc.clss = NORMAL;
316 /* no further rounding is needed, because rounding overflow means
317 * the carry of the original rounding was propagated all the way
318 * up to the bit left of the radix point. This implies the bits
319 * to the right are all zeros (rounding is +1) */
321 /* check for exponent overflow */
322 sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
323 if (sc_comp(_exp(out_val), temp) != -1) {
324 DEBUGPRINTF(("Exponent overflow!\n"));
325 /* exponent overflow, reaction depends on rounding method:
327 * mode | sign of value | result
328 *--------------------------------------------------------------
329 * TO_NEAREST | + | +inf
331 *--------------------------------------------------------------
332 * TO_POSITIVE | + | +inf
333 * | - | smallest representable value
334 *--------------------------------------------------------------
335 * TO_NEAGTIVE | + | largest representable value
337 *--------------------------------------------------------------
338 * TO_ZERO | + | largest representable value
339 * | - | smallest representable value
340 *--------------------------------------------------------------*/
341 if (out_val->sign == 0) {
342 /* value is positive */
343 switch (rounding_mode) {
346 out_val->desc.clss = INF;
351 fc_get_max(&out_val->desc, out_val);
354 /* value is negative */
355 switch (rounding_mode) {
358 out_val->desc.clss = INF;
363 fc_get_min(&out_val->desc, out_val);
371 * Operations involving NaN's must return NaN.
372 * They are NOT exact.
374 #define handle_NAN(a, b, result) \
376 if (a->desc.clss == NAN) { \
377 if (a != result) memcpy(result, a, calc_buffer_size); \
381 if (b->desc.clss == NAN) { \
382 if (b != result) memcpy(result, b, calc_buffer_size); \
390 * calculate a + b, where a is the value with the bigger exponent
392 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result)
402 handle_NAN(a, b, result);
404 /* make sure result has a descriptor */
405 if (result != a && result != b)
406 result->desc = a->desc;
408 /* determine if this is an addition or subtraction */
409 sign = a->sign ^ b->sign;
411 /* produce NaN on inf - inf */
412 if (sign && (a->desc.clss == INF) && (b->desc.clss == INF)) {
414 fc_get_qnan(&a->desc, result);
418 temp = (char*) alloca(value_size);
419 exp_diff = (char*) alloca(value_size);
421 /* get exponent difference */
422 sc_sub(_exp(a), _exp(b), exp_diff);
424 /* initially set sign to be the sign of a, special treatment of subtraction
425 * when exponents are equal is required though.
426 * Also special care about the sign is needed when the mantissas are equal
428 if (sign && sc_val_to_long(exp_diff) == 0) {
429 switch (sc_comp(_mant(a), _mant(b))) {
431 res_sign = a->sign; /* abs(a) is bigger and a is negative */
434 res_sign = (rounding_mode == FC_TONEGATIVE);
437 res_sign = b->sign; /* abs(b) is bigger and b is negative */
440 /* can't be reached */
447 result->sign = res_sign;
449 /* sign has been taken care of, check for special cases */
450 if (a->desc.clss == ZERO || b->desc.clss == INF) {
452 memcpy(result, b, calc_buffer_size);
453 fc_exact = b->desc.clss == NORMAL;
454 result->sign = res_sign;
457 if (b->desc.clss == ZERO || a->desc.clss == INF) {
459 memcpy(result, a, calc_buffer_size);
460 fc_exact = a->desc.clss == NORMAL;
461 result->sign = res_sign;
465 /* shift the smaller value to the right to align the radix point */
466 /* subnormals have their radix point shifted to the right,
467 * take care of this first */
468 if ((b->desc.clss == SUBNORMAL) && (a->desc.clss != SUBNORMAL)) {
469 sc_val_from_ulong(1, temp);
470 sc_sub(exp_diff, temp, exp_diff);
473 _shift_right(_mant(b), exp_diff, temp);
474 sticky = sc_had_carry();
477 if (sticky && sign) {
478 /* if subtracting a little more than the represented value or adding a little
479 * more than the represented value to a negative value this, in addition to the
480 * still set sticky bit, takes account of the 'little more' */
481 char *temp1 = (char*) alloca(calc_buffer_size);
482 sc_val_from_ulong(1, temp1);
483 sc_add(temp, temp1, temp);
487 if (sc_comp(_mant(a), temp) == -1)
488 sc_sub(temp, _mant(a), _mant(result));
490 sc_sub(_mant(a), temp, _mant(result));
492 sc_add(_mant(a), temp, _mant(result));
495 /* _normalize expects a 'normal' radix point, adding two subnormals
496 * results in a subnormal radix point -> shifting before normalizing */
497 if ((a->desc.clss == SUBNORMAL) && (b->desc.clss == SUBNORMAL)) {
498 sc_val_from_ulong(1, NULL);
499 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
502 /* resulting exponent is the bigger one */
503 memmove(_exp(result), _exp(a), value_size);
505 fc_exact &= normalize(result, result, sticky);
511 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result)
519 handle_NAN(a, b, result);
521 temp = (char*) alloca(value_size);
523 if (result != a && result != b)
524 result->desc = a->desc;
526 result->sign = res_sign = a->sign ^ b->sign;
528 /* produce NaN on 0 * inf */
529 if (a->desc.clss == ZERO) {
530 if (b->desc.clss == INF) {
531 fc_get_qnan(&a->desc, result);
535 memcpy(result, a, calc_buffer_size);
536 result->sign = res_sign;
540 if (b->desc.clss == ZERO) {
541 if (a->desc.clss == INF) {
542 fc_get_qnan(&a->desc, result);
546 memcpy(result, b, calc_buffer_size);
547 result->sign = res_sign;
552 if (a->desc.clss == INF) {
555 memcpy(result, a, calc_buffer_size);
556 result->sign = res_sign;
559 if (b->desc.clss == INF) {
562 memcpy(result, b, calc_buffer_size);
563 result->sign = res_sign;
567 /* exp = exp(a) + exp(b) - excess */
568 sc_add(_exp(a), _exp(b), _exp(result));
570 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
571 sc_sub(_exp(result), temp, _exp(result));
573 /* mixed normal, subnormal values introduce an error of 1, correct it */
574 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
575 sc_val_from_ulong(1, temp);
576 sc_add(_exp(result), temp, _exp(result));
579 sc_mul(_mant(a), _mant(b), _mant(result));
581 /* realign result: after a multiplication the digits right of the radix
582 * point are the sum of the factors' digits after the radix point. As all
583 * values are normalized they both have the same amount of these digits,
584 * which has to be restored by proper shifting
585 * because of the rounding bits */
586 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
588 _shift_right(_mant(result), temp, _mant(result));
589 sticky = sc_had_carry();
592 fc_exact &= normalize(result, result, sticky);
598 static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result)
601 char *temp, *dividend;
606 handle_NAN(a, b, result);
608 temp = (char*) alloca(value_size);
609 dividend = (char*) alloca(value_size);
611 if (result != a && result != b)
612 result->desc = a->desc;
614 result->sign = res_sign = a->sign ^ b->sign;
616 /* produce NAN on 0/0 and inf/inf */
617 if (a->desc.clss == ZERO) {
618 if (b->desc.clss == ZERO) {
620 fc_get_qnan(&a->desc, result);
625 memcpy(result, a, calc_buffer_size);
626 result->sign = res_sign;
631 if (b->desc.clss == INF) {
633 if (a->desc.clss == INF) {
635 fc_get_qnan(&a->desc, result);
638 sc_val_from_ulong(0, NULL);
639 _save_result(_exp(result));
640 _save_result(_mant(result));
641 result->desc.clss = ZERO;
646 if (a->desc.clss == INF) {
650 memcpy(result, a, calc_buffer_size);
651 result->sign = res_sign;
654 if (b->desc.clss == ZERO) {
656 /* division by zero */
658 fc_get_minusinf(&a->desc, result);
660 fc_get_plusinf(&a->desc, result);
664 /* exp = exp(a) - exp(b) + excess - 1*/
665 sc_sub(_exp(a), _exp(b), _exp(result));
666 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 2, temp);
667 sc_add(_exp(result), temp, _exp(result));
669 /* mixed normal, subnormal values introduce an error of 1, correct it */
670 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
671 sc_val_from_ulong(1, temp);
672 sc_add(_exp(result), temp, _exp(result));
675 /* mant(res) = mant(a) / 1/2mant(b) */
676 /* to gain more bits of precision in the result the dividend could be
677 * shifted left, as this operation does not loose bits. This would not
678 * fit into the integer precision, but due to the rounding bits (which
679 * are always zero because the values are all normalized) the divisor
680 * can be shifted right instead to achieve the same result */
681 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
683 _shift_left(_mant(a), temp, dividend);
686 char *divisor = (char*) alloca(calc_buffer_size);
687 sc_val_from_ulong(1, divisor);
688 _shift_right(_mant(b), divisor, divisor);
689 sc_div(dividend, divisor, _mant(result));
690 sticky = sc_had_carry();
694 fc_exact &= normalize(result, result, sticky);
698 static void _power_of_ten(int exp, ieee_descriptor_t *desc, char *result)
706 /* set new descriptor (else result is supposed to already have one) */
708 result->desc = *desc;
710 build = alloca(value_size);
711 temp = alloca(value_size);
713 sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
716 /* temp is value of ten now */
717 sc_val_from_ulong(10, NULL);
720 for (exp--; exp > 0; exp--) {
722 sc_mul(build, temp, NULL);
726 /* temp is amount of left shift needed to put the value left of the radix point */
727 sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
729 _shift_left(build, temp, _mant(result));
731 _normalize(result, result, 0);
737 * Truncate the fractional part away.
739 * This does not clip to any integer range.
741 static void _trunc(const fp_value *a, fp_value *result)
744 * When exponent == 0 all bits left of the radix point
745 * are the integral part of the value. For 15bit exp_size
746 * this would require a left shift of max. 16383 bits which
748 * But it is enough to ensure that no bit right of the radix
749 * point remains set. This restricts the interesting
750 * exponents to the interval [0, mant_size-1].
751 * Outside this interval the truncated value is either 0 or
752 * it does not have fractional parts.
755 int exp_bias, exp_val;
758 /* fixme: can be exact */
761 temp = (char*) alloca(value_size);
764 result->desc = a->desc;
766 exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
767 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
770 sc_val_from_ulong(0, NULL);
771 _save_result(_exp(result));
772 _save_result(_mant(result));
773 result->desc.clss = ZERO;
778 if (exp_val > a->desc.mantissa_size) {
780 memcpy(result, a, calc_buffer_size);
785 /* set up a proper mask to delete all bits right of the
786 * radix point if the mantissa had been shifted until exp == 0 */
787 sc_max_from_bits(1 + exp_val, 0, temp);
788 sc_val_from_long(a->desc.mantissa_size - exp_val + 2, NULL);
789 _shift_left(temp, sc_get_buffer(), temp);
791 /* and the mask and return the result */
792 sc_and(_mant(a), temp, _mant(result));
795 memcpy(_exp(result), _exp(a), value_size);
796 result->sign = a->sign;
801 * functions defined in fltcalc.h
803 const void *fc_get_buffer(void)
808 int fc_get_buffer_length(void)
810 return calc_buffer_size;
813 void *fc_val_from_str(const char *str, size_t len, const ieee_descriptor_t *desc, void *result)
817 /* XXX excuse of an implementation to make things work */
819 fp_value *tmp = (fp_value*) alloca(calc_buffer_size);
820 ieee_descriptor_t tmp_desc;
822 buffer = (char*) alloca(len+1);
823 memcpy(buffer, str, len);
825 val = strtold(buffer, NULL);
827 DEBUGPRINTF(("val_from_str(%s)\n", str));
828 tmp_desc.exponent_size = 15;
829 tmp_desc.mantissa_size = 63;
830 tmp_desc.explicit_one = 1;
831 tmp_desc.clss = NORMAL;
832 fc_val_from_ieee754(val, &tmp_desc, tmp);
834 return fc_cast(tmp, desc, (fp_value*) result);
837 fp_value *fc_val_from_ieee754(long double l, const ieee_descriptor_t *desc, fp_value *result)
840 int bias_res, bias_val, mant_val;
843 uint32_t exponent, mantissa0, mantissa1;
846 bias_res = ((1 << (desc->exponent_size - 1)) - 1);
848 #ifdef SIZEOF_LONG_DOUBLE_8
851 sign = (srcval.val.high & 0x80000000) != 0;
852 exponent = (srcval.val.high & 0x7FF00000) >> 20;
853 mantissa0 = srcval.val.high & 0x000FFFFF;
854 mantissa1 = srcval.val.low;
858 sign = (srcval.val.high & 0x00008000) != 0;
859 exponent = (srcval.val.high & 0x00007FFF) ;
860 mantissa0 = srcval.val.mid;
861 mantissa1 = srcval.val.low;
864 if (result == NULL) result = calc_buffer;
865 temp = (char*) alloca(value_size);
867 /* CLEAR the buffer, else some bits might be uninitialized */
868 memset(result, 0, fc_get_buffer_length());
870 result->desc.exponent_size = desc->exponent_size;
871 result->desc.mantissa_size = desc->mantissa_size;
872 result->desc.explicit_one = desc->explicit_one;
877 /* sign and flag suffice to identify NaN or inf, no exponent/mantissa
878 * encoding is needed. the function can return immediately in these cases */
880 result->desc.clss = NAN;
881 TRACEPRINTF(("val_from_float resulted in NAN\n"));
883 } else if (isinf(l)) {
884 result->desc.clss = INF;
885 TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
889 /* build exponent, because input and output exponent and mantissa sizes may differ
890 * this looks more complicated than it is: unbiased input exponent + output bias,
891 * minus the mantissa difference which is added again later when the output float
892 * becomes normalized */
893 sc_val_from_long((exponent - bias_val + bias_res) - (mant_val - desc->mantissa_size), _exp(result));
895 /* build mantissa representation */
897 /* insert the hidden bit */
898 sc_val_from_ulong(1, temp);
899 sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
900 _shift_left(temp, sc_get_buffer(), NULL);
903 sc_val_from_ulong(0, NULL);
906 _save_result(_mant(result));
908 /* bits from the upper word */
909 sc_val_from_ulong(mantissa0, temp);
910 sc_val_from_ulong(34, NULL);
911 _shift_left(temp, sc_get_buffer(), temp);
912 sc_or(_mant(result), temp, _mant(result));
914 /* bits from the lower word */
915 sc_val_from_ulong(mantissa1, temp);
916 sc_val_from_ulong(ROUNDING_BITS, NULL);
917 _shift_left(temp, sc_get_buffer(), temp);
918 sc_or(_mant(result), temp, _mant(result));
920 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
921 * origin one to the left */
923 sc_val_from_ulong(1, NULL);
924 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
927 normalize(result, result, 0);
929 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
934 long double fc_val_to_ieee754(const fp_value *val)
937 fp_value *temp = NULL;
947 ieee_descriptor_t desc;
948 unsigned mantissa_size;
950 #ifdef SIZEOF_LONG_DOUBLE_8
951 desc.exponent_size = 11;
952 desc.mantissa_size = 52;
953 desc.explicit_one = 0;
956 desc.exponent_size = 15;
957 desc.mantissa_size = 63;
958 desc.explicit_one = 1;
961 mantissa_size = desc.mantissa_size + desc.explicit_one;
963 temp = (fp_value*) alloca(calc_buffer_size);
964 value = fc_cast(val, &desc, temp);
968 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
969 * lead to wrong results */
970 exponent = sc_val_to_long(_exp(value)) ;
972 sc_val_from_ulong(ROUNDING_BITS, NULL);
973 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
978 for (byte_offset = 0; byte_offset < 4; byte_offset++)
979 mantissa1 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << (byte_offset << 3);
981 for (; (byte_offset<<3) < desc.mantissa_size; byte_offset++)
982 mantissa0 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << ((byte_offset - 4) << 3);
984 #ifdef SIZEOF_LONG_DOUBLE_8
985 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
986 buildval.val.high = sign << 31;
987 buildval.val.high |= exponent << 20;
988 buildval.val.high |= mantissa0;
989 buildval.val.low = mantissa1;
991 buildval.val.high = sign << 15;
992 buildval.val.high |= exponent;
993 buildval.val.mid = mantissa0;
994 buildval.val.low = mantissa1;
997 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1001 fp_value *fc_cast(const fp_value *value, const ieee_descriptor_t *desc, fp_value *result)
1004 int exp_offset, val_bias, res_bias;
1006 if (result == NULL) result = calc_buffer;
1007 temp = (char*) alloca(value_size);
1009 if (value->desc.exponent_size == desc->exponent_size &&
1010 value->desc.mantissa_size == desc->mantissa_size &&
1011 value->desc.explicit_one == desc->explicit_one) {
1012 if (value != result)
1013 memcpy(result, value, calc_buffer_size);
1017 if (value->desc.clss == NAN) {
1018 if (sc_get_highest_set_bit(_mant(value)) == value->desc.mantissa_size + 1)
1019 return fc_get_qnan(desc, result);
1021 return fc_get_snan(desc, result);
1023 else if (value->desc.clss == INF) {
1024 if (value->sign == 0)
1025 return fc_get_plusinf(desc, result);
1027 return fc_get_minusinf(desc, result);
1030 /* set the descriptor of the new value */
1031 result->desc.exponent_size = desc->exponent_size;
1032 result->desc.mantissa_size = desc->mantissa_size;
1033 result->desc.explicit_one = desc->explicit_one;
1034 result->desc.clss = value->desc.clss;
1036 result->sign = value->sign;
1038 /* when the mantissa sizes differ normalizing has to shift to align it.
1039 * this would change the exponent, which is unwanted. So calculate this
1040 * offset and add it */
1041 val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1042 res_bias = (1 << (desc->exponent_size - 1)) - 1;
1044 exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - desc->mantissa_size);
1045 sc_val_from_long(exp_offset, temp);
1046 sc_add(_exp(value), temp, _exp(result));
1048 /* _normalize expects normalized radix point */
1049 if (value->desc.clss == SUBNORMAL) {
1050 sc_val_from_ulong(1, NULL);
1051 _shift_left(_mant(value), sc_get_buffer(), _mant(result));
1052 } else if (value != result) {
1053 memcpy(_mant(result), _mant(value), value_size);
1055 memmove(_mant(result), _mant(value), value_size);
1058 normalize(result, result, 0);
1059 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1063 fp_value *fc_get_max(const ieee_descriptor_t *desc, fp_value *result)
1065 if (result == NULL) result = calc_buffer;
1067 result->desc.exponent_size = desc->exponent_size;
1068 result->desc.mantissa_size = desc->mantissa_size;
1069 result->desc.explicit_one = desc->explicit_one;
1070 result->desc.clss = NORMAL;
1074 sc_val_from_ulong((1 << desc->exponent_size) - 2, _exp(result));
1076 sc_max_from_bits(desc->mantissa_size + 1, 0, _mant(result));
1077 sc_val_from_ulong(ROUNDING_BITS, NULL);
1078 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1083 fp_value *fc_get_min(const ieee_descriptor_t *desc, fp_value *result)
1085 if (result == NULL) result = calc_buffer;
1087 fc_get_max(desc, result);
1093 fp_value *fc_get_snan(const ieee_descriptor_t *desc, fp_value *result)
1095 if (result == NULL) result = calc_buffer;
1097 result->desc.exponent_size = desc->exponent_size;
1098 result->desc.mantissa_size = desc->mantissa_size;
1099 result->desc.explicit_one = desc->explicit_one;
1100 result->desc.clss = NAN;
1104 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1106 /* signaling NaN has non-zero mantissa with msb not set */
1107 sc_val_from_ulong(1, _mant(result));
1112 fp_value *fc_get_qnan(const ieee_descriptor_t *desc, fp_value *result)
1114 if (result == NULL) result = calc_buffer;
1116 result->desc.exponent_size = desc->exponent_size;
1117 result->desc.mantissa_size = desc->mantissa_size;
1118 result->desc.explicit_one = desc->explicit_one;
1119 result->desc.clss = NAN;
1123 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1125 /* quiet NaN has the msb of the mantissa set, so shift one there */
1126 sc_val_from_ulong(1, _mant(result));
1127 /* mantissa_size >+< 1 because of two extra rounding bits */
1128 sc_val_from_ulong(desc->mantissa_size + 1, NULL);
1129 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1134 fp_value *fc_get_plusinf(const ieee_descriptor_t *desc, fp_value *result)
1138 if (result == NULL) result = calc_buffer;
1140 result->desc.exponent_size = desc->exponent_size;
1141 result->desc.mantissa_size = desc->mantissa_size;
1142 result->desc.explicit_one = desc->explicit_one;
1143 result->desc.clss = INF;
1147 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1149 mant = _mant(result);
1150 sc_val_from_ulong(0, mant);
1151 if (desc->explicit_one) {
1152 sc_set_bit_at(mant, result->desc.mantissa_size + ROUNDING_BITS);
1158 fp_value *fc_get_minusinf(const ieee_descriptor_t *desc, fp_value *result)
1160 if (result == NULL) result = calc_buffer;
1162 fc_get_plusinf(desc, result);
1168 int fc_comp(const fp_value *val_a, const fp_value *val_b)
1173 * shortcut: if both values are identical, they are either
1174 * Unordered if NaN or equal
1177 return val_a->desc.clss == NAN ? 2 : 0;
1179 /* unordered if one is a NaN */
1180 if (val_a->desc.clss == NAN || val_b->desc.clss == NAN)
1183 /* zero is equal independent of sign */
1184 if ((val_a->desc.clss == ZERO) && (val_b->desc.clss == ZERO))
1187 /* different signs make compare easy */
1188 if (val_a->sign != val_b->sign)
1189 return (val_a->sign == 0) ? (1) : (-1);
1191 mul = val_a->sign ? -1 : 1;
1193 /* both infinity means equality */
1194 if ((val_a->desc.clss == INF) && (val_b->desc.clss == INF))
1197 /* infinity is bigger than the rest */
1198 if (val_a->desc.clss == INF)
1200 if (val_b->desc.clss == INF)
1203 /* check first exponent, that mantissa if equal */
1204 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1210 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1216 int fc_is_zero(const fp_value *a)
1218 return a->desc.clss == ZERO;
1221 int fc_is_negative(const fp_value *a)
1226 int fc_is_inf(const fp_value *a)
1228 return a->desc.clss == INF;
1231 int fc_is_nan(const fp_value *a)
1233 return a->desc.clss == NAN;
1236 int fc_is_subnormal(const fp_value *a)
1238 return a->desc.clss == SUBNORMAL;
1241 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base)
1244 long double flt_val;
1246 mul_1 = (char*) alloca(calc_buffer_size);
1250 switch ((value_class_t)val->desc.clss) {
1252 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1255 snprintf(buf, buflen, "NaN");
1258 snprintf(buf, buflen, "0.0");
1261 flt_val = fc_val_to_ieee754(val);
1262 /* XXX 30 is arbitrary */
1263 snprintf(buf, buflen, "%.30LE", flt_val);
1268 switch ((value_class_t)val->desc.clss) {
1270 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1273 snprintf(buf, buflen, "NAN");
1276 snprintf(buf, buflen, "0.0");
1279 flt_val = fc_val_to_ieee754(val);
1280 snprintf(buf, buflen, "%LA", flt_val);
1286 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1287 buf[buflen - 1] = '\0';
1293 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs)
1295 /* this is used to cache the packed version of the value */
1296 static char *packed_value = NULL;
1298 if (packed_value == NULL) packed_value = XMALLOCN(char, value_size);
1301 pack(value, packed_value);
1303 return sc_sub_bits(packed_value, num_bits, byte_ofs);
1306 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1307 int fc_zero_mantissa(const fp_value *value)
1309 return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1312 /* Returns the exponent of a value. */
1313 int fc_get_exponent(const fp_value *value)
1315 int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1316 return sc_val_to_long(_exp(value)) - exp_bias;
1319 /* Return non-zero if a given value can be converted lossless into another precision */
1320 int fc_can_lossless_conv_to(const fp_value *value, const ieee_descriptor_t *desc)
1325 /* handle some special cases first */
1326 switch (value->desc.clss) {
1335 /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
1336 exp_bias = (1 << (desc->exponent_size - 1)) - 1;
1337 v = fc_get_exponent(value) + exp_bias;
1338 if (0 < v && v < (1 << desc->exponent_size) - 1) {
1339 /* exponent can be encoded, now check the mantissa */
1340 v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
1341 return v <= desc->mantissa_size;
1347 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode)
1349 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1350 rounding_mode = mode;
1352 return rounding_mode;
1355 fc_rounding_mode_t fc_get_rounding_mode(void)
1357 return rounding_mode;
1360 void init_fltcalc(int precision)
1362 if (calc_buffer == NULL) {
1363 /* does nothing if already init */
1364 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1366 init_strcalc(precision + 2 + ROUNDING_BITS);
1368 /* needs additionally rounding bits, one bit as explicit 1., and one for
1369 * addition overflow */
1370 max_precision = sc_get_precision() - (2 + ROUNDING_BITS);
1371 if (max_precision < precision)
1372 printf("WARNING: not enough precision available, using %d\n", max_precision);
1374 rounding_mode = FC_TONEAREST;
1375 value_size = sc_get_buffer_length();
1376 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1378 calc_buffer = (fp_value*) xmalloc(calc_buffer_size);
1379 memset(calc_buffer, 0, calc_buffer_size);
1380 DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
1384 void finish_fltcalc (void)
1386 free(calc_buffer); calc_buffer = NULL;
1389 #ifdef FLTCALC_TRACE_CALC
1390 static char buffer[100];
1393 /* definition of interface functions */
1394 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result)
1396 if (result == NULL) result = calc_buffer;
1398 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1399 TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1401 /* make the value with the bigger exponent the first one */
1402 if (sc_comp(_exp(a), _exp(b)) == -1)
1403 _fadd(b, a, result);
1405 _fadd(a, b, result);
1407 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1411 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result)
1415 if (result == NULL) result = calc_buffer;
1417 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1418 TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1420 temp = (fp_value*) alloca(calc_buffer_size);
1421 memcpy(temp, b, calc_buffer_size);
1422 temp->sign = !b->sign;
1423 if (sc_comp(_exp(a), _exp(temp)) == -1)
1424 _fadd(temp, a, result);
1426 _fadd(a, temp, result);
1428 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1432 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result)
1434 if (result == NULL) result = calc_buffer;
1436 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1437 TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1439 _fmul(a, b, result);
1441 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1445 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result)
1447 if (result == NULL) result = calc_buffer;
1449 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1450 TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1452 _fdiv(a, b, result);
1454 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1458 fp_value *fc_neg(const fp_value *a, fp_value *result)
1460 if (result == NULL) result = calc_buffer;
1462 TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1465 memcpy(result, a, calc_buffer_size);
1466 result->sign = !a->sign;
1468 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1472 fp_value *fc_int(const fp_value *a, fp_value *result)
1474 if (result == NULL) result = calc_buffer;
1476 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1477 TRACEPRINTF(("truncated to integer "));
1481 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1485 fp_value *fc_rnd(const fp_value *a, fp_value *result)
1487 if (result == NULL) result = calc_buffer;
1490 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1491 TRACEPRINTF(("rounded to integer "));
1493 panic("fc_rnd() not yet implemented");
1497 * convert a floating point value into an sc value ...
1499 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode)
1501 if (a->desc.clss == NORMAL) {
1502 int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
1503 int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
1508 if (a->sign && !mode_is_signed(dst_mode)) {
1509 /* FIXME: for now we cannot convert this */
1513 tgt_bits = get_mode_size_bits(dst_mode);
1514 if (mode_is_signed(dst_mode))
1517 assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
1518 mantissa_size = a->desc.mantissa_size + ROUNDING_BITS;
1519 shift = exp_val - mantissa_size;
1521 if (tgt_bits < mantissa_size + 1)
1522 tgt_bits = mantissa_size + 1;
1524 sc_shlI(_mant(a), shift, tgt_bits, 0, result);
1526 sc_shrI(_mant(a), -shift, tgt_bits, 0, result);
1529 /* check for overflow */
1530 highest = sc_get_highest_set_bit(result);
1532 if (mode_is_signed(dst_mode)) {
1533 if (highest == sc_get_lowest_set_bit(result)) {
1534 /* need extra test for MIN_INT */
1535 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1536 /* FIXME: handle overflow */
1540 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1541 /* FIXME: handle overflow */
1546 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1547 /* FIXME: handle overflow */
1553 sc_neg(result, result);
1557 else if (a->desc.clss == ZERO) {
1565 unsigned fc_set_immediate_precision(unsigned bits)
1567 unsigned old = immediate_prec;
1569 immediate_prec = bits;
1573 int fc_is_exact(void)