2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief tarval floating point calculations
24 * @author Mathias Heil
33 /* undef some reused constants defined by math.h */
46 /** The number of extra precision rounding bits */
47 #define ROUNDING_BITS 2
49 typedef uint32_t UINT32;
51 #ifdef HAVE_LONG_DOUBLE
52 #ifdef WORDS_BIGENDIAN
59 volatile long double d;
68 volatile long double d;
72 #ifdef WORDS_BIGENDIAN
91 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
93 /* our floating point value */
95 ieee_descriptor_t desc;
97 char value[1]; /* exp[value_size] + mant[value_size] */
100 #define _exp(a) &((a)->value[0])
101 #define _mant(a) &((a)->value[value_size])
103 #define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
104 #define _shift_right(x, y, res) sc_shr((x), (y), value_size*4, 0, (res))
105 #define _shift_left(x, y, res) sc_shl((x), (y), value_size*4, 0, (res))
109 # define DEBUGPRINTF(x) printf x
111 # define DEBUGPRINTF(x) ((void)0)
114 #ifdef FLTCALC_TRACE_CALC
115 # define TRACEPRINTF(x) printf x
117 # define TRACEPRINTF(x) ((void)0)
120 /** The immediate precision. */
121 static unsigned immediate_prec = 0;
123 /** A temporal buffer. */
124 static fp_value *calc_buffer = NULL;
126 /** Current rounding mode.*/
127 static fc_rounding_mode_t rounding_mode;
129 static int calc_buffer_size;
130 static int value_size;
131 static int max_precision;
134 static int fc_exact = 1;
137 static void fail_char(const char *str, unsigned int len, int pos)
140 printf("ERROR: Unexpected character '%c'\n", *(str + pos));
142 printf("ERROR: Unexpected end of string\n");
143 while (len-- && *str) printf("%c", *str++); printf("\n");
144 while (pos--) printf(" "); printf("^\n");
145 /* the front end has to to check constant strings */
150 /** pack machine-like */
151 static void *pack(const fp_value *int_float, void *packed)
155 fp_value *val_buffer;
158 temp = alloca(value_size);
159 shift_val = alloca(value_size);
161 switch ((value_class_t)int_float->desc.clss) {
163 val_buffer = alloca(calc_buffer_size);
164 fc_get_qnan(&int_float->desc, val_buffer);
165 int_float = val_buffer;
169 val_buffer = alloca(calc_buffer_size);
170 fc_get_plusinf(&int_float->desc, val_buffer);
171 val_buffer->sign = int_float->sign;
172 int_float = val_buffer;
178 assert(int_float->desc.explicit_one <= 1);
180 /* pack sign: move it to the left after exponent AND mantissa */
181 sc_val_from_ulong(int_float->sign, temp);
183 pos = int_float->desc.exponent_size + int_float->desc.mantissa_size + int_float->desc.explicit_one;
184 sc_val_from_ulong(pos, NULL);
185 _shift_left(temp, sc_get_buffer(), packed);
187 /* pack exponent: move it to the left after mantissa */
188 pos = int_float->desc.mantissa_size + int_float->desc.explicit_one;
189 sc_val_from_ulong(pos, shift_val);
190 _shift_left(_exp(int_float), shift_val, temp);
192 /* combine sign|exponent */
193 sc_or(temp, packed, packed);
195 /* extract mantissa */
196 /* remove rounding bits */
197 sc_val_from_ulong(ROUNDING_BITS, shift_val);
198 _shift_right(_mant(int_float), shift_val, temp);
200 /* remove leading 1 (or 0 if denormalized) */
201 sc_max_from_bits(pos, 0, shift_val); /* all mantissa bits are 1's */
202 sc_and(temp, shift_val, temp);
204 /* combine sign|exponent|mantissa */
205 sc_or(temp, packed, packed);
211 * Normalize a fp_value.
213 * @return non-zero if result is exact
215 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky)
219 char lsb, guard, round, round_dir = 0;
220 char *temp = alloca(value_size);
222 /* save rounding bits at the end */
223 hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
225 if (in_val != out_val) {
226 out_val->sign = in_val->sign;
227 memcpy(&out_val->desc, &in_val->desc, sizeof(out_val->desc));
230 out_val->desc.clss = NORMAL;
232 /* mantissa all zeros, so zero exponent (because of explicit one) */
233 if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
234 sc_val_from_ulong(0, _exp(out_val));
238 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
241 sc_val_from_ulong(-hsb-1, temp);
243 _shift_right(_mant(in_val), temp, _mant(out_val));
245 /* remember if some bits were shifted away */
246 if (sc_had_carry()) {
250 sc_add(_exp(in_val), temp, _exp(out_val));
251 } else if (hsb > -1) {
253 sc_val_from_ulong(hsb+1, temp);
255 _shift_left(_mant(in_val), temp, _mant(out_val));
257 sc_sub(_exp(in_val), temp, _exp(out_val));
260 /* check for exponent underflow */
261 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
262 DEBUGPRINTF(("Exponent underflow!\n"));
263 /* exponent underflow */
264 /* shift the mantissa right to have a zero exponent */
265 sc_val_from_ulong(1, temp);
266 sc_sub(temp, _exp(out_val), NULL);
268 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
269 if (sc_had_carry()) {
273 /* denormalized means exponent of zero */
274 sc_val_from_ulong(0, _exp(out_val));
276 out_val->desc.clss = SUBNORMAL;
279 /* perform rounding by adding a value that clears the guard bit and the round bit
280 * and either causes a carry to round up or not */
281 /* get the last 3 bits of the value */
282 lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
283 guard = (lsb&0x2)>>1;
286 switch (rounding_mode) {
288 /* round to nearest representable value, if in doubt choose the version
290 round_dir = guard && (sticky || round || lsb>>2);
293 /* if positive: round to one if the exact value is bigger, else to zero */
294 round_dir = (!out_val->sign && (guard || round || sticky));
297 /* if negative: round to one if the exact value is bigger, else to zero */
298 round_dir = (out_val->sign && (guard || round || sticky));
301 /* always round to 0 (chopping mode) */
305 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"));
307 if (round_dir == 1) {
308 guard = (round^guard)<<1;
309 lsb = !(round || guard)<<2 | guard | round;
311 lsb = -((guard<<1) | round);
314 /* add the rounded value */
316 sc_val_from_long(lsb, temp);
317 sc_add(_mant(out_val), temp, _mant(out_val));
321 /* could have rounded down to zero */
322 if (sc_is_zero(_mant(out_val)) && (out_val->desc.clss == SUBNORMAL))
323 out_val->desc.clss = ZERO;
325 /* check for rounding overflow */
326 hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
327 if ((out_val->desc.clss != SUBNORMAL) && (hsb < -1)) {
328 sc_val_from_ulong(1, temp);
329 _shift_right(_mant(out_val), temp, _mant(out_val));
330 if (exact && sc_had_carry())
332 sc_add(_exp(out_val), temp, _exp(out_val));
333 } else if ((out_val->desc.clss == SUBNORMAL) && (hsb == -1)) {
334 /* overflow caused the mantissa to be normal again,
335 * so adapt the exponent accordingly */
336 sc_val_from_ulong(1, temp);
337 sc_add(_exp(out_val), temp, _exp(out_val));
339 out_val->desc.clss = NORMAL;
341 /* no further rounding is needed, because rounding overflow means
342 * the carry of the original rounding was propagated all the way
343 * up to the bit left of the radix point. This implies the bits
344 * to the right are all zeros (rounding is +1) */
346 /* check for exponent overflow */
347 sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
348 if (sc_comp(_exp(out_val), temp) != -1) {
349 DEBUGPRINTF(("Exponent overflow!\n"));
350 /* exponent overflow, reaction depends on rounding method:
352 * mode | sign of value | result
353 *--------------------------------------------------------------
354 * TO_NEAREST | + | +inf
356 *--------------------------------------------------------------
357 * TO_POSITIVE | + | +inf
358 * | - | smallest representable value
359 *--------------------------------------------------------------
360 * TO_NEAGTIVE | + | largest representable value
362 *--------------------------------------------------------------
363 * TO_ZERO | + | largest representable value
364 * | - | smallest representable value
365 *--------------------------------------------------------------*/
366 if (out_val->sign == 0) {
367 /* value is positive */
368 switch (rounding_mode) {
371 out_val->desc.clss = INF;
376 fc_get_max(&out_val->desc, out_val);
379 /* value is negative */
380 switch (rounding_mode) {
383 out_val->desc.clss = INF;
388 fc_get_min(&out_val->desc, out_val);
396 * Operations involving NaN's must return NaN.
397 * They are NOT exact.
399 #define handle_NAN(a, b, result) \
401 if (a->desc.clss == NAN) { \
402 if (a != result) memcpy(result, a, calc_buffer_size); \
406 if (b->desc.clss == NAN) { \
407 if (b != result) memcpy(result, b, calc_buffer_size); \
415 * calculate a + b, where a is the value with the bigger exponent
417 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result)
427 handle_NAN(a, b, result);
429 /* make sure result has a descriptor */
430 if (result != a && result != b)
431 result->desc = a->desc;
433 /* determine if this is an addition or subtraction */
434 sign = a->sign ^ b->sign;
436 /* produce NaN on inf - inf */
437 if (sign && (a->desc.clss == INF) && (b->desc.clss == INF)) {
439 fc_get_qnan(&a->desc, result);
443 temp = alloca(value_size);
444 exp_diff = alloca(value_size);
446 /* get exponent difference */
447 sc_sub(_exp(a), _exp(b), exp_diff);
449 /* initially set sign to be the sign of a, special treatment of subtraction
450 * when exponents are equal is required though.
451 * Also special care about the sign is needed when the mantissas are equal
453 if (sign && sc_val_to_long(exp_diff) == 0) {
454 switch (sc_comp(_mant(a), _mant(b))) {
456 res_sign = a->sign; /* abs(a) is bigger and a is negative */
459 res_sign = (rounding_mode == FC_TONEGATIVE);
462 res_sign = b->sign; /* abs(b) is bigger and b is negative */
465 /* can't be reached */
472 result->sign = res_sign;
474 /* sign has been taken care of, check for special cases */
475 if (a->desc.clss == ZERO || b->desc.clss == INF) {
477 memcpy(result, b, calc_buffer_size);
478 fc_exact = b->desc.clss == NORMAL;
479 result->sign = res_sign;
482 if (b->desc.clss == ZERO || a->desc.clss == INF) {
484 memcpy(result, a, calc_buffer_size);
485 fc_exact = a->desc.clss == NORMAL;
486 result->sign = res_sign;
490 /* shift the smaller value to the right to align the radix point */
491 /* subnormals have their radix point shifted to the right,
492 * take care of this first */
493 if ((b->desc.clss == SUBNORMAL) && (a->desc.clss != SUBNORMAL)) {
494 sc_val_from_ulong(1, temp);
495 sc_sub(exp_diff, temp, exp_diff);
498 _shift_right(_mant(b), exp_diff, temp);
499 sticky = sc_had_carry();
502 if (sticky && sign) {
503 /* if subtracting a little more than the represented value or adding a little
504 * more than the represented value to a negative value this, in addition to the
505 * still set sticky bit, takes account of the 'little more' */
506 char *temp1 = alloca(calc_buffer_size);
507 sc_val_from_ulong(1, temp1);
508 sc_add(temp, temp1, temp);
512 if (sc_comp(_mant(a), temp) == -1)
513 sc_sub(temp, _mant(a), _mant(result));
515 sc_sub(_mant(a), temp, _mant(result));
517 sc_add(_mant(a), temp, _mant(result));
520 /* _normalize expects a 'normal' radix point, adding two subnormals
521 * results in a subnormal radix point -> shifting before normalizing */
522 if ((a->desc.clss == SUBNORMAL) && (b->desc.clss == SUBNORMAL)) {
523 sc_val_from_ulong(1, NULL);
524 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
527 /* resulting exponent is the bigger one */
528 memmove(_exp(result), _exp(a), value_size);
530 fc_exact &= normalize(result, result, sticky);
536 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result)
544 handle_NAN(a, b, result);
546 temp = alloca(value_size);
548 if (result != a && result != b)
549 result->desc = a->desc;
551 result->sign = res_sign = a->sign ^ b->sign;
553 /* produce NaN on 0 * inf */
554 if (a->desc.clss == ZERO) {
555 if (b->desc.clss == INF) {
556 fc_get_qnan(&a->desc, result);
560 memcpy(result, a, calc_buffer_size);
561 result->sign = res_sign;
565 if (b->desc.clss == ZERO) {
566 if (a->desc.clss == INF) {
567 fc_get_qnan(&a->desc, result);
571 memcpy(result, b, calc_buffer_size);
572 result->sign = res_sign;
577 if (a->desc.clss == INF) {
580 memcpy(result, a, calc_buffer_size);
581 result->sign = res_sign;
584 if (b->desc.clss == INF) {
587 memcpy(result, b, calc_buffer_size);
588 result->sign = res_sign;
592 /* exp = exp(a) + exp(b) - excess */
593 sc_add(_exp(a), _exp(b), _exp(result));
595 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
596 sc_sub(_exp(result), temp, _exp(result));
598 /* mixed normal, subnormal values introduce an error of 1, correct it */
599 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
600 sc_val_from_ulong(1, temp);
601 sc_add(_exp(result), temp, _exp(result));
604 sc_mul(_mant(a), _mant(b), _mant(result));
606 /* realign result: after a multiplication the digits right of the radix
607 * point are the sum of the factors' digits after the radix point. As all
608 * values are normalized they both have the same amount of these digits,
609 * which has to be restored by proper shifting
610 * because of the rounding bits */
611 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
613 _shift_right(_mant(result), temp, _mant(result));
614 sticky = sc_had_carry();
617 fc_exact &= normalize(result, result, sticky);
623 static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result)
626 char *temp, *dividend;
631 handle_NAN(a, b, result);
633 temp = alloca(value_size);
634 dividend = alloca(value_size);
636 if (result != a && result != b)
637 result->desc = a->desc;
639 result->sign = res_sign = a->sign ^ b->sign;
641 /* produce NAN on 0/0 and inf/inf */
642 if (a->desc.clss == ZERO) {
643 if (b->desc.clss == ZERO) {
645 fc_get_qnan(&a->desc, result);
650 memcpy(result, a, calc_buffer_size);
651 result->sign = res_sign;
656 if (b->desc.clss == INF) {
658 if (a->desc.clss == INF) {
660 fc_get_qnan(&a->desc, result);
663 sc_val_from_ulong(0, NULL);
664 _save_result(_exp(result));
665 _save_result(_mant(result));
666 result->desc.clss = ZERO;
671 if (a->desc.clss == INF) {
675 memcpy(result, a, calc_buffer_size);
676 result->sign = res_sign;
679 if (b->desc.clss == ZERO) {
681 /* division by zero */
683 fc_get_minusinf(&a->desc, result);
685 fc_get_plusinf(&a->desc, result);
689 /* exp = exp(a) - exp(b) + excess - 1*/
690 sc_sub(_exp(a), _exp(b), _exp(result));
691 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 2, temp);
692 sc_add(_exp(result), temp, _exp(result));
694 /* mixed normal, subnormal values introduce an error of 1, correct it */
695 if ((a->desc.clss == SUBNORMAL) ^ (b->desc.clss == SUBNORMAL)) {
696 sc_val_from_ulong(1, temp);
697 sc_add(_exp(result), temp, _exp(result));
700 /* mant(res) = mant(a) / 1/2mant(b) */
701 /* to gain more bits of precision in the result the dividend could be
702 * shifted left, as this operation does not loose bits. This would not
703 * fit into the integer precision, but due to the rounding bits (which
704 * are always zero because the values are all normalized) the divisor
705 * can be shifted right instead to achieve the same result */
706 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
708 _shift_left(_mant(a), temp, dividend);
711 char *divisor = alloca(calc_buffer_size);
712 sc_val_from_ulong(1, divisor);
713 _shift_right(_mant(b), divisor, divisor);
714 sc_div(dividend, divisor, _mant(result));
715 sticky = sc_had_carry();
719 fc_exact &= normalize(result, result, sticky);
723 static void _power_of_ten(int exp, ieee_descriptor_t *desc, char *result)
731 /* set new descriptor (else result is supposed to already have one) */
733 result->desc = *desc;
735 build = alloca(value_size);
736 temp = alloca(value_size);
738 sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
741 /* temp is value of ten now */
742 sc_val_from_ulong(10, NULL);
745 for (exp--; exp > 0; exp--) {
747 sc_mul(build, temp, NULL);
751 /* temp is amount of left shift needed to put the value left of the radix point */
752 sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
754 _shift_left(build, temp, _mant(result));
756 _normalize(result, result, 0);
762 * Truncate the fractional part away.
764 * This does not clip to any integer range.
766 static void _trunc(const fp_value *a, fp_value *result)
769 * When exponent == 0 all bits left of the radix point
770 * are the integral part of the value. For 15bit exp_size
771 * this would require a left shift of max. 16383 bits which
773 * But it is enough to ensure that no bit right of the radix
774 * point remains set. This restricts the interesting
775 * exponents to the interval [0, mant_size-1].
776 * Outside this interval the truncated value is either 0 or
777 * it does not have fractional parts.
780 int exp_bias, exp_val;
783 /* fixme: can be exact */
786 temp = alloca(value_size);
789 result->desc = a->desc;
791 exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
792 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
795 sc_val_from_ulong(0, NULL);
796 _save_result(_exp(result));
797 _save_result(_mant(result));
798 result->desc.clss = ZERO;
803 if (exp_val > a->desc.mantissa_size) {
805 memcpy(result, a, calc_buffer_size);
810 /* set up a proper mask to delete all bits right of the
811 * radix point if the mantissa had been shifted until exp == 0 */
812 sc_max_from_bits(1 + exp_val, 0, temp);
813 sc_val_from_long(a->desc.mantissa_size - exp_val + 2, NULL);
814 _shift_left(temp, sc_get_buffer(), temp);
816 /* and the mask and return the result */
817 sc_and(_mant(a), temp, _mant(result));
820 memcpy(_exp(result), _exp(a), value_size);
821 result->sign = a->sign;
826 * functions defined in fltcalc.h
828 const void *fc_get_buffer(void)
833 int fc_get_buffer_length(void)
835 return calc_buffer_size;
838 void *fc_val_from_str(const char *str, unsigned int len, const ieee_descriptor_t *desc, void *result)
851 int exp_int, hsb, state;
856 char *mant_str, *exp_val, *power_val;
859 if (result == NULL) result = calc_buffer;
861 exp_val = alloca(value_size);
862 power_val = alloca(calc_buffer_size);
863 mant_str = alloca((len)?(len):(strlen(str)));
865 result->desc.exponent_size = desc->exponent_size;
866 result->desc.mantissa_size = desc->mantissa_size;
867 result->desc.explicit_one = desc->explicit_one;
868 result->desc.clss = NORMAL;
875 while (len == 0 || str-old_str < len) {
891 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
898 state = RIGHT_OF_DOT;
909 fail_char(old_str, len, str - old_str);
915 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
916 mant_str[pos++] = *(str++);
920 state = RIGHT_OF_DOT;
931 mant_str[pos] = '\0';
935 fail_char(old_str, len, str - old_str);
941 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
942 mant_str[pos++] = *(str++);
953 mant_str[pos] = '\0';
957 fail_char(old_str, len, str - old_str);
967 if (*(str-1) != 'e' && *(str-1) != 'E') fail_char(old_str, len, str - old_str);
971 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
972 mant_str[pos] = '\0';
979 fail_char(old_str, len, str - old_str);
985 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
990 case '\0': goto done;
993 fail_char(old_str, len, str - old_str);
996 } /* switch(state) */
999 sc_val_from_str(mant_str, strlen(mant_str), _mant(result));
1001 /* shift to put value left of radix point */
1002 sc_val_from_ulong(mant_size + ROUNDING_BITS, exp_val);
1004 _shift_left(_mant(result), exp_val, _mant(result));
1006 sc_val_from_ulong((1 << (exp_size - 1)) - 1, _exp(result));
1008 _normalize(result, result, 0);
1010 if (state == EXPONENT) {
1011 exp_int -= atoi(str-pos);
1014 _power_of_ten(exp_int, &result->desc, power_val);
1016 _fdiv(result, power_val, result);
1020 /* XXX excuse of an implementation to make things work */
1022 fp_value *tmp = alloca(calc_buffer_size);
1023 ieee_descriptor_t tmp_desc;
1026 #if defined(HAVE_LONG_DOUBLE) && !defined(__CYGWIN__)
1027 val = strtold(str, NULL);
1028 DEBUGPRINTF(("val_from_str(%s)\n", str));
1029 tmp_desc.exponent_size = 15;
1030 tmp_desc.mantissa_size = 63;
1031 tmp_desc.explicit_one = 1;
1032 tmp_desc.clss = NORMAL;
1033 fc_val_from_ieee754(val, &tmp_desc, tmp);
1035 val = strtod(str, NULL);
1036 DEBUGPRINTF(("val_from_str(%s)\n", str));
1037 tmp_desc.exponent_size = 11;
1038 tmp_desc.mantissa_size = 52;
1039 tmp_desc.explicit_one = 0;
1040 tmp_desc.clss = NORMAL;
1041 fc_val_from_ieee754(val, &tmp_desc, tmp);
1042 #endif /* HAVE_LONG_DOUBLE */
1043 return fc_cast(tmp, desc, result);
1047 fp_value *fc_val_from_ieee754(LLDBL l, const ieee_descriptor_t *desc, fp_value *result)
1050 int bias_res, bias_val, mant_val;
1053 UINT32 exponent, mantissa0, mantissa1;
1056 bias_res = ((1 << (desc->exponent_size - 1)) - 1);
1058 #ifdef HAVE_LONG_DOUBLE
1061 sign = (srcval.val.high & 0x00008000) != 0;
1062 exponent = (srcval.val.high & 0x00007FFF) ;
1063 mantissa0 = srcval.val.mid;
1064 mantissa1 = srcval.val.low;
1065 #else /* no long double */
1068 sign = (srcval.val.high & 0x80000000) != 0;
1069 exponent = (srcval.val.high & 0x7FF00000) >> 20;
1070 mantissa0 = srcval.val.high & 0x000FFFFF;
1071 mantissa1 = srcval.val.low;
1074 #ifdef HAVE_LONG_DOUBLE
1075 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)); */
1076 DEBUGPRINTF(("(%d-%.4X-%.8X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1078 TRACEPRINTF(("val_from_float(%.8X%.8X)\n", srcval.val.high, srcval.val.low));
1079 DEBUGPRINTF(("(%d-%.3X-%.5X%.8X)\n", sign, exponent, mantissa0, mantissa1));
1082 if (result == NULL) result = calc_buffer;
1083 temp = alloca(value_size);
1085 /* CLEAR the buffer, else some bits might be uninitialized */
1086 memset(result, 0, fc_get_buffer_length());
1088 result->desc.exponent_size = desc->exponent_size;
1089 result->desc.mantissa_size = desc->mantissa_size;
1090 result->desc.explicit_one = desc->explicit_one;
1093 result->sign = sign;
1095 /* sign and flag suffice to identify NaN or inf, no exponent/mantissa
1096 * encoding is needed. the function can return immediately in these cases */
1098 result->desc.clss = NAN;
1099 TRACEPRINTF(("val_from_float resulted in NAN\n"));
1102 else if (isinf(l)) {
1103 result->desc.clss = INF;
1104 TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
1108 /* build exponent, because input and output exponent and mantissa sizes may differ
1109 * this looks more complicated than it is: unbiased input exponent + output bias,
1110 * minus the mantissa difference which is added again later when the output float
1111 * becomes normalized */
1112 sc_val_from_long((exponent - bias_val + bias_res) - (mant_val - desc->mantissa_size), _exp(result));
1114 /* build mantissa representation */
1115 if (exponent != 0) {
1116 /* insert the hidden bit */
1117 sc_val_from_ulong(1, temp);
1118 sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
1119 _shift_left(temp, sc_get_buffer(), NULL);
1122 sc_val_from_ulong(0, NULL);
1125 _save_result(_mant(result));
1127 /* bits from the upper word */
1128 sc_val_from_ulong(mantissa0, temp);
1129 sc_val_from_ulong(34, NULL);
1130 _shift_left(temp, sc_get_buffer(), temp);
1131 sc_or(_mant(result), temp, _mant(result));
1133 /* bits from the lower word */
1134 sc_val_from_ulong(mantissa1, temp);
1135 sc_val_from_ulong(ROUNDING_BITS, NULL);
1136 _shift_left(temp, sc_get_buffer(), temp);
1137 sc_or(_mant(result), temp, _mant(result));
1139 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
1140 * origin one to the left */
1141 if (exponent == 0) {
1142 sc_val_from_ulong(1, NULL);
1143 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1146 normalize(result, result, 0);
1148 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
1153 LLDBL fc_val_to_ieee754(const fp_value *val)
1156 fp_value *temp = NULL;
1166 ieee_descriptor_t desc;
1167 unsigned mantissa_size;
1169 #ifdef HAVE_LONG_DOUBLE
1170 desc.exponent_size = 15;
1171 desc.mantissa_size = 63;
1172 desc.explicit_one = 1;
1175 desc.exponent_size = 11;
1176 desc.mantissa_size = 52;
1177 desc.explicit_one = 0;
1180 mantissa_size = desc.mantissa_size + desc.explicit_one;
1182 temp = alloca(calc_buffer_size);
1183 value = fc_cast(val, &desc, temp);
1187 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
1188 * lead to wrong results */
1189 exponent = sc_val_to_long(_exp(value)) ;
1191 sc_val_from_ulong(ROUNDING_BITS, NULL);
1192 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1197 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1198 mantissa1 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << (byte_offset << 3);
1200 for (; (byte_offset<<3) < desc.mantissa_size; byte_offset++)
1201 mantissa0 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << ((byte_offset - 4) << 3);
1203 #ifdef HAVE_LONG_DOUBLE
1204 buildval.val.high = sign << 15;
1205 buildval.val.high |= exponent;
1206 buildval.val.mid = mantissa0;
1207 buildval.val.low = mantissa1;
1208 #else /* no long double */
1209 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1210 buildval.val.high = sign << 31;
1211 buildval.val.high |= exponent << 20;
1212 buildval.val.high |= mantissa0;
1213 buildval.val.low = mantissa1;
1216 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1220 fp_value *fc_cast(const fp_value *value, const ieee_descriptor_t *desc, fp_value *result)
1223 int exp_offset, val_bias, res_bias;
1225 if (result == NULL) result = calc_buffer;
1226 temp = alloca(value_size);
1228 if (value->desc.exponent_size == desc->exponent_size &&
1229 value->desc.mantissa_size == desc->mantissa_size &&
1230 value->desc.explicit_one == desc->explicit_one) {
1231 if (value != result)
1232 memcpy(result, value, calc_buffer_size);
1236 if (value->desc.clss == NAN) {
1237 if (sc_get_highest_set_bit(_mant(value)) == value->desc.mantissa_size + 1)
1238 return fc_get_qnan(desc, result);
1240 return fc_get_snan(desc, result);
1242 else if(value->desc.clss == INF) {
1243 if (value->sign == 0)
1244 return fc_get_plusinf(desc, result);
1246 return fc_get_minusinf(desc, result);
1249 /* set the descriptor of the new value */
1250 result->desc.exponent_size = desc->exponent_size;
1251 result->desc.mantissa_size = desc->mantissa_size;
1252 result->desc.explicit_one = desc->explicit_one;
1253 result->desc.clss = value->desc.clss;
1255 result->sign = value->sign;
1257 /* when the mantissa sizes differ normalizing has to shift to align it.
1258 * this would change the exponent, which is unwanted. So calculate this
1259 * offset and add it */
1260 val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1261 res_bias = (1 << (desc->exponent_size - 1)) - 1;
1263 exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - desc->mantissa_size);
1264 sc_val_from_long(exp_offset, temp);
1265 sc_add(_exp(value), temp, _exp(result));
1267 /* _normalize expects normalized radix point */
1268 if (value->desc.clss == SUBNORMAL) {
1269 sc_val_from_ulong(1, NULL);
1270 _shift_left(_mant(value), sc_get_buffer(), _mant(result));
1271 } else if (value != result) {
1272 memcpy(_mant(result), _mant(value), value_size);
1274 memmove(_mant(result), _mant(value), value_size);
1277 normalize(result, result, 0);
1278 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1282 fp_value *fc_get_max(const ieee_descriptor_t *desc, fp_value *result)
1284 if (result == NULL) result = calc_buffer;
1286 result->desc.exponent_size = desc->exponent_size;
1287 result->desc.mantissa_size = desc->mantissa_size;
1288 result->desc.explicit_one = desc->explicit_one;
1289 result->desc.clss = NORMAL;
1293 sc_val_from_ulong((1 << desc->exponent_size) - 2, _exp(result));
1295 sc_max_from_bits(desc->mantissa_size + 1, 0, _mant(result));
1296 sc_val_from_ulong(ROUNDING_BITS, NULL);
1297 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1302 fp_value *fc_get_min(const ieee_descriptor_t *desc, fp_value *result)
1304 if (result == NULL) result = calc_buffer;
1306 fc_get_max(desc, result);
1312 fp_value *fc_get_snan(const ieee_descriptor_t *desc, fp_value *result)
1314 if (result == NULL) result = calc_buffer;
1316 result->desc.exponent_size = desc->exponent_size;
1317 result->desc.mantissa_size = desc->mantissa_size;
1318 result->desc.explicit_one = desc->explicit_one;
1319 result->desc.clss = NAN;
1323 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1325 /* signaling NaN has non-zero mantissa with msb not set */
1326 sc_val_from_ulong(1, _mant(result));
1331 fp_value *fc_get_qnan(const ieee_descriptor_t *desc, fp_value *result)
1333 if (result == NULL) result = calc_buffer;
1335 result->desc.exponent_size = desc->exponent_size;
1336 result->desc.mantissa_size = desc->mantissa_size;
1337 result->desc.explicit_one = desc->explicit_one;
1338 result->desc.clss = NAN;
1342 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1344 /* quiet NaN has the msb of the mantissa set, so shift one there */
1345 sc_val_from_ulong(1, _mant(result));
1346 /* mantissa_size >+< 1 because of two extra rounding bits */
1347 sc_val_from_ulong(desc->mantissa_size + 1, NULL);
1348 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1353 fp_value *fc_get_plusinf(const ieee_descriptor_t *desc, fp_value *result)
1357 if (result == NULL) result = calc_buffer;
1359 result->desc.exponent_size = desc->exponent_size;
1360 result->desc.mantissa_size = desc->mantissa_size;
1361 result->desc.explicit_one = desc->explicit_one;
1362 result->desc.clss = INF;
1366 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1368 mant = _mant(result);
1369 sc_val_from_ulong(0, mant);
1370 if (desc->explicit_one) {
1371 sc_set_bit_at(mant, result->desc.mantissa_size + ROUNDING_BITS);
1377 fp_value *fc_get_minusinf(const ieee_descriptor_t *desc, fp_value *result)
1379 if (result == NULL) result = calc_buffer;
1381 fc_get_plusinf(desc, result);
1387 int fc_comp(const fp_value *val_a, const fp_value *val_b)
1392 * shortcut: if both values are identical, they are either
1393 * Unordered if NaN or equal
1396 return val_a->desc.clss == NAN ? 2 : 0;
1398 /* unordered if one is a NaN */
1399 if (val_a->desc.clss == NAN || val_b->desc.clss == NAN)
1402 /* zero is equal independent of sign */
1403 if ((val_a->desc.clss == ZERO) && (val_b->desc.clss == ZERO))
1406 /* different signs make compare easy */
1407 if (val_a->sign != val_b->sign)
1408 return (val_a->sign == 0) ? (1) : (-1);
1410 mul = val_a->sign ? -1 : 1;
1412 /* both infinity means equality */
1413 if ((val_a->desc.clss == INF) && (val_b->desc.clss == INF))
1416 /* infinity is bigger than the rest */
1417 if (val_a->desc.clss == INF)
1419 if (val_b->desc.clss == INF)
1422 /* check first exponent, that mantissa if equal */
1423 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1429 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1435 int fc_is_zero(const fp_value *a)
1437 return a->desc.clss == ZERO;
1440 int fc_is_negative(const fp_value *a)
1445 int fc_is_inf(const fp_value *a)
1447 return a->desc.clss == INF;
1450 int fc_is_nan(const fp_value *a)
1452 return a->desc.clss == NAN;
1455 int fc_is_subnormal(const fp_value *a)
1457 return a->desc.clss == SUBNORMAL;
1460 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base)
1465 mul_1 = alloca(calc_buffer_size);
1469 switch ((value_class_t)val->desc.clss) {
1471 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1474 snprintf(buf, buflen, "NaN");
1477 snprintf(buf, buflen, "0.0");
1480 flt_val = fc_val_to_ieee754(val);
1481 #ifdef HAVE_LONG_DOUBLE
1482 /* XXX 30 is arbitrary */
1483 snprintf(buf, buflen, "%.30LE", flt_val);
1485 snprintf(buf, buflen, "%.18E", flt_val);
1491 switch ((value_class_t)val->desc.clss) {
1493 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1496 snprintf(buf, buflen, "NAN");
1499 snprintf(buf, buflen, "0.0");
1502 flt_val = fc_val_to_ieee754(val);
1503 #ifdef HAVE_LONG_DOUBLE
1504 snprintf(buf, buflen, "%LA", flt_val);
1506 snprintf(buf, buflen, "%A", flt_val);
1513 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1514 buf[buflen - 1] = '\0';
1520 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs)
1522 /* this is used to cache the packed version of the value */
1523 static char *packed_value = NULL;
1525 if (packed_value == NULL) packed_value = XMALLOCN(char, value_size);
1528 pack(value, packed_value);
1530 return sc_sub_bits(packed_value, num_bits, byte_ofs);
1533 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1534 int fc_zero_mantissa(const fp_value *value)
1536 return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1539 /* Returns the exponent of a value. */
1540 int fc_get_exponent(const fp_value *value)
1542 int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1543 return sc_val_to_long(_exp(value)) - exp_bias;
1546 /* Return non-zero if a given value can be converted lossless into another precision */
1547 int fc_can_lossless_conv_to(const fp_value *value, const ieee_descriptor_t *desc)
1552 /* handle some special cases first */
1553 switch (value->desc.clss) {
1562 /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
1563 exp_bias = (1 << (desc->exponent_size - 1)) - 1;
1564 v = fc_get_exponent(value) + exp_bias;
1565 if (0 < v && v < (1 << desc->exponent_size) - 1) {
1566 /* exponent can be encoded, now check the mantissa */
1567 v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
1568 return v <= desc->mantissa_size;
1574 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode)
1576 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1577 rounding_mode = mode;
1579 return rounding_mode;
1582 fc_rounding_mode_t fc_get_rounding_mode(void)
1584 return rounding_mode;
1587 void init_fltcalc(int precision)
1589 if (calc_buffer == NULL) {
1590 /* does nothing if already init */
1591 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1593 init_strcalc(precision + 2 + ROUNDING_BITS);
1595 /* needs additionally rounding bits, one bit as explicit 1., and one for
1596 * addition overflow */
1597 max_precision = sc_get_precision() - (2 + ROUNDING_BITS);
1598 if (max_precision < precision)
1599 printf("WARNING: not enough precision available, using %d\n", max_precision);
1601 rounding_mode = FC_TONEAREST;
1602 value_size = sc_get_buffer_length();
1603 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1605 calc_buffer = xmalloc(calc_buffer_size);
1606 memset(calc_buffer, 0, calc_buffer_size);
1607 DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
1608 #ifdef HAVE_LONG_DOUBLE
1609 DEBUGPRINTF(("\tUsing long double (1-15-64) interface\n"));
1611 DEBUGPRINTF(("\tUsing double (1-11-52) interface\n"));
1613 #ifdef WORDS_BIGENDIAN
1614 DEBUGPRINTF(("\tWord order is big endian\n\n"));
1616 DEBUGPRINTF(("\tWord order is little endian\n\n"));
1621 void finish_fltcalc (void)
1623 free(calc_buffer); calc_buffer = NULL;
1626 #ifdef FLTCALC_TRACE_CALC
1627 static char buffer[100];
1630 /* definition of interface functions */
1631 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result)
1633 if (result == NULL) result = calc_buffer;
1635 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1636 TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1638 /* make the value with the bigger exponent the first one */
1639 if (sc_comp(_exp(a), _exp(b)) == -1)
1640 _fadd(b, a, result);
1642 _fadd(a, b, result);
1644 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1648 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result)
1652 if (result == NULL) result = calc_buffer;
1654 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1655 TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1657 temp = alloca(calc_buffer_size);
1658 memcpy(temp, b, calc_buffer_size);
1659 temp->sign = !b->sign;
1660 if (sc_comp(_exp(a), _exp(temp)) == -1)
1661 _fadd(temp, a, result);
1663 _fadd(a, temp, result);
1665 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1669 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result)
1671 if (result == NULL) result = calc_buffer;
1673 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1674 TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1676 _fmul(a, b, result);
1678 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1682 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result)
1684 if (result == NULL) result = calc_buffer;
1686 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1687 TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1689 _fdiv(a, b, result);
1691 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1695 fp_value *fc_neg(const fp_value *a, fp_value *result)
1697 if (result == NULL) result = calc_buffer;
1699 TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1702 memcpy(result, a, calc_buffer_size);
1703 result->sign = !a->sign;
1705 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1709 fp_value *fc_int(const fp_value *a, fp_value *result)
1711 if (result == NULL) result = calc_buffer;
1713 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1714 TRACEPRINTF(("truncated to integer "));
1718 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1722 fp_value *fc_rnd(const fp_value *a, fp_value *result)
1724 if (result == NULL) result = calc_buffer;
1727 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1728 TRACEPRINTF(("rounded to integer "));
1730 assert(!"fc_rnd() not yet implemented");
1732 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1737 * convert a floating point value into an sc value ...
1739 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode)
1741 if (a->desc.clss == NORMAL) {
1742 int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
1743 int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
1748 if (a->sign && !mode_is_signed(dst_mode)) {
1749 /* FIXME: for now we cannot convert this */
1753 tgt_bits = get_mode_size_bits(dst_mode);
1754 if (mode_is_signed(dst_mode))
1757 assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
1758 mantissa_size = a->desc.mantissa_size + ROUNDING_BITS;
1759 shift = exp_val - mantissa_size;
1761 if (tgt_bits < mantissa_size + 1)
1762 tgt_bits = mantissa_size + 1;
1764 sc_shlI(_mant(a), shift, tgt_bits, 0, result);
1766 sc_shrI(_mant(a), -shift, tgt_bits, 0, result);
1769 /* check for overflow */
1770 highest = sc_get_highest_set_bit(result);
1772 if (mode_is_signed(dst_mode)) {
1773 if (highest == sc_get_lowest_set_bit(result)) {
1774 /* need extra test for MIN_INT */
1775 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1776 /* FIXME: handle overflow */
1780 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1781 /* FIXME: handle overflow */
1786 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1787 /* FIXME: handle overflow */
1793 sc_neg(result, result);
1797 else if (a->desc.clss == ZERO) {
1805 unsigned fc_set_immediate_precision(unsigned bits)
1807 unsigned old = immediate_prec;
1809 immediate_prec = bits;
1813 int fc_is_exact(void)