tv: Remove add_table[][][] and simply use +.
[libfirm] / ir / tv / strcalc.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    Provides basic mathematical operations on values represented as strings.
23  * @date     2003
24  * @author   Mathias Heil
25  */
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <limits.h>
33
34 #include "strcalc.h"
35 #include "xmalloc.h"
36 #include "error.h"
37
38 /*
39  * local definitions and macros
40  */
41 #define SC_BITS      4
42 #define SC_RESULT(x) ((x) & ((1U << SC_BITS) - 1U))
43 #define SC_CARRY(x)  ((unsigned)(x) >> SC_BITS)
44
45 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
46 #define SHIFT(count) (SC_1 << (count))
47 #define _val(a) ((a)-SC_0)
48 #define _digit(a) ((a)+SC_0)
49 #define _bitisset(digit, pos) (((digit) & SHIFT(pos)) != SC_0)
50
51 /* shortcut output for debugging */
52 #  define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
53 #  define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
54 #  define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
55 #  define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
56
57 #ifdef STRCALC_DEBUG_PRINTCOMP
58 #  define DEBUGPRINTF_COMPUTATION(x) printf x
59 #else
60 #  define DEBUGPRINTF_COMPUTATION(x) ((void)0)
61 #endif
62 #ifdef STRCALC_DEBUG
63 #  define DEBUGPRINTF(x) printf x
64 #else
65 #  define DEBUGPRINTF(x) ((void)0)
66 #endif
67
68
69 /*
70  * private variables
71  */
72 static char *calc_buffer = NULL;    /* buffer holding all results */
73 static char *output_buffer = NULL;  /* buffer for output */
74 static int bit_pattern_size;        /* maximum number of bits */
75 static int calc_buffer_size;        /* size of internally stored values */
76 static int max_value_size;          /* maximum size of values */
77
78 static int carry_flag;              /**< some computation set the carry_flag:
79                                          - right shift if bits were lost due to shifting
80                                          - division if there was a remainder
81                                          However, the meaning of carry is machine dependent
82                                          and often defined in other ways! */
83
84 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
85 static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
86 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
87 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
88
89 static char const mul_table[16][16][2] = {
90                        { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
91                          {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
92                          {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
93                          {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
94
95                        { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
96                          {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
97                          {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
98                          {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
99
100                        { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
101                          {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
102                          {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
103                          {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
104
105                        { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
106                          {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
107                          {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
108                          {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
109
110                        { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
111                          {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
112                          {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
113                          {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
114
115                        { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
116                          {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
117                          {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
118                          {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
119
120                        { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
121                          {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
122                          {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
123                          {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
124
125                        { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
126                          {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
127                          {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
128                          {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
129
130                        { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
131                          {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
132                          {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
133                          {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
134
135                        { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
136                          {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
137                          {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
138                          {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
139
140                        { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
141                          {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
142                          {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
143                          {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
144
145                        { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
146                          {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
147                          {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
148                          {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
149
150                        { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
151                          {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
152                          {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
153                          {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
154
155                        { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
156                          {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
157                          {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
158                          {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
159
160                        { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
161                          {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
162                          {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
163                          {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
164
165                        { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
166                          {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
167                          {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
168                          {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
169                              };
170
171 static char const shrs_table[16][4][2] = {
172                        { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
173                        { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
174                        { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
175                        { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
176                        { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
177                        { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
178                        { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
179                        { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
180                        { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
181                        { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
182                        { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
183                        { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
184                        { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
185                        { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
186                        { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
187                        { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
188                                    };
189
190 /** converting a digit to a binary string */
191 static char const *const binary_table[] = {
192         "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
193         "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
194 };
195
196 /*****************************************************************************
197  * private functions
198  *****************************************************************************/
199
200 /**
201  * implements the bitwise NOT operation
202  */
203 static void do_bitnot(const char *val, char *buffer)
204 {
205         int counter;
206
207         for (counter = 0; counter<calc_buffer_size; counter++)
208                 buffer[counter] = val[counter] ^ SC_F;
209 }
210
211 /**
212  * implements the bitwise OR operation
213  */
214 static void do_bitor(const char *val1, const char *val2, char *buffer)
215 {
216         int counter;
217
218         for (counter = 0; counter<calc_buffer_size; counter++)
219                 buffer[counter] = val1[counter] | val2[counter];
220 }
221
222 /**
223  * implements the bitwise eXclusive OR operation
224  */
225 static void do_bitxor(const char *val1, const char *val2, char *buffer)
226 {
227         int counter;
228
229         for (counter = 0; counter<calc_buffer_size; counter++)
230                 buffer[counter] = val1[counter] ^ val2[counter];
231 }
232
233 /**
234  * implements the bitwise AND operation
235  */
236 static void do_bitand(const char *val1, const char *val2, char *buffer)
237 {
238         int counter;
239
240         for (counter = 0; counter<calc_buffer_size; counter++)
241                 buffer[counter] = val1[counter] & val2[counter];
242 }
243
244 /**
245  * implements the bitwise AND not operation
246  */
247 static void do_bitandnot(const char *val1, const char *val2, char *buffer)
248 {
249         int counter;
250
251         for (counter = 0; counter < calc_buffer_size; ++counter)
252                 buffer[counter] = val1[counter] & (SC_F ^ val2[counter]);
253 }
254
255 /**
256  * returns the sign bit.
257  *
258  * @todo This implementation is wrong, as it returns the highest bit of the buffer
259  *       NOT the highest bit depending on the real mode
260  */
261 static int do_sign(const char *val)
262 {
263         return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
264 }
265
266 /**
267  * returns non-zero if bit at position pos is set
268  */
269 static int do_bit(const char *val, int pos)
270 {
271         int bit    = pos & 3;
272         int nibble = pos >> 2;
273
274         return _bitisset(val[nibble], bit);
275 }
276
277 /**
278  * Implements a fast ADD + 1
279  */
280 static void do_inc(const char *val, char *buffer)
281 {
282         int counter = 0;
283
284         while (counter++ < calc_buffer_size) {
285                 if (*val == SC_F) {
286                         *buffer++ = SC_0;
287                         val++;
288                 } else {
289                         /* No carry here, *val != SC_F */
290                         *buffer = *val + SC_1;
291                         return;
292                 }
293         }
294         /* here a carry could be lost, this is intended because this should
295          * happen only when a value changes sign. */
296 }
297
298 /**
299  * Implements a unary MINUS
300  */
301 static void do_negate(const char *val, char *buffer)
302 {
303         do_bitnot(val, buffer);
304         do_inc(buffer, buffer);
305 }
306
307 /**
308  * Implements a binary ADD
309  *
310  * @todo The implementation of carry is wrong, as it is the
311  *       calc_buffer_size carry, not the mode depending
312  */
313 static void do_add(const char *val1, const char *val2, char *buffer)
314 {
315         unsigned carry = SC_0;
316         for (int counter = 0; counter < calc_buffer_size; ++counter) {
317                 unsigned const sum = val1[counter] + val2[counter] + carry;
318                 buffer[counter] = SC_RESULT(sum);
319                 carry           = SC_CARRY(sum);
320         }
321         carry_flag = carry != SC_0;
322 }
323
324 /**
325  * Implements a binary SUB
326  */
327 static void do_sub(const char *val1, const char *val2, char *buffer)
328 {
329         char *temp_buffer = (char*) alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
330
331         do_negate(val2, temp_buffer);
332         do_add(val1, temp_buffer, buffer);
333 }
334
335 /**
336  * Implements a binary MUL
337  */
338 static void do_mul(const char *val1, const char *val2, char *buffer)
339 {
340         char *temp_buffer; /* result buffer */
341         char *neg_val1;    /* abs of val1 */
342         char *neg_val2;    /* abs of val2 */
343
344         char sign = 0;                      /* marks result sign */
345         int c_inner, c_outer;               /* loop counters */
346
347         temp_buffer = (char*) alloca(calc_buffer_size);
348         neg_val1 = (char*) alloca(calc_buffer_size);
349         neg_val2 = (char*) alloca(calc_buffer_size);
350
351         /* init result buffer to zeros */
352         memset(temp_buffer, SC_0, calc_buffer_size);
353
354         /* the multiplication works only for positive values, for negative values *
355          * it is necessary to negate them and adjust the result accordingly       */
356         if (do_sign(val1) == -1) {
357                 do_negate(val1, neg_val1);
358                 val1 = neg_val1;
359                 sign ^= 1;
360         }
361         if (do_sign(val2) == -1) {
362                 do_negate(val2, neg_val2);
363                 val2 = neg_val2;
364                 sign ^= 1;
365         }
366
367         for (c_outer = 0; c_outer < max_value_size; c_outer++) {
368                 if (val2[c_outer] != SC_0) {
369                         unsigned carry = SC_0; /* container for carries */
370                         for (c_inner = 0; c_inner < max_value_size; c_inner++) {
371                                 /* do the following calculation:                                    *
372                                  * Add the current carry, the value at position c_outer+c_inner     *
373                                  * and the result of the multiplication of val1[c_inner] and        *
374                                  * val2[c_outer]. This is the usual pen-and-paper multiplication.   */
375
376                                 /* multiplicate the two digits */
377                                 char const *const mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
378                                 /* add old value to result of multiplication and the carry */
379                                 unsigned const sum = temp_buffer[c_inner + c_outer] + (mul[1] << SC_BITS) + mul[0] + carry;
380
381                                 /* all carries together result in new carry. This is always smaller *
382                                  * than the base b:                                                 *
383                                  * Both multiplicands, the carry and the value already in the temp  *
384                                  * buffer are single digits and their value is therefore at most    *
385                                  * equal to (b-1).                                                  *
386                                  * This leads to:                                                   *
387                                  * (b-1)(b-1)+(b-1)+(b-1) = b*b-1                                   *
388                                  * The tables list all operations rem b, so the carry is at most    *
389                                  * (b*b-1)rem b = -1rem b = b-1                                     */
390                                 temp_buffer[c_inner + c_outer] = SC_RESULT(sum);
391                                 carry                          = SC_CARRY(sum);
392                         }
393
394                         /* A carry may hang over */
395                         /* c_outer is always smaller than max_value_size! */
396                         temp_buffer[max_value_size + c_outer] = carry;
397                 }
398         }
399
400         if (sign)
401                 do_negate(temp_buffer, buffer);
402         else
403                 memcpy(buffer, temp_buffer, calc_buffer_size);
404 }
405
406 /**
407  * Shift the buffer to left and add a 4 bit digit
408  */
409 static void do_push(const char digit, char *buffer)
410 {
411         int counter;
412
413         for (counter = calc_buffer_size - 2; counter >= 0; counter--) {
414                 buffer[counter+1] = buffer[counter];
415         }
416         buffer[0] = digit;
417 }
418
419 /**
420  * Implements truncating integer division and remainder.
421  *
422  * Note: This is MOST slow
423  */
424 static void do_divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
425 {
426         const char *dividend = rDividend;
427         const char *minus_divisor;
428         char *neg_val1;
429         char *neg_val2;
430
431         char div_sign = 0;     /* remember division result sign */
432         char rem_sign = 0;     /* remember remainder result sign */
433
434         int c_dividend;      /* loop counters */
435
436         neg_val1 = (char*) alloca(calc_buffer_size);
437         neg_val2 = (char*) alloca(calc_buffer_size);
438
439         /* clear result buffer */
440         memset(quot, SC_0, calc_buffer_size);
441         memset(rem, SC_0, calc_buffer_size);
442
443         /* if the divisor is zero this won't work (quot is zero) */
444         if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
445
446         /* if the dividend is zero result is zero (quot is zero) */
447         if (sc_comp(dividend, quot) == 0)
448                 return;
449
450         if (do_sign(dividend) == -1) {
451                 do_negate(dividend, neg_val1);
452                 div_sign ^= 1;
453                 rem_sign ^= 1;
454                 dividend = neg_val1;
455         }
456
457         do_negate(divisor, neg_val2);
458         if (do_sign(divisor) == -1) {
459                 div_sign ^= 1;
460                 minus_divisor = divisor;
461                 divisor = neg_val2;
462         } else
463                 minus_divisor = neg_val2;
464
465         /* if divisor >= dividend division is easy
466          * (remember these are absolute values) */
467         switch (sc_comp(dividend, divisor)) {
468         case 0: /* dividend == divisor */
469                 quot[0] = SC_1;
470                 goto end;
471
472         case -1: /* dividend < divisor */
473                 memcpy(rem, dividend, calc_buffer_size);
474                 goto end;
475
476         default: /* unluckily division is necessary :( */
477                 break;
478         }
479
480         for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--) {
481                 do_push(dividend[c_dividend], rem);
482                 do_push(SC_0, quot);
483
484                 if (sc_comp(rem, divisor) != -1) {  /* remainder >= divisor */
485                         /* subtract until the remainder becomes negative, this should
486                          * be faster than comparing remainder with divisor  */
487                         do_add(rem, minus_divisor, rem);
488
489                         while (do_sign(rem) == 1) {
490                                 quot[0] = SC_RESULT(quot[0] + SC_1); /* TODO can this generate carry or is masking redundant? */
491                                 do_add(rem, minus_divisor, rem);
492                         }
493
494                         /* subtracted one too much */
495                         do_add(rem, divisor, rem);
496                 }
497         }
498 end:
499         /* sets carry if remainder is non-zero ??? */
500         carry_flag = !sc_is_zero(rem);
501
502         if (div_sign)
503                 do_negate(quot, quot);
504
505         if (rem_sign)
506                 do_negate(rem, rem);
507 }
508
509 /**
510  * Implements a Shift Left, which can either preserve the sign bit
511  * or not.
512  *
513  * @todo Assertions seems to be wrong
514  */
515 static void do_shl(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed)
516 {
517         const char *shl;
518         char shift;
519         char carry = SC_0;
520
521         int counter;
522         int bitoffset = 0;
523
524         assert((shift_cnt >= 0) || (0 && "negative leftshift"));
525         assert(((do_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
526         assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
527         assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
528
529         /* if shifting far enough the result is zero */
530         if (shift_cnt >= bitsize) {
531                 memset(buffer, SC_0, calc_buffer_size);
532                 return;
533         }
534
535         shift     = SHIFT(shift_cnt % 4); /* this is 2 ** (offset % 4) */
536         shift_cnt = shift_cnt / 4;
537
538         /* shift the single digits some bytes (offset) and some bits (table)
539          * to the left */
540         for (counter = 0; counter < bitsize/4 - shift_cnt; counter++) {
541                 shl = mul_table[_val(val1[counter])][_val(shift)];
542                 buffer[counter + shift_cnt] = shl[0] | carry;
543                 carry = shl[1];
544         }
545         if (bitsize%4 > 0) {
546                 shl = mul_table[_val(val1[counter])][_val(shift)];
547                 buffer[counter + shift_cnt] = shl[0] | carry;
548                 bitoffset = counter;
549         } else {
550                 bitoffset = counter - 1;
551         }
552
553         /* fill with zeroes */
554         for (counter = 0; counter < shift_cnt; counter++)
555                 buffer[counter] = SC_0;
556
557         /* if the mode was signed, change sign when the mode's msb is now 1 */
558         shift_cnt = bitoffset + shift_cnt;
559         bitoffset = (bitsize-1) % 4;
560         if (is_signed && _bitisset(buffer[shift_cnt], bitoffset)) {
561                 /* this sets the upper bits of the leftmost digit */
562                 buffer[shift_cnt] |= min_digit[bitoffset];
563                 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
564                         buffer[counter] = SC_F;
565                 }
566         } else if (is_signed && !_bitisset(buffer[shift_cnt], bitoffset)) {
567                 /* this clears the upper bits of the leftmost digit */
568                 buffer[shift_cnt] &= max_digit[bitoffset];
569                 for (counter = shift_cnt+1; counter < calc_buffer_size; counter++) {
570                         buffer[counter] = SC_0;
571                 }
572         }
573 }
574
575 /**
576  * Implements a Shift Right, which can either preserve the sign bit
577  * or not.
578  *
579  * @param bitsize   bitsize of the value to be shifted
580  *
581  * @todo Assertions seems to be wrong
582  */
583 static void do_shr(const char *val1, char *buffer, long shift_cnt, int bitsize, unsigned is_signed, int signed_shift)
584 {
585         const char *shrs;
586         char sign;
587         char msd;
588
589         int shift_mod, shift_nib;
590
591         int counter;
592         int bitoffset = 0;
593
594         assert((shift_cnt >= 0) || (0 && "negative rightshift"));
595         assert(((!_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
596         assert(((_bitisset(val1[(bitsize-1)/4], (bitsize-1)%4)) || !is_signed || (do_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
597
598         sign = signed_shift && do_bit(val1, bitsize - 1) ? SC_F : SC_0;
599
600         /* if shifting far enough the result is either 0 or -1 */
601         if (shift_cnt >= bitsize) {
602                 if (!sc_is_zero(val1)) {
603                         carry_flag = 1;
604                 }
605                 memset(buffer, sign, calc_buffer_size);
606                 return;
607         }
608
609         shift_mod = shift_cnt &  3;
610         shift_nib = shift_cnt >> 2;
611
612         /* check if any bits are lost, and set carry_flag if so */
613         for (counter = 0; counter < shift_nib; ++counter) {
614                 if (val1[counter] != 0) {
615                         carry_flag = 1;
616                         break;
617                 }
618         }
619         if ((_val(val1[counter]) & ((1<<shift_mod)-1)) != 0)
620                 carry_flag = 1;
621
622         /* shift digits to the right with offset, carry and all */
623         buffer[0] = shrs_table[_val(val1[shift_nib])][shift_mod][0];
624         for (counter = 1; counter < ((bitsize + 3) >> 2) - shift_nib; counter++) {
625                 shrs = shrs_table[_val(val1[counter + shift_nib])][shift_mod];
626                 buffer[counter]      = shrs[0];
627                 buffer[counter - 1] |= shrs[1];
628         }
629
630         /* the last digit is special in regard of signed/unsigned shift */
631         bitoffset = bitsize & 3;
632         msd = sign;  /* most significant digit */
633
634         /* remove sign bits if mode was signed and this is an unsigned shift */
635         if (!signed_shift && is_signed) {
636                 msd &= max_digit[bitoffset];
637         }
638
639         shrs = shrs_table[_val(msd)][shift_mod];
640
641         /* signed shift and signed mode and negative value means all bits to the left are set */
642         if (signed_shift && sign == SC_F) {
643                 buffer[counter] = shrs[0] | min_digit[bitoffset];
644         } else {
645                 buffer[counter] = shrs[0];
646         }
647
648         if (counter > 0)
649                 buffer[counter - 1] |= shrs[1];
650
651         /* fill with SC_F or SC_0 depending on sign */
652         for (counter++; counter < calc_buffer_size; counter++) {
653                 buffer[counter] = sign;
654         }
655 }
656
657 /**
658  * Implements a Rotate Left.
659  * positive: low-order -> high order, negative other direction
660  */
661 static void do_rotl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
662 {
663         char *temp1, *temp2;
664         temp1 = (char*) alloca(calc_buffer_size);
665         temp2 = (char*) alloca(calc_buffer_size);
666
667         offset = offset % radius;
668
669         /* rotation by multiples of the type length is identity */
670         if (offset == 0) {
671                 memmove(buffer, val1, calc_buffer_size);
672                 return;
673         }
674
675         do_shl(val1, temp1, offset, radius, is_signed);
676         do_shr(val1, temp2, radius - offset, radius, is_signed, 0);
677         do_bitor(temp1, temp2, buffer);
678         carry_flag = 0; /* set by shr, but due to rot this is false */
679 }
680
681 /*****************************************************************************
682  * public functions, declared in strcalc.h
683  *****************************************************************************/
684 const void *sc_get_buffer(void)
685 {
686         return (void*)calc_buffer;
687 }
688
689 int sc_get_buffer_length(void)
690 {
691         return calc_buffer_size;
692 }
693
694 /**
695  * Do sign extension if the mode is signed, otherwise to zero extension.
696  */
697 void sign_extend(void *buffer, ir_mode *mode)
698 {
699         char *calc_buffer = (char*) buffer;
700         int bits          = get_mode_size_bits(mode) - 1;
701         int nibble        = bits >> 2;
702         int max           = max_digit[bits & 3];
703         int i;
704
705         if (mode_is_signed(mode)) {
706                 if (calc_buffer[nibble] > max) {
707                         /* sign bit is set, we need sign expansion */
708
709                         for (i = nibble + 1; i < calc_buffer_size; ++i)
710                                 calc_buffer[i] = SC_F;
711                         calc_buffer[nibble] |= sex_digit[bits & 3];
712                 } else {
713                         /* set all bits to zero */
714                         for (i = nibble + 1; i < calc_buffer_size; ++i)
715                                 calc_buffer[i] = SC_0;
716                         calc_buffer[nibble] &= zex_digit[bits & 3];
717                 }
718         } else {
719                 /* do zero extension */
720                 for (i = nibble + 1; i < calc_buffer_size; ++i)
721                         calc_buffer[i] = SC_0;
722                 calc_buffer[nibble] &= zex_digit[bits & 3];
723         }
724 }
725
726 /* we assume that '0'-'9', 'a'-'z' and 'A'-'Z' are a range.
727  * The C-standard does theoretically allow otherwise. */
728 static inline void check_ascii(void)
729 {
730         /* C standard guarantees that '0'-'9' is a range */
731         assert('b'-'a' == 1
732                 && 'c'-'a' == 2
733                 && 'd'-'a' == 3
734                 && 'e'-'a' == 4
735                 && 'f'-'a' == 5);
736         assert('B'-'A' == 1
737                 && 'C'-'A' == 2
738                 && 'D'-'A' == 3
739                 && 'E'-'A' == 4
740                 && 'F'-'A' == 5);
741 }
742
743 int sc_val_from_str(char sign, unsigned base, const char *str,
744                     size_t len, void *buffer)
745 {
746         char *sc_base, *val;
747
748         assert(sign == -1 || sign == 1);
749         assert(str != NULL);
750         assert(len > 0);
751         check_ascii();
752
753         assert(base > 1 && base <= 16);
754         sc_base = (char*) alloca(calc_buffer_size);
755         sc_val_from_ulong(base, sc_base);
756
757         val = (char*) alloca(calc_buffer_size);
758         if (buffer == NULL)
759                 buffer = calc_buffer;
760
761         CLEAR_BUFFER(buffer);
762         CLEAR_BUFFER(val);
763
764         /* BEGIN string evaluation, from left to right */
765         while (len > 0) {
766                 char c = *str;
767                 unsigned v;
768                 if (c >= '0' && c <= '9')
769                         v = c - '0';
770                 else if (c >= 'A' && c <= 'F')
771                         v = c - 'A' + 10;
772                 else if (c >= 'a' && c <= 'f')
773                         v = c - 'a' + 10;
774                 else
775                         return 0;
776
777                 if (v >= base)
778                         return 0;
779                 val[0] = v;
780
781                 /* Radix conversion from base b to base B:
782                  *  (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
783                 /* multiply current value with base */
784                 do_mul(sc_base, (const char*) buffer, (char*) buffer);
785                 /* add next digit to current value  */
786                 do_add(val, (const char*) buffer, (char*) buffer);
787
788                 /* get ready for the next letter */
789                 str++;
790                 len--;
791         }
792
793         if (sign < 0)
794                 do_negate((const char*) buffer, (char*) buffer);
795
796         return 1;
797 }
798
799 void sc_val_from_long(long value, void *buffer)
800 {
801         char *pos;
802         char sign, is_minlong;
803
804         if (buffer == NULL) buffer = calc_buffer;
805         pos = (char*) buffer;
806
807         sign = (value < 0);
808         is_minlong = value == LONG_MIN;
809
810         /* use absolute value, special treatment of MIN_LONG to avoid overflow */
811         if (sign) {
812                 if (is_minlong)
813                         value = -(value+1);
814                 else
815                         value = -value;
816         }
817
818         CLEAR_BUFFER(buffer);
819
820         while ((value != 0) && (pos < (char*)buffer + calc_buffer_size)) {
821                 *pos++ = _digit(value & 0xf);
822                 value >>= 4;
823         }
824
825         if (sign) {
826                 if (is_minlong)
827                         do_inc((const char*) buffer, (char*) buffer);
828
829                 do_negate((const char*) buffer, (char*) buffer);
830         }
831 }
832
833 void sc_val_from_ulong(unsigned long value, void *buffer)
834 {
835         unsigned char *pos;
836
837         if (buffer == NULL) buffer = calc_buffer;
838         pos = (unsigned char*) buffer;
839
840         while (pos < (unsigned char *)buffer + calc_buffer_size) {
841                 *pos++ = (unsigned char)_digit(value & 0xf);
842                 value >>= 4;
843         }
844 }
845
846 long sc_val_to_long(const void *val)
847 {
848         int i;
849         long l = 0;
850
851         for (i = calc_buffer_size - 1; i >= 0; i--) {
852                 l = (l << 4) + _val(((char *)val)[i]);
853         }
854         return l;
855 }
856
857 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
858 {
859         char *pos;
860         int i, bits;
861
862         if (buffer == NULL) buffer = calc_buffer;
863         CLEAR_BUFFER(buffer);
864
865         if (!sign) return;  /* unsigned means minimum is 0(zero) */
866
867         pos = (char*) buffer;
868
869         bits = num_bits - 1;
870         for (i = 0; i < bits/4; i++)
871                 *pos++ = SC_0;
872
873         *pos++ = min_digit[bits%4];
874
875         for (i++; i <= calc_buffer_size - 1; i++)
876                 *pos++ = SC_F;
877 }
878
879 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
880 {
881         char* pos;
882         int i, bits;
883
884         if (buffer == NULL) buffer = calc_buffer;
885         CLEAR_BUFFER(buffer);
886         pos = (char*) buffer;
887
888         bits = num_bits - sign;
889         for (i = 0; i < bits/4; i++)
890                 *pos++ = SC_F;
891
892         *pos++ = max_digit[bits%4];
893
894         for (i++; i <= calc_buffer_size - 1; i++)
895                 *pos++ = SC_0;
896 }
897
898 void sc_truncate(unsigned int num_bits, void *buffer)
899 {
900         char *cbuffer = (char*) buffer;
901         char *pos = cbuffer + (num_bits / 4);
902         char *end = cbuffer + calc_buffer_size;
903
904         assert(pos < end);
905
906         switch (num_bits % 4) {
907         case 0: /* nothing to do */ break;
908         case 1: *pos++ &= SC_1; break;
909         case 2: *pos++ &= SC_3; break;
910         case 3: *pos++ &= SC_7; break;
911         }
912
913         for ( ; pos < end; ++pos)
914                 *pos = SC_0;
915 }
916
917 int sc_comp(const void* value1, const void* value2)
918 {
919         int counter = calc_buffer_size - 1;
920         const char *val1 = (const char *)value1;
921         const char *val2 = (const char *)value2;
922
923         /* compare signs first:
924          * the loop below can only compare values of the same sign! */
925         if (do_sign(val1) != do_sign(val2))
926                 return (do_sign(val1) == 1)?(1):(-1);
927
928         /* loop until two digits differ, the values are equal if there
929          * are no such two digits */
930         while (val1[counter] == val2[counter]) {
931                 counter--;
932                 if (counter < 0) return 0;
933         }
934
935         /* the leftmost digit is the most significant, so this returns
936          * the correct result.
937          * This implies the digit enum is ordered */
938         return (val1[counter] > val2[counter]) ? (1) : (-1);
939 }
940
941 int sc_get_highest_set_bit(const void *value)
942 {
943         const char *val = (const char*)value;
944         int high, counter;
945
946         high = calc_buffer_size * 4 - 1;
947
948         for (counter = calc_buffer_size-1; counter >= 0; counter--) {
949                 if (val[counter] == SC_0)
950                         high -= 4;
951                 else {
952                         if (val[counter] > SC_7) return high;
953                         else if (val[counter] > SC_3) return high - 1;
954                         else if (val[counter] > SC_1) return high - 2;
955                         else return high - 3;
956                 }
957         }
958         return high;
959 }
960
961 int sc_get_lowest_set_bit(const void *value)
962 {
963         const char *val = (const char*)value;
964         int low, counter;
965
966         low = 0;
967         for (counter = 0; counter < calc_buffer_size; counter++) {
968                 switch (val[counter]) {
969                 case SC_1:
970                 case SC_3:
971                 case SC_5:
972                 case SC_7:
973                 case SC_9:
974                 case SC_B:
975                 case SC_D:
976                 case SC_F:
977                         return low;
978                 case SC_2:
979                 case SC_6:
980                 case SC_A:
981                 case SC_E:
982                         return low + 1;
983                 case SC_4:
984                 case SC_C:
985                         return low + 2;
986                 case SC_8:
987                         return low + 3;
988                 default:
989                         low += 4;
990                 }
991         }
992         return -1;
993 }
994
995 int sc_get_bit_at(const void *value, unsigned pos)
996 {
997         const char *val = (const char*) value;
998         unsigned nibble = pos >> 2;
999
1000         return (val[nibble] & SHIFT(pos & 3)) != SC_0;
1001 }
1002
1003 void sc_set_bit_at(void *value, unsigned pos)
1004 {
1005         char *val = (char*) value;
1006         unsigned nibble = pos >> 2;
1007
1008         val[nibble] |= SHIFT(pos & 3);
1009 }
1010
1011 int sc_is_zero(const void *value)
1012 {
1013         const char* val = (const char *)value;
1014         int counter;
1015
1016         for (counter = 0; counter < calc_buffer_size; ++counter) {
1017                 if (val[counter] != SC_0)
1018                         return 0;
1019         }
1020         return 1;
1021 }
1022
1023 int sc_is_negative(const void *value)
1024 {
1025         return do_sign((const char*) value) == -1;
1026 }
1027
1028 int sc_had_carry(void)
1029 {
1030         return carry_flag;
1031 }
1032
1033 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1034 {
1035         const char *val = (const char *)value;
1036         int nibble_ofs  = 2 * byte_ofs;
1037         unsigned char res;
1038
1039         /* the current scheme uses one byte to store a nibble */
1040         if (4 * nibble_ofs >= len)
1041                 return 0;
1042
1043         res = _val(val[nibble_ofs]);
1044         if (len > 4 * (nibble_ofs + 1))
1045                 res |= _val(val[nibble_ofs + 1]) << 4;
1046
1047         /* kick bits outsize */
1048         if (len - 8 * byte_ofs < 8) {
1049                 res &= (1 << (len - 8 * byte_ofs)) - 1;
1050         }
1051         return res;
1052 }
1053
1054 /*
1055  * convert to a string
1056  * FIXME: Doesn't check buffer bounds
1057  */
1058 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
1059 {
1060         static const char big_digits[]   = "0123456789ABCDEF";
1061         static const char small_digits[] = "0123456789abcdef";
1062
1063         char *base_val, *div1_res, *div2_res, *rem_res;
1064         int counter, nibbles, i, sign, mask;
1065         char x;
1066
1067         const char *val = (const char *)value;
1068         const char *p;
1069         char *m, *n, *t;
1070         char *pos;
1071         const char *digits = small_digits;
1072
1073         base_val = (char*) alloca(calc_buffer_size);
1074         div1_res = (char*) alloca(calc_buffer_size);
1075         div2_res = (char*) alloca(calc_buffer_size);
1076         rem_res  = (char*) alloca(calc_buffer_size);
1077
1078         pos = output_buffer + bit_pattern_size;
1079         *(--pos) = '\0';
1080
1081         /* special case */
1082         if (bits == 0) {
1083                 bits = bit_pattern_size;
1084 #ifdef STRCALC_DEBUG_FULLPRINT
1085                 bits <<= 1;
1086 #endif
1087         }
1088         nibbles = bits >> 2;
1089         switch (base) {
1090
1091         case SC_HEX:
1092                 digits = big_digits;
1093         case SC_hex:
1094                 for (counter = 0; counter < nibbles; ++counter) {
1095                         *(--pos) = digits[_val(val[counter])];
1096 #ifdef STRCALC_DEBUG_GROUPPRINT
1097                         if ((counter+1)%8 == 0)
1098                                 *(--pos) = ' ';
1099 #endif
1100                 }
1101
1102                 /* last nibble must be masked */
1103                 if (bits & 3) {
1104                         mask = zex_digit[(bits & 3) - 1];
1105                         x    = val[counter++] & mask;
1106                         *(--pos) = digits[_val(x)];
1107                 }
1108
1109                 /* now kill zeros */
1110                 for (; counter > 1; --counter, ++pos) {
1111 #ifdef STRCALC_DEBUG_GROUPPRINT
1112                         if (pos[0] == ' ') ++pos;
1113 #endif
1114                         if (pos[0] != '0')
1115                                 break;
1116                 }
1117                 break;
1118
1119         case SC_BIN:
1120                 for (counter = 0; counter < nibbles; ++counter) {
1121                         pos -= 4;
1122                         p = binary_table[_val(val[counter])];
1123                         pos[0] = p[0];
1124                         pos[1] = p[1];
1125                         pos[2] = p[2];
1126                         pos[3] = p[3];
1127                 }
1128
1129                 /* last nibble must be masked */
1130                 if (bits & 3) {
1131                         mask = zex_digit[(bits & 3) - 1];
1132                         x    = val[counter++] & mask;
1133
1134                         pos -= 4;
1135                         p = binary_table[_val(x)];
1136                         pos[0] = p[0];
1137                         pos[1] = p[1];
1138                         pos[2] = p[2];
1139                         pos[3] = p[3];
1140                 }
1141
1142                 /* now kill zeros */
1143                 for (counter <<= 2; counter > 1; --counter, ++pos)
1144                         if (pos[0] != '0')
1145                                 break;
1146                         break;
1147
1148         case SC_DEC:
1149         case SC_OCT:
1150                 memset(base_val, SC_0, calc_buffer_size);
1151                 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1152
1153                 p    = val;
1154                 sign = 0;
1155                 if (signed_mode && base == SC_DEC) {
1156                         /* check for negative values */
1157                         if (do_bit(val, bits - 1)) {
1158                                 do_negate(val, div2_res);
1159                                 sign = 1;
1160                                 p = div2_res;
1161                         }
1162                 }
1163
1164                 /* transfer data into oscillating buffers */
1165                 memset(div1_res, SC_0, calc_buffer_size);
1166                 for (counter = 0; counter < nibbles; ++counter)
1167                         div1_res[counter] = p[counter];
1168
1169                 /* last nibble must be masked */
1170                 if (bits & 3) {
1171                         mask = zex_digit[(bits & 3) - 1];
1172                         div1_res[counter] = p[counter] & mask;
1173                         ++counter;
1174                 }
1175
1176                 m = div1_res;
1177                 n = div2_res;
1178                 for (;;) {
1179                         do_divmod(m, base_val, n, rem_res);
1180                         t = m;
1181                         m = n;
1182                         n = t;
1183                         *(--pos) = digits[_val(rem_res[0])];
1184
1185                         x = 0;
1186                         for (i = 0; i < calc_buffer_size; ++i)
1187                                 x |= _val(m[i]);
1188
1189                         if (x == 0)
1190                                 break;
1191                 }
1192                 if (sign)
1193                         *(--pos) = '-';
1194                 break;
1195
1196         default:
1197                 panic("Unsupported base %d", base);
1198         }
1199         return pos;
1200 }
1201
1202 void init_strcalc(int precision)
1203 {
1204         if (calc_buffer == NULL) {
1205                 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1206
1207                 /* round up to multiple of 4 */
1208                 precision = (precision + 3) & ~3;
1209
1210                 bit_pattern_size = (precision);
1211                 calc_buffer_size = (precision / 2);
1212                 max_value_size   = (precision / 4);
1213
1214                 calc_buffer   = XMALLOCN(char, calc_buffer_size + 1);
1215                 output_buffer = XMALLOCN(char, bit_pattern_size + 1);
1216
1217                 DEBUGPRINTF(("init strcalc: \n\tPRECISION: %d\n\tCALC_BUFFER_SIZE = %d\n\tMAX_VALUE_SIZE = %d\n\tbuffer pointer: %p\n", precision, calc_buffer_size, max_value_size, calc_buffer));
1218         }
1219 }
1220
1221
1222 void finish_strcalc(void)
1223 {
1224         free(calc_buffer);   calc_buffer   = NULL;
1225         free(output_buffer); output_buffer = NULL;
1226 }
1227
1228 int sc_get_precision(void)
1229 {
1230         return bit_pattern_size;
1231 }
1232
1233
1234 void sc_add(const void *value1, const void *value2, void *buffer)
1235 {
1236         CLEAR_BUFFER(calc_buffer);
1237         carry_flag = 0;
1238
1239         DEBUGPRINTF_COMPUTATION(("%s + ", sc_print_hex(value1)));
1240         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1241
1242         do_add((const char*) value1, (const char*) value2, (char*) calc_buffer);
1243
1244         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1245
1246         if ((buffer != NULL) && (buffer != calc_buffer)) {
1247                 memcpy(buffer, calc_buffer, calc_buffer_size);
1248         }
1249 }
1250
1251 void sc_sub(const void *value1, const void *value2, void *buffer)
1252 {
1253         CLEAR_BUFFER(calc_buffer);
1254         carry_flag = 0;
1255
1256         DEBUGPRINTF_COMPUTATION(("%s - ", sc_print_hex(value1)));
1257         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1258
1259         do_sub((const char*) value1, (const char*) value2, calc_buffer);
1260
1261         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1262
1263         if ((buffer != NULL) && (buffer != calc_buffer)) {
1264                 memcpy(buffer, calc_buffer, calc_buffer_size);
1265         }
1266 }
1267
1268 void sc_neg(const void *value1, void *buffer)
1269 {
1270         carry_flag = 0;
1271
1272         DEBUGPRINTF_COMPUTATION(("- %s ->", sc_print_hex(value1)));
1273
1274         do_negate((const char*) value1, calc_buffer);
1275
1276         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1277
1278         if ((buffer != NULL) && (buffer != calc_buffer)) {
1279                 memcpy(buffer, calc_buffer, calc_buffer_size);
1280         }
1281 }
1282
1283 void sc_and(const void *value1, const void *value2, void *buffer)
1284 {
1285         CLEAR_BUFFER(calc_buffer);
1286         carry_flag = 0;
1287
1288         DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1289         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1290
1291         do_bitand((const char*) value1, (const char*) value2, calc_buffer);
1292
1293         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1294
1295         if ((buffer != NULL) && (buffer != calc_buffer)) {
1296                 memcpy(buffer, calc_buffer, calc_buffer_size);
1297         }
1298 }
1299
1300 void sc_andnot(const void *value1, const void *value2, void *buffer)
1301 {
1302         CLEAR_BUFFER(calc_buffer);
1303         carry_flag = 0;
1304
1305         DEBUGPRINTF_COMPUTATION(("%s & ", sc_print_hex(value1)));
1306         DEBUGPRINTF_COMPUTATION(("~%s -> ", sc_print_hex(value2)));
1307
1308         do_bitandnot((const char*) value1, (const char*) value2, calc_buffer);
1309
1310         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1311
1312         if (buffer != NULL && buffer != calc_buffer) {
1313                 memcpy(buffer, calc_buffer, calc_buffer_size);
1314         }
1315 }
1316
1317 void sc_or(const void *value1, const void *value2, void *buffer)
1318 {
1319         CLEAR_BUFFER(calc_buffer);
1320         carry_flag = 0;
1321
1322         DEBUGPRINTF_COMPUTATION(("%s | ", sc_print_hex(value1)));
1323         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1324
1325         do_bitor((const char*) value1, (const char*) value2, calc_buffer);
1326
1327         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1328
1329         if ((buffer != NULL) && (buffer != calc_buffer)) {
1330                 memcpy(buffer, calc_buffer, calc_buffer_size);
1331         }
1332 }
1333
1334 void sc_xor(const void *value1, const void *value2, void *buffer)
1335 {
1336         CLEAR_BUFFER(calc_buffer);
1337         carry_flag = 0;
1338
1339         DEBUGPRINTF_COMPUTATION(("%s ^ ", sc_print_hex(value1)));
1340         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1341
1342         do_bitxor((const char*) value1, (const char*) value2, calc_buffer);
1343
1344         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1345
1346         if ((buffer != NULL) && (buffer != calc_buffer)) {
1347                 memcpy(buffer, calc_buffer, calc_buffer_size);
1348         }
1349 }
1350
1351 void sc_not(const void *value1, void *buffer)
1352 {
1353         CLEAR_BUFFER(calc_buffer);
1354         carry_flag = 0;
1355
1356         DEBUGPRINTF_COMPUTATION(("~ %s ->", sc_print_hex(value1)));
1357
1358         do_bitnot((const char*) value1, calc_buffer);
1359
1360         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1361
1362         if ((buffer != NULL) && (buffer != calc_buffer)) {
1363                 memcpy(buffer, calc_buffer, calc_buffer_size);
1364         }
1365 }
1366
1367 void sc_mul(const void *value1, const void *value2, void *buffer)
1368 {
1369         CLEAR_BUFFER(calc_buffer);
1370         carry_flag = 0;
1371
1372         DEBUGPRINTF_COMPUTATION(("%s * ", sc_print_hex(value1)));
1373         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1374
1375         do_mul((const char*) value1, (const char*) value2, calc_buffer);
1376
1377         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1378
1379         if ((buffer != NULL) && (buffer != calc_buffer)) {
1380                 memcpy(buffer, calc_buffer, calc_buffer_size);
1381         }
1382 }
1383
1384 void sc_div(const void *value1, const void *value2, void *buffer)
1385 {
1386         /* temp buffer holding unused result of divmod */
1387         char *unused_res = (char*) alloca(calc_buffer_size);
1388
1389         CLEAR_BUFFER(calc_buffer);
1390         carry_flag = 0;
1391
1392         DEBUGPRINTF_COMPUTATION(("%s / ", sc_print_hex(value1)));
1393         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1394
1395         do_divmod((const char*) value1, (const char*) value2, calc_buffer, unused_res);
1396
1397         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1398
1399         if ((buffer != NULL) && (buffer != calc_buffer)) {
1400                 memcpy(buffer, calc_buffer, calc_buffer_size);
1401         }
1402 }
1403
1404 void sc_mod(const void *value1, const void *value2, void *buffer)
1405 {
1406         /* temp buffer holding unused result of divmod */
1407         char *unused_res = (char*) alloca(calc_buffer_size);
1408
1409         CLEAR_BUFFER(calc_buffer);
1410         carry_flag = 0;
1411
1412         DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1413         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1414
1415         do_divmod((const char*) value1, (const char*) value2, unused_res, calc_buffer);
1416
1417         DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1418
1419         if ((buffer != NULL) && (buffer != calc_buffer)) {
1420                 memcpy(buffer, calc_buffer, calc_buffer_size);
1421         }
1422 }
1423
1424 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer)
1425 {
1426         CLEAR_BUFFER(calc_buffer);
1427         carry_flag = 0;
1428
1429         DEBUGPRINTF_COMPUTATION(("%s %% ", sc_print_hex(value1)));
1430         DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1431
1432         do_divmod((const char*) value1, (const char*) value2, (char*) div_buffer, (char*) mod_buffer);
1433
1434         DEBUGPRINTF_COMPUTATION(("%s:%s\n", sc_print_hex(div_buffer), sc_print_hex(mod_buffer)));
1435 }
1436
1437
1438 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1439 {
1440         carry_flag = 0;
1441
1442         DEBUGPRINTF_COMPUTATION(("%s << %ld ", sc_print_hex(value1), shift_cnt));
1443         do_shl((const char*) val1, calc_buffer, shift_cnt, bitsize, sign);
1444
1445         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1446
1447         if ((buffer != NULL) && (buffer != calc_buffer)) {
1448                 memmove(buffer, calc_buffer, calc_buffer_size);
1449         }
1450 }
1451
1452 void sc_shl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1453 {
1454         long offset = sc_val_to_long(val2);
1455
1456         sc_shlI(val1, offset, bitsize, sign, buffer);
1457 }
1458
1459 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1460 {
1461         carry_flag = 0;
1462
1463         DEBUGPRINTF_COMPUTATION(("%s >>u %ld ", sc_print_hex(value1), shift_cnt));
1464         do_shr((const char*) val1, calc_buffer, shift_cnt, bitsize, sign, 0);
1465
1466         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1467
1468         if ((buffer != NULL) && (buffer != calc_buffer)) {
1469                 memmove(buffer, calc_buffer, calc_buffer_size);
1470         }
1471 }
1472
1473 void sc_shr(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1474 {
1475         long shift_cnt = sc_val_to_long(val2);
1476
1477         sc_shrI(val1, shift_cnt, bitsize, sign, buffer);
1478 }
1479
1480 void sc_shrsI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer)
1481 {
1482         carry_flag = 0;
1483
1484         DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), shift_cnt));
1485         do_shr((const char*) val1, calc_buffer, shift_cnt, bitsize, sign, 1);
1486
1487         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1488
1489         if ((buffer != NULL) && (buffer != calc_buffer)) {
1490                 memmove(buffer, calc_buffer, calc_buffer_size);
1491         }
1492 }
1493
1494 void sc_shrs(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1495 {
1496         long offset = sc_val_to_long(val2);
1497
1498         carry_flag = 0;
1499
1500         DEBUGPRINTF_COMPUTATION(("%s >>s %ld ", sc_print_hex(value1), offset));
1501         do_shr((const char*) val1, calc_buffer, offset, bitsize, sign, 1);
1502
1503         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1504
1505         if ((buffer != NULL) && (buffer != calc_buffer)) {
1506                 memmove(buffer, calc_buffer, calc_buffer_size);
1507         }
1508 }
1509
1510 void sc_rotl(const void *val1, const void *val2, int bitsize, int sign, void *buffer)
1511 {
1512         long offset = sc_val_to_long(val2);
1513
1514         carry_flag = 0;
1515
1516         DEBUGPRINTF_COMPUTATION(("%s <<>> %ld ", sc_print_hex(value1), offset));
1517         do_rotl((const char*) val1, calc_buffer, offset, bitsize, sign);
1518
1519         DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1520
1521         if ((buffer != NULL) && (buffer != calc_buffer)) {
1522                 memmove(buffer, calc_buffer, calc_buffer_size);
1523         }
1524 }
1525
1526 void sc_zero(void *buffer)
1527 {
1528         if (buffer == NULL)
1529                 buffer = calc_buffer;
1530         CLEAR_BUFFER(buffer);
1531         carry_flag = 0;
1532 }