2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * File name: ir/tv/strcalc.c
24 * Author: Mathias Heil
28 * Copyright: (c) 2003 Universität Karlsruhe
40 # include <string.h> /* memset/memcmp */
42 #include <assert.h> /* assertions */
43 #include <stdio.h> /* output for error messages */
44 #include <limits.h> /* definition of LONG_MIN, used in sc_get_val_from_long */
50 * local definitions and macros
52 #define CLEAR_BUFFER(b) assert(b); memset(b, SC_0, calc_buffer_size)
53 #define _val(a) ((a)-SC_0)
54 #define _digit(a) ((a)+SC_0)
55 #define _bitisset(digit, pos) (and_table[_val(digit)][_val(shift_table[pos])] != SC_0)
57 #define fail_char(a, b, c, d) _fail_char((a), (b), (c), (d), __FILE__, __LINE__)
59 /* shortcut output for debugging */
60 # define sc_print_hex(a) sc_print((a), 0, SC_HEX, 0)
61 # define sc_print_dec(a) sc_print((a), 0, SC_DEC, 1)
62 # define sc_print_oct(a) sc_print((a), 0, SC_OCT, 0)
63 # define sc_print_bin(a) sc_print((a), 0, SC_BIN, 0)
65 #ifdef STRCALC_DEBUG_PRINTCOMP
66 # define DEBUGPRINTF_COMPUTATION(x) printf x
68 # define DEBUGPRINTF_COMPUTATION(x) ((void)0)
71 # define DEBUGPRINTF(x) printf x
73 # define DEBUGPRINTF(x) ((void)0)
80 static char *calc_buffer = NULL; /* buffer holding all results */
81 static char *output_buffer = NULL; /* buffer for output */
82 static int bit_pattern_size; /* maximum number of bits */
83 static int calc_buffer_size; /* size of internally stored values */
84 static int max_value_size; /* maximum size of values */
86 static int carry_flag; /**< some computation set the carry_flag:
87 - right shift if bits were lost due to shifting
88 - division if there was a remainder
89 However, the meaning of carry is machine dependent
90 and often defined in other ways! */
92 static const char sex_digit[4] = { SC_E, SC_C, SC_8, SC_0 };
93 static const char zex_digit[4] = { SC_1, SC_3, SC_7, SC_F };
94 static const char max_digit[4] = { SC_0, SC_1, SC_3, SC_7 };
95 static const char min_digit[4] = { SC_F, SC_E, SC_C, SC_8 };
97 static const char not_table[16] = { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
98 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 };
100 static const char shift_table[4] = { SC_1, SC_2, SC_4, SC_8 };
102 static const char and_table[16][16] = {
103 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
104 SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0 },
106 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
107 SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1 },
109 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
110 SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2 },
112 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
113 SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3 },
115 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
116 SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4 },
118 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
119 SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5 },
121 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
122 SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6 },
124 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
125 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
127 { SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0, SC_0,
128 SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8, SC_8 },
130 { SC_0, SC_1, SC_0, SC_1, SC_0, SC_1, SC_0, SC_1,
131 SC_8, SC_9, SC_8, SC_9, SC_8, SC_9, SC_8, SC_9 },
133 { SC_0, SC_0, SC_2, SC_2, SC_0, SC_0, SC_2, SC_2,
134 SC_8, SC_8, SC_A, SC_A, SC_8, SC_8, SC_A, SC_A },
136 { SC_0, SC_1, SC_2, SC_3, SC_0, SC_1, SC_2, SC_3,
137 SC_8, SC_9, SC_A, SC_B, SC_8, SC_9, SC_A, SC_B },
139 { SC_0, SC_0, SC_0, SC_0, SC_4, SC_4, SC_4, SC_4,
140 SC_8, SC_8, SC_8, SC_8, SC_C, SC_C, SC_C, SC_C },
142 { SC_0, SC_1, SC_0, SC_1, SC_4, SC_5, SC_4, SC_5,
143 SC_8, SC_9, SC_8, SC_9, SC_C, SC_D, SC_C, SC_D },
145 { SC_0, SC_0, SC_2, SC_2, SC_4, SC_4, SC_6, SC_6,
146 SC_8, SC_8, SC_A, SC_A, SC_C, SC_C, SC_E, SC_E },
148 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
149 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F } };
151 static const char or_table[16][16] = {
152 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
153 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
155 { SC_1, SC_1, SC_3, SC_3, SC_5, SC_5, SC_7, SC_7,
156 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
158 { SC_2, SC_3, SC_2, SC_3, SC_6, SC_7, SC_6, SC_7,
159 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
161 { SC_3, SC_3, SC_3, SC_3, SC_7, SC_7, SC_7, SC_7,
162 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
164 { SC_4, SC_5, SC_6, SC_7, SC_4, SC_5, SC_6, SC_7,
165 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
167 { SC_5, SC_5, SC_7, SC_7, SC_5, SC_5, SC_7, SC_7,
168 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
170 { SC_6, SC_7, SC_6, SC_7, SC_6, SC_7, SC_6, SC_7,
171 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
173 { SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7, SC_7,
174 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F },
176 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
177 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
179 { SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F,
180 SC_9, SC_9, SC_B, SC_B, SC_D, SC_D, SC_F, SC_F },
182 { SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F,
183 SC_A, SC_B, SC_A, SC_B, SC_E, SC_F, SC_E, SC_F },
185 { SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F,
186 SC_B, SC_B, SC_B, SC_B, SC_F, SC_F, SC_F, SC_F },
188 { SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F,
189 SC_C, SC_D, SC_E, SC_F, SC_C, SC_D, SC_E, SC_F },
191 { SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F,
192 SC_D, SC_D, SC_F, SC_F, SC_D, SC_D, SC_F, SC_F },
194 { SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F,
195 SC_E, SC_F, SC_E, SC_F, SC_E, SC_F, SC_E, SC_F },
197 { SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F,
198 SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F, SC_F } };
200 static char const xor_table[16][16] = {
201 { SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7,
202 SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F },
204 { SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6,
205 SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E },
207 { SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5,
208 SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D },
210 { SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4,
211 SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C },
213 { SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3,
214 SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B },
216 { SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2,
217 SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A },
219 { SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1,
220 SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9 },
222 { SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0,
223 SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8 },
225 { SC_8, SC_9, SC_A, SC_B, SC_C, SC_D, SC_E, SC_F,
226 SC_0, SC_1, SC_2, SC_3, SC_4, SC_5, SC_6, SC_7 },
228 { SC_9, SC_8, SC_B, SC_A, SC_D, SC_C, SC_F, SC_E,
229 SC_1, SC_0, SC_3, SC_2, SC_5, SC_4, SC_7, SC_6 },
231 { SC_A, SC_B, SC_8, SC_9, SC_E, SC_F, SC_C, SC_D,
232 SC_2, SC_3, SC_0, SC_1, SC_6, SC_7, SC_4, SC_5 },
234 { SC_B, SC_A, SC_9, SC_8, SC_F, SC_E, SC_D, SC_C,
235 SC_3, SC_2, SC_1, SC_0, SC_7, SC_6, SC_5, SC_4 },
237 { SC_C, SC_D, SC_E, SC_F, SC_8, SC_9, SC_A, SC_B,
238 SC_4, SC_5, SC_6, SC_7, SC_0, SC_1, SC_2, SC_3 },
240 { SC_D, SC_C, SC_F, SC_E, SC_9, SC_8, SC_B, SC_A,
241 SC_5, SC_4, SC_7, SC_6, SC_1, SC_0, SC_3, SC_2 },
243 { SC_E, SC_F, SC_C, SC_D, SC_A, SC_B, SC_8, SC_9,
244 SC_6, SC_7, SC_4, SC_5, SC_2, SC_3, SC_0, SC_1 },
246 { SC_F, SC_E, SC_D, SC_C, SC_B, SC_A, SC_9, SC_8,
247 SC_7, SC_6, SC_5, SC_4, SC_3, SC_2, SC_1, SC_0 }
250 static char const add_table[16][16][2] = {
251 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
252 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
253 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
254 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
256 { {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0},
257 {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
258 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
259 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1} },
261 { {SC_2, SC_0}, {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0},
262 {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
263 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
264 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1} },
266 { {SC_3, SC_0}, {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0},
267 {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
268 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
269 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1} },
271 { {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
272 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
273 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
274 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1} },
276 { {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0},
277 {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
278 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
279 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1} },
281 { {SC_6, SC_0}, {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0},
282 {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
283 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
284 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1} },
286 { {SC_7, SC_0}, {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0},
287 {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
288 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
289 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1} },
291 { {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
292 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
293 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
294 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1} },
296 { {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0},
297 {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
298 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
299 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1} },
301 { {SC_A, SC_0}, {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0},
302 {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
303 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
304 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1} },
306 { {SC_B, SC_0}, {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0},
307 {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
308 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
309 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1} },
311 { {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0},
312 {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1},
313 {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1},
314 {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1} },
316 { {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1},
317 {SC_1, SC_1}, {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1},
318 {SC_5, SC_1}, {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1},
319 {SC_9, SC_1}, {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1} },
321 { {SC_E, SC_0}, {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1},
322 {SC_2, SC_1}, {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1},
323 {SC_6, SC_1}, {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1},
324 {SC_A, SC_1}, {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1} },
326 { {SC_F, SC_0}, {SC_0, SC_1}, {SC_1, SC_1}, {SC_2, SC_1},
327 {SC_3, SC_1}, {SC_4, SC_1}, {SC_5, SC_1}, {SC_6, SC_1},
328 {SC_7, SC_1}, {SC_8, SC_1}, {SC_9, SC_1}, {SC_A, SC_1},
329 {SC_B, SC_1}, {SC_C, SC_1}, {SC_D, SC_1}, {SC_E, SC_1} }
332 static char const mul_table[16][16][2] = {
333 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
334 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
335 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0},
336 {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
338 { {SC_0, SC_0}, {SC_1, SC_0}, {SC_2, SC_0}, {SC_3, SC_0},
339 {SC_4, SC_0}, {SC_5, SC_0}, {SC_6, SC_0}, {SC_7, SC_0},
340 {SC_8, SC_0}, {SC_9, SC_0}, {SC_A, SC_0}, {SC_B, SC_0},
341 {SC_C, SC_0}, {SC_D, SC_0}, {SC_E, SC_0}, {SC_F, SC_0} },
343 { {SC_0, SC_0}, {SC_2, SC_0}, {SC_4, SC_0}, {SC_6, SC_0},
344 {SC_8, SC_0}, {SC_A, SC_0}, {SC_C, SC_0}, {SC_E, SC_0},
345 {SC_0, SC_1}, {SC_2, SC_1}, {SC_4, SC_1}, {SC_6, SC_1},
346 {SC_8, SC_1}, {SC_A, SC_1}, {SC_C, SC_1}, {SC_E, SC_1} },
348 { {SC_0, SC_0}, {SC_3, SC_0}, {SC_6, SC_0}, {SC_9, SC_0},
349 {SC_C, SC_0}, {SC_F, SC_0}, {SC_2, SC_1}, {SC_5, SC_1},
350 {SC_8, SC_1}, {SC_B, SC_1}, {SC_E, SC_1}, {SC_1, SC_2},
351 {SC_4, SC_2}, {SC_7, SC_2}, {SC_A, SC_2}, {SC_D, SC_2} },
353 { {SC_0, SC_0}, {SC_4, SC_0}, {SC_8, SC_0}, {SC_C, SC_0},
354 {SC_0, SC_1}, {SC_4, SC_1}, {SC_8, SC_1}, {SC_C, SC_1},
355 {SC_0, SC_2}, {SC_4, SC_2}, {SC_8, SC_2}, {SC_C, SC_2},
356 {SC_0, SC_3}, {SC_4, SC_3}, {SC_8, SC_3}, {SC_C, SC_3} },
358 { {SC_0, SC_0}, {SC_5, SC_0}, {SC_A, SC_0}, {SC_F, SC_0},
359 {SC_4, SC_1}, {SC_9, SC_1}, {SC_E, SC_1}, {SC_3, SC_2},
360 {SC_8, SC_2}, {SC_D, SC_2}, {SC_2, SC_3}, {SC_7, SC_3},
361 {SC_C, SC_3}, {SC_1, SC_4}, {SC_6, SC_4}, {SC_B, SC_4} },
363 { {SC_0, SC_0}, {SC_6, SC_0}, {SC_C, SC_0}, {SC_2, SC_1},
364 {SC_8, SC_1}, {SC_E, SC_1}, {SC_4, SC_2}, {SC_A, SC_2},
365 {SC_0, SC_3}, {SC_6, SC_3}, {SC_C, SC_3}, {SC_2, SC_4},
366 {SC_8, SC_4}, {SC_E, SC_4}, {SC_4, SC_5}, {SC_A, SC_5} },
368 { {SC_0, SC_0}, {SC_7, SC_0}, {SC_E, SC_0}, {SC_5, SC_1},
369 {SC_C, SC_1}, {SC_3, SC_2}, {SC_A, SC_2}, {SC_1, SC_3},
370 {SC_8, SC_3}, {SC_F, SC_3}, {SC_6, SC_4}, {SC_D, SC_4},
371 {SC_4, SC_5}, {SC_B, SC_5}, {SC_2, SC_6}, {SC_9, SC_6} },
373 { {SC_0, SC_0}, {SC_8, SC_0}, {SC_0, SC_1}, {SC_8, SC_1},
374 {SC_0, SC_2}, {SC_8, SC_2}, {SC_0, SC_3}, {SC_8, SC_3},
375 {SC_0, SC_4}, {SC_8, SC_4}, {SC_0, SC_5}, {SC_8, SC_5},
376 {SC_0, SC_6}, {SC_8, SC_6}, {SC_0, SC_7}, {SC_8, SC_7} },
378 { {SC_0, SC_0}, {SC_9, SC_0}, {SC_2, SC_1}, {SC_B, SC_1},
379 {SC_4, SC_2}, {SC_D, SC_2}, {SC_6, SC_3}, {SC_F, SC_3},
380 {SC_8, SC_4}, {SC_1, SC_5}, {SC_A, SC_5}, {SC_3, SC_6},
381 {SC_C, SC_6}, {SC_5, SC_7}, {SC_E, SC_7}, {SC_7, SC_8} },
383 { {SC_0, SC_0}, {SC_A, SC_0}, {SC_4, SC_1}, {SC_E, SC_1},
384 {SC_8, SC_2}, {SC_2, SC_3}, {SC_C, SC_3}, {SC_6, SC_4},
385 {SC_0, SC_5}, {SC_A, SC_5}, {SC_4, SC_6}, {SC_E, SC_6},
386 {SC_8, SC_7}, {SC_2, SC_8}, {SC_C, SC_8}, {SC_6, SC_9} },
388 { {SC_0, SC_0}, {SC_B, SC_0}, {SC_6, SC_1}, {SC_1, SC_2},
389 {SC_C, SC_2}, {SC_7, SC_3}, {SC_2, SC_4}, {SC_D, SC_4},
390 {SC_8, SC_5}, {SC_3, SC_6}, {SC_E, SC_6}, {SC_9, SC_7},
391 {SC_4, SC_8}, {SC_F, SC_8}, {SC_A, SC_9}, {SC_5, SC_A} },
393 { {SC_0, SC_0}, {SC_C, SC_0}, {SC_8, SC_1}, {SC_4, SC_2},
394 {SC_0, SC_3}, {SC_C, SC_3}, {SC_8, SC_4}, {SC_4, SC_5},
395 {SC_0, SC_6}, {SC_C, SC_6}, {SC_8, SC_7}, {SC_4, SC_8},
396 {SC_0, SC_9}, {SC_C, SC_9}, {SC_8, SC_A}, {SC_4, SC_B} },
398 { {SC_0, SC_0}, {SC_D, SC_0}, {SC_A, SC_1}, {SC_7, SC_2},
399 {SC_4, SC_3}, {SC_1, SC_4}, {SC_E, SC_4}, {SC_B, SC_5},
400 {SC_8, SC_6}, {SC_5, SC_7}, {SC_2, SC_8}, {SC_F, SC_8},
401 {SC_C, SC_9}, {SC_9, SC_A}, {SC_6, SC_B}, {SC_3, SC_C} },
403 { {SC_0, SC_0}, {SC_E, SC_0}, {SC_C, SC_1}, {SC_A, SC_2},
404 {SC_8, SC_3}, {SC_6, SC_4}, {SC_4, SC_5}, {SC_2, SC_6},
405 {SC_0, SC_7}, {SC_E, SC_7}, {SC_C, SC_8}, {SC_A, SC_9},
406 {SC_8, SC_A}, {SC_6, SC_B}, {SC_4, SC_C}, {SC_2, SC_D} },
408 { {SC_0, SC_0}, {SC_F, SC_0}, {SC_E, SC_1}, {SC_D, SC_2},
409 {SC_C, SC_3}, {SC_B, SC_4}, {SC_A, SC_5}, {SC_9, SC_6},
410 {SC_8, SC_7}, {SC_7, SC_8}, {SC_6, SC_9}, {SC_5, SC_A},
411 {SC_4, SC_B}, {SC_3, SC_C}, {SC_2, SC_D}, {SC_1, SC_E} }
414 static char const shrs_table[16][4][2] = {
415 { {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0}, {SC_0, SC_0} },
416 { {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4}, {SC_0, SC_2} },
417 { {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8}, {SC_0, SC_4} },
418 { {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C}, {SC_0, SC_6} },
419 { {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0}, {SC_0, SC_8} },
420 { {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4}, {SC_0, SC_A} },
421 { {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8}, {SC_0, SC_C} },
422 { {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C}, {SC_0, SC_E} },
423 { {SC_8, SC_0}, {SC_4, SC_0}, {SC_2, SC_0}, {SC_1, SC_0} },
424 { {SC_9, SC_0}, {SC_4, SC_8}, {SC_2, SC_4}, {SC_1, SC_2} },
425 { {SC_A, SC_0}, {SC_5, SC_0}, {SC_2, SC_8}, {SC_1, SC_4} },
426 { {SC_B, SC_0}, {SC_5, SC_8}, {SC_2, SC_C}, {SC_1, SC_6} },
427 { {SC_C, SC_0}, {SC_6, SC_0}, {SC_3, SC_0}, {SC_1, SC_8} },
428 { {SC_D, SC_0}, {SC_6, SC_8}, {SC_3, SC_4}, {SC_1, SC_A} },
429 { {SC_E, SC_0}, {SC_7, SC_0}, {SC_3, SC_8}, {SC_1, SC_C} },
430 { {SC_F, SC_0}, {SC_7, SC_8}, {SC_3, SC_C}, {SC_1, SC_E} }
433 /** converting a digit to a binary string */
434 static const char *binary_table[16] = {
435 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
436 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
439 /*****************************************************************************
441 *****************************************************************************/
442 static void _fail_char(const char *str, size_t len, const char fchar, int pos,
443 const char *file, int line)
446 printf("Unexpected character '%c' in %s:%d\n", fchar, file, line);
447 while (len-- && *str) printf("%c", *str++); printf("\n");
448 while (--pos) printf(" "); printf("^\n");
453 * implements the bitwise NOT operation
455 static void _bitnot(const char *val, char *buffer)
459 for (counter = 0; counter<calc_buffer_size; counter++)
460 buffer[counter] = not_table[_val(val[counter])];
464 * implements the bitwise OR operation
466 static void _bitor(const char *val1, const char *val2, char *buffer)
470 for (counter = 0; counter<calc_buffer_size; counter++)
471 buffer[counter] = or_table[_val(val1[counter])][_val(val2[counter])];
475 * implements the bitwise eXclusive OR operation
477 static void _bitxor(const char *val1, const char *val2, char *buffer)
481 for (counter = 0; counter<calc_buffer_size; counter++)
482 buffer[counter] = xor_table[_val(val1[counter])][_val(val2[counter])];
486 * implements the bitwise AND operation
488 static void _bitand(const char *val1, const char *val2, char *buffer)
492 for (counter = 0; counter<calc_buffer_size; counter++)
493 buffer[counter] = and_table[_val(val1[counter])][_val(val2[counter])];
497 * returns the sign bit.
499 * @todo This implementation is wrong, as it returns the highest bit of the buffer
500 * NOT the highest bit depending on the real mode
502 static int _sign(const char *val)
504 return (val[calc_buffer_size-1] <= SC_7) ? (1) : (-1);
508 * returns non-zero if bit at position pos is set
510 static int _bit(const char *val, int pos)
513 int nibble = pos >> 2;
515 return _bitisset(val[nibble], bit);
519 * Implements a fast ADD + 1
521 static void _inc(const char *val, char *buffer)
525 while (counter++ < calc_buffer_size)
534 /* No carry here, *val != SC_F */
535 *buffer = add_table[_val(*val)][SC_1][0];
539 /* here a carry could be lost, this is intended because this should
540 * happen only when a value changes sign. */
544 * Implements a unary MINUS
546 static void _negate(const char *val, char *buffer)
548 _bitnot(val, buffer);
549 _inc(buffer, buffer);
553 * Implements a binary ADD
555 * @todo The implementation of carry is wrong, as it is the
556 * calc_buffer_size carry, not the mode depending
558 static void _add(const char *val1, const char *val2, char *buffer)
561 const char *add1, *add2;
564 for (counter = 0; counter < calc_buffer_size; counter++)
566 add1 = add_table[_val(val1[counter])][_val(val2[counter])];
567 add2 = add_table[_val(add1[0])][_val(carry)];
568 /* carry might be zero */
569 buffer[counter] = add2[0];
570 carry = add_table[_val(add1[1])][_val(add2[1])][0];
572 carry_flag = carry != SC_0;
576 * Implements a binary SUB
578 static void _sub(const char *val1, const char *val2, char *buffer)
580 char *temp_buffer = alloca(calc_buffer_size); /* intermediate buffer to hold -val2 */
582 _negate(val2, temp_buffer);
583 _add(val1, temp_buffer, buffer);
587 * Implements a binary MUL
589 static void _mul(const char *val1, const char *val2, char *buffer)
591 char *temp_buffer; /* result buffer */
592 char *neg_val1; /* abs of val1 */
593 char *neg_val2; /* abs of val2 */
595 const char *mul, *add1, *add2; /* intermediate result containers */
596 char carry = SC_0; /* container for carries */
597 char sign = 0; /* marks result sign */
598 int c_inner, c_outer; /* loop counters */
600 temp_buffer = alloca(calc_buffer_size);
601 neg_val1 = alloca(calc_buffer_size);
602 neg_val2 = alloca(calc_buffer_size);
604 /* init result buffer to zeroes */
605 memset(temp_buffer, SC_0, calc_buffer_size);
607 /* the multiplication works only for positive values, for negative values *
608 * it is necessary to negate them and adjust the result accordingly */
609 if (_sign(val1) == -1) {
610 _negate(val1, neg_val1);
614 if (_sign(val2) == -1) {
615 _negate(val2, neg_val2);
620 for (c_outer = 0; c_outer < max_value_size; c_outer++)
622 if (val2[c_outer] != SC_0)
624 for (c_inner = 0; c_inner < max_value_size; c_inner++)
626 /* do the following calculation: *
627 * Add the current carry, the value at position c_outer+c_inner *
628 * and the result of the multiplication of val1[c_inner] and *
629 * val2[c_outer]. This is the usual pen-and-paper multiplication. */
631 /* multiplicate the two digits */
632 mul = mul_table[_val(val1[c_inner])][_val(val2[c_outer])];
633 /* add old value to result of multiplication */
634 add1 = add_table[_val(temp_buffer[c_inner + c_outer])][_val(mul[0])];
635 /* add carry to the sum */
636 add2 = add_table[_val(add1[0])][_val(carry)];
638 /* all carries together result in new carry. This is always smaller *
640 * Both multiplicands, the carry and the value already in the temp *
641 * buffer are single digits and their value is therefore at most *
644 * (b-1)(b-1)+(b-1)+(b-1) = b*b-1 *
645 * The tables list all operations rem b, so the carry is at most *
646 * (b*b-1)rem b = -1rem b = b-1 */
647 carry = add_table[_val(mul[1])][_val(add1[1])][0];
648 carry = add_table[_val(carry)][_val(add2[1])][0];
650 temp_buffer[c_inner + c_outer] = add2[0];
653 /* A carry may hang over */
654 /* c_outer is always smaller than max_value_size! */
655 temp_buffer[max_value_size + c_outer] = carry;
661 _negate(temp_buffer, buffer);
663 memcpy(buffer, temp_buffer, calc_buffer_size);
667 * Shift the buffer to left and add a 4 bit digit
669 static void _push(const char digit, char *buffer)
673 for (counter = calc_buffer_size - 2; counter >= 0; counter--)
675 buffer[counter+1] = buffer[counter];
681 * Implements truncating integer division and remainder.
683 * Note: This is MOST slow
685 static void _divmod(const char *rDividend, const char *divisor, char *quot, char *rem)
687 const char *dividend = rDividend;
688 const char *minus_divisor;
692 char div_sign = 0; /* remember division result sign */
693 char rem_sign = 0; /* remember remainder esult sign */
695 int c_dividend; /* loop counters */
697 neg_val1 = alloca(calc_buffer_size);
698 neg_val2 = alloca(calc_buffer_size);
700 /* clear result buffer */
701 memset(quot, SC_0, calc_buffer_size);
702 memset(rem, SC_0, calc_buffer_size);
704 /* if the divisor is zero this won't work (quot is zero) */
705 if (sc_comp(divisor, quot) == 0) assert(0 && "division by zero!");
707 /* if the dividend is zero result is zero (quot is zero)*/
708 if (sc_comp(dividend, quot) == 0)
711 if (_sign(dividend) == -1) {
712 _negate(dividend, neg_val1);
718 _negate(divisor, neg_val2);
719 if (_sign(divisor) == -1) {
721 minus_divisor = divisor;
725 minus_divisor = neg_val2;
727 /* if divisor >= dividend division is easy
728 * (remember these are absolute values) */
729 switch (sc_comp(dividend, divisor))
731 case 0: /* dividend == divisor */
735 case -1: /* dividend < divisor */
736 memcpy(rem, rDividend, calc_buffer_size);
739 default: /* unluckily division is necessary :( */
743 for (c_dividend = calc_buffer_size - 1; c_dividend >= 0; c_dividend--)
745 _push(dividend[c_dividend], rem);
748 if (sc_comp(rem, divisor) != -1) /* remainder >= divisor */
750 /* subtract until the remainder becomes negative, this should
751 * be faster than comparing remainder with divisor */
752 _add(rem, minus_divisor, rem);
754 while (_sign(rem) == 1)
756 quot[0] = add_table[_val(quot[0])][SC_1][0];
757 _add(rem, minus_divisor, rem);
760 /* subtracted one too much */
761 _add(rem, divisor, rem);
765 /* sets carry if remainder is non-zero ??? */
766 carry_flag = !sc_is_zero(rem);
776 * Implements a Shift Left, which can either preserve the sign bit
779 * @todo Assertions seems to be wrong
781 static void _shl(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
790 assert((offset >= 0) || (0 && "negative leftshift"));
791 assert(((_sign(val1) != -1) || is_signed) || (0 && "unsigned mode and negative value"));
792 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
793 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
795 /* if shifting far enough the result is zero */
796 if (offset >= radius)
798 memset(buffer, SC_0, calc_buffer_size);
802 shift = shift_table[_val(offset%4)]; /* this is 2 ** (offset % 4) */
805 /* shift the single digits some bytes (offset) and some bits (table)
807 for (counter = 0; counter < radius/4 - offset; counter++)
809 shl = mul_table[_val(val1[counter])][_val(shift)];
810 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
815 shl = mul_table[_val(val1[counter])][_val(shift)];
816 buffer[counter + offset] = or_table[_val(shl[0])][_val(carry)];
819 bitoffset = counter - 1;
822 /* fill with zeroes */
823 for (counter = 0; counter < offset; counter++) buffer[counter] = SC_0;
825 /* if the mode was signed, change sign when the mode's msb is now 1 */
826 offset = bitoffset + offset;
827 bitoffset = (radius-1) % 4;
828 if (is_signed && _bitisset(buffer[offset], bitoffset))
830 /* this sets the upper bits of the leftmost digit */
831 buffer[offset] = or_table[_val(buffer[offset])][_val(min_digit[bitoffset])];
832 for (counter = offset+1; counter < calc_buffer_size; counter++)
834 buffer[counter] = SC_F;
837 else if (is_signed && !_bitisset(buffer[offset], bitoffset))
839 /* this clears the upper bits of the leftmost digit */
840 buffer[offset] = and_table[_val(buffer[offset])][_val(max_digit[bitoffset])];
841 for (counter = offset+1; counter < calc_buffer_size; counter++)
843 buffer[counter] = SC_0;
849 * Implements a Shift Right, which can either preserve the sign bit
852 * @todo Assertions seems to be wrong
854 static void _shr(const char *val1, char *buffer, long offset, int radius, unsigned is_signed, int signed_shift)
865 assert((offset >= 0) || (0 && "negative rightshift"));
866 assert(((!_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == -1)) || (0 && "value is positive, should be negative"));
867 assert(((_bitisset(val1[(radius-1)/4], (radius-1)%4)) || !is_signed || (_sign(val1) == 1)) || (0 && "value is negative, should be positive"));
869 sign = ((signed_shift) && (_sign(val1) == -1))?(SC_F):(SC_0);
871 /* if shifting far enough the result is either 0 or -1 */
872 if (offset >= radius)
874 if (!sc_is_zero(val1)) {
877 memset(buffer, sign, calc_buffer_size);
884 /* check if any bits are lost, and set carry_flag if so */
885 for (counter = 0; counter < offset; counter++)
887 if (val1[counter] != 0)
893 if ((_val(val1[counter]) & ((1<<shift)-1)) != 0)
897 /* shift digits to the right with offset, carry and all */
899 if (radius/4 - offset > 0) {
900 buffer[counter] = shrs_table[_val(val1[offset])][shift][0];
903 for (; counter < radius/4 - offset; counter++)
905 shrs = shrs_table[_val(val1[counter + offset])][shift];
906 buffer[counter] = shrs[0];
907 buffer[counter-1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
910 /* the last digit is special in regard of signed/unsigned shift */
911 bitoffset = radius%4;
912 msd = (radius/4<calc_buffer_size)?(val1[radius/4]):(sign); /* most significant digit */
914 /* remove sign bits if mode was signed and this is an unsigned shift */
915 if (!signed_shift && is_signed) {
916 msd = and_table[_val(msd)][_val(max_digit[bitoffset])];
919 shrs = shrs_table[_val(msd)][shift];
921 /* signed shift and signed mode and negative value means all bits to the left are set */
922 if (signed_shift && is_signed && (_sign(val1) == -1)) {
923 buffer[counter] = or_table[_val(shrs[0])][_val(min_digit[bitoffset])];
925 buffer[counter] = shrs[0];
928 if (counter > 0) buffer[counter - 1] = or_table[_val(buffer[counter-1])][_val(shrs[1])];
930 /* fill with SC_F or SC_0 depending on sign */
931 for (counter++; counter < calc_buffer_size; counter++)
933 buffer[counter] = sign;
938 * Implements a Rotate Right.
939 * positive: low-order -> high order, negative other direction
941 static void _rot(const char *val1, char *buffer, long offset, int radius, unsigned is_signed)
944 temp1 = alloca(calc_buffer_size);
945 temp2 = alloca(calc_buffer_size);
947 offset = offset % radius;
949 /* rotation by multiples of the type length is identity */
951 memmove(buffer, val1, calc_buffer_size);
955 _shl(val1, temp1, offset, radius, is_signed);
956 _shr(val1, temp2, radius - offset, radius, is_signed, 0);
957 _bitor(temp1, temp2, buffer);
958 carry_flag = 0; /* set by shr, but due to rot this is false */
961 /*****************************************************************************
962 * public functions, declared in strcalc.h
963 *****************************************************************************/
964 const void *sc_get_buffer(void)
966 return (void*)calc_buffer;
969 int sc_get_buffer_length(void)
971 return calc_buffer_size;
975 * Do sign extension if the mode is signed, otherwise to zero extension.
977 void sign_extend(char *calc_buffer, ir_mode *mode) {
978 int bits = get_mode_size_bits(mode) - 1;
979 int nibble = bits >> 2;
980 int max = max_digit[bits & 3];
983 if (mode_is_signed(mode)) {
984 if (calc_buffer[nibble] > max) {
985 /* sign bit is set, we need sign expansion */
987 for (i = nibble + 1; i < calc_buffer_size; ++i)
988 calc_buffer[i] = SC_F;
989 calc_buffer[nibble] = or_table[(int)calc_buffer[nibble]][(int)sex_digit[bits & 3]];
991 /* set all bits to zero */
992 for (i = nibble + 1; i < calc_buffer_size; ++i)
993 calc_buffer[i] = SC_0;
994 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
997 /* do zero extension */
998 for (i = nibble + 1; i < calc_buffer_size; ++i)
999 calc_buffer[i] = SC_0;
1000 calc_buffer[nibble] = and_table[(int)calc_buffer[nibble]][(int)zex_digit[bits & 3]];
1004 /* FIXME doesn't check for overflows */
1005 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode)
1007 const char *orig_str = str;
1008 unsigned int orig_len = len;
1013 base = alloca(calc_buffer_size);
1014 val = alloca(calc_buffer_size);
1016 /* verify valid pointers (not null) */
1018 /* a string no characters long is an error */
1021 if (buffer == NULL) buffer = calc_buffer;
1023 CLEAR_BUFFER(buffer);
1027 /* strip leading spaces */
1028 while ((len > 0) && (*str == ' ')) { len--; str++; }
1030 /* if the first two characters are 0x or 0X -> hex
1031 * if the first is a 0 -> oct
1032 * else dec, strip leading -/+ and remember sign
1034 * only a + or - sign is no number resulting in an error */
1039 if (str[1] == 'x' || str[1] == 'X') /* hex */
1043 base[1] = SC_1; base[0] = SC_0;
1049 base[1] = SC_0; base[0] = SC_8;
1057 base[1] = SC_0; base[0] = SC_A;
1066 base[1] = SC_0; base[0] = SC_A;
1070 default: /* dec, else would have begun with 0x or 0 */
1071 base[1] = SC_0; base[0] = SC_A;
1074 else /* dec, else would have begun with 0x or 0 */
1076 base[1] = SC_0; base[0] = SC_A;
1079 /* BEGIN string evaluation, from left to right */
1090 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1092 val[0] = _digit((*str)-'a'+10);
1094 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1103 if (base[0] > SC_9 || base[1] > SC_0) /* (base > 10) */
1105 val[0] = _digit((*str)-'A'+10);
1107 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1112 if (base[0] > SC_7 || base[1] > SC_0) /* (base > 8) */
1114 val[0] = _digit((*str)-'0');
1116 else fail_char(orig_str, orig_len, *str, str-orig_str+1);
1128 val[0] = _digit((*str)-'0');
1133 fail_char(orig_str, orig_len, *str, str-orig_str+1);
1134 } /* switch(*str) */
1136 /* Radix conversion from base b to base B:
1137 * (UnUn-1...U1U0)b == ((((Un*b + Un-1)*b + ...)*b + U1)*b + U0)B */
1138 _mul(base, calc_buffer, calc_buffer); /* multiply current value with base */
1139 _add(val, calc_buffer, calc_buffer); /* add next digit to current value */
1141 /* get ready for the next letter */
1145 } /* while (len > 0 ) */
1148 _negate(calc_buffer, calc_buffer);
1150 /* beware: even if hex numbers have no sign, we need sign extension here */
1151 sign_extend(calc_buffer, mode);
1154 void sc_val_from_long(long value, void *buffer)
1157 char sign, is_minlong;
1159 if (buffer == NULL) buffer = calc_buffer;
1163 is_minlong = value == LONG_MIN;
1165 /* use absolute value, special treatment of MIN_LONG to avoid overflow */
1173 CLEAR_BUFFER(buffer);
1175 while ((value != 0) && (pos < (char*)buffer + calc_buffer_size))
1177 *pos++ = _digit(value & 0xf);
1183 _inc(buffer, buffer);
1185 _negate(buffer, buffer);
1189 void sc_val_from_ulong(unsigned long value, void *buffer)
1193 if (buffer == NULL) buffer = calc_buffer;
1196 while (pos < (unsigned char *)buffer + calc_buffer_size)
1198 *pos++ = (unsigned char)_digit(value & 0xf);
1203 long sc_val_to_long(const void *val)
1208 for (i = calc_buffer_size - 1; i >= 0; i--)
1210 l = (l << 4) + _val(((char *)val)[i]);
1215 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1220 if (buffer == NULL) buffer = calc_buffer;
1221 CLEAR_BUFFER(buffer);
1223 if (!sign) return; /* unsigned means minimum is 0(zero) */
1227 bits = num_bits - 1;
1228 for (i = 0; i < bits/4; i++)
1231 *pos++ = min_digit[bits%4];
1233 for (i++; i <= calc_buffer_size - 1; i++)
1237 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer)
1242 if (buffer == NULL) buffer = calc_buffer;
1243 CLEAR_BUFFER(buffer);
1246 bits = num_bits - sign;
1247 for (i = 0; i < bits/4; i++)
1250 *pos++ = max_digit[bits%4];
1252 for (i++; i <= calc_buffer_size - 1; i++)
1256 void sc_calc(const void* value1, const void* value2, unsigned op, void *buffer)
1258 char *unused_res; /* temp buffer holding unused result of divmod */
1260 const char *val1 = (const char *)value1;
1261 const char *val2 = (const char *)value2;
1263 unused_res = alloca(calc_buffer_size);
1265 CLEAR_BUFFER(calc_buffer);
1268 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1273 _negate(val1, calc_buffer);
1274 DEBUGPRINTF_COMPUTATION(("negated: %s\n", sc_print_hex(calc_buffer)));
1277 DEBUGPRINTF_COMPUTATION(("| "));
1278 _bitor(val1, val2, calc_buffer);
1281 DEBUGPRINTF_COMPUTATION(("& "));
1282 _bitand(val1, val2, calc_buffer);
1285 DEBUGPRINTF_COMPUTATION(("^ "));
1286 _bitxor(val1, val2, calc_buffer);
1289 _bitnot(val1, calc_buffer);
1290 DEBUGPRINTF_COMPUTATION(("bit-negated: %s\n", sc_print_hex(calc_buffer)));
1293 DEBUGPRINTF_COMPUTATION(("+ "));
1294 _add(val1, val2, calc_buffer);
1297 DEBUGPRINTF_COMPUTATION(("- "));
1298 _sub(val1, val2, calc_buffer);
1301 DEBUGPRINTF_COMPUTATION(("* "));
1302 _mul(val1, val2, calc_buffer);
1305 DEBUGPRINTF_COMPUTATION(("/ "));
1306 _divmod(val1, val2, calc_buffer, unused_res);
1309 DEBUGPRINTF_COMPUTATION(("%% "));
1310 _divmod(val1, val2, unused_res, calc_buffer);
1315 DEBUGPRINTF_COMPUTATION(("%s -> ", sc_print_hex(value2)));
1316 DEBUGPRINTF_COMPUTATION(("%s\n", sc_print_hex(calc_buffer)));
1318 if ((buffer != NULL) && (buffer != calc_buffer))
1320 memcpy(buffer, calc_buffer, calc_buffer_size);
1324 void sc_bitcalc(const void* value1, const void* value2, int radius, int sign, unsigned op, void* buffer)
1326 const char *val1 = (const char *)value1;
1327 const char *val2 = (const char *)value2;
1331 offset = sc_val_to_long(val2);
1333 DEBUGPRINTF_COMPUTATION(("%s ", sc_print_hex(value1)));
1337 DEBUGPRINTF_COMPUTATION(("<< %ld ", offset));
1338 _shl(val1, calc_buffer, offset, radius, sign);
1341 DEBUGPRINTF_COMPUTATION((">> %ld ", offset));
1342 _shr(val1, calc_buffer, offset, radius, sign, 0);
1345 DEBUGPRINTF_COMPUTATION((">>> %ld ", offset));
1346 _shr(val1, calc_buffer, offset, radius, sign, 1);
1349 DEBUGPRINTF_COMPUTATION(("<<>> %ld ", offset));
1350 _rot(val1, calc_buffer, offset, radius, sign);
1355 DEBUGPRINTF_COMPUTATION(("-> %s\n", sc_print_hex(calc_buffer)));
1357 if ((buffer != NULL) && (buffer != calc_buffer))
1359 memmove(buffer, calc_buffer, calc_buffer_size);
1363 int sc_comp(const void* value1, const void* value2)
1365 int counter = calc_buffer_size - 1;
1366 const char *val1 = (const char *)value1;
1367 const char *val2 = (const char *)value2;
1369 /* compare signs first:
1370 * the loop below can only compare values of the same sign! */
1371 if (_sign(val1) != _sign(val2)) return (_sign(val1) == 1)?(1):(-1);
1373 /* loop until two digits differ, the values are equal if there
1374 * are no such two digits */
1375 while (val1[counter] == val2[counter])
1378 if (counter < 0) return 0;
1381 /* the leftmost digit is the most significant, so this returns
1382 * the correct result.
1383 * This implies the digit enum is ordered */
1384 return (val1[counter] > val2[counter]) ? (1) : (-1);
1387 int sc_get_highest_set_bit(const void *value)
1389 const char *val = (const char*)value;
1392 high = calc_buffer_size * 4 - 1;
1394 for (counter = calc_buffer_size-1; counter >= 0; counter--) {
1395 if (val[counter] == SC_0) high -= 4;
1397 if (val[counter] > SC_7) return high;
1398 else if (val[counter] > SC_3) return high - 1;
1399 else if (val[counter] > SC_1) return high - 2;
1400 else return high - 3;
1406 int sc_get_lowest_set_bit(const void *value)
1408 const char *val = (const char*)value;
1412 sign = (_sign(val)==1)?(SC_0):(SC_F);
1415 for (counter = 0; counter < calc_buffer_size; counter++) {
1416 if (val[counter] == SC_0) low += 4;
1418 if (val[counter] < SC_2) return low;
1419 else if (val[counter] < SC_4) return low + 1;
1420 else if (val[counter] < SC_8) return low + 2;
1421 else return low + 3;
1427 int sc_is_zero(const void *value)
1429 const char* val = (const char *)value;
1432 for (counter = 0; counter < calc_buffer_size; counter++) {
1433 if (val[counter] != SC_0) return 0;
1438 int sc_is_negative(const void *value)
1440 return _sign(value) == -1;
1443 int sc_had_carry(void)
1448 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
1450 const char *val = (const char *)value;
1451 int nibble_ofs = 2 * byte_ofs;
1454 /* the current scheme uses one byte to store a nibble */
1455 if (nibble_ofs >= len)
1458 res = _val(val[nibble_ofs]);
1459 if (len > nibble_ofs + 1)
1460 res |= _val(val[nibble_ofs + 1]) << 4;
1466 * convert to a string
1467 * FIXME: Doesn't check buffer bounds
1469 const char *sc_print(const void *value, unsigned bits, enum base_t base, int signed_mode)
1471 static const char big_digits[] = "0123456789ABCDEF";
1472 static const char small_digits[] = "0123456789abcdef";
1474 char *base_val, *div1_res, *div2_res, *rem_res;
1475 int counter, nibbles, i, sign;
1478 const char *val = (const char *)value;
1482 const char *digits = small_digits;
1484 base_val = alloca(calc_buffer_size);
1485 div1_res = alloca(calc_buffer_size);
1486 div2_res = alloca(calc_buffer_size);
1487 rem_res = alloca(calc_buffer_size);
1489 pos = output_buffer + bit_pattern_size;
1494 bits = bit_pattern_size;
1495 #ifdef STRCALC_DEBUG_FULLPRINT
1499 nibbles = bits >> 2;
1503 digits = big_digits;
1505 for (counter = 0; counter < nibbles; ++counter) {
1506 *(--pos) = digits[_val(val[counter])];
1507 #ifdef STRCALC_DEBUG_GROUPPRINT
1508 if ((counter+1)%8 == 0)
1513 /* last nibble must be masked */
1515 x = and_table[_val(val[++counter])][bits & 3];
1516 *(--pos) = digits[_val(x)];
1519 /* now kill zeros */
1520 for (; counter > 1; --counter, ++pos) {
1521 #ifdef STRCALC_DEBUG_GROUPPRINT
1522 if (pos[0] == ' ') ++pos;
1530 for (counter = 0; counter < nibbles; ++counter) {
1532 p = binary_table[_val(val[counter])];
1539 /* last nibble must be masked */
1541 x = and_table[_val(val[++counter])][bits & 3];
1544 p = binary_table[_val(x)];
1551 /* now kill zeros */
1552 for (counter <<= 2; counter > 1; --counter, ++pos)
1559 memset(base_val, SC_0, calc_buffer_size);
1560 base_val[0] = base == SC_DEC ? SC_A : SC_8;
1564 if (signed_mode && base == SC_DEC) {
1565 /* check for negative values */
1566 if (_bit(val, bits - 1)) {
1567 _negate(val, div2_res);
1573 /* transfer data into oscillating buffers */
1574 memset(div1_res, SC_0, calc_buffer_size);
1575 for (counter = 0; counter < nibbles; ++counter)
1576 div1_res[counter] = p[counter];
1578 /* last nibble must be masked */
1582 div1_res[counter] = and_table[_val(p[counter])][bits & 3];
1588 _divmod(m, base_val, n, rem_res);
1592 *(--pos) = digits[_val(rem_res[0])];
1595 for (i = 0; i < calc_buffer_size; ++i)
1606 printf("%i\n", base);
1613 void init_strcalc(int precision)
1615 if (calc_buffer == NULL) {
1616 if (precision <= 0) precision = SC_DEFAULT_PRECISION;
1618 /* round up to multiple of 4 */
1619 precision = (precision + 3) & ~3;
1621 bit_pattern_size = (precision);
1622 calc_buffer_size = (precision / 2);
1623 max_value_size = (precision / 4);
1625 calc_buffer = xmalloc(calc_buffer_size+1 * sizeof(char));
1626 output_buffer = xmalloc(bit_pattern_size+1 * sizeof(char));
1628 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));
1633 void finish_strcalc() {
1634 free(calc_buffer); calc_buffer = NULL;
1635 free(output_buffer); output_buffer = NULL;
1638 int sc_get_precision(void)
1640 return bit_pattern_size;