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
33 /* undef some reused constants defined by math.h */
46 #if !defined(HAVE_LONG_DOUBLE) || defined(__CYGWIN__)
47 /* No strtold on windows and no replacement yet */
48 #define strtold(s, e) strtod(s, e)
51 /** The number of extra precision rounding bits */
52 #define ROUNDING_BITS 2
54 typedef uint32_t UINT32;
56 #ifdef HAVE_LONG_DOUBLE
57 #ifdef WORDS_BIGENDIAN
64 volatile long double d;
73 volatile long double d;
77 #ifdef WORDS_BIGENDIAN
96 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
98 /* our floating point value */
100 ieee_descriptor_t desc;
102 char value[1]; /* exp[value_size] + mant[value_size] */
105 #define _exp(a) &((a)->value[0])
106 #define _mant(a) &((a)->value[value_size])
108 #define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
109 #define _shift_right(x, y, res) sc_shr((x), (y), value_size*4, 0, (res))
110 #define _shift_left(x, y, res) sc_shl((x), (y), value_size*4, 0, (res))
114 # define DEBUGPRINTF(x) printf x
116 # define DEBUGPRINTF(x) ((void)0)
119 #ifdef FLTCALC_TRACE_CALC
120 # define TRACEPRINTF(x) printf x
122 # define TRACEPRINTF(x) ((void)0)
125 /** The immediate precision. */
126 static unsigned immediate_prec = 0;
128 /** A temporal buffer. */
129 static fp_value *calc_buffer = NULL;
131 /** Current rounding mode.*/
132 static fc_rounding_mode_t rounding_mode;
134 static int calc_buffer_size;
135 static int value_size;
136 static int max_precision;
139 static int fc_exact = 1;
142 static void fail_char(const char *str, unsigned int len, int pos)
145 printf("ERROR: Unexpected character '%c'\n", *(str + pos));
147 printf("ERROR: Unexpected end of string\n");
148 while (len-- && *str) printf("%c", *str++); printf("\n");
149 while (pos--) printf(" "); printf("^\n");
150 /* the front end has to to check constant strings */
155 /** pack machine-like */
156 static void *pack(const fp_value *int_float, void *packed)
160 fp_value *val_buffer;
163 temp = alloca(value_size);
164 shift_val = alloca(value_size);
166 switch ((value_class_t)int_float->desc.clss) {
168 val_buffer = alloca(calc_buffer_size);
169 fc_get_qnan(&int_float->desc, val_buffer);
170 int_float = val_buffer;
174 val_buffer = alloca(calc_buffer_size);
175 fc_get_plusinf(&int_float->desc, val_buffer);
176 val_buffer->sign = int_float->sign;
177 int_float = val_buffer;
183 assert(int_float->desc.explicit_one <= 1);
185 /* pack sign: move it to the left after exponent AND mantissa */
186 sc_val_from_ulong(int_float->sign, temp);
188 pos = int_float->desc.exponent_size + int_float->desc.mantissa_size + int_float->desc.explicit_one;
189 sc_val_from_ulong(pos, NULL);
190 _shift_left(temp, sc_get_buffer(), packed);
192 /* pack exponent: move it to the left after mantissa */
193 pos = int_float->desc.mantissa_size + int_float->desc.explicit_one;
194 sc_val_from_ulong(pos, shift_val);
195 _shift_left(_exp(int_float), shift_val, temp);
197 /* combine sign|exponent */
198 sc_or(temp, packed, packed);
200 /* extract mantissa */
201 /* remove rounding bits */
202 sc_val_from_ulong(ROUNDING_BITS, shift_val);
203 _shift_right(_mant(int_float), shift_val, temp);
205 /* remove leading 1 (or 0 if denormalized) */
206 sc_max_from_bits(pos, 0, shift_val); /* all mantissa bits are 1's */
207 sc_and(temp, shift_val, temp);
209 /* combine sign|exponent|mantissa */
210 sc_or(temp, packed, packed);
216 * Normalize a fp_value.
218 * @return non-zero if result is exact
220 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky)
224 char lsb, guard, round, round_dir = 0;
225 char *temp = alloca(value_size);
227 /* save rounding bits at the end */
228 hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
230 if (in_val != out_val) {
231 out_val->sign = in_val->sign;
232 memcpy(&out_val->desc, &in_val->desc, sizeof(out_val->desc));
235 out_val->desc.clss = NORMAL;
237 /* mantissa all zeros, so zero exponent (because of explicit one) */
238 if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
239 sc_val_from_ulong(0, _exp(out_val));
243 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
246 sc_val_from_ulong(-hsb-1, temp);
248 _shift_right(_mant(in_val), temp, _mant(out_val));
250 /* remember if some bits were shifted away */
251 if (sc_had_carry()) {
255 sc_add(_exp(in_val), temp, _exp(out_val));
256 } else if (hsb > -1) {
258 sc_val_from_ulong(hsb+1, temp);
260 _shift_left(_mant(in_val), temp, _mant(out_val));
262 sc_sub(_exp(in_val), temp, _exp(out_val));
265 /* check for exponent underflow */
266 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
267 DEBUGPRINTF(("Exponent underflow!\n"));
268 /* exponent underflow */
269 /* shift the mantissa right to have a zero exponent */
270 sc_val_from_ulong(1, temp);
271 sc_sub(temp, _exp(out_val), NULL);
273 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
274 if (sc_had_carry()) {
278 /* denormalized means exponent of zero */
279 sc_val_from_ulong(0, _exp(out_val));
281 out_val->desc.clss = SUBNORMAL;
284 /* perform rounding by adding a value that clears the guard bit and the round bit
285 * and either causes a carry to round up or not */
286 /* get the last 3 bits of the value */
287 lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
288 guard = (lsb&0x2)>>1;
291 switch (rounding_mode) {
293 /* round to nearest representable value, if in doubt choose the version
295 round_dir = guard && (sticky || round || lsb>>2);
298 /* if positive: round to one if the exact value is bigger, else to zero */
299 round_dir = (!out_val->sign && (guard || round || sticky));
302 /* if negative: round to one if the exact value is bigger, else to zero */
303 round_dir = (out_val->sign && (guard || round || sticky));
306 /* always round to 0 (chopping mode) */
310 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"));
312 if (round_dir == 1) {
313 guard = (round^guard)<<1;
314 lsb = !(round || guard)<<2 | guard | round;
316 lsb = -((guard<<1) | round);
319 /* add the rounded value */
321 sc_val_from_long(lsb, temp);
322 sc_add(_mant(out_val), temp, _mant(out_val));
326 /* could have rounded down to zero */
327 if (sc_is_zero(_mant(out_val)) && (out_val->desc.clss == SUBNORMAL))
328 out_val->desc.clss = ZERO;
330 /* check for rounding overflow */
331 hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
332 if ((out_val->desc.clss != SUBNORMAL) && (hsb < -1)) {
333 sc_val_from_ulong(1, temp);
334 _shift_right(_mant(out_val), temp, _mant(out_val));
335 if (exact && sc_had_carry())
337 sc_add(_exp(out_val), temp, _exp(out_val));
338 } else if ((out_val->desc.clss == SUBNORMAL) && (hsb == -1)) {
339 /* overflow caused the mantissa to be normal again,
340 * so adapt the exponent accordingly */
341 sc_val_from_ulong(1, temp);
342 sc_add(_exp(out_val), temp, _exp(out_val));
344 out_val->desc.clss = NORMAL;
346 /* no further rounding is needed, because rounding overflow means
347 * the carry of the original rounding was propagated all the way
348 * up to the bit left of the radix point. This implies the bits
349 * to the right are all zeros (rounding is +1) */
351 /* check for exponent overflow */
352 sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
353 if (sc_comp(_exp(out_val), temp) != -1) {
354 DEBUGPRINTF(("Exponent overflow!\n"));
355 /* exponent overflow, reaction depends on rounding method:
357 * mode | sign of value | result
358 *--------------------------------------------------------------
359 * TO_NEAREST | + | +inf
361 *--------------------------------------------------------------
362 * TO_POSITIVE | + | +inf
363 * | - | smallest representable value
364 *--------------------------------------------------------------
365 * TO_NEAGTIVE | + | largest representable value
367 *--------------------------------------------------------------
368 * TO_ZERO | + | largest representable value
369 * | - | smallest representable value
370 *--------------------------------------------------------------*/
371 if (out_val->sign == 0) {
372 /* value is positive */
373 switch (rounding_mode) {
376 out_val->desc.clss = INF;
381 fc_get_max(&out_val->desc, out_val);
384 /* value is negative */
385 switch (rounding_mode) {
388 out_val->desc.clss = INF;
393 fc_get_min(&out_val->desc, out_val);
401 * Operations involving NaN's must return NaN.
402 * They are NOT exact.
404 #define handle_NAN(a, b, result) \
406 if (a->desc.clss == NAN) { \
407 if (a != result) memcpy(result, a, calc_buffer_size); \
411 if (b->desc.clss == NAN) { \
412 if (b != result) memcpy(result, b, calc_buffer_size); \
420 * calculate a + b, where a is the value with the bigger exponent
422 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result)
432 handle_NAN(a, b, result);
434 /* make sure result has a descriptor */
435 if (result != a && result != b)
436 result->desc = a->desc;
438 /* determine if this is an addition or subtraction */
439 sign = a->sign ^ b->sign;
441 /* produce NaN on inf - inf */
442 if (sign && (a->desc.clss == INF) && (b->desc.clss == INF)) {
444 fc_get_qnan(&a->desc, result);
448 temp = alloca(value_size);
449 exp_diff = alloca(value_size);
451 /* get exponent difference */
452 sc_sub(_exp(a), _exp(b), exp_diff);
454 /* initially set sign to be the sign of a, special treatment of subtraction
455 * when exponents are equal is required though.
456 * Also special care about the sign is needed when the mantissas are equal
458 if (sign && sc_val_to_long(exp_diff) == 0) {
459 switch (sc_comp(_mant(a), _mant(b))) {
461 res_sign = a->sign; /* abs(a) is bigger and a is negative */
464 res_sign = (rounding_mode == FC_TONEGATIVE);
467 res_sign = b->sign; /* abs(b) is bigger and b is negative */
470 /* can't be reached */
477 result->sign = res_sign;
479 /* sign has been taken care of, check for special cases */
480 if (a->desc.clss == ZERO || b->desc.clss == INF) {
482 memcpy(result, b, calc_buffer_size);
483 fc_exact = b->desc.clss == NORMAL;
484 result->sign = res_sign;
487 if (b->desc.clss == ZERO || a->desc.clss == INF) {
489 memcpy(result, a, calc_buffer_size);
490 fc_exact = a->desc.clss == NORMAL;
491 result->sign = res_sign;
495 /* shift the smaller value to the right to align the radix point */
496 /* subnormals have their radix point shifted to the right,
497 * take care of this first */
498 if ((b->desc.clss == SUBNORMAL) && (a->desc.clss != SUBNORMAL)) {
499 sc_val_from_ulong(1, temp);
500 sc_sub(exp_diff, temp, exp_diff);
503 _shift_right(_mant(b), exp_diff, temp);
504 sticky = sc_had_carry();
507 if (sticky && sign) {
508 /* if subtracting a little more than the represented value or adding a little
509 * more than the represented value to a negative value this, in addition to the
510 * still set sticky bit, takes account of the 'little more' */
511 char *temp1 = alloca(calc_buffer_size);
512 sc_val_from_ulong(1, temp1);
513 sc_add(temp, temp1, temp);
517 if (sc_comp(_mant(a), temp) == -1)
518 sc_sub(temp, _mant(a), _mant(result));
520 sc_sub(_mant(a), temp, _mant(result));
522 sc_add(_mant(a), temp, _mant(result));
525 /* _normalize expects a 'normal' radix point, adding two subnormals
526 * results in a subnormal radix point -> shifting before normalizing */
527 if ((a->desc.clss == SUBNORMAL) && (b->desc.clss == SUBNORMAL)) {
528 sc_val_from_ulong(1, NULL);
529 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
532 /* resulting exponent is the bigger one */
533 memmove(_exp(result), _exp(a), value_size);
535 fc_exact &= normalize(result, result, sticky);
541 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result)
549 handle_NAN(a, b, result);
551 temp = alloca(value_size);
553 if (result != a && result != b)
554 result->desc = a->desc;
556 result->sign = res_sign = a->sign ^ b->sign;
558 /* produce NaN on 0 * inf */
559 if (a->desc.clss == ZERO) {
560 if (b->desc.clss == INF) {
561 fc_get_qnan(&a->desc, result);
565 memcpy(result, a, calc_buffer_size);
566 result->sign = res_sign;
570 if (b->desc.clss == ZERO) {
571 if (a->desc.clss == INF) {
572 fc_get_qnan(&a->desc, result);
576 memcpy(result, b, calc_buffer_size);
577 result->sign = res_sign;
582 if (a->desc.clss == INF) {
585 memcpy(result, a, calc_buffer_size);
586 result->sign = res_sign;
589 if (b->desc.clss == INF) {
592 memcpy(result, b, calc_buffer_size);
593 result->sign = res_sign;
597 /* exp = exp(a) + exp(b) - excess */
598 sc_add(_exp(a), _exp(b), _exp(result));
600 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
601 sc_sub(_exp(result), temp, _exp(result));
603 /* mixed normal, subnormal values introduce an error of 1, correct it */
604 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
605 sc_val_from_ulong(1, temp);
606 sc_add(_exp(result), temp, _exp(result));
609 sc_mul(_mant(a), _mant(b), _mant(result));
611 /* realign result: after a multiplication the digits right of the radix
612 * point are the sum of the factors' digits after the radix point. As all
613 * values are normalized they both have the same amount of these digits,
614 * which has to be restored by proper shifting
615 * because of the rounding bits */
616 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
618 _shift_right(_mant(result), temp, _mant(result));
619 sticky = sc_had_carry();
622 fc_exact &= normalize(result, result, sticky);
628 static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result)
631 char *temp, *dividend;
636 handle_NAN(a, b, result);
638 temp = alloca(value_size);
639 dividend = alloca(value_size);
641 if (result != a && result != b)
642 result->desc = a->desc;
644 result->sign = res_sign = a->sign ^ b->sign;
646 /* produce NAN on 0/0 and inf/inf */
647 if (a->desc.clss == ZERO) {
648 if (b->desc.clss == ZERO) {
650 fc_get_qnan(&a->desc, result);
655 memcpy(result, a, calc_buffer_size);
656 result->sign = res_sign;
661 if (b->desc.clss == INF) {
663 if (a->desc.clss == INF) {
665 fc_get_qnan(&a->desc, result);
668 sc_val_from_ulong(0, NULL);
669 _save_result(_exp(result));
670 _save_result(_mant(result));
671 result->desc.clss = ZERO;
676 if (a->desc.clss == INF) {
680 memcpy(result, a, calc_buffer_size);
681 result->sign = res_sign;
684 if (b->desc.clss == ZERO) {
686 /* division by zero */
688 fc_get_minusinf(&a->desc, result);
690 fc_get_plusinf(&a->desc, result);
694 /* exp = exp(a) - exp(b) + excess - 1*/
695 sc_sub(_exp(a), _exp(b), _exp(result));
696 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 2, temp);
697 sc_add(_exp(result), temp, _exp(result));
699 /* mixed normal, subnormal values introduce an error of 1, correct it */
700 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
701 sc_val_from_ulong(1, temp);
702 sc_add(_exp(result), temp, _exp(result));
705 /* mant(res) = mant(a) / 1/2mant(b) */
706 /* to gain more bits of precision in the result the dividend could be
707 * shifted left, as this operation does not loose bits. This would not
708 * fit into the integer precision, but due to the rounding bits (which
709 * are always zero because the values are all normalized) the divisor
710 * can be shifted right instead to achieve the same result */
711 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
713 _shift_left(_mant(a), temp, dividend);
716 char *divisor = alloca(calc_buffer_size);
717 sc_val_from_ulong(1, divisor);
718 _shift_right(_mant(b), divisor, divisor);
719 sc_div(dividend, divisor, _mant(result));
720 sticky = sc_had_carry();
724 fc_exact &= normalize(result, result, sticky);
728 static void _power_of_ten(int exp, ieee_descriptor_t *desc, char *result)
736 /* set new descriptor (else result is supposed to already have one) */
738 result->desc = *desc;
740 build = alloca(value_size);
741 temp = alloca(value_size);
743 sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
746 /* temp is value of ten now */
747 sc_val_from_ulong(10, NULL);
750 for (exp--; exp > 0; exp--) {
752 sc_mul(build, temp, NULL);
756 /* temp is amount of left shift needed to put the value left of the radix point */
757 sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
759 _shift_left(build, temp, _mant(result));
761 _normalize(result, result, 0);
767 * Truncate the fractional part away.
769 * This does not clip to any integer range.
771 static void _trunc(const fp_value *a, fp_value *result)
774 * When exponent == 0 all bits left of the radix point
775 * are the integral part of the value. For 15bit exp_size
776 * this would require a left shift of max. 16383 bits which
778 * But it is enough to ensure that no bit right of the radix
779 * point remains set. This restricts the interesting
780 * exponents to the interval [0, mant_size-1].
781 * Outside this interval the truncated value is either 0 or
782 * it does not have fractional parts.
785 int exp_bias, exp_val;
788 /* fixme: can be exact */
791 temp = alloca(value_size);
794 result->desc = a->desc;
796 exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
797 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
800 sc_val_from_ulong(0, NULL);
801 _save_result(_exp(result));
802 _save_result(_mant(result));
803 result->desc.clss = ZERO;
808 if (exp_val > a->desc.mantissa_size) {
810 memcpy(result, a, calc_buffer_size);
815 /* set up a proper mask to delete all bits right of the
816 * radix point if the mantissa had been shifted until exp == 0 */
817 sc_max_from_bits(1 + exp_val, 0, temp);
818 sc_val_from_long(a->desc.mantissa_size - exp_val + 2, NULL);
819 _shift_left(temp, sc_get_buffer(), temp);
821 /* and the mask and return the result */
822 sc_and(_mant(a), temp, _mant(result));
825 memcpy(_exp(result), _exp(a), value_size);
826 result->sign = a->sign;
831 * functions defined in fltcalc.h
833 const void *fc_get_buffer(void)
838 int fc_get_buffer_length(void)
840 return calc_buffer_size;
843 void *fc_val_from_str(const char *str, unsigned int len, const ieee_descriptor_t *desc, void *result)
847 /* XXX excuse of an implementation to make things work */
849 fp_value *tmp = alloca(calc_buffer_size);
850 ieee_descriptor_t tmp_desc;
852 buffer = alloca(len+1);
853 memcpy(buffer, str, len);
855 val = strtold(buffer, NULL);
857 DEBUGPRINTF(("val_from_str(%s)\n", str));
858 tmp_desc.exponent_size = 15;
859 tmp_desc.mantissa_size = 63;
860 tmp_desc.explicit_one = 1;
861 tmp_desc.clss = NORMAL;
862 fc_val_from_ieee754(val, &tmp_desc, tmp);
864 return fc_cast(tmp, desc, result);
867 fp_value *fc_val_from_ieee754(LLDBL l, const ieee_descriptor_t *desc, fp_value *result)
870 int bias_res, bias_val, mant_val;
873 UINT32 exponent, mantissa0, mantissa1;
876 bias_res = ((1 << (desc->exponent_size - 1)) - 1);
878 #ifdef HAVE_LONG_DOUBLE
881 sign = (srcval.val.high & 0x00008000) != 0;
882 exponent = (srcval.val.high & 0x00007FFF) ;
883 mantissa0 = srcval.val.mid;
884 mantissa1 = srcval.val.low;
885 #else /* no long double */
888 sign = (srcval.val.high & 0x80000000) != 0;
889 exponent = (srcval.val.high & 0x7FF00000) >> 20;
890 mantissa0 = srcval.val.high & 0x000FFFFF;
891 mantissa1 = srcval.val.low;
894 #ifdef HAVE_LONG_DOUBLE
895 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)); */
896 DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
898 TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
899 DEBUGPRINTF(("(%d-%.3X-%.5X%.8X)\n", sign, exponent, mantissa0, mantissa1));
902 if (result == NULL) result = calc_buffer;
903 temp = alloca(value_size);
905 /* CLEAR the buffer, else some bits might be uninitialized */
906 memset(result, 0, fc_get_buffer_length());
908 result->desc.exponent_size = desc->exponent_size;
909 result->desc.mantissa_size = desc->mantissa_size;
910 result->desc.explicit_one = desc->explicit_one;
915 /* sign and flag suffice to identify NaN or inf, no exponent/mantissa
916 * encoding is needed. the function can return immediately in these cases */
918 result->desc.clss = NAN;
919 TRACEPRINTF(("val_from_float resulted in NAN\n"));
923 result->desc.clss = INF;
924 TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
928 /* build exponent, because input and output exponent and mantissa sizes may differ
929 * this looks more complicated than it is: unbiased input exponent + output bias,
930 * minus the mantissa difference which is added again later when the output float
931 * becomes normalized */
932 sc_val_from_long((exponent - bias_val + bias_res) - (mant_val - desc->mantissa_size), _exp(result));
934 /* build mantissa representation */
936 /* insert the hidden bit */
937 sc_val_from_ulong(1, temp);
938 sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
939 _shift_left(temp, sc_get_buffer(), NULL);
942 sc_val_from_ulong(0, NULL);
945 _save_result(_mant(result));
947 /* bits from the upper word */
948 sc_val_from_ulong(mantissa0, temp);
949 sc_val_from_ulong(34, NULL);
950 _shift_left(temp, sc_get_buffer(), temp);
951 sc_or(_mant(result), temp, _mant(result));
953 /* bits from the lower word */
954 sc_val_from_ulong(mantissa1, temp);
955 sc_val_from_ulong(ROUNDING_BITS, NULL);
956 _shift_left(temp, sc_get_buffer(), temp);
957 sc_or(_mant(result), temp, _mant(result));
959 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
960 * origin one to the left */
962 sc_val_from_ulong(1, NULL);
963 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
966 normalize(result, result, 0);
968 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
973 LLDBL fc_val_to_ieee754(const fp_value *val)
976 fp_value *temp = NULL;
986 ieee_descriptor_t desc;
987 unsigned mantissa_size;
989 #ifdef HAVE_LONG_DOUBLE
990 desc.exponent_size = 15;
991 desc.mantissa_size = 63;
992 desc.explicit_one = 1;
995 desc.exponent_size = 11;
996 desc.mantissa_size = 52;
997 desc.explicit_one = 0;
1000 mantissa_size = desc.mantissa_size + desc.explicit_one;
1002 temp = alloca(calc_buffer_size);
1003 value = fc_cast(val, &desc, temp);
1007 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
1008 * lead to wrong results */
1009 exponent = sc_val_to_long(_exp(value)) ;
1011 sc_val_from_ulong(ROUNDING_BITS, NULL);
1012 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1017 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1018 mantissa1 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << (byte_offset << 3);
1020 for (; (byte_offset<<3) < desc.mantissa_size; byte_offset++)
1021 mantissa0 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << ((byte_offset - 4) << 3);
1023 #ifdef HAVE_LONG_DOUBLE
1024 buildval.val.high = sign << 15;
1025 buildval.val.high |= exponent;
1026 buildval.val.mid = mantissa0;
1027 buildval.val.low = mantissa1;
1028 #else /* no long double */
1029 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1030 buildval.val.high = sign << 31;
1031 buildval.val.high |= exponent << 20;
1032 buildval.val.high |= mantissa0;
1033 buildval.val.low = mantissa1;
1036 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1040 fp_value *fc_cast(const fp_value *value, const ieee_descriptor_t *desc, fp_value *result)
1043 int exp_offset, val_bias, res_bias;
1045 if (result == NULL) result = calc_buffer;
1046 temp = alloca(value_size);
1048 if (value->desc.exponent_size == desc->exponent_size &&
1049 value->desc.mantissa_size == desc->mantissa_size &&
1050 value->desc.explicit_one == desc->explicit_one) {
1051 if (value != result)
1052 memcpy(result, value, calc_buffer_size);
1056 if (value->desc.clss == NAN) {
1057 if (sc_get_highest_set_bit(_mant(value)) == value->desc.mantissa_size + 1)
1058 return fc_get_qnan(desc, result);
1060 return fc_get_snan(desc, result);
1062 else if (value->desc.clss == INF) {
1063 if (value->sign == 0)
1064 return fc_get_plusinf(desc, result);
1066 return fc_get_minusinf(desc, result);
1069 /* set the descriptor of the new value */
1070 result->desc.exponent_size = desc->exponent_size;
1071 result->desc.mantissa_size = desc->mantissa_size;
1072 result->desc.explicit_one = desc->explicit_one;
1073 result->desc.clss = value->desc.clss;
1075 result->sign = value->sign;
1077 /* when the mantissa sizes differ normalizing has to shift to align it.
1078 * this would change the exponent, which is unwanted. So calculate this
1079 * offset and add it */
1080 val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1081 res_bias = (1 << (desc->exponent_size - 1)) - 1;
1083 exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - desc->mantissa_size);
1084 sc_val_from_long(exp_offset, temp);
1085 sc_add(_exp(value), temp, _exp(result));
1087 /* _normalize expects normalized radix point */
1088 if (value->desc.clss == SUBNORMAL) {
1089 sc_val_from_ulong(1, NULL);
1090 _shift_left(_mant(value), sc_get_buffer(), _mant(result));
1091 } else if (value != result) {
1092 memcpy(_mant(result), _mant(value), value_size);
1094 memmove(_mant(result), _mant(value), value_size);
1097 normalize(result, result, 0);
1098 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1102 fp_value *fc_get_max(const ieee_descriptor_t *desc, fp_value *result)
1104 if (result == NULL) result = calc_buffer;
1106 result->desc.exponent_size = desc->exponent_size;
1107 result->desc.mantissa_size = desc->mantissa_size;
1108 result->desc.explicit_one = desc->explicit_one;
1109 result->desc.clss = NORMAL;
1113 sc_val_from_ulong((1 << desc->exponent_size) - 2, _exp(result));
1115 sc_max_from_bits(desc->mantissa_size + 1, 0, _mant(result));
1116 sc_val_from_ulong(ROUNDING_BITS, NULL);
1117 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1122 fp_value *fc_get_min(const ieee_descriptor_t *desc, fp_value *result)
1124 if (result == NULL) result = calc_buffer;
1126 fc_get_max(desc, result);
1132 fp_value *fc_get_snan(const ieee_descriptor_t *desc, fp_value *result)
1134 if (result == NULL) result = calc_buffer;
1136 result->desc.exponent_size = desc->exponent_size;
1137 result->desc.mantissa_size = desc->mantissa_size;
1138 result->desc.explicit_one = desc->explicit_one;
1139 result->desc.clss = NAN;
1143 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1145 /* signaling NaN has non-zero mantissa with msb not set */
1146 sc_val_from_ulong(1, _mant(result));
1151 fp_value *fc_get_qnan(const ieee_descriptor_t *desc, fp_value *result)
1153 if (result == NULL) result = calc_buffer;
1155 result->desc.exponent_size = desc->exponent_size;
1156 result->desc.mantissa_size = desc->mantissa_size;
1157 result->desc.explicit_one = desc->explicit_one;
1158 result->desc.clss = NAN;
1162 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1164 /* quiet NaN has the msb of the mantissa set, so shift one there */
1165 sc_val_from_ulong(1, _mant(result));
1166 /* mantissa_size >+< 1 because of two extra rounding bits */
1167 sc_val_from_ulong(desc->mantissa_size + 1, NULL);
1168 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1173 fp_value *fc_get_plusinf(const ieee_descriptor_t *desc, fp_value *result)
1177 if (result == NULL) result = calc_buffer;
1179 result->desc.exponent_size = desc->exponent_size;
1180 result->desc.mantissa_size = desc->mantissa_size;
1181 result->desc.explicit_one = desc->explicit_one;
1182 result->desc.clss = INF;
1186 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1188 mant = _mant(result);
1189 sc_val_from_ulong(0, mant);
1190 if (desc->explicit_one) {
1191 sc_set_bit_at(mant, result->desc.mantissa_size + ROUNDING_BITS);
1197 fp_value *fc_get_minusinf(const ieee_descriptor_t *desc, fp_value *result)
1199 if (result == NULL) result = calc_buffer;
1201 fc_get_plusinf(desc, result);
1207 int fc_comp(const fp_value *val_a, const fp_value *val_b)
1212 * shortcut: if both values are identical, they are either
1213 * Unordered if NaN or equal
1216 return val_a->desc.clss == NAN ? 2 : 0;
1218 /* unordered if one is a NaN */
1219 if (val_a->desc.clss == NAN || val_b->desc.clss == NAN)
1222 /* zero is equal independent of sign */
1223 if ((val_a->desc.clss == ZERO) && (val_b->desc.clss == ZERO))
1226 /* different signs make compare easy */
1227 if (val_a->sign != val_b->sign)
1228 return (val_a->sign == 0) ? (1) : (-1);
1230 mul = val_a->sign ? -1 : 1;
1232 /* both infinity means equality */
1233 if ((val_a->desc.clss == INF) && (val_b->desc.clss == INF))
1236 /* infinity is bigger than the rest */
1237 if (val_a->desc.clss == INF)
1239 if (val_b->desc.clss == INF)
1242 /* check first exponent, that mantissa if equal */
1243 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1249 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1255 int fc_is_zero(const fp_value *a)
1257 return a->desc.clss == ZERO;
1260 int fc_is_negative(const fp_value *a)
1265 int fc_is_inf(const fp_value *a)
1267 return a->desc.clss == INF;
1270 int fc_is_nan(const fp_value *a)
1272 return a->desc.clss == NAN;
1275 int fc_is_subnormal(const fp_value *a)
1277 return a->desc.clss == SUBNORMAL;
1280 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base)
1285 mul_1 = alloca(calc_buffer_size);
1289 switch ((value_class_t)val->desc.clss) {
1291 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1294 snprintf(buf, buflen, "NaN");
1297 snprintf(buf, buflen, "0.0");
1300 flt_val = fc_val_to_ieee754(val);
1301 #ifdef HAVE_LONG_DOUBLE
1302 /* XXX 30 is arbitrary */
1303 snprintf(buf, buflen, "%.30LE", flt_val);
1305 snprintf(buf, buflen, "%.18E", flt_val);
1311 switch ((value_class_t)val->desc.clss) {
1313 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1316 snprintf(buf, buflen, "NAN");
1319 snprintf(buf, buflen, "0.0");
1322 flt_val = fc_val_to_ieee754(val);
1323 #ifdef HAVE_LONG_DOUBLE
1324 snprintf(buf, buflen, "%LA", flt_val);
1326 snprintf(buf, buflen, "%A", flt_val);
1333 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1334 buf[buflen - 1] = '\0';
1340 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs)
1342 /* this is used to cache the packed version of the value */
1343 static char *packed_value = NULL;
1345 if (packed_value == NULL) packed_value = XMALLOCN(char, value_size);
1348 pack(value, packed_value);
1350 return sc_sub_bits(packed_value, num_bits, byte_ofs);
1353 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1354 int fc_zero_mantissa(const fp_value *value)
1356 return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1359 /* Returns the exponent of a value. */
1360 int fc_get_exponent(const fp_value *value)
1362 int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1363 return sc_val_to_long(_exp(value)) - exp_bias;
1366 /* Return non-zero if a given value can be converted lossless into another precision */
1367 int fc_can_lossless_conv_to(const fp_value *value, const ieee_descriptor_t *desc)
1372 /* handle some special cases first */
1373 switch (value->desc.clss) {
1382 /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
1383 exp_bias = (1 << (desc->exponent_size - 1)) - 1;
1384 v = fc_get_exponent(value) + exp_bias;
1385 if (0 < v && v < (1 << desc->exponent_size) - 1) {
1386 /* exponent can be encoded, now check the mantissa */
1387 v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
1388 return v <= desc->mantissa_size;
1394 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode)
1396 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1397 rounding_mode = mode;
1399 return rounding_mode;
1402 fc_rounding_mode_t fc_get_rounding_mode(void)
1404 return rounding_mode;
1407 void init_fltcalc(int precision)
1409 if (calc_buffer == NULL) {
1410 /* does nothing if already init */
1411 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1413 init_strcalc(precision + 2 + ROUNDING_BITS);
1415 /* needs additionally rounding bits, one bit as explicit 1., and one for
1416 * addition overflow */
1417 max_precision = sc_get_precision() - (2 + ROUNDING_BITS);
1418 if (max_precision < precision)
1419 printf("WARNING: not enough precision available, using %d\n", max_precision);
1421 rounding_mode = FC_TONEAREST;
1422 value_size = sc_get_buffer_length();
1423 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1425 calc_buffer = xmalloc(calc_buffer_size);
1426 memset(calc_buffer, 0, calc_buffer_size);
1427 DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
1428 #ifdef HAVE_LONG_DOUBLE
1429 DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
1431 DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
1433 #ifdef WORDS_BIGENDIAN
1434 DEBUGPRINTF(("\tWord order is big endian\n\n"));
1436 DEBUGPRINTF(("\tWord order is little endian\n\n"));
1441 void finish_fltcalc (void)
1443 free(calc_buffer); calc_buffer = NULL;
1446 #ifdef FLTCALC_TRACE_CALC
1447 static char buffer[100];
1450 /* definition of interface functions */
1451 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result)
1453 if (result == NULL) result = calc_buffer;
1455 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1456 TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1458 /* make the value with the bigger exponent the first one */
1459 if (sc_comp(_exp(a), _exp(b)) == -1)
1460 _fadd(b, a, result);
1462 _fadd(a, b, result);
1464 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1468 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result)
1472 if (result == NULL) result = calc_buffer;
1474 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1475 TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1477 temp = alloca(calc_buffer_size);
1478 memcpy(temp, b, calc_buffer_size);
1479 temp->sign = !b->sign;
1480 if (sc_comp(_exp(a), _exp(temp)) == -1)
1481 _fadd(temp, a, result);
1483 _fadd(a, temp, result);
1485 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1489 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result)
1491 if (result == NULL) result = calc_buffer;
1493 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1494 TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1496 _fmul(a, b, result);
1498 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1502 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result)
1504 if (result == NULL) result = calc_buffer;
1506 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1507 TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1509 _fdiv(a, b, result);
1511 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1515 fp_value *fc_neg(const fp_value *a, fp_value *result)
1517 if (result == NULL) result = calc_buffer;
1519 TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1522 memcpy(result, a, calc_buffer_size);
1523 result->sign = !a->sign;
1525 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1529 fp_value *fc_int(const fp_value *a, fp_value *result)
1531 if (result == NULL) result = calc_buffer;
1533 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1534 TRACEPRINTF(("truncated to integer "));
1538 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1542 fp_value *fc_rnd(const fp_value *a, fp_value *result)
1544 if (result == NULL) result = calc_buffer;
1547 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1548 TRACEPRINTF(("rounded to integer "));
1550 assert(!"fc_rnd() not yet implemented");
1552 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1557 * convert a floating point value into an sc value ...
1559 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode)
1561 if (a->desc.clss == NORMAL) {
1562 int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
1563 int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
1568 if (a->sign && !mode_is_signed(dst_mode)) {
1569 /* FIXME: for now we cannot convert this */
1573 tgt_bits = get_mode_size_bits(dst_mode);
1574 if (mode_is_signed(dst_mode))
1577 assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
1578 mantissa_size = a->desc.mantissa_size + ROUNDING_BITS;
1579 shift = exp_val - mantissa_size;
1581 if (tgt_bits < mantissa_size + 1)
1582 tgt_bits = mantissa_size + 1;
1584 sc_shlI(_mant(a), shift, tgt_bits, 0, result);
1586 sc_shrI(_mant(a), -shift, tgt_bits, 0, result);
1589 /* check for overflow */
1590 highest = sc_get_highest_set_bit(result);
1592 if (mode_is_signed(dst_mode)) {
1593 if (highest == sc_get_lowest_set_bit(result)) {
1594 /* need extra test for MIN_INT */
1595 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1596 /* FIXME: handle overflow */
1600 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1601 /* FIXME: handle overflow */
1606 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1607 /* FIXME: handle overflow */
1613 sc_neg(result, result);
1617 else if (a->desc.clss == ZERO) {
1625 unsigned fc_set_immediate_precision(unsigned bits)
1627 unsigned old = immediate_prec;
1629 immediate_prec = bits;
1633 int fc_is_exact(void)