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
44 * portability stuff (why do we even care about the msvc people with their C89?)
48 static long double string_to_long_double(const char *str)
50 #if __STDC_VERSION__ >= 199901L || _POSIX_C_SOURCE >= 200112L
51 return strtold(str, NULL);
53 return strtod(str, NULL);
57 static bool my_isnan(long double val)
59 #if __STDC_VERSION__ >= 199901L
62 /* hopefully the compiler does not optimize aggressively (=incorrect) */
67 static bool my_isinf(long double val)
69 #if __STDC_VERSION__ >= 199901L
72 /* hopefully the compiler does not optimize aggressively (=incorrect) */
73 return my_isnan(val-val) && !my_isnan(val);
77 /** The number of extra precision rounding bits */
78 #define ROUNDING_BITS 2
82 #ifdef WORDS_BIGENDIAN
88 #ifdef WORDS_BIGENDIAN
95 #ifdef WORDS_BIGENDIAN
100 #ifdef WORDS_BIGENDIAN
106 volatile long double d;
109 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
111 /* our floating point value */
113 float_descriptor_t desc;
116 char value[1]; /* exp[value_size] + mant[value_size] */
119 #define _exp(a) &((a)->value[0])
120 #define _mant(a) &((a)->value[value_size])
122 #define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
123 #define _shift_right(x, y, res) sc_shr((x), (y), value_size*4, 0, (res))
124 #define _shift_left(x, y, res) sc_shl((x), (y), value_size*4, 0, (res))
128 # define DEBUGPRINTF(x) printf x
130 # define DEBUGPRINTF(x) ((void)0)
133 #ifdef FLTCALC_TRACE_CALC
134 # define TRACEPRINTF(x) printf x
136 # define TRACEPRINTF(x) ((void)0)
139 /** A temporal buffer. */
140 static fp_value *calc_buffer = NULL;
142 /** Current rounding mode.*/
143 static fc_rounding_mode_t rounding_mode;
145 static int calc_buffer_size;
146 static int value_size;
147 static int max_precision;
150 static int fc_exact = 1;
152 /** pack machine-like */
153 static void *pack(const fp_value *int_float, void *packed)
157 fp_value *val_buffer;
160 temp = (char*) alloca(value_size);
161 shift_val = (char*) alloca(value_size);
163 switch ((value_class_t)int_float->clss) {
165 val_buffer = (fp_value*) alloca(calc_buffer_size);
166 fc_get_qnan(&int_float->desc, val_buffer);
167 int_float = val_buffer;
171 val_buffer = (fp_value*) alloca(calc_buffer_size);
172 fc_get_plusinf(&int_float->desc, val_buffer);
173 val_buffer->sign = int_float->sign;
174 int_float = val_buffer;
180 assert(int_float->desc.explicit_one <= 1);
182 /* pack sign: move it to the left after exponent AND mantissa */
183 sc_val_from_ulong(int_float->sign, temp);
185 pos = int_float->desc.exponent_size + int_float->desc.mantissa_size + int_float->desc.explicit_one;
186 sc_val_from_ulong(pos, NULL);
187 _shift_left(temp, sc_get_buffer(), packed);
189 /* pack exponent: move it to the left after mantissa */
190 pos = int_float->desc.mantissa_size + int_float->desc.explicit_one;
191 sc_val_from_ulong(pos, shift_val);
192 _shift_left(_exp(int_float), shift_val, temp);
194 /* combine sign|exponent */
195 sc_or(temp, packed, packed);
197 /* extract mantissa */
198 /* remove rounding bits */
199 sc_val_from_ulong(ROUNDING_BITS, shift_val);
200 _shift_right(_mant(int_float), shift_val, temp);
202 /* remove leading 1 (or 0 if denormalized) */
203 sc_max_from_bits(pos, 0, shift_val); /* all mantissa bits are 1's */
204 sc_and(temp, shift_val, temp);
206 /* combine sign|exponent|mantissa */
207 sc_or(temp, packed, packed);
213 * Normalize a fp_value.
215 * @return non-zero if result is exact
217 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky)
221 char lsb, guard, round, round_dir = 0;
222 char *temp = (char*) alloca(value_size);
224 /* save rounding bits at the end */
225 hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
227 if (in_val != out_val) {
228 out_val->sign = in_val->sign;
229 out_val->desc = in_val->desc;
232 out_val->clss = FC_NORMAL;
234 /* mantissa all zeros, so zero exponent (because of explicit one) */
235 if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
236 sc_val_from_ulong(0, _exp(out_val));
240 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
243 sc_val_from_ulong(-hsb-1, temp);
245 _shift_right(_mant(in_val), temp, _mant(out_val));
247 /* remember if some bits were shifted away */
248 if (sc_had_carry()) {
252 sc_add(_exp(in_val), temp, _exp(out_val));
253 } else if (hsb > -1) {
255 sc_val_from_ulong(hsb+1, temp);
257 _shift_left(_mant(in_val), temp, _mant(out_val));
259 sc_sub(_exp(in_val), temp, _exp(out_val));
262 /* check for exponent underflow */
263 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
264 DEBUGPRINTF(("Exponent underflow!\n"));
265 /* exponent underflow */
266 /* shift the mantissa right to have a zero exponent */
267 sc_val_from_ulong(1, temp);
268 sc_sub(temp, _exp(out_val), NULL);
270 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
271 if (sc_had_carry()) {
275 /* denormalized means exponent of zero */
276 sc_val_from_ulong(0, _exp(out_val));
278 out_val->clss = FC_SUBNORMAL;
281 /* perform rounding by adding a value that clears the guard bit and the round bit
282 * and either causes a carry to round up or not */
283 /* get the last 3 bits of the value */
284 lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
285 guard = (lsb&0x2)>>1;
288 switch (rounding_mode) {
290 /* round to nearest representable value, if in doubt choose the version
292 round_dir = guard && (sticky || round || lsb>>2);
295 /* if positive: round to one if the exact value is bigger, else to zero */
296 round_dir = (!out_val->sign && (guard || round || sticky));
299 /* if negative: round to one if the exact value is bigger, else to zero */
300 round_dir = (out_val->sign && (guard || round || sticky));
303 /* always round to 0 (chopping mode) */
307 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"));
309 if (round_dir == 1) {
310 guard = (round^guard)<<1;
311 lsb = !(round || guard)<<2 | guard | round;
313 lsb = -((guard<<1) | round);
316 /* add the rounded value */
318 sc_val_from_long(lsb, temp);
319 sc_add(_mant(out_val), temp, _mant(out_val));
323 /* could have rounded down to zero */
324 if (sc_is_zero(_mant(out_val)) && (out_val->clss == FC_SUBNORMAL))
325 out_val->clss = FC_ZERO;
327 /* check for rounding overflow */
328 hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
329 if ((out_val->clss != FC_SUBNORMAL) && (hsb < -1)) {
330 sc_val_from_ulong(1, temp);
331 _shift_right(_mant(out_val), temp, _mant(out_val));
332 if (exact && sc_had_carry())
334 sc_add(_exp(out_val), temp, _exp(out_val));
335 } else if ((out_val->clss == FC_SUBNORMAL) && (hsb == -1)) {
336 /* overflow caused the mantissa to be normal again,
337 * so adapt the exponent accordingly */
338 sc_val_from_ulong(1, temp);
339 sc_add(_exp(out_val), temp, _exp(out_val));
341 out_val->clss = FC_NORMAL;
343 /* no further rounding is needed, because rounding overflow means
344 * the carry of the original rounding was propagated all the way
345 * up to the bit left of the radix point. This implies the bits
346 * to the right are all zeros (rounding is +1) */
348 /* check for exponent overflow */
349 sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
350 if (sc_comp(_exp(out_val), temp) != -1) {
351 DEBUGPRINTF(("Exponent overflow!\n"));
352 /* exponent overflow, reaction depends on rounding method:
354 * mode | sign of value | result
355 *--------------------------------------------------------------
356 * TO_NEAREST | + | +inf
358 *--------------------------------------------------------------
359 * TO_POSITIVE | + | +inf
360 * | - | smallest representable value
361 *--------------------------------------------------------------
362 * TO_NEAGTIVE | + | largest representable value
364 *--------------------------------------------------------------
365 * TO_ZERO | + | largest representable value
366 * | - | smallest representable value
367 *--------------------------------------------------------------*/
368 if (out_val->sign == 0) {
369 /* value is positive */
370 switch (rounding_mode) {
373 out_val->clss = FC_INF;
378 fc_get_max(&out_val->desc, out_val);
381 /* value is negative */
382 switch (rounding_mode) {
385 out_val->clss = FC_INF;
390 fc_get_min(&out_val->desc, out_val);
398 * Operations involving NaN's must return NaN.
399 * They are NOT exact.
401 #define handle_NAN(a, b, result) \
403 if (a->clss == FC_NAN) { \
404 if (a != result) memcpy(result, a, calc_buffer_size); \
408 if (b->clss == FC_NAN) { \
409 if (b != result) memcpy(result, b, calc_buffer_size); \
417 * calculate a + b, where a is the value with the bigger exponent
419 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result)
429 handle_NAN(a, b, result);
431 /* make sure result has a descriptor */
432 if (result != a && result != b)
433 result->desc = a->desc;
435 /* determine if this is an addition or subtraction */
436 sign = a->sign ^ b->sign;
438 /* produce NaN on inf - inf */
439 if (sign && (a->clss == FC_INF) && (b->clss == FC_INF)) {
441 fc_get_qnan(&a->desc, result);
445 temp = (char*) alloca(value_size);
446 exp_diff = (char*) alloca(value_size);
448 /* get exponent difference */
449 sc_sub(_exp(a), _exp(b), exp_diff);
451 /* initially set sign to be the sign of a, special treatment of subtraction
452 * when exponents are equal is required though.
453 * Also special care about the sign is needed when the mantissas are equal
455 if (sign && sc_val_to_long(exp_diff) == 0) {
456 switch (sc_comp(_mant(a), _mant(b))) {
458 res_sign = a->sign; /* abs(a) is bigger and a is negative */
461 res_sign = (rounding_mode == FC_TONEGATIVE);
464 res_sign = b->sign; /* abs(b) is bigger and b is negative */
467 /* can't be reached */
474 result->sign = res_sign;
476 /* sign has been taken care of, check for special cases */
477 if (a->clss == FC_ZERO || b->clss == FC_INF) {
479 memcpy(result, b, calc_buffer_size);
480 fc_exact = b->clss == FC_NORMAL;
481 result->sign = res_sign;
484 if (b->clss == FC_ZERO || a->clss == FC_INF) {
486 memcpy(result, a, calc_buffer_size);
487 fc_exact = a->clss == FC_NORMAL;
488 result->sign = res_sign;
492 /* shift the smaller value to the right to align the radix point */
493 /* subnormals have their radix point shifted to the right,
494 * take care of this first */
495 if ((b->clss == FC_SUBNORMAL) && (a->clss != FC_SUBNORMAL)) {
496 sc_val_from_ulong(1, temp);
497 sc_sub(exp_diff, temp, exp_diff);
500 _shift_right(_mant(b), exp_diff, temp);
501 sticky = sc_had_carry();
504 if (sticky && sign) {
505 /* if subtracting a little more than the represented value or adding a little
506 * more than the represented value to a negative value this, in addition to the
507 * still set sticky bit, takes account of the 'little more' */
508 char *temp1 = (char*) alloca(calc_buffer_size);
509 sc_val_from_ulong(1, temp1);
510 sc_add(temp, temp1, temp);
514 if (sc_comp(_mant(a), temp) == -1)
515 sc_sub(temp, _mant(a), _mant(result));
517 sc_sub(_mant(a), temp, _mant(result));
519 sc_add(_mant(a), temp, _mant(result));
522 /* _normalize expects a 'normal' radix point, adding two subnormals
523 * results in a subnormal radix point -> shifting before normalizing */
524 if ((a->clss == FC_SUBNORMAL) && (b->clss == FC_SUBNORMAL)) {
525 sc_val_from_ulong(1, NULL);
526 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
529 /* resulting exponent is the bigger one */
530 memmove(_exp(result), _exp(a), value_size);
532 fc_exact &= normalize(result, result, sticky);
538 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result)
546 handle_NAN(a, b, result);
548 temp = (char*) alloca(value_size);
550 if (result != a && result != b)
551 result->desc = a->desc;
553 result->sign = res_sign = a->sign ^ b->sign;
555 /* produce NaN on 0 * inf */
556 if (a->clss == FC_ZERO) {
557 if (b->clss == FC_INF) {
558 fc_get_qnan(&a->desc, result);
562 memcpy(result, a, calc_buffer_size);
563 result->sign = res_sign;
567 if (b->clss == FC_ZERO) {
568 if (a->clss == FC_INF) {
569 fc_get_qnan(&a->desc, result);
573 memcpy(result, b, calc_buffer_size);
574 result->sign = res_sign;
579 if (a->clss == FC_INF) {
582 memcpy(result, a, calc_buffer_size);
583 result->sign = res_sign;
586 if (b->clss == FC_INF) {
589 memcpy(result, b, calc_buffer_size);
590 result->sign = res_sign;
594 /* exp = exp(a) + exp(b) - excess */
595 sc_add(_exp(a), _exp(b), _exp(result));
597 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
598 sc_sub(_exp(result), temp, _exp(result));
600 /* mixed normal, subnormal values introduce an error of 1, correct it */
601 if ((a->clss == FC_SUBNORMAL) ^ (b->clss == FC_SUBNORMAL)) {
602 sc_val_from_ulong(1, temp);
603 sc_add(_exp(result), temp, _exp(result));
606 sc_mul(_mant(a), _mant(b), _mant(result));
608 /* realign result: after a multiplication the digits right of the radix
609 * point are the sum of the factors' digits after the radix point. As all
610 * values are normalized they both have the same amount of these digits,
611 * which has to be restored by proper shifting
612 * because of the rounding bits */
613 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
615 _shift_right(_mant(result), temp, _mant(result));
616 sticky = sc_had_carry();
619 fc_exact &= normalize(result, result, sticky);
625 static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result)
628 char *temp, *dividend;
633 handle_NAN(a, b, result);
635 temp = (char*) alloca(value_size);
636 dividend = (char*) alloca(value_size);
638 if (result != a && result != b)
639 result->desc = a->desc;
641 result->sign = res_sign = a->sign ^ b->sign;
643 /* produce FC_NAN on 0/0 and inf/inf */
644 if (a->clss == FC_ZERO) {
645 if (b->clss == FC_ZERO) {
647 fc_get_qnan(&a->desc, result);
652 memcpy(result, a, calc_buffer_size);
653 result->sign = res_sign;
658 if (b->clss == FC_INF) {
660 if (a->clss == FC_INF) {
662 fc_get_qnan(&a->desc, result);
665 sc_val_from_ulong(0, NULL);
666 _save_result(_exp(result));
667 _save_result(_mant(result));
668 result->clss = FC_ZERO;
673 if (a->clss == FC_INF) {
677 memcpy(result, a, calc_buffer_size);
678 result->sign = res_sign;
681 if (b->clss == FC_ZERO) {
683 /* division by zero */
685 fc_get_minusinf(&a->desc, result);
687 fc_get_plusinf(&a->desc, result);
691 /* exp = exp(a) - exp(b) + excess - 1*/
692 sc_sub(_exp(a), _exp(b), _exp(result));
693 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 2, temp);
694 sc_add(_exp(result), temp, _exp(result));
696 /* mixed normal, subnormal values introduce an error of 1, correct it */
697 if ((a->clss == FC_SUBNORMAL) ^ (b->clss == FC_SUBNORMAL)) {
698 sc_val_from_ulong(1, temp);
699 sc_add(_exp(result), temp, _exp(result));
702 /* mant(res) = mant(a) / 1/2mant(b) */
703 /* to gain more bits of precision in the result the dividend could be
704 * shifted left, as this operation does not loose bits. This would not
705 * fit into the integer precision, but due to the rounding bits (which
706 * are always zero because the values are all normalized) the divisor
707 * can be shifted right instead to achieve the same result */
708 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
710 _shift_left(_mant(a), temp, dividend);
713 char *divisor = (char*) alloca(calc_buffer_size);
714 sc_val_from_ulong(1, divisor);
715 _shift_right(_mant(b), divisor, divisor);
716 sc_div(dividend, divisor, _mant(result));
717 sticky = sc_had_carry();
721 fc_exact &= normalize(result, result, sticky);
725 static void _power_of_ten(int exp, float_descriptor_t *desc, char *result)
733 /* set new descriptor (else result is supposed to already have one) */
735 result->desc = *desc;
737 build = alloca(value_size);
738 temp = alloca(value_size);
740 sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
743 /* temp is value of ten now */
744 sc_val_from_ulong(10, NULL);
747 for (exp--; exp > 0; exp--) {
749 sc_mul(build, temp, NULL);
753 /* temp is amount of left shift needed to put the value left of the radix point */
754 sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
756 _shift_left(build, temp, _mant(result));
758 _normalize(result, result, 0);
764 * Truncate the fractional part away.
766 * This does not clip to any integer range.
768 static void _trunc(const fp_value *a, fp_value *result)
771 * When exponent == 0 all bits left of the radix point
772 * are the integral part of the value. For 15bit exp_size
773 * this would require a left shift of max. 16383 bits which
775 * But it is enough to ensure that no bit right of the radix
776 * point remains set. This restricts the interesting
777 * exponents to the interval [0, mant_size-1].
778 * Outside this interval the truncated value is either 0 or
779 * it does not have fractional parts.
782 int exp_bias, exp_val;
785 /* fixme: can be exact */
788 temp = (char*) alloca(value_size);
791 result->desc = a->desc;
793 exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
794 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
797 sc_val_from_ulong(0, NULL);
798 _save_result(_exp(result));
799 _save_result(_mant(result));
800 result->clss = FC_ZERO;
805 if (exp_val > (long)a->desc.mantissa_size) {
807 memcpy(result, a, calc_buffer_size);
812 /* set up a proper mask to delete all bits right of the
813 * radix point if the mantissa had been shifted until exp == 0 */
814 sc_max_from_bits(1 + exp_val, 0, temp);
815 sc_val_from_long(a->desc.mantissa_size - exp_val + 2, NULL);
816 _shift_left(temp, sc_get_buffer(), temp);
818 /* and the mask and return the result */
819 sc_and(_mant(a), temp, _mant(result));
822 memcpy(_exp(result), _exp(a), value_size);
823 result->sign = a->sign;
828 * functions defined in fltcalc.h
830 const void *fc_get_buffer(void)
835 int fc_get_buffer_length(void)
837 return calc_buffer_size;
840 void *fc_val_from_str(const char *str, size_t len,
841 const float_descriptor_t *desc, void *result)
845 /* XXX excuse of an implementation to make things work */
847 fp_value *tmp = (fp_value*) alloca(calc_buffer_size);
848 float_descriptor_t tmp_desc;
850 buffer = (char*) alloca(len+1);
851 memcpy(buffer, str, len);
853 val = string_to_long_double(buffer);
855 DEBUGPRINTF(("val_from_str(%s)\n", str));
856 tmp_desc.exponent_size = 15;
857 tmp_desc.mantissa_size = 63;
858 tmp_desc.explicit_one = 1;
859 fc_val_from_ieee754(val, &tmp_desc, tmp);
861 return fc_cast(tmp, desc, (fp_value*) result);
864 fp_value *fc_val_from_ieee754(long double l, const float_descriptor_t *desc,
868 int bias_res, bias_val, mant_val;
871 uint32_t exponent, mantissa0, mantissa1;
872 size_t long_double_size = sizeof(long double);
875 bias_res = ((1 << (desc->exponent_size - 1)) - 1);
877 if (long_double_size == 8) {
880 sign = (srcval.val_ld8.high & 0x80000000) != 0;
881 exponent = (srcval.val_ld8.high & 0x7FF00000) >> 20;
882 mantissa0 = srcval.val_ld8.high & 0x000FFFFF;
883 mantissa1 = srcval.val_ld8.low;
885 /* we assume an x86-like 80bit representation of the value... */
886 assert(sizeof(long double)==12 || sizeof(long double)==16);
889 sign = (srcval.val_ld12.high & 0x00008000) != 0;
890 exponent = (srcval.val_ld12.high & 0x00007FFF) ;
891 mantissa0 = srcval.val_ld12.mid;
892 mantissa1 = srcval.val_ld12.low;
896 result = calc_buffer;
897 temp = (char*) alloca(value_size);
899 /* CLEAR the buffer, else some bits might be uninitialized */
900 memset(result, 0, fc_get_buffer_length());
902 result->desc = *desc;
903 result->clss = FC_NORMAL;
906 /* sign and flag suffice to identify NaN or inf, no exponent/mantissa
907 * encoding is needed. the function can return immediately in these cases */
909 result->clss = FC_NAN;
910 TRACEPRINTF(("val_from_float resulted in NAN\n"));
912 } else if (my_isinf(l)) {
913 result->clss = FC_INF;
914 TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
918 /* build exponent, because input and output exponent and mantissa sizes may differ
919 * this looks more complicated than it is: unbiased input exponent + output bias,
920 * minus the mantissa difference which is added again later when the output float
921 * becomes normalized */
922 sc_val_from_long((exponent - bias_val + bias_res) - (mant_val - desc->mantissa_size), _exp(result));
924 /* build mantissa representation */
926 /* insert the hidden bit */
927 sc_val_from_ulong(1, temp);
928 sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
929 _shift_left(temp, sc_get_buffer(), NULL);
932 sc_val_from_ulong(0, NULL);
935 _save_result(_mant(result));
937 /* bits from the upper word */
938 sc_val_from_ulong(mantissa0, temp);
939 sc_val_from_ulong(34, NULL);
940 _shift_left(temp, sc_get_buffer(), temp);
941 sc_or(_mant(result), temp, _mant(result));
943 /* bits from the lower word */
944 sc_val_from_ulong(mantissa1, temp);
945 sc_val_from_ulong(ROUNDING_BITS, NULL);
946 _shift_left(temp, sc_get_buffer(), temp);
947 sc_or(_mant(result), temp, _mant(result));
949 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
950 * origin one to the left */
952 sc_val_from_ulong(1, NULL);
953 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
956 normalize(result, result, 0);
958 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
963 long double fc_val_to_ieee754(const fp_value *val)
966 fp_value *temp = NULL;
968 unsigned byte_offset;
976 float_descriptor_t desc;
977 unsigned mantissa_size;
979 size_t long_double_size = sizeof(long double);
981 if (long_double_size == 8) {
982 desc.exponent_size = 11;
983 desc.mantissa_size = 52;
984 desc.explicit_one = 0;
986 desc.exponent_size = 15;
987 desc.mantissa_size = 63;
988 desc.explicit_one = 1;
990 mantissa_size = desc.mantissa_size + desc.explicit_one;
992 temp = (fp_value*) alloca(calc_buffer_size);
993 value = fc_cast(val, &desc, temp);
997 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
998 * lead to wrong results */
999 exponent = sc_val_to_long(_exp(value)) ;
1001 sc_val_from_ulong(ROUNDING_BITS, NULL);
1002 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1007 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1008 mantissa1 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << (byte_offset << 3);
1010 for (; (byte_offset<<3) < desc.mantissa_size; byte_offset++)
1011 mantissa0 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << ((byte_offset - 4) << 3);
1013 if (long_double_size == 8) {
1014 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1015 buildval.val_ld8.high = sign << 31;
1016 buildval.val_ld8.high |= exponent << 20;
1017 buildval.val_ld8.high |= mantissa0;
1018 buildval.val_ld8.low = mantissa1;
1020 buildval.val_ld12.high = sign << 15;
1021 buildval.val_ld12.high |= exponent;
1022 buildval.val_ld12.mid = mantissa0;
1023 buildval.val_ld12.low = mantissa1;
1026 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1030 fp_value *fc_cast(const fp_value *value, const float_descriptor_t *desc,
1034 int exp_offset, val_bias, res_bias;
1036 if (result == NULL) result = calc_buffer;
1037 temp = (char*) alloca(value_size);
1039 if (value->desc.exponent_size == desc->exponent_size &&
1040 value->desc.mantissa_size == desc->mantissa_size &&
1041 value->desc.explicit_one == desc->explicit_one) {
1042 if (value != result)
1043 memcpy(result, value, calc_buffer_size);
1047 if (value->clss == FC_NAN) {
1048 if (sc_get_highest_set_bit(_mant(value)) == value->desc.mantissa_size + 1)
1049 return fc_get_qnan(desc, result);
1051 return fc_get_snan(desc, result);
1053 else if (value->clss == FC_INF) {
1054 if (value->sign == 0)
1055 return fc_get_plusinf(desc, result);
1057 return fc_get_minusinf(desc, result);
1060 /* set the descriptor of the new value */
1061 result->desc = *desc;
1062 result->clss = value->clss;
1063 result->sign = value->sign;
1065 /* when the mantissa sizes differ normalizing has to shift to align it.
1066 * this would change the exponent, which is unwanted. So calculate this
1067 * offset and add it */
1068 val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1069 res_bias = (1 << (desc->exponent_size - 1)) - 1;
1071 exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - desc->mantissa_size);
1072 sc_val_from_long(exp_offset, temp);
1073 sc_add(_exp(value), temp, _exp(result));
1075 /* _normalize expects normalized radix point */
1076 if (value->clss == FC_SUBNORMAL) {
1077 sc_val_from_ulong(1, NULL);
1078 _shift_left(_mant(value), sc_get_buffer(), _mant(result));
1079 } else if (value != result) {
1080 memcpy(_mant(result), _mant(value), value_size);
1082 memmove(_mant(result), _mant(value), value_size);
1085 normalize(result, result, 0);
1086 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1090 fp_value *fc_get_max(const float_descriptor_t *desc, fp_value *result)
1092 if (result == NULL) result = calc_buffer;
1094 result->desc = *desc;
1095 result->clss = FC_NORMAL;
1098 sc_val_from_ulong((1 << desc->exponent_size) - 2, _exp(result));
1100 sc_max_from_bits(desc->mantissa_size + 1, 0, _mant(result));
1101 sc_val_from_ulong(ROUNDING_BITS, NULL);
1102 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1107 fp_value *fc_get_min(const float_descriptor_t *desc, fp_value *result)
1109 if (result == NULL) result = calc_buffer;
1111 fc_get_max(desc, result);
1117 fp_value *fc_get_snan(const float_descriptor_t *desc, fp_value *result)
1119 if (result == NULL) result = calc_buffer;
1121 result->desc = *desc;
1122 result->clss = FC_NAN;
1125 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1127 /* signaling NaN has non-zero mantissa with msb not set */
1128 sc_val_from_ulong(1, _mant(result));
1133 fp_value *fc_get_qnan(const float_descriptor_t *desc, fp_value *result)
1135 if (result == NULL) result = calc_buffer;
1137 result->desc = *desc;
1138 result->clss = FC_NAN;
1141 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1143 /* quiet NaN has the msb of the mantissa set, so shift one there */
1144 sc_val_from_ulong(1, _mant(result));
1145 /* mantissa_size >+< 1 because of two extra rounding bits */
1146 sc_val_from_ulong(desc->mantissa_size + 1, NULL);
1147 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1152 fp_value *fc_get_plusinf(const float_descriptor_t *desc, fp_value *result)
1156 if (result == NULL) result = calc_buffer;
1158 result->desc = *desc;
1159 result->clss = FC_INF;
1162 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1164 mant = _mant(result);
1165 sc_val_from_ulong(0, mant);
1166 if (desc->explicit_one) {
1167 sc_set_bit_at(mant, result->desc.mantissa_size + ROUNDING_BITS);
1173 fp_value *fc_get_minusinf(const float_descriptor_t *desc, fp_value *result)
1175 if (result == NULL) result = calc_buffer;
1177 fc_get_plusinf(desc, result);
1183 int fc_comp(const fp_value *val_a, const fp_value *val_b)
1188 * shortcut: if both values are identical, they are either
1189 * Unordered if NaN or equal
1192 return val_a->clss == FC_NAN ? 2 : 0;
1194 /* unordered if one is a NaN */
1195 if (val_a->clss == FC_NAN || val_b->clss == FC_NAN)
1198 /* zero is equal independent of sign */
1199 if ((val_a->clss == FC_ZERO) && (val_b->clss == FC_ZERO))
1202 /* different signs make compare easy */
1203 if (val_a->sign != val_b->sign)
1204 return (val_a->sign == 0) ? (1) : (-1);
1206 mul = val_a->sign ? -1 : 1;
1208 /* both infinity means equality */
1209 if ((val_a->clss == FC_INF) && (val_b->clss == FC_INF))
1212 /* infinity is bigger than the rest */
1213 if (val_a->clss == FC_INF)
1215 if (val_b->clss == FC_INF)
1218 /* check first exponent, that mantissa if equal */
1219 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1225 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1231 int fc_is_zero(const fp_value *a)
1233 return a->clss == FC_ZERO;
1236 int fc_is_negative(const fp_value *a)
1241 int fc_is_inf(const fp_value *a)
1243 return a->clss == FC_INF;
1246 int fc_is_nan(const fp_value *a)
1248 return a->clss == FC_NAN;
1251 int fc_is_subnormal(const fp_value *a)
1253 return a->clss == FC_SUBNORMAL;
1256 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base)
1259 long double flt_val;
1261 mul_1 = (char*) alloca(calc_buffer_size);
1265 switch ((value_class_t)val->clss) {
1267 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1270 snprintf(buf, buflen, "NaN");
1273 snprintf(buf, buflen, "0.0");
1276 flt_val = fc_val_to_ieee754(val);
1277 /* XXX 30 is arbitrary */
1278 snprintf(buf, buflen, "%.30LE", flt_val);
1283 switch ((value_class_t)val->clss) {
1285 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1288 snprintf(buf, buflen, "NaN");
1291 snprintf(buf, buflen, "0.0");
1294 flt_val = fc_val_to_ieee754(val);
1295 snprintf(buf, buflen, "%LA", flt_val);
1301 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1302 buf[buflen - 1] = '\0';
1308 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs)
1310 /* this is used to cache the packed version of the value */
1311 static char *packed_value = NULL;
1313 if (packed_value == NULL) packed_value = XMALLOCN(char, value_size);
1316 pack(value, packed_value);
1318 return sc_sub_bits(packed_value, num_bits, byte_ofs);
1321 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1322 int fc_zero_mantissa(const fp_value *value)
1324 return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1327 /* Returns the exponent of a value. */
1328 int fc_get_exponent(const fp_value *value)
1330 int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1331 return sc_val_to_long(_exp(value)) - exp_bias;
1334 /* Return non-zero if a given value can be converted lossless into another precision */
1335 int fc_can_lossless_conv_to(const fp_value *value, const float_descriptor_t *desc)
1340 /* handle some special cases first */
1341 switch (value->clss) {
1350 /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
1351 exp_bias = (1 << (desc->exponent_size - 1)) - 1;
1352 v = fc_get_exponent(value) + exp_bias;
1353 if (0 < v && v < (1 << desc->exponent_size) - 1) {
1354 /* exponent can be encoded, now check the mantissa */
1355 v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
1356 return v <= (int)desc->mantissa_size;
1362 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode)
1364 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1365 rounding_mode = mode;
1367 return rounding_mode;
1370 fc_rounding_mode_t fc_get_rounding_mode(void)
1372 return rounding_mode;
1375 void init_fltcalc(int precision)
1377 if (calc_buffer == NULL) {
1378 /* does nothing if already init */
1379 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1381 init_strcalc(precision + 2 + ROUNDING_BITS);
1383 /* needs additionally rounding bits, one bit as explicit 1., and one for
1384 * addition overflow */
1385 max_precision = sc_get_precision() - (2 + ROUNDING_BITS);
1386 if (max_precision < precision)
1387 printf("WARNING: not enough precision available, using %d\n", max_precision);
1389 rounding_mode = FC_TONEAREST;
1390 value_size = sc_get_buffer_length();
1391 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1393 calc_buffer = (fp_value*) xmalloc(calc_buffer_size);
1394 memset(calc_buffer, 0, calc_buffer_size);
1395 DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
1399 void finish_fltcalc (void)
1401 free(calc_buffer); calc_buffer = NULL;
1404 #ifdef FLTCALC_TRACE_CALC
1405 static char buffer[100];
1408 /* definition of interface functions */
1409 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result)
1411 if (result == NULL) result = calc_buffer;
1413 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1414 TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1416 /* make the value with the bigger exponent the first one */
1417 if (sc_comp(_exp(a), _exp(b)) == -1)
1418 _fadd(b, a, result);
1420 _fadd(a, b, result);
1422 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1426 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result)
1430 if (result == NULL) result = calc_buffer;
1432 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1433 TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1435 temp = (fp_value*) alloca(calc_buffer_size);
1436 memcpy(temp, b, calc_buffer_size);
1437 temp->sign = !b->sign;
1438 if (sc_comp(_exp(a), _exp(temp)) == -1)
1439 _fadd(temp, a, result);
1441 _fadd(a, temp, result);
1443 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1447 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result)
1449 if (result == NULL) result = calc_buffer;
1451 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1452 TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1454 _fmul(a, b, result);
1456 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1460 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result)
1462 if (result == NULL) result = calc_buffer;
1464 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1465 TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1467 _fdiv(a, b, result);
1469 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1473 fp_value *fc_neg(const fp_value *a, fp_value *result)
1475 if (result == NULL) result = calc_buffer;
1477 TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1480 memcpy(result, a, calc_buffer_size);
1481 result->sign = !a->sign;
1483 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1487 fp_value *fc_int(const fp_value *a, fp_value *result)
1489 if (result == NULL) result = calc_buffer;
1491 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1492 TRACEPRINTF(("truncated to integer "));
1496 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1500 fp_value *fc_rnd(const fp_value *a, fp_value *result)
1502 if (result == NULL) result = calc_buffer;
1505 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1506 TRACEPRINTF(("rounded to integer "));
1508 panic("fc_rnd() not yet implemented");
1512 * convert a floating point value into an sc value ...
1514 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode)
1516 if (a->clss == FC_NORMAL) {
1517 int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
1518 int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
1523 if (a->sign && !mode_is_signed(dst_mode)) {
1524 /* FIXME: for now we cannot convert this */
1528 tgt_bits = get_mode_size_bits(dst_mode);
1529 if (mode_is_signed(dst_mode))
1532 assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
1533 mantissa_size = a->desc.mantissa_size + ROUNDING_BITS;
1534 shift = exp_val - mantissa_size;
1536 if (tgt_bits < mantissa_size + 1)
1537 tgt_bits = mantissa_size + 1;
1539 sc_shlI(_mant(a), shift, tgt_bits, 0, result);
1541 sc_shrI(_mant(a), -shift, tgt_bits, 0, result);
1544 /* check for overflow */
1545 highest = sc_get_highest_set_bit(result);
1547 if (mode_is_signed(dst_mode)) {
1548 if (highest == sc_get_lowest_set_bit(result)) {
1549 /* need extra test for MIN_INT */
1550 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1551 /* FIXME: handle overflow */
1555 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1556 /* FIXME: handle overflow */
1561 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1562 /* FIXME: handle overflow */
1568 sc_neg(result, result);
1571 } else if (a->clss == FC_ZERO) {
1578 int fc_is_exact(void)