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
43 * portability stuff (why do we even care about the msvc people with their C89?)
47 static long double string_to_long_double(const char *str)
49 #if __STDC_VERSION__ >= 199901L || _POSIX_C_SOURCE >= 200112L
50 return strtold(str, NULL);
52 return strtod(str, NULL);
56 static bool my_isnan(long double val)
58 #if __STDC_VERSION__ >= 199901L
61 /* hopefully the compiler does not optimize aggressively (=incorrect) */
66 static bool my_isinf(long double val)
68 #if __STDC_VERSION__ >= 199901L
71 /* hopefully the compiler does not optimize aggressively (=incorrect) */
72 return my_isnan(val-val) && !my_isnan(val);
76 /** The number of extra precision rounding bits */
77 #define ROUNDING_BITS 2
81 #ifdef WORDS_BIGENDIAN
87 #ifdef WORDS_BIGENDIAN
94 #ifdef WORDS_BIGENDIAN
99 #ifdef WORDS_BIGENDIAN
105 volatile long double d;
108 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
110 /* our floating point value */
112 float_descriptor_t desc;
115 char value[1]; /* exp[value_size] + mant[value_size] */
118 #define _exp(a) &((a)->value[0])
119 #define _mant(a) &((a)->value[value_size])
121 #define _save_result(x) memcpy((x), sc_get_buffer(), value_size)
122 #define _shift_right(x, y, res) sc_shr((x), (y), value_size*4, 0, (res))
123 #define _shift_left(x, y, res) sc_shl((x), (y), value_size*4, 0, (res))
127 # define DEBUGPRINTF(x) printf x
129 # define DEBUGPRINTF(x) ((void)0)
132 #ifdef FLTCALC_TRACE_CALC
133 # define TRACEPRINTF(x) printf x
135 # define TRACEPRINTF(x) ((void)0)
138 /** A temporal buffer. */
139 static fp_value *calc_buffer = NULL;
141 /** Current rounding mode.*/
142 static fc_rounding_mode_t rounding_mode;
144 static int calc_buffer_size;
145 static int value_size;
146 static int max_precision;
149 static int fc_exact = 1;
151 /** pack machine-like */
152 static void *pack(const fp_value *int_float, void *packed)
156 fp_value *val_buffer;
159 temp = (char*) alloca(value_size);
160 shift_val = (char*) alloca(value_size);
162 switch ((value_class_t)int_float->clss) {
164 val_buffer = (fp_value*) alloca(calc_buffer_size);
165 fc_get_qnan(&int_float->desc, val_buffer);
166 int_float = val_buffer;
170 val_buffer = (fp_value*) alloca(calc_buffer_size);
171 fc_get_plusinf(&int_float->desc, val_buffer);
172 val_buffer->sign = int_float->sign;
173 int_float = val_buffer;
179 assert(int_float->desc.explicit_one <= 1);
181 /* pack sign: move it to the left after exponent AND mantissa */
182 sc_val_from_ulong(int_float->sign, temp);
184 pos = int_float->desc.exponent_size + int_float->desc.mantissa_size + int_float->desc.explicit_one;
185 sc_val_from_ulong(pos, NULL);
186 _shift_left(temp, sc_get_buffer(), packed);
188 /* pack exponent: move it to the left after mantissa */
189 pos = int_float->desc.mantissa_size + int_float->desc.explicit_one;
190 sc_val_from_ulong(pos, shift_val);
191 _shift_left(_exp(int_float), shift_val, temp);
193 /* combine sign|exponent */
194 sc_or(temp, packed, packed);
196 /* extract mantissa */
197 /* remove rounding bits */
198 sc_val_from_ulong(ROUNDING_BITS, shift_val);
199 _shift_right(_mant(int_float), shift_val, temp);
201 /* remove leading 1 (or 0 if denormalized) */
202 sc_max_from_bits(pos, 0, shift_val); /* all mantissa bits are 1's */
203 sc_and(temp, shift_val, temp);
205 /* combine sign|exponent|mantissa */
206 sc_or(temp, packed, packed);
212 * Normalize a fp_value.
214 * @return non-zero if result is exact
216 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky)
220 char lsb, guard, round, round_dir = 0;
221 char *temp = (char*) alloca(value_size);
223 /* save rounding bits at the end */
224 hsb = ROUNDING_BITS + in_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(in_val)) - 1;
226 if (in_val != out_val) {
227 out_val->sign = in_val->sign;
228 out_val->desc = in_val->desc;
231 out_val->clss = FC_NORMAL;
233 /* mantissa all zeros, so zero exponent (because of explicit one) */
234 if (hsb == ROUNDING_BITS + in_val->desc.mantissa_size) {
235 sc_val_from_ulong(0, _exp(out_val));
239 /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
242 sc_val_from_ulong(-hsb-1, temp);
244 _shift_right(_mant(in_val), temp, _mant(out_val));
246 /* remember if some bits were shifted away */
247 if (sc_had_carry()) {
251 sc_add(_exp(in_val), temp, _exp(out_val));
252 } else if (hsb > -1) {
254 sc_val_from_ulong(hsb+1, temp);
256 _shift_left(_mant(in_val), temp, _mant(out_val));
258 sc_sub(_exp(in_val), temp, _exp(out_val));
261 /* check for exponent underflow */
262 if (sc_is_negative(_exp(out_val)) || sc_is_zero(_exp(out_val))) {
263 DEBUGPRINTF(("Exponent underflow!\n"));
264 /* exponent underflow */
265 /* shift the mantissa right to have a zero exponent */
266 sc_val_from_ulong(1, temp);
267 sc_sub(temp, _exp(out_val), NULL);
269 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
270 if (sc_had_carry()) {
274 /* denormalized means exponent of zero */
275 sc_val_from_ulong(0, _exp(out_val));
277 out_val->clss = FC_SUBNORMAL;
280 /* perform rounding by adding a value that clears the guard bit and the round bit
281 * and either causes a carry to round up or not */
282 /* get the last 3 bits of the value */
283 lsb = sc_sub_bits(_mant(out_val), out_val->desc.mantissa_size + ROUNDING_BITS, 0) & 0x7;
284 guard = (lsb&0x2)>>1;
287 switch (rounding_mode) {
289 /* round to nearest representable value, if in doubt choose the version
291 round_dir = guard && (sticky || round || lsb>>2);
294 /* if positive: round to one if the exact value is bigger, else to zero */
295 round_dir = (!out_val->sign && (guard || round || sticky));
298 /* if negative: round to one if the exact value is bigger, else to zero */
299 round_dir = (out_val->sign && (guard || round || sticky));
302 /* always round to 0 (chopping mode) */
306 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"));
308 if (round_dir == 1) {
309 guard = (round^guard)<<1;
310 lsb = !(round || guard)<<2 | guard | round;
312 lsb = -((guard<<1) | round);
315 /* add the rounded value */
317 sc_val_from_long(lsb, temp);
318 sc_add(_mant(out_val), temp, _mant(out_val));
322 /* could have rounded down to zero */
323 if (sc_is_zero(_mant(out_val)) && (out_val->clss == FC_SUBNORMAL))
324 out_val->clss = FC_ZERO;
326 /* check for rounding overflow */
327 hsb = ROUNDING_BITS + out_val->desc.mantissa_size - sc_get_highest_set_bit(_mant(out_val)) - 1;
328 if ((out_val->clss != FC_SUBNORMAL) && (hsb < -1)) {
329 sc_val_from_ulong(1, temp);
330 _shift_right(_mant(out_val), temp, _mant(out_val));
331 if (exact && sc_had_carry())
333 sc_add(_exp(out_val), temp, _exp(out_val));
334 } else if ((out_val->clss == FC_SUBNORMAL) && (hsb == -1)) {
335 /* overflow caused the mantissa to be normal again,
336 * so adapt the exponent accordingly */
337 sc_val_from_ulong(1, temp);
338 sc_add(_exp(out_val), temp, _exp(out_val));
340 out_val->clss = FC_NORMAL;
342 /* no further rounding is needed, because rounding overflow means
343 * the carry of the original rounding was propagated all the way
344 * up to the bit left of the radix point. This implies the bits
345 * to the right are all zeros (rounding is +1) */
347 /* check for exponent overflow */
348 sc_val_from_ulong((1 << out_val->desc.exponent_size) - 1, temp);
349 if (sc_comp(_exp(out_val), temp) != -1) {
350 DEBUGPRINTF(("Exponent overflow!\n"));
351 /* exponent overflow, reaction depends on rounding method:
353 * mode | sign of value | result
354 *--------------------------------------------------------------
355 * TO_NEAREST | + | +inf
357 *--------------------------------------------------------------
358 * TO_POSITIVE | + | +inf
359 * | - | smallest representable value
360 *--------------------------------------------------------------
361 * TO_NEAGTIVE | + | largest representable value
363 *--------------------------------------------------------------
364 * TO_ZERO | + | largest representable value
365 * | - | smallest representable value
366 *--------------------------------------------------------------*/
367 if (out_val->sign == 0) {
368 /* value is positive */
369 switch (rounding_mode) {
372 out_val->clss = FC_INF;
377 fc_get_max(&out_val->desc, out_val);
380 /* value is negative */
381 switch (rounding_mode) {
384 out_val->clss = FC_INF;
389 fc_get_min(&out_val->desc, out_val);
397 * Operations involving NaN's must return NaN.
398 * They are NOT exact.
400 #define handle_NAN(a, b, result) \
402 if (a->clss == FC_NAN) { \
403 if (a != result) memcpy(result, a, calc_buffer_size); \
407 if (b->clss == FC_NAN) { \
408 if (b != result) memcpy(result, b, calc_buffer_size); \
416 * calculate a + b, where a is the value with the bigger exponent
418 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result)
428 handle_NAN(a, b, result);
430 /* make sure result has a descriptor */
431 if (result != a && result != b)
432 result->desc = a->desc;
434 /* determine if this is an addition or subtraction */
435 sign = a->sign ^ b->sign;
437 /* produce NaN on inf - inf */
438 if (sign && (a->clss == FC_INF) && (b->clss == FC_INF)) {
440 fc_get_qnan(&a->desc, result);
444 temp = (char*) alloca(value_size);
445 exp_diff = (char*) alloca(value_size);
447 /* get exponent difference */
448 sc_sub(_exp(a), _exp(b), exp_diff);
450 /* initially set sign to be the sign of a, special treatment of subtraction
451 * when exponents are equal is required though.
452 * Also special care about the sign is needed when the mantissas are equal
454 if (sign && sc_val_to_long(exp_diff) == 0) {
455 switch (sc_comp(_mant(a), _mant(b))) {
457 res_sign = a->sign; /* abs(a) is bigger and a is negative */
460 res_sign = (rounding_mode == FC_TONEGATIVE);
463 res_sign = b->sign; /* abs(b) is bigger and b is negative */
466 /* can't be reached */
473 result->sign = res_sign;
475 /* sign has been taken care of, check for special cases */
476 if (a->clss == FC_ZERO || b->clss == FC_INF) {
478 memcpy(result, b, calc_buffer_size);
479 fc_exact = b->clss == FC_NORMAL;
480 result->sign = res_sign;
483 if (b->clss == FC_ZERO || a->clss == FC_INF) {
485 memcpy(result, a, calc_buffer_size);
486 fc_exact = a->clss == FC_NORMAL;
487 result->sign = res_sign;
491 /* shift the smaller value to the right to align the radix point */
492 /* subnormals have their radix point shifted to the right,
493 * take care of this first */
494 if ((b->clss == FC_SUBNORMAL) && (a->clss != FC_SUBNORMAL)) {
495 sc_val_from_ulong(1, temp);
496 sc_sub(exp_diff, temp, exp_diff);
499 _shift_right(_mant(b), exp_diff, temp);
500 sticky = sc_had_carry();
503 if (sticky && sign) {
504 /* if subtracting a little more than the represented value or adding a little
505 * more than the represented value to a negative value this, in addition to the
506 * still set sticky bit, takes account of the 'little more' */
507 char *temp1 = (char*) alloca(calc_buffer_size);
508 sc_val_from_ulong(1, temp1);
509 sc_add(temp, temp1, temp);
513 if (sc_comp(_mant(a), temp) == -1)
514 sc_sub(temp, _mant(a), _mant(result));
516 sc_sub(_mant(a), temp, _mant(result));
518 sc_add(_mant(a), temp, _mant(result));
521 /* _normalize expects a 'normal' radix point, adding two subnormals
522 * results in a subnormal radix point -> shifting before normalizing */
523 if ((a->clss == FC_SUBNORMAL) && (b->clss == FC_SUBNORMAL)) {
524 sc_val_from_ulong(1, NULL);
525 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
528 /* resulting exponent is the bigger one */
529 memmove(_exp(result), _exp(a), value_size);
531 fc_exact &= normalize(result, result, sticky);
537 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result)
545 handle_NAN(a, b, result);
547 temp = (char*) alloca(value_size);
549 if (result != a && result != b)
550 result->desc = a->desc;
552 result->sign = res_sign = a->sign ^ b->sign;
554 /* produce NaN on 0 * inf */
555 if (a->clss == FC_ZERO) {
556 if (b->clss == FC_INF) {
557 fc_get_qnan(&a->desc, result);
561 memcpy(result, a, calc_buffer_size);
562 result->sign = res_sign;
566 if (b->clss == FC_ZERO) {
567 if (a->clss == FC_INF) {
568 fc_get_qnan(&a->desc, result);
572 memcpy(result, b, calc_buffer_size);
573 result->sign = res_sign;
578 if (a->clss == FC_INF) {
581 memcpy(result, a, calc_buffer_size);
582 result->sign = res_sign;
585 if (b->clss == FC_INF) {
588 memcpy(result, b, calc_buffer_size);
589 result->sign = res_sign;
593 /* exp = exp(a) + exp(b) - excess */
594 sc_add(_exp(a), _exp(b), _exp(result));
596 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
597 sc_sub(_exp(result), temp, _exp(result));
599 /* mixed normal, subnormal values introduce an error of 1, correct it */
600 if ((a->clss == FC_SUBNORMAL) ^ (b->clss == FC_SUBNORMAL)) {
601 sc_val_from_ulong(1, temp);
602 sc_add(_exp(result), temp, _exp(result));
605 sc_mul(_mant(a), _mant(b), _mant(result));
607 /* realign result: after a multiplication the digits right of the radix
608 * point are the sum of the factors' digits after the radix point. As all
609 * values are normalized they both have the same amount of these digits,
610 * which has to be restored by proper shifting
611 * because of the rounding bits */
612 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
614 _shift_right(_mant(result), temp, _mant(result));
615 sticky = sc_had_carry();
618 fc_exact &= normalize(result, result, sticky);
624 static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result)
627 char *temp, *dividend;
632 handle_NAN(a, b, result);
634 temp = (char*) alloca(value_size);
635 dividend = (char*) alloca(value_size);
637 if (result != a && result != b)
638 result->desc = a->desc;
640 result->sign = res_sign = a->sign ^ b->sign;
642 /* produce FC_NAN on 0/0 and inf/inf */
643 if (a->clss == FC_ZERO) {
644 if (b->clss == FC_ZERO) {
646 fc_get_qnan(&a->desc, result);
651 memcpy(result, a, calc_buffer_size);
652 result->sign = res_sign;
657 if (b->clss == FC_INF) {
659 if (a->clss == FC_INF) {
661 fc_get_qnan(&a->desc, result);
664 sc_val_from_ulong(0, NULL);
665 _save_result(_exp(result));
666 _save_result(_mant(result));
667 result->clss = FC_ZERO;
672 if (a->clss == FC_INF) {
676 memcpy(result, a, calc_buffer_size);
677 result->sign = res_sign;
680 if (b->clss == FC_ZERO) {
682 /* division by zero */
684 fc_get_minusinf(&a->desc, result);
686 fc_get_plusinf(&a->desc, result);
690 /* exp = exp(a) - exp(b) + excess - 1*/
691 sc_sub(_exp(a), _exp(b), _exp(result));
692 sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 2, temp);
693 sc_add(_exp(result), temp, _exp(result));
695 /* mixed normal, subnormal values introduce an error of 1, correct it */
696 if ((a->clss == FC_SUBNORMAL) ^ (b->clss == FC_SUBNORMAL)) {
697 sc_val_from_ulong(1, temp);
698 sc_add(_exp(result), temp, _exp(result));
701 /* mant(res) = mant(a) / 1/2mant(b) */
702 /* to gain more bits of precision in the result the dividend could be
703 * shifted left, as this operation does not loose bits. This would not
704 * fit into the integer precision, but due to the rounding bits (which
705 * are always zero because the values are all normalized) the divisor
706 * can be shifted right instead to achieve the same result */
707 sc_val_from_ulong(ROUNDING_BITS + result->desc.mantissa_size, temp);
709 _shift_left(_mant(a), temp, dividend);
712 char *divisor = (char*) alloca(calc_buffer_size);
713 sc_val_from_ulong(1, divisor);
714 _shift_right(_mant(b), divisor, divisor);
715 sc_div(dividend, divisor, _mant(result));
716 sticky = sc_had_carry();
720 fc_exact &= normalize(result, result, sticky);
724 static void _power_of_ten(int exp, float_descriptor_t *desc, char *result)
732 /* set new descriptor (else result is supposed to already have one) */
734 result->desc = *desc;
736 build = alloca(value_size);
737 temp = alloca(value_size);
739 sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
742 /* temp is value of ten now */
743 sc_val_from_ulong(10, NULL);
746 for (exp--; exp > 0; exp--) {
748 sc_mul(build, temp, NULL);
752 /* temp is amount of left shift needed to put the value left of the radix point */
753 sc_val_from_ulong(result->desc.mantissa_size + ROUNDING_BITS, temp);
755 _shift_left(build, temp, _mant(result));
757 _normalize(result, result, 0);
763 * Truncate the fractional part away.
765 * This does not clip to any integer range.
767 static void _trunc(const fp_value *a, fp_value *result)
770 * When exponent == 0 all bits left of the radix point
771 * are the integral part of the value. For 15bit exp_size
772 * this would require a left shift of max. 16383 bits which
774 * But it is enough to ensure that no bit right of the radix
775 * point remains set. This restricts the interesting
776 * exponents to the interval [0, mant_size-1].
777 * Outside this interval the truncated value is either 0 or
778 * it does not have fractional parts.
781 int exp_bias, exp_val;
784 /* fixme: can be exact */
787 temp = (char*) alloca(value_size);
790 result->desc = a->desc;
791 result->clss = a->clss;
794 exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
795 exp_val = sc_val_to_long(_exp(a)) - exp_bias;
798 sc_val_from_ulong(0, NULL);
799 _save_result(_exp(result));
800 _save_result(_mant(result));
801 result->clss = FC_ZERO;
806 if (exp_val > (long)a->desc.mantissa_size) {
808 memcpy(result, a, calc_buffer_size);
813 /* set up a proper mask to delete all bits right of the
814 * radix point if the mantissa had been shifted until exp == 0 */
815 sc_max_from_bits(1 + exp_val, 0, temp);
816 sc_val_from_long(a->desc.mantissa_size - exp_val + 2, NULL);
817 _shift_left(temp, sc_get_buffer(), temp);
819 /* and the mask and return the result */
820 sc_and(_mant(a), temp, _mant(result));
823 memcpy(_exp(result), _exp(a), value_size);
824 result->sign = a->sign;
829 * functions defined in fltcalc.h
831 const void *fc_get_buffer(void)
836 int fc_get_buffer_length(void)
838 return calc_buffer_size;
841 void *fc_val_from_str(const char *str, size_t len,
842 const float_descriptor_t *desc, void *result)
846 /* XXX excuse of an implementation to make things work */
848 fp_value *tmp = (fp_value*) alloca(calc_buffer_size);
849 float_descriptor_t tmp_desc;
851 buffer = (char*) alloca(len+1);
852 memcpy(buffer, str, len);
854 val = string_to_long_double(buffer);
856 DEBUGPRINTF(("val_from_str(%s)\n", str));
857 tmp_desc.exponent_size = 15;
858 tmp_desc.mantissa_size = 63;
859 tmp_desc.explicit_one = 1;
860 fc_val_from_ieee754(val, &tmp_desc, tmp);
862 return fc_cast(tmp, desc, (fp_value*) result);
865 fp_value *fc_val_from_ieee754(long double l, const float_descriptor_t *desc,
869 int bias_res, bias_val, mant_val;
872 uint32_t exponent, mantissa0, mantissa1;
873 size_t long_double_size = sizeof(long double);
876 bias_res = ((1 << (desc->exponent_size - 1)) - 1);
878 if (long_double_size == 8) {
881 sign = (srcval.val_ld8.high & 0x80000000) != 0;
882 exponent = (srcval.val_ld8.high & 0x7FF00000) >> 20;
883 mantissa0 = srcval.val_ld8.high & 0x000FFFFF;
884 mantissa1 = srcval.val_ld8.low;
886 /* we assume an x86-like 80bit representation of the value... */
887 assert(sizeof(long double)==12 || sizeof(long double)==16);
890 sign = (srcval.val_ld12.high & 0x00008000) != 0;
891 exponent = (srcval.val_ld12.high & 0x00007FFF) ;
892 mantissa0 = srcval.val_ld12.mid;
893 mantissa1 = srcval.val_ld12.low;
897 result = calc_buffer;
898 temp = (char*) alloca(value_size);
900 /* CLEAR the buffer, else some bits might be uninitialized */
901 memset(result, 0, fc_get_buffer_length());
903 result->desc = *desc;
904 result->clss = FC_NORMAL;
907 /* sign and flag suffice to identify NaN or inf, no exponent/mantissa
908 * encoding is needed. the function can return immediately in these cases */
910 result->clss = FC_NAN;
911 TRACEPRINTF(("val_from_float resulted in NAN\n"));
913 } else if (my_isinf(l)) {
914 result->clss = FC_INF;
915 TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
919 /* build exponent, because input and output exponent and mantissa sizes may differ
920 * this looks more complicated than it is: unbiased input exponent + output bias,
921 * minus the mantissa difference which is added again later when the output float
922 * becomes normalized */
923 sc_val_from_long((exponent - bias_val + bias_res) - (mant_val - desc->mantissa_size), _exp(result));
925 /* build mantissa representation */
927 /* insert the hidden bit */
928 sc_val_from_ulong(1, temp);
929 sc_val_from_ulong(mant_val + ROUNDING_BITS, NULL);
930 _shift_left(temp, sc_get_buffer(), NULL);
933 sc_val_from_ulong(0, NULL);
936 _save_result(_mant(result));
938 /* bits from the upper word */
939 sc_val_from_ulong(mantissa0, temp);
940 sc_val_from_ulong(34, NULL);
941 _shift_left(temp, sc_get_buffer(), temp);
942 sc_or(_mant(result), temp, _mant(result));
944 /* bits from the lower word */
945 sc_val_from_ulong(mantissa1, temp);
946 sc_val_from_ulong(ROUNDING_BITS, NULL);
947 _shift_left(temp, sc_get_buffer(), temp);
948 sc_or(_mant(result), temp, _mant(result));
950 /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
951 * origin one to the left */
953 sc_val_from_ulong(1, NULL);
954 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
957 normalize(result, result, 0);
959 TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
964 long double fc_val_to_ieee754(const fp_value *val)
967 fp_value *temp = NULL;
969 unsigned byte_offset;
977 float_descriptor_t desc;
978 unsigned mantissa_size;
980 size_t long_double_size = sizeof(long double);
982 if (long_double_size == 8) {
983 desc.exponent_size = 11;
984 desc.mantissa_size = 52;
985 desc.explicit_one = 0;
987 desc.exponent_size = 15;
988 desc.mantissa_size = 63;
989 desc.explicit_one = 1;
991 mantissa_size = desc.mantissa_size + desc.explicit_one;
993 temp = (fp_value*) alloca(calc_buffer_size);
994 value = fc_cast(val, &desc, temp);
998 /* @@@ long double exponent is 15bit, so the use of sc_val_to_long should not
999 * lead to wrong results */
1000 exponent = sc_val_to_long(_exp(value)) ;
1002 sc_val_from_ulong(ROUNDING_BITS, NULL);
1003 _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1008 for (byte_offset = 0; byte_offset < 4; byte_offset++)
1009 mantissa1 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << (byte_offset << 3);
1011 for (; (byte_offset<<3) < desc.mantissa_size; byte_offset++)
1012 mantissa0 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << ((byte_offset - 4) << 3);
1014 if (long_double_size == 8) {
1015 mantissa0 &= 0x000FFFFF; /* get rid of garbage */
1016 buildval.val_ld8.high = sign << 31;
1017 buildval.val_ld8.high |= exponent << 20;
1018 buildval.val_ld8.high |= mantissa0;
1019 buildval.val_ld8.low = mantissa1;
1021 buildval.val_ld12.high = sign << 15;
1022 buildval.val_ld12.high |= exponent;
1023 buildval.val_ld12.mid = mantissa0;
1024 buildval.val_ld12.low = mantissa1;
1027 TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1031 fp_value *fc_cast(const fp_value *value, const float_descriptor_t *desc,
1035 int exp_offset, val_bias, res_bias;
1037 if (result == NULL) result = calc_buffer;
1038 temp = (char*) alloca(value_size);
1040 if (value->desc.exponent_size == desc->exponent_size &&
1041 value->desc.mantissa_size == desc->mantissa_size &&
1042 value->desc.explicit_one == desc->explicit_one) {
1043 if (value != result)
1044 memcpy(result, value, calc_buffer_size);
1048 if (value->clss == FC_NAN) {
1049 if (sc_get_highest_set_bit(_mant(value)) == value->desc.mantissa_size + 1)
1050 return fc_get_qnan(desc, result);
1052 return fc_get_snan(desc, result);
1054 else if (value->clss == FC_INF) {
1055 if (value->sign == 0)
1056 return fc_get_plusinf(desc, result);
1058 return fc_get_minusinf(desc, result);
1061 /* set the descriptor of the new value */
1062 result->desc = *desc;
1063 result->clss = value->clss;
1064 result->sign = value->sign;
1066 /* when the mantissa sizes differ normalizing has to shift to align it.
1067 * this would change the exponent, which is unwanted. So calculate this
1068 * offset and add it */
1069 val_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1070 res_bias = (1 << (desc->exponent_size - 1)) - 1;
1072 exp_offset = (res_bias - val_bias) - (value->desc.mantissa_size - desc->mantissa_size);
1073 sc_val_from_long(exp_offset, temp);
1074 sc_add(_exp(value), temp, _exp(result));
1076 /* _normalize expects normalized radix point */
1077 if (value->clss == FC_SUBNORMAL) {
1078 sc_val_from_ulong(1, NULL);
1079 _shift_left(_mant(value), sc_get_buffer(), _mant(result));
1080 } else if (value != result) {
1081 memcpy(_mant(result), _mant(value), value_size);
1083 memmove(_mant(result), _mant(value), value_size);
1086 normalize(result, result, 0);
1087 TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1091 fp_value *fc_get_max(const float_descriptor_t *desc, fp_value *result)
1093 if (result == NULL) result = calc_buffer;
1095 result->desc = *desc;
1096 result->clss = FC_NORMAL;
1099 sc_val_from_ulong((1 << desc->exponent_size) - 2, _exp(result));
1101 sc_max_from_bits(desc->mantissa_size + 1, 0, _mant(result));
1102 sc_val_from_ulong(ROUNDING_BITS, NULL);
1103 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1108 fp_value *fc_get_min(const float_descriptor_t *desc, fp_value *result)
1110 if (result == NULL) result = calc_buffer;
1112 fc_get_max(desc, result);
1118 fp_value *fc_get_snan(const float_descriptor_t *desc, fp_value *result)
1120 if (result == NULL) result = calc_buffer;
1122 result->desc = *desc;
1123 result->clss = FC_NAN;
1126 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1128 /* signaling NaN has non-zero mantissa with msb not set */
1129 sc_val_from_ulong(1, _mant(result));
1134 fp_value *fc_get_qnan(const float_descriptor_t *desc, fp_value *result)
1136 if (result == NULL) result = calc_buffer;
1138 result->desc = *desc;
1139 result->clss = FC_NAN;
1142 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1144 /* quiet NaN has the msb of the mantissa set, so shift one there */
1145 sc_val_from_ulong(1, _mant(result));
1146 /* mantissa_size >+< 1 because of two extra rounding bits */
1147 sc_val_from_ulong(desc->mantissa_size + 1, NULL);
1148 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
1153 fp_value *fc_get_plusinf(const float_descriptor_t *desc, fp_value *result)
1157 if (result == NULL) result = calc_buffer;
1159 result->desc = *desc;
1160 result->clss = FC_INF;
1163 sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1165 mant = _mant(result);
1166 sc_val_from_ulong(0, mant);
1167 if (desc->explicit_one) {
1168 sc_set_bit_at(mant, result->desc.mantissa_size + ROUNDING_BITS);
1174 fp_value *fc_get_minusinf(const float_descriptor_t *desc, fp_value *result)
1176 if (result == NULL) result = calc_buffer;
1178 fc_get_plusinf(desc, result);
1184 int fc_comp(const fp_value *val_a, const fp_value *val_b)
1189 * shortcut: if both values are identical, they are either
1190 * Unordered if NaN or equal
1193 return val_a->clss == FC_NAN ? 2 : 0;
1195 /* unordered if one is a NaN */
1196 if (val_a->clss == FC_NAN || val_b->clss == FC_NAN)
1199 /* zero is equal independent of sign */
1200 if ((val_a->clss == FC_ZERO) && (val_b->clss == FC_ZERO))
1203 /* different signs make compare easy */
1204 if (val_a->sign != val_b->sign)
1205 return (val_a->sign == 0) ? (1) : (-1);
1207 mul = val_a->sign ? -1 : 1;
1209 /* both infinity means equality */
1210 if ((val_a->clss == FC_INF) && (val_b->clss == FC_INF))
1213 /* infinity is bigger than the rest */
1214 if (val_a->clss == FC_INF)
1216 if (val_b->clss == FC_INF)
1219 /* check first exponent, that mantissa if equal */
1220 switch (sc_comp(_exp(val_a), _exp(val_b))) {
1226 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1232 int fc_is_zero(const fp_value *a)
1234 return a->clss == FC_ZERO;
1237 int fc_is_negative(const fp_value *a)
1242 int fc_is_inf(const fp_value *a)
1244 return a->clss == FC_INF;
1247 int fc_is_nan(const fp_value *a)
1249 return a->clss == FC_NAN;
1252 int fc_is_subnormal(const fp_value *a)
1254 return a->clss == FC_SUBNORMAL;
1257 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base)
1260 long double flt_val;
1262 mul_1 = (char*) alloca(calc_buffer_size);
1266 switch ((value_class_t)val->clss) {
1268 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1271 snprintf(buf, buflen, "NaN");
1274 snprintf(buf, buflen, "0.0");
1277 flt_val = fc_val_to_ieee754(val);
1278 /* XXX 30 is arbitrary */
1279 snprintf(buf, buflen, "%.30LE", flt_val);
1284 switch ((value_class_t)val->clss) {
1286 snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1289 snprintf(buf, buflen, "NaN");
1292 snprintf(buf, buflen, "0.0");
1295 flt_val = fc_val_to_ieee754(val);
1296 snprintf(buf, buflen, "%LA", flt_val);
1302 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1303 buf[buflen - 1] = '\0';
1309 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs)
1311 /* this is used to cache the packed version of the value */
1312 static char *packed_value = NULL;
1314 if (packed_value == NULL) packed_value = XMALLOCN(char, value_size);
1317 pack(value, packed_value);
1319 return sc_sub_bits(packed_value, num_bits, byte_ofs);
1322 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1323 int fc_zero_mantissa(const fp_value *value)
1325 return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1328 /* Returns the exponent of a value. */
1329 int fc_get_exponent(const fp_value *value)
1331 int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1332 return sc_val_to_long(_exp(value)) - exp_bias;
1335 /* Return non-zero if a given value can be converted lossless into another precision */
1336 int fc_can_lossless_conv_to(const fp_value *value, const float_descriptor_t *desc)
1341 /* handle some special cases first */
1342 switch (value->clss) {
1351 /* check if the exponent can be encoded: note, 0 and all ones are reserved for the exponent */
1352 exp_bias = (1 << (desc->exponent_size - 1)) - 1;
1353 v = fc_get_exponent(value) + exp_bias;
1354 if (0 < v && v < (1 << desc->exponent_size) - 1) {
1355 /* exponent can be encoded, now check the mantissa */
1356 v = value->desc.mantissa_size + ROUNDING_BITS - sc_get_lowest_set_bit(_mant(value));
1357 return v <= (int)desc->mantissa_size;
1363 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode)
1365 if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1366 rounding_mode = mode;
1368 return rounding_mode;
1371 fc_rounding_mode_t fc_get_rounding_mode(void)
1373 return rounding_mode;
1376 void init_fltcalc(int precision)
1378 if (calc_buffer == NULL) {
1379 /* does nothing if already init */
1380 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1382 init_strcalc(precision + 2 + ROUNDING_BITS);
1384 /* needs additionally rounding bits, one bit as explicit 1., and one for
1385 * addition overflow */
1386 max_precision = sc_get_precision() - (2 + ROUNDING_BITS);
1387 if (max_precision < precision)
1388 printf("WARNING: not enough precision available, using %d\n", max_precision);
1390 rounding_mode = FC_TONEAREST;
1391 value_size = sc_get_buffer_length();
1392 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1394 calc_buffer = (fp_value*) xmalloc(calc_buffer_size);
1395 memset(calc_buffer, 0, calc_buffer_size);
1396 DEBUGPRINTF(("init fltcalc:\n\tVALUE_SIZE = %d\ntCALC_BUFFER_SIZE = %d\n\tcalc_buffer = %p\n\n", value_size, calc_buffer_size, calc_buffer));
1400 void finish_fltcalc (void)
1402 free(calc_buffer); calc_buffer = NULL;
1405 #ifdef FLTCALC_TRACE_CALC
1406 static char buffer[100];
1409 /* definition of interface functions */
1410 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result)
1412 if (result == NULL) result = calc_buffer;
1414 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1415 TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1417 /* make the value with the bigger exponent the first one */
1418 if (sc_comp(_exp(a), _exp(b)) == -1)
1419 _fadd(b, a, result);
1421 _fadd(a, b, result);
1423 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1427 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result)
1431 if (result == NULL) result = calc_buffer;
1433 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1434 TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1436 temp = (fp_value*) alloca(calc_buffer_size);
1437 memcpy(temp, b, calc_buffer_size);
1438 temp->sign = !b->sign;
1439 if (sc_comp(_exp(a), _exp(temp)) == -1)
1440 _fadd(temp, a, result);
1442 _fadd(a, temp, result);
1444 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1448 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result)
1450 if (result == NULL) result = calc_buffer;
1452 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1453 TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1455 _fmul(a, b, result);
1457 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1461 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result)
1463 if (result == NULL) result = calc_buffer;
1465 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1466 TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1468 _fdiv(a, b, result);
1470 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1474 fp_value *fc_neg(const fp_value *a, fp_value *result)
1476 if (result == NULL) result = calc_buffer;
1478 TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1481 memcpy(result, a, calc_buffer_size);
1482 result->sign = !a->sign;
1484 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1488 fp_value *fc_int(const fp_value *a, fp_value *result)
1490 if (result == NULL) result = calc_buffer;
1492 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1493 TRACEPRINTF(("truncated to integer "));
1497 TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1501 fp_value *fc_rnd(const fp_value *a, fp_value *result)
1503 if (result == NULL) result = calc_buffer;
1506 TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1507 TRACEPRINTF(("rounded to integer "));
1509 panic("fc_rnd() not yet implemented");
1513 * convert a floating point value into an sc value ...
1515 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode)
1517 if (a->clss == FC_NORMAL) {
1518 int exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
1519 int exp_val = sc_val_to_long(_exp(a)) - exp_bias;
1524 if (a->sign && !mode_is_signed(dst_mode)) {
1525 /* FIXME: for now we cannot convert this */
1529 tgt_bits = get_mode_size_bits(dst_mode);
1530 if (mode_is_signed(dst_mode))
1533 assert(exp_val >= 0 && "floating point value not integral before fc_flt2int() call");
1534 mantissa_size = a->desc.mantissa_size + ROUNDING_BITS;
1535 shift = exp_val - mantissa_size;
1537 if (tgt_bits < mantissa_size + 1)
1538 tgt_bits = mantissa_size + 1;
1540 sc_shlI(_mant(a), shift, tgt_bits, 0, result);
1542 sc_shrI(_mant(a), -shift, tgt_bits, 0, result);
1545 /* check for overflow */
1546 highest = sc_get_highest_set_bit(result);
1548 if (mode_is_signed(dst_mode)) {
1549 if (highest == sc_get_lowest_set_bit(result)) {
1550 /* need extra test for MIN_INT */
1551 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1552 /* FIXME: handle overflow */
1556 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1557 /* FIXME: handle overflow */
1562 if (highest >= (int) get_mode_size_bits(dst_mode)) {
1563 /* FIXME: handle overflow */
1569 sc_neg(result, result);
1572 } else if (a->clss == FC_ZERO) {
1579 int fc_is_exact(void)