beifg: Factorise code to count interference components.
[libfirm] / ir / tv / strcalc.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief    Provides basic mathematical operations on values represented as strings.
9  * @date     2003
10  * @author   Mathias Heil
11  * @brief
12  *
13  * The module uses a string to represent values, and provides operations
14  * to perform calculations with these values.
15  * Results are stored in an internal buffer, so you have to make a copy
16  * of them if you need to store the result.
17  *
18  */
19 #ifndef FIRM_TV_STRCALC_H
20 #define FIRM_TV_STRCALC_H
21
22 #include "irmode.h"
23
24 #ifdef STRCALC_DEBUG_ALL             /* switch on all debug options */
25 #  ifndef STRCALC_DEBUG
26 #    define STRCALC_DEBUG            /* switch on debug output */
27 #  endif
28 #  ifndef STRCALC_DEBUG_PRINTCOMP    /* print arguments and result of each computation */
29 #    define STRCALC_DEBUG_PRINTCOMP
30 #  endif
31 #  ifndef STRCALC_DEBUG_FULLPRINT
32 #    define STRCALC_DEBUG_FULLPRINT  /* print full length of values (e.g. 128 bit instead of 64 bit using default init) */
33 #  endif
34 #  ifndef STRCALC_DEBUG_GROUPPRINT
35 #    define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
36 #  endif
37 #endif
38
39 /*
40  * constants, typedefs, enums
41  */
42
43 #define SC_DEFAULT_PRECISION 64
44
45 enum {
46   SC_0 = 0,
47   SC_1,
48   SC_2,
49   SC_3,
50   SC_4,
51   SC_5,
52   SC_6,
53   SC_7,
54   SC_8,
55   SC_9,
56   SC_A,
57   SC_B,
58   SC_C,
59   SC_D,
60   SC_E,
61   SC_F
62 };
63
64 /**
65  * The output mode for integer values.
66  */
67 enum base_t {
68   SC_hex,   /**< hexadecimal output with small letters */
69   SC_HEX,   /**< hexadecimal output with BIG letters */
70   SC_DEC,   /**< decimal output */
71   SC_OCT,   /**< octal output */
72   SC_BIN    /**< binary output */
73 };
74
75 /**
76  * buffer = value1 + value2
77  */
78 void sc_add(const void *value1, const void *value2, void *buffer);
79
80 /**
81  * buffer = value1 - value2
82  */
83 void sc_sub(const void *value1, const void *value2, void *buffer);
84
85 /**
86  * buffer = -value
87  */
88 void sc_neg(const void *value, void *buffer);
89
90 /**
91  * buffer = value1 & value2
92  */
93 void sc_and(const void *value1, const void *value2, void *buffer);
94
95 /**
96  * buffer = value1 & ~value2
97  */
98 void sc_andnot(const void *value1, const void *value2, void *buffer);
99
100 /**
101  * buffer = value1 | value2
102  */
103 void sc_or(const void *value1, const void *value2, void *buffer);
104
105 /**
106  * buffer = value1 ^ value2
107  */
108 void sc_xor(const void *value1, const void *value2, void *buffer);
109
110 /**
111  * buffer = ~value
112  */
113 void sc_not(const void *value, void *buffer);
114
115 /**
116  * buffer = value1 * value2
117  */
118 void sc_mul(const void *value1, const void *value2, void *buffer);
119
120 /**
121  * buffer = value1 / value2
122  */
123 void sc_div(const void *value1, const void *value2, void *buffer);
124
125 /**
126  * buffer = value1 % value2
127  */
128 void sc_mod(const void *value1, const void *value2, void *buffer);
129
130 /**
131  * div_buffer = value1 / value2
132  * mod_buffer = value1 % value2
133  */
134 void sc_divmod(const void *value1, const void *value2, void *div_buffer, void *mod_buffer);
135
136 /**
137  * buffer = value1 << offset
138  */
139 void sc_shlI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
140
141 /**
142  * buffer = value1 << value2
143  */
144 void sc_shl(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
145
146 /**
147  * buffer = value1 >>u offset
148  */
149 void sc_shrI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
150
151 /**
152  * buffer = value1 >>u value2
153  */
154 void sc_shr(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
155
156 /**
157  * buffer = value1 >>s offset
158  */
159 void sc_shrsI(const void *val1, long shift_cnt, int bitsize, int sign, void *buffer);
160
161 /**
162  * buffer = value1 >>s value2
163  */
164 void sc_shrs(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
165
166 /**
167  * buffer = value1 <<left>> value2
168  */
169 void sc_rotl(const void *value1, const void *value2, int bitsize, int sign, void *buffer);
170
171 /**
172  * buffer = 0
173  */
174 void sc_zero(void *buffer);
175
176 /*
177  * function declarations
178  */
179 const void *sc_get_buffer(void);
180 int sc_get_buffer_length(void);
181
182 void sign_extend(void *buffer, ir_mode *mode);
183
184 /**
185  * create an value form a string representation
186  * @return 1 if ok, 0 in case of parse error
187  */
188 int sc_val_from_str(char sign, unsigned base, const char *str,
189                     size_t len, void *buffer);
190
191 /** create a value from a long */
192 void sc_val_from_long(long l, void *buffer);
193
194 /** create a value form an unsigned long */
195 void sc_val_from_ulong(unsigned long l, void *buffer);
196
197 /** converts a value to a long */
198 long sc_val_to_long(const void *val);
199 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
200 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
201
202 /** truncates a value to lowest @p num_bits bits */
203 void sc_truncate(unsigned num_bits, void *buffer);
204
205 /**
206  * Compares val1 and val2
207  */
208 ir_relation sc_comp(void const *val1, void const *val2);
209
210 int sc_get_highest_set_bit(const void *value);
211 int sc_get_lowest_set_bit(const void *value);
212 int sc_is_zero(const void *value);
213 int sc_is_negative(const void *value);
214
215 /**
216  * Return the bits of a tarval at a given byte-offset.
217  *
218  * @param value     the value
219  * @param len       number of valid bits in the value
220  * @param byte_ofs  the byte offset
221  */
222 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
223
224 /**
225  * Converts a tarval into a string.
226  *
227  * @param val1        the value pointer
228  * @param bits        number of valid bits in this value
229  * @param base        output base
230  * @param signed_mode print it signed (only decimal mode supported
231  */
232 const char *sc_print(const void *val1, unsigned bits, enum base_t base, int signed_mode);
233
234 /** Initialize the strcalc module.
235  * Sets up internal data structures and constants
236  * After the first call subsequent calls have no effect
237  *
238  * @param precision_in_bytes Specifies internal precision to be used
239  *   for calculations. The reason for being multiples of 8 eludes me
240  */
241 void init_strcalc(int precision_in_bytes);
242 void finish_strcalc(void);
243 int sc_get_precision(void);
244
245 /** Return the bit at a given position. */
246 int sc_get_bit_at(const void *value, unsigned pos);
247
248 /** Set the bit at the specified position. */
249 void sc_set_bit_at(void *value, unsigned pos);
250
251 /* Strange semantics */
252 int sc_had_carry(void);
253
254 #endif /* FIRM_TV_STRCALC_H */