ir_mode: simplify interface, improve float-mode handling
[libfirm] / ir / tv / fltcalc.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    tarval floating point calculations
23  * @date     2003
24  * @author   Mathias Heil
25  * @version  $Id$
26  */
27 #include "config.h"
28
29 #include "fltcalc.h"
30 #include "strcalc.h"
31 #include "error.h"
32
33 #include <math.h>
34 #include <inttypes.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <assert.h>
39 #include <stdbool.h>
40
41 #include "xmalloc.h"
42
43 /*
44  * portability stuff (why do we even care about the msvc people with their C89?)
45  */
46
47
48 static long double string_to_long_double(const char *str)
49 {
50 #if __STDC_VERSION__ >= 199901L || _POSIX_C_SOURCE >= 200112L
51         return strtold(str, NULL);
52 #else
53         return strtod(str, NULL);
54 #endif
55 }
56
57 static bool my_isnan(long double val)
58 {
59 #if __STDC_VERSION__ >= 199901L
60         return isnan(val);
61 #else
62         /* hopefully the compiler does not optimize aggressively (=incorrect) */
63         return val != val;
64 #endif
65 }
66
67 static bool my_isinf(long double val)
68 {
69 #if __STDC_VERSION__ >= 199901L
70         return isinf(val);
71 #else
72         /* hopefully the compiler does not optimize aggressively (=incorrect) */
73         return my_isnan(val-val) && !my_isnan(val);
74 #endif
75 }
76
77 /** The number of extra precision rounding bits */
78 #define ROUNDING_BITS 2
79
80 typedef union {
81         struct {
82 #ifdef WORDS_BIGENDIAN
83                 uint32_t high;
84 #else
85                 uint32_t low;
86 #endif
87                 uint32_t mid;
88 #ifdef WORDS_BIGENDIAN
89                 uint32_t low;
90 #else
91                 uint32_t high;
92 #endif
93         } val_ld12;
94         struct {
95 #ifdef WORDS_BIGENDIAN
96                 uint32_t high;
97 #else
98                 uint32_t low;
99 #endif
100 #ifdef WORDS_BIGENDIAN
101                 uint32_t low;
102 #else
103                 uint32_t high;
104 #endif
105         } val_ld8;
106         volatile long double d;
107 } value_t;
108
109 #define CLEAR_BUFFER(buffer) memset(buffer, 0, calc_buffer_size)
110
111 /* our floating point value */
112 struct fp_value {
113         float_descriptor_t desc;
114         unsigned char clss;
115         char sign;
116         char value[1];        /* exp[value_size] + mant[value_size] */
117 };
118
119 #define _exp(a)  &((a)->value[0])
120 #define _mant(a) &((a)->value[value_size])
121
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))
125
126
127 #ifdef FLTCALC_DEBUG
128 #  define DEBUGPRINTF(x) printf x
129 #else
130 #  define DEBUGPRINTF(x) ((void)0)
131 #endif
132
133 #ifdef FLTCALC_TRACE_CALC
134 #  define TRACEPRINTF(x) printf x
135 #else
136 #  define TRACEPRINTF(x) ((void)0)
137 #endif
138
139 /** A temporal buffer. */
140 static fp_value *calc_buffer = NULL;
141
142 /** Current rounding mode.*/
143 static fc_rounding_mode_t rounding_mode;
144
145 static int calc_buffer_size;
146 static int value_size;
147 static int max_precision;
148
149 /** Exact flag. */
150 static int fc_exact = 1;
151
152 /** pack machine-like */
153 static void *pack(const fp_value *int_float, void *packed)
154 {
155         char     *shift_val;
156         char     *temp;
157         fp_value *val_buffer;
158         int      pos;
159
160         temp      = (char*) alloca(value_size);
161         shift_val = (char*) alloca(value_size);
162
163         switch ((value_class_t)int_float->clss) {
164         case FC_NAN:
165                 val_buffer = (fp_value*) alloca(calc_buffer_size);
166                 fc_get_qnan(&int_float->desc, val_buffer);
167                 int_float = val_buffer;
168                 break;
169
170         case FC_INF:
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;
175                 break;
176
177         default:
178                 break;
179         }
180         assert(int_float->desc.explicit_one <= 1);
181
182         /* pack sign: move it to the left after exponent AND mantissa */
183         sc_val_from_ulong(int_float->sign, temp);
184
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);
188
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);
193
194         /* combine sign|exponent */
195         sc_or(temp, packed, packed);
196
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);
201
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);
205
206         /* combine sign|exponent|mantissa */
207         sc_or(temp, packed, packed);
208
209         return packed;
210 }
211
212 /**
213  * Normalize a fp_value.
214  *
215  * @return non-zero if result is exact
216  */
217 static int normalize(const fp_value *in_val, fp_value *out_val, int sticky)
218 {
219         int exact = 1;
220         int hsb;
221         char lsb, guard, round, round_dir = 0;
222         char *temp = (char*) alloca(value_size);
223
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;
226
227         if (in_val != out_val)   {
228                 out_val->sign = in_val->sign;
229                 out_val->desc = in_val->desc;
230         }
231
232         out_val->clss = FC_NORMAL;
233
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));
237                 hsb = -1;
238         }
239
240         /* shift the first 1 into the left of the radix point (i.e. hsb == -1) */
241         if (hsb < -1)   {
242                 /* shift right */
243                 sc_val_from_ulong(-hsb-1, temp);
244
245                 _shift_right(_mant(in_val), temp, _mant(out_val));
246
247                 /* remember if some bits were shifted away */
248                 if (sc_had_carry()) {
249                         exact = 0;
250                         sticky = 1;
251                 }
252                 sc_add(_exp(in_val), temp, _exp(out_val));
253         } else if (hsb > -1) {
254                 /* shift left */
255                 sc_val_from_ulong(hsb+1, temp);
256
257                 _shift_left(_mant(in_val), temp, _mant(out_val));
258
259                 sc_sub(_exp(in_val), temp, _exp(out_val));
260         }
261
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);
269
270                 _shift_right(_mant(out_val), sc_get_buffer(), _mant(out_val));
271                 if (sc_had_carry()) {
272                         exact  = 0;
273                         sticky = 1;
274                 }
275                 /* denormalized means exponent of zero */
276                 sc_val_from_ulong(0, _exp(out_val));
277
278                 out_val->clss = FC_SUBNORMAL;
279         }
280
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;
286         round = lsb&0x1;
287
288         switch (rounding_mode) {
289         case FC_TONEAREST:
290                 /* round to nearest representable value, if in doubt choose the version
291                  * with lsb == 0 */
292                 round_dir = guard && (sticky || round || lsb>>2);
293                 break;
294         case FC_TOPOSITIVE:
295                 /* if positive: round to one if the exact value is bigger, else to zero */
296                 round_dir = (!out_val->sign && (guard || round || sticky));
297                 break;
298         case FC_TONEGATIVE:
299                 /* if negative: round to one if the exact value is bigger, else to zero */
300                 round_dir = (out_val->sign && (guard || round || sticky));
301                 break;
302         case FC_TOZERO:
303                 /* always round to 0 (chopping mode) */
304                 round_dir = 0;
305                 break;
306         }
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"));
308
309         if (round_dir == 1) {
310                 guard = (round^guard)<<1;
311                 lsb = !(round || guard)<<2 | guard | round;
312         } else {
313                 lsb = -((guard<<1) | round);
314         }
315
316         /* add the rounded value */
317         if (lsb != 0) {
318                 sc_val_from_long(lsb, temp);
319                 sc_add(_mant(out_val), temp, _mant(out_val));
320                 exact = 0;
321         }
322
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;
326
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())
333                         exact = 0;
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));
340
341                 out_val->clss = FC_NORMAL;
342         }
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) */
347
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:
353                  *
354                  * mode        | sign of value |  result
355                  *--------------------------------------------------------------
356                  * TO_NEAREST  |      +        |   +inf
357                  *             |      -        |   -inf
358                  *--------------------------------------------------------------
359                  * TO_POSITIVE |      +        |   +inf
360                  *             |      -        |   smallest representable value
361                  *--------------------------------------------------------------
362                  * TO_NEAGTIVE |      +        |   largest representable value
363                  *             |      -        |   -inf
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) {
371                         case FC_TONEAREST:
372                         case FC_TOPOSITIVE:
373                                 out_val->clss = FC_INF;
374                                 break;
375
376                         case FC_TONEGATIVE:
377                         case FC_TOZERO:
378                                 fc_get_max(&out_val->desc, out_val);
379                         }
380                 } else {
381                         /* value is negative */
382                         switch (rounding_mode) {
383                         case FC_TONEAREST:
384                         case FC_TONEGATIVE:
385                                 out_val->clss = FC_INF;
386                                 break;
387
388                         case FC_TOPOSITIVE:
389                         case FC_TOZERO:
390                                 fc_get_min(&out_val->desc, out_val);
391                         }
392                 }
393         }
394         return exact;
395 }
396
397 /**
398  * Operations involving NaN's must return NaN.
399  * They are NOT exact.
400  */
401 #define handle_NAN(a, b, result) \
402 do {                                                      \
403   if (a->clss == FC_NAN) {                                \
404     if (a != result) memcpy(result, a, calc_buffer_size); \
405     fc_exact = 0;                                         \
406     return;                                               \
407   }                                                       \
408   if (b->clss == FC_NAN) {                                \
409     if (b != result) memcpy(result, b, calc_buffer_size); \
410     fc_exact = 0;                                         \
411     return;                                               \
412   }                                                       \
413 }while (0)
414
415
416 /**
417  * calculate a + b, where a is the value with the bigger exponent
418  */
419 static void _fadd(const fp_value *a, const fp_value *b, fp_value *result)
420 {
421         char *temp;
422         char *exp_diff;
423
424         char sign, res_sign;
425         char sticky;
426
427         fc_exact = 1;
428
429         handle_NAN(a, b, result);
430
431         /* make sure result has a descriptor */
432         if (result != a && result != b)
433                 result->desc = a->desc;
434
435         /* determine if this is an addition or subtraction */
436         sign = a->sign ^ b->sign;
437
438         /* produce NaN on inf - inf */
439         if (sign && (a->clss == FC_INF) && (b->clss == FC_INF)) {
440                 fc_exact = 0;
441                 fc_get_qnan(&a->desc, result);
442                 return;
443         }
444
445         temp     = (char*) alloca(value_size);
446         exp_diff = (char*) alloca(value_size);
447
448         /* get exponent difference */
449         sc_sub(_exp(a), _exp(b), exp_diff);
450
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
454          * (+/- 0 ?) */
455         if (sign && sc_val_to_long(exp_diff) == 0) {
456                 switch (sc_comp(_mant(a), _mant(b))) {
457                 case 1:  /* a > b */
458                         res_sign = a->sign;  /* abs(a) is bigger and a is negative */
459                         break;
460                 case 0:  /* a == b */
461                         res_sign = (rounding_mode == FC_TONEGATIVE);
462                         break;
463                 case -1: /* a < b */
464                         res_sign = b->sign; /* abs(b) is bigger and b is negative */
465                         break;
466                 default:
467                         /* can't be reached */
468                         res_sign = 0;
469                         break;
470                 }
471         }
472         else
473                 res_sign = a->sign;
474         result->sign = res_sign;
475
476         /* sign has been taken care of, check for special cases */
477         if (a->clss == FC_ZERO || b->clss == FC_INF) {
478                 if (b != result)
479                         memcpy(result, b, calc_buffer_size);
480                 fc_exact = b->clss == FC_NORMAL;
481                 result->sign = res_sign;
482                 return;
483         }
484         if (b->clss == FC_ZERO || a->clss == FC_INF) {
485                 if (a != result)
486                         memcpy(result, a, calc_buffer_size);
487                 fc_exact = a->clss == FC_NORMAL;
488                 result->sign = res_sign;
489                 return;
490         }
491
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);
498         }
499
500         _shift_right(_mant(b), exp_diff, temp);
501         sticky = sc_had_carry();
502         fc_exact &= !sticky;
503
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);
511         }
512
513         if (sign) {
514                 if (sc_comp(_mant(a), temp) == -1)
515                         sc_sub(temp, _mant(a), _mant(result));
516                 else
517                         sc_sub(_mant(a), temp, _mant(result));
518         } else {
519                 sc_add(_mant(a), temp, _mant(result));
520         }
521
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));
527         }
528
529         /* resulting exponent is the bigger one */
530         memmove(_exp(result), _exp(a), value_size);
531
532         fc_exact &= normalize(result, result, sticky);
533 }
534
535 /**
536  * calculate a * b
537  */
538 static void _fmul(const fp_value *a, const fp_value *b, fp_value *result)
539 {
540         int sticky;
541         char *temp;
542         char res_sign;
543
544         fc_exact = 1;
545
546         handle_NAN(a, b, result);
547
548         temp = (char*) alloca(value_size);
549
550         if (result != a && result != b)
551                 result->desc = a->desc;
552
553         result->sign = res_sign = a->sign ^ b->sign;
554
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);
559                         fc_exact = 0;
560                 } else {
561                         if (a != result)
562                                 memcpy(result, a, calc_buffer_size);
563                         result->sign = res_sign;
564                 }
565                 return;
566         }
567         if (b->clss == FC_ZERO) {
568                 if (a->clss == FC_INF) {
569                         fc_get_qnan(&a->desc, result);
570                         fc_exact = 0;
571                 } else {
572                         if (b != result)
573                                 memcpy(result, b, calc_buffer_size);
574                         result->sign = res_sign;
575                 }
576                 return;
577         }
578
579         if (a->clss == FC_INF) {
580                 fc_exact = 0;
581                 if (a != result)
582                         memcpy(result, a, calc_buffer_size);
583                 result->sign = res_sign;
584                 return;
585         }
586         if (b->clss == FC_INF) {
587                 fc_exact = 0;
588                 if (b != result)
589                         memcpy(result, b, calc_buffer_size);
590                 result->sign = res_sign;
591                 return;
592         }
593
594         /* exp = exp(a) + exp(b) - excess */
595         sc_add(_exp(a), _exp(b), _exp(result));
596
597         sc_val_from_ulong((1 << (a->desc.exponent_size - 1)) - 1, temp);
598         sc_sub(_exp(result), temp, _exp(result));
599
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));
604         }
605
606         sc_mul(_mant(a), _mant(b), _mant(result));
607
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);
614
615         _shift_right(_mant(result), temp, _mant(result));
616         sticky = sc_had_carry();
617         fc_exact &= !sticky;
618
619         fc_exact &= normalize(result, result, sticky);
620 }
621
622 /**
623  * calculate a / b
624  */
625 static void _fdiv(const fp_value *a, const fp_value *b, fp_value *result)
626 {
627         int sticky;
628         char *temp, *dividend;
629         char res_sign;
630
631         fc_exact = 1;
632
633         handle_NAN(a, b, result);
634
635         temp = (char*) alloca(value_size);
636         dividend = (char*) alloca(value_size);
637
638         if (result != a && result != b)
639                 result->desc = a->desc;
640
641         result->sign = res_sign = a->sign ^ b->sign;
642
643         /* produce FC_NAN on 0/0 and inf/inf */
644         if (a->clss == FC_ZERO) {
645                 if (b->clss == FC_ZERO) {
646                         /* 0/0 -> NaN */
647                         fc_get_qnan(&a->desc, result);
648                         fc_exact = 0;
649                 } else {
650                         /* 0/x -> a */
651                         if (a != result)
652                                 memcpy(result, a, calc_buffer_size);
653                         result->sign = res_sign;
654                 }
655                 return;
656         }
657
658         if (b->clss == FC_INF) {
659                 fc_exact = 0;
660                 if (a->clss == FC_INF) {
661                         /* inf/inf -> NaN */
662                         fc_get_qnan(&a->desc, result);
663                 } else {
664                         /* x/inf -> 0 */
665                         sc_val_from_ulong(0, NULL);
666                         _save_result(_exp(result));
667                         _save_result(_mant(result));
668                         result->clss = FC_ZERO;
669                 }
670                 return;
671         }
672
673         if (a->clss == FC_INF) {
674                 fc_exact = 0;
675                 /* inf/x -> inf */
676                 if (a != result)
677                         memcpy(result, a, calc_buffer_size);
678                 result->sign = res_sign;
679                 return;
680         }
681         if (b->clss == FC_ZERO) {
682                 fc_exact = 0;
683                 /* division by zero */
684                 if (result->sign)
685                         fc_get_minusinf(&a->desc, result);
686                 else
687                         fc_get_plusinf(&a->desc, result);
688                 return;
689         }
690
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));
695
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));
700         }
701
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);
709
710         _shift_left(_mant(a), temp, dividend);
711
712         {
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();
718                 fc_exact &= !sticky;
719         }
720
721         fc_exact &= normalize(result, result, sticky);
722 }
723
724 #if 0
725 static void _power_of_ten(int exp, float_descriptor_t *desc, char *result)
726 {
727         char *build;
728         char *temp;
729
730         /* positive sign */
731         result->sign = 0;
732
733         /* set new descriptor (else result is supposed to already have one) */
734         if (desc != NULL)
735                 result->desc = *desc;
736
737         build = alloca(value_size);
738         temp = alloca(value_size);
739
740         sc_val_from_ulong((1 << (result->desc.exponent_size - 1)) - 1, _exp(result));
741
742         if (exp > 0) {
743                 /* temp is value of ten now */
744                 sc_val_from_ulong(10, NULL);
745                 _save_result(temp);
746
747                 for (exp--; exp > 0; exp--) {
748                         _save_result(build);
749                         sc_mul(build, temp, NULL);
750                 }
751                 _save_result(build);
752
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);
755
756                 _shift_left(build, temp, _mant(result));
757
758                 _normalize(result, result, 0);
759         }
760 }
761 #endif
762
763 /**
764  * Truncate the fractional part away.
765  *
766  * This does not clip to any integer range.
767  */
768 static void _trunc(const fp_value *a, fp_value *result)
769 {
770         /*
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
774          * is too much.
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.
780          */
781
782         int exp_bias, exp_val;
783         char *temp;
784
785         /* fixme: can be exact */
786         fc_exact = 0;
787
788         temp = (char*) alloca(value_size);
789
790         if (a != result)
791                 result->desc = a->desc;
792
793         exp_bias = (1 << (a->desc.exponent_size - 1)) - 1;
794         exp_val  = sc_val_to_long(_exp(a)) - exp_bias;
795
796         if (exp_val < 0) {
797                 sc_val_from_ulong(0, NULL);
798                 _save_result(_exp(result));
799                 _save_result(_mant(result));
800                 result->clss = FC_ZERO;
801
802                 return;
803         }
804
805         if (exp_val > (long)a->desc.mantissa_size) {
806                 if (a != result)
807                         memcpy(result, a, calc_buffer_size);
808
809                 return;
810         }
811
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);
817
818         /* and the mask and return the result */
819         sc_and(_mant(a), temp, _mant(result));
820
821         if (a != result) {
822                 memcpy(_exp(result), _exp(a), value_size);
823                 result->sign = a->sign;
824         }
825 }
826
827 /********
828  * functions defined in fltcalc.h
829  ********/
830 const void *fc_get_buffer(void)
831 {
832         return calc_buffer;
833 }
834
835 int fc_get_buffer_length(void)
836 {
837         return calc_buffer_size;
838 }
839
840 void *fc_val_from_str(const char *str, size_t len,
841                       const float_descriptor_t *desc, void *result)
842 {
843         char *buffer;
844
845         /* XXX excuse of an implementation to make things work */
846         long double        val;
847         fp_value          *tmp = (fp_value*) alloca(calc_buffer_size);
848         float_descriptor_t tmp_desc;
849
850         buffer = (char*) alloca(len+1);
851         memcpy(buffer, str, len);
852         buffer[len] = '\0';
853         val = string_to_long_double(buffer);
854
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);
860
861         return fc_cast(tmp, desc, (fp_value*) result);
862 }
863
864 fp_value *fc_val_from_ieee754(long double l, const float_descriptor_t *desc,
865                               fp_value *result)
866 {
867         char    *temp;
868         int      bias_res, bias_val, mant_val;
869         value_t  srcval;
870         char     sign;
871         uint32_t exponent, mantissa0, mantissa1;
872         size_t   long_double_size = sizeof(long double);
873
874         srcval.d = l;
875         bias_res = ((1 << (desc->exponent_size - 1)) - 1);
876
877         if (long_double_size == 8) {
878                 mant_val  = 52;
879                 bias_val  = 0x3ff;
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;
884         } else {
885                 /* we assume an x86-like 80bit representation of the value... */
886                 assert(sizeof(long double)==12 || sizeof(long double)==16);
887                 mant_val  = 63;
888                 bias_val  = 0x3fff;
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;
893         }
894
895         if (result == NULL)
896                 result = calc_buffer;
897         temp = (char*) alloca(value_size);
898
899         /* CLEAR the buffer, else some bits might be uninitialized */
900         memset(result, 0, fc_get_buffer_length());
901
902         result->desc = *desc;
903         result->clss = FC_NORMAL;
904         result->sign = sign;
905
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 */
908         if (my_isnan(l)) {
909                 result->clss = FC_NAN;
910                 TRACEPRINTF(("val_from_float resulted in NAN\n"));
911                 return result;
912         } else if (my_isinf(l)) {
913                 result->clss = FC_INF;
914                 TRACEPRINTF(("val_from_float resulted in %sINF\n", (result->sign == 1) ? "-" : ""));
915                 return result;
916         }
917
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));
923
924         /* build mantissa representation */
925         if (exponent != 0) {
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);
930         }
931         else {
932                 sc_val_from_ulong(0, NULL);
933         }
934
935         _save_result(_mant(result));
936
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));
942
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));
948
949         /* _normalize expects the radix point to be normal, so shift mantissa of subnormal
950          * origin one to the left */
951         if (exponent == 0) {
952                 sc_val_from_ulong(1, NULL);
953                 _shift_left(_mant(result), sc_get_buffer(), _mant(result));
954         }
955
956         normalize(result, result, 0);
957
958         TRACEPRINTF(("val_from_float results in %s\n", fc_print(result, temp, calc_buffer_size, FC_PACKED)));
959
960         return result;
961 }
962
963 long double fc_val_to_ieee754(const fp_value *val)
964 {
965         fp_value *value;
966         fp_value *temp = NULL;
967
968         unsigned byte_offset;
969
970         uint32_t sign;
971         uint32_t exponent;
972         uint32_t mantissa0;
973         uint32_t mantissa1;
974
975         value_t           buildval;
976         float_descriptor_t desc;
977         unsigned          mantissa_size;
978
979         size_t            long_double_size = sizeof(long double);
980
981         if (long_double_size == 8) {
982                 desc.exponent_size = 11;
983                 desc.mantissa_size = 52;
984                 desc.explicit_one  = 0;
985         } else {
986                 desc.exponent_size = 15;
987                 desc.mantissa_size = 63;
988                 desc.explicit_one  = 1;
989         }
990         mantissa_size = desc.mantissa_size + desc.explicit_one;
991
992         temp = (fp_value*) alloca(calc_buffer_size);
993         value = fc_cast(val, &desc, temp);
994
995         sign = value->sign;
996
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)) ;
1000
1001         sc_val_from_ulong(ROUNDING_BITS, NULL);
1002         _shift_right(_mant(value), sc_get_buffer(), _mant(value));
1003
1004         mantissa0 = 0;
1005         mantissa1 = 0;
1006
1007         for (byte_offset = 0; byte_offset < 4; byte_offset++)
1008                 mantissa1 |= sc_sub_bits(_mant(value), mantissa_size, byte_offset) << (byte_offset << 3);
1009
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);
1012
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;
1019         } else {
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;
1024         }
1025
1026         TRACEPRINTF(("val_to_float: %d-%x-%x%x\n", sign, exponent, mantissa0, mantissa1));
1027         return buildval.d;
1028 }
1029
1030 fp_value *fc_cast(const fp_value *value, const float_descriptor_t *desc,
1031                   fp_value *result)
1032 {
1033         char *temp;
1034         int exp_offset, val_bias, res_bias;
1035
1036         if (result == NULL) result = calc_buffer;
1037         temp = (char*) alloca(value_size);
1038
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);
1044                 return result;
1045         }
1046
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);
1050                 else
1051                         return fc_get_snan(desc, result);
1052         }
1053         else if (value->clss == FC_INF) {
1054                 if (value->sign == 0)
1055                         return fc_get_plusinf(desc, result);
1056                 else
1057                         return fc_get_minusinf(desc, result);
1058         }
1059
1060         /* set the descriptor of the new value */
1061         result->desc = *desc;
1062         result->clss = value->clss;
1063         result->sign = value->sign;
1064
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;
1070
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));
1074
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);
1081         } else {
1082                 memmove(_mant(result), _mant(value), value_size);
1083         }
1084
1085         normalize(result, result, 0);
1086         TRACEPRINTF(("Cast results in %s\n", fc_print(result, temp, value_size, FC_PACKED)));
1087         return result;
1088 }
1089
1090 fp_value *fc_get_max(const float_descriptor_t *desc, fp_value *result)
1091 {
1092         if (result == NULL) result = calc_buffer;
1093
1094         result->desc = *desc;
1095         result->clss = FC_NORMAL;
1096         result->sign = 0;
1097
1098         sc_val_from_ulong((1 << desc->exponent_size) - 2, _exp(result));
1099
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));
1103
1104         return result;
1105 }
1106
1107 fp_value *fc_get_min(const float_descriptor_t *desc, fp_value *result)
1108 {
1109         if (result == NULL) result = calc_buffer;
1110
1111         fc_get_max(desc, result);
1112         result->sign = 1;
1113
1114         return result;
1115 }
1116
1117 fp_value *fc_get_snan(const float_descriptor_t *desc, fp_value *result)
1118 {
1119         if (result == NULL) result = calc_buffer;
1120
1121         result->desc = *desc;
1122         result->clss = FC_NAN;
1123         result->sign = 0;
1124
1125         sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1126
1127         /* signaling NaN has non-zero mantissa with msb not set */
1128         sc_val_from_ulong(1, _mant(result));
1129
1130         return result;
1131 }
1132
1133 fp_value *fc_get_qnan(const float_descriptor_t *desc, fp_value *result)
1134 {
1135         if (result == NULL) result = calc_buffer;
1136
1137         result->desc = *desc;
1138         result->clss = FC_NAN;
1139         result->sign = 0;
1140
1141         sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1142
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));
1148
1149         return result;
1150 }
1151
1152 fp_value *fc_get_plusinf(const float_descriptor_t *desc, fp_value *result)
1153 {
1154         char *mant;
1155
1156         if (result == NULL) result = calc_buffer;
1157
1158         result->desc = *desc;
1159         result->clss = FC_INF;
1160         result->sign = 0;
1161
1162         sc_val_from_ulong((1 << desc->exponent_size) - 1, _exp(result));
1163
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);
1168         }
1169
1170         return result;
1171 }
1172
1173 fp_value *fc_get_minusinf(const float_descriptor_t *desc, fp_value *result)
1174 {
1175         if (result == NULL) result = calc_buffer;
1176
1177         fc_get_plusinf(desc, result);
1178         result->sign = 1;
1179
1180         return result;
1181 }
1182
1183 int fc_comp(const fp_value *val_a, const fp_value *val_b)
1184 {
1185         int mul = 1;
1186
1187         /*
1188          * shortcut: if both values are identical, they are either
1189          * Unordered if NaN or equal
1190          */
1191         if (val_a == val_b)
1192                 return val_a->clss == FC_NAN ? 2 : 0;
1193
1194         /* unordered if one is a NaN */
1195         if (val_a->clss == FC_NAN || val_b->clss == FC_NAN)
1196                 return 2;
1197
1198         /* zero is equal independent of sign */
1199         if ((val_a->clss == FC_ZERO) && (val_b->clss == FC_ZERO))
1200                 return 0;
1201
1202         /* different signs make compare easy */
1203         if (val_a->sign != val_b->sign)
1204                 return (val_a->sign == 0) ? (1) : (-1);
1205
1206         mul = val_a->sign ? -1 : 1;
1207
1208         /* both infinity means equality */
1209         if ((val_a->clss == FC_INF) && (val_b->clss == FC_INF))
1210                 return 0;
1211
1212         /* infinity is bigger than the rest */
1213         if (val_a->clss == FC_INF)
1214                 return  1 * mul;
1215         if (val_b->clss == FC_INF)
1216                 return -1 * mul;
1217
1218         /* check first exponent, that mantissa if equal */
1219         switch (sc_comp(_exp(val_a), _exp(val_b))) {
1220         case -1:
1221                 return -1 * mul;
1222         case  1:
1223                 return  1 * mul;
1224         case  0:
1225                 return sc_comp(_mant(val_a), _mant(val_b)) * mul;
1226         default:
1227                 return 2;
1228         }
1229 }
1230
1231 int fc_is_zero(const fp_value *a)
1232 {
1233         return a->clss == FC_ZERO;
1234 }
1235
1236 int fc_is_negative(const fp_value *a)
1237 {
1238         return a->sign;
1239 }
1240
1241 int fc_is_inf(const fp_value *a)
1242 {
1243         return a->clss == FC_INF;
1244 }
1245
1246 int fc_is_nan(const fp_value *a)
1247 {
1248         return a->clss == FC_NAN;
1249 }
1250
1251 int fc_is_subnormal(const fp_value *a)
1252 {
1253         return a->clss == FC_SUBNORMAL;
1254 }
1255
1256 char *fc_print(const fp_value *val, char *buf, int buflen, unsigned base)
1257 {
1258         char *mul_1;
1259         long double flt_val;
1260
1261         mul_1 = (char*) alloca(calc_buffer_size);
1262
1263         switch (base) {
1264         case FC_DEC:
1265                 switch ((value_class_t)val->clss) {
1266                 case FC_INF:
1267                         snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1268                         break;
1269                 case FC_NAN:
1270                         snprintf(buf, buflen, "NaN");
1271                         break;
1272                 case FC_ZERO:
1273                         snprintf(buf, buflen, "0.0");
1274                         break;
1275                 default:
1276                         flt_val = fc_val_to_ieee754(val);
1277                         /* XXX 30 is arbitrary */
1278                         snprintf(buf, buflen, "%.30LE", flt_val);
1279                 }
1280                 break;
1281
1282         case FC_HEX:
1283                 switch ((value_class_t)val->clss) {
1284                 case FC_INF:
1285                         snprintf(buf, buflen, "%cINF", val->sign ? '-' : '+');
1286                         break;
1287                 case FC_NAN:
1288                         snprintf(buf, buflen, "NaN");
1289                         break;
1290                 case FC_ZERO:
1291                         snprintf(buf, buflen, "0.0");
1292                         break;
1293                 default:
1294                         flt_val = fc_val_to_ieee754(val);
1295                         snprintf(buf, buflen, "%LA", flt_val);
1296                 }
1297                 break;
1298
1299         case FC_PACKED:
1300         default:
1301                 snprintf(buf, buflen, "%s", sc_print(pack(val, mul_1), value_size*4, SC_HEX, 0));
1302                 buf[buflen - 1] = '\0';
1303                 break;
1304         }
1305         return buf;
1306 }
1307
1308 unsigned char fc_sub_bits(const fp_value *value, unsigned num_bits, unsigned byte_ofs)
1309 {
1310         /* this is used to cache the packed version of the value */
1311         static char *packed_value = NULL;
1312
1313         if (packed_value == NULL) packed_value = XMALLOCN(char, value_size);
1314
1315         if (value != NULL)
1316                 pack(value, packed_value);
1317
1318         return sc_sub_bits(packed_value, num_bits, byte_ofs);
1319 }
1320
1321 /* Returns non-zero if the mantissa is zero, i.e. 1.0Exxx */
1322 int fc_zero_mantissa(const fp_value *value)
1323 {
1324         return sc_get_lowest_set_bit(_mant(value)) == ROUNDING_BITS + value->desc.mantissa_size;
1325 }
1326
1327 /* Returns the exponent of a value. */
1328 int fc_get_exponent(const fp_value *value)
1329 {
1330         int exp_bias = (1 << (value->desc.exponent_size - 1)) - 1;
1331         return sc_val_to_long(_exp(value)) - exp_bias;
1332 }
1333
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)
1336 {
1337         int v;
1338         int exp_bias;
1339
1340         /* handle some special cases first */
1341         switch (value->clss) {
1342         case FC_ZERO:
1343         case FC_INF:
1344         case FC_NAN:
1345                 return 1;
1346         default:
1347                 break;
1348         }
1349
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;
1357         }
1358         return 0;
1359 }
1360
1361
1362 fc_rounding_mode_t fc_set_rounding_mode(fc_rounding_mode_t mode)
1363 {
1364         if (mode == FC_TONEAREST || mode == FC_TOPOSITIVE || mode == FC_TONEGATIVE || mode == FC_TOZERO)
1365                 rounding_mode = mode;
1366
1367         return rounding_mode;
1368 }
1369
1370 fc_rounding_mode_t fc_get_rounding_mode(void)
1371 {
1372         return rounding_mode;
1373 }
1374
1375 void init_fltcalc(int precision)
1376 {
1377         if (calc_buffer == NULL) {
1378                 /* does nothing if already init */
1379                 if (precision == 0) precision = FC_DEFAULT_PRECISION;
1380
1381                 init_strcalc(precision + 2 + ROUNDING_BITS);
1382
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);
1388
1389                 rounding_mode    = FC_TONEAREST;
1390                 value_size       = sc_get_buffer_length();
1391                 calc_buffer_size = sizeof(fp_value) + 2*value_size - 1;
1392
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));
1396         }
1397 }
1398
1399 void finish_fltcalc (void)
1400 {
1401         free(calc_buffer); calc_buffer = NULL;
1402 }
1403
1404 #ifdef FLTCALC_TRACE_CALC
1405 static char buffer[100];
1406 #endif
1407
1408 /* definition of interface functions */
1409 fp_value *fc_add(const fp_value *a, const fp_value *b, fp_value *result)
1410 {
1411         if (result == NULL) result = calc_buffer;
1412
1413         TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1414         TRACEPRINTF(("+ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1415
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);
1419         else
1420                 _fadd(a, b, result);
1421
1422         TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1423         return result;
1424 }
1425
1426 fp_value *fc_sub(const fp_value *a, const fp_value *b, fp_value *result)
1427 {
1428         fp_value *temp;
1429
1430         if (result == NULL) result = calc_buffer;
1431
1432         TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1433         TRACEPRINTF(("- %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1434
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);
1440         else
1441                 _fadd(a, temp, result);
1442
1443         TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1444         return result;
1445 }
1446
1447 fp_value *fc_mul(const fp_value *a, const fp_value *b, fp_value *result)
1448 {
1449         if (result == NULL) result = calc_buffer;
1450
1451         TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1452         TRACEPRINTF(("* %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1453
1454         _fmul(a, b, result);
1455
1456         TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1457         return result;
1458 }
1459
1460 fp_value *fc_div(const fp_value *a, const fp_value *b, fp_value *result)
1461 {
1462         if (result == NULL) result = calc_buffer;
1463
1464         TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1465         TRACEPRINTF(("/ %s ", fc_print(b, buffer, sizeof(buffer), FC_PACKED)));
1466
1467         _fdiv(a, b, result);
1468
1469         TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1470         return result;
1471 }
1472
1473 fp_value *fc_neg(const fp_value *a, fp_value *result)
1474 {
1475         if (result == NULL) result = calc_buffer;
1476
1477         TRACEPRINTF(("- %s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1478
1479         if (a != result)
1480                 memcpy(result, a, calc_buffer_size);
1481         result->sign = !a->sign;
1482
1483         TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1484         return result;
1485 }
1486
1487 fp_value *fc_int(const fp_value *a, fp_value *result)
1488 {
1489         if (result == NULL) result = calc_buffer;
1490
1491         TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1492         TRACEPRINTF(("truncated to integer "));
1493
1494         _trunc(a, result);
1495
1496         TRACEPRINTF(("= %s\n", fc_print(result, buffer, sizeof(buffer), FC_PACKED)));
1497         return result;
1498 }
1499
1500 fp_value *fc_rnd(const fp_value *a, fp_value *result)
1501 {
1502         if (result == NULL) result = calc_buffer;
1503
1504         (void) a;
1505         TRACEPRINTF(("%s ", fc_print(a, buffer, sizeof(buffer), FC_PACKED)));
1506         TRACEPRINTF(("rounded to integer "));
1507
1508         panic("fc_rnd() not yet implemented");
1509 }
1510
1511 /*
1512  * convert a floating point value into an sc value ...
1513  */
1514 int fc_flt2int(const fp_value *a, void *result, ir_mode *dst_mode)
1515 {
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;
1519                 int shift, highest;
1520                 int mantissa_size;
1521                 int tgt_bits;
1522
1523                 if (a->sign && !mode_is_signed(dst_mode)) {
1524                         /* FIXME: for now we cannot convert this */
1525                         return 0;
1526                 }
1527
1528                 tgt_bits = get_mode_size_bits(dst_mode);
1529                 if (mode_is_signed(dst_mode))
1530                         --tgt_bits;
1531
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;
1535
1536                 if (tgt_bits < mantissa_size + 1)
1537                         tgt_bits = mantissa_size + 1;
1538                 if (shift > 0) {
1539                         sc_shlI(_mant(a),  shift, tgt_bits, 0, result);
1540                 } else {
1541                         sc_shrI(_mant(a), -shift, tgt_bits, 0, result);
1542                 }
1543
1544                 /* check for overflow */
1545                 highest = sc_get_highest_set_bit(result);
1546
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 */
1552                                         return 0;
1553                                 }
1554                         } else {
1555                                 if (highest >= (int) get_mode_size_bits(dst_mode) - 1) {
1556                                         /* FIXME: handle overflow */
1557                                         return 0;
1558                                 }
1559                         }
1560                 } else {
1561                         if (highest >= (int) get_mode_size_bits(dst_mode)) {
1562                                 /* FIXME: handle overflow */
1563                                 return 0;
1564                         }
1565                 }
1566
1567                 if (a->sign)
1568                         sc_neg(result, result);
1569
1570                 return 1;
1571         } else if (a->clss == FC_ZERO) {
1572                 sc_zero(result);
1573                 return 1;
1574         }
1575         return 0;
1576 }
1577
1578 int fc_is_exact(void)
1579 {
1580         return fc_exact;
1581 }