fbb92874ab6592d02bf89431da7275a39d71ad9c
[libfirm] / ir / tv / strcalc.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tv/strcalc.h
4  * Purpose:     Provides basic mathematical operations on values represented as strings.
5  * Author:      Mathias Heil
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file strcalc.h
15  *
16  * The module uses a string to represent values, and provides operations
17  * to perform calculations with these values.
18  * Results are stored in an internal buffer, so you have to make a copy
19  * of them if you need to store the result.
20  *
21  */
22
23 #ifndef _STRCALC_H_
24 #define _STRCALC_H_
25
26 #include "irmode.h"
27
28 #ifdef STRCALC_DEBUG_ALL             /* switch on all debug options */
29 #  ifndef STRCALC_DEBUG
30 #    define STRCALC_DEBUG            /* switch on debug output */
31 #  endif
32 #  ifndef STRCALC_DEBUG_PRINTCOMP    /* print arguments and result of each computation */
33 #    define STRCALC_DEBUG_PRINTCOMP
34 #  endif
35 #  ifndef STRCALC_DEBUG_FULLPRINT
36 #    define STRCALC_DEBUG_FULLPRINT  /* print full length of values (e.g. 128 bit instead of 64 bit using default init) */
37 #  endif
38 #  ifndef STRCALC_DEBUG_GROUPPRINT
39 #    define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
40 #  endif
41 #endif
42
43 /*
44  * constants, typedefs, enums
45  */
46
47 #define SC_DEFAULT_PRECISION 64
48
49 enum {
50   SC_0 = 0,
51   SC_1,
52   SC_2,
53   SC_3,
54   SC_4,
55   SC_5,
56   SC_6,
57   SC_7,
58   SC_8,
59   SC_9,
60   SC_A,
61   SC_B,
62   SC_C,
63   SC_D,
64   SC_E,
65   SC_F
66 };
67
68 /**
69  * Possible operations on integer values.
70  */
71 typedef enum {
72   SC_ADD = 0,   /**< Addition */
73   SC_SUB,       /**< Substraction */
74   SC_NEG,       /**< Unary Minus */
75   SC_MUL,       /**< Multiplication */
76   SC_DIV,       /**< Integer Division (with rounding toward zero ?) */
77   SC_MOD,       /**< Devision Remainder */
78   SC_SHL,       /**< Left Shift */
79   SC_SHR,       /**< Logical (unsigned) Right Shift */
80   SC_SHRS,      /**< Arithmetic (signed) Right Shift */
81   SC_ROT,       /**< Rotation (both directions) */
82   SC_AND,       /**< Bitwise And */
83   SC_OR,        /**< Bitwise Or */
84   SC_NOT,       /**< Bitwise Not */
85   SC_XOR        /**< Bitwise Exclusive Or */
86 } sc_op_t;
87
88 /**
89  * The output mode for integer values.
90  */
91 enum base_t {
92   SC_hex,   /**< hexadecimal output with small letters */
93   SC_HEX,   /**< hexadecimal output with BIG letters */
94   SC_DEC,   /**< decimal output */
95   SC_OCT,   /**< octal output */
96   SC_BIN    /**< binary output */
97 };
98
99 /*
100  * definitions and macros
101  */
102 #define sc_add(a, b, c) sc_calc((a), (b), SC_ADD, (c))
103 #define sc_sub(a, b, c) sc_calc((a), (b), SC_SUB, (c))
104 #define sc_neg(a, c)    sc_calc((a), NULL, SC_NEG, (c))
105 #define sc_and(a, b, c) sc_calc((a), (b), SC_AND, (c))
106 #define sc_or(a, b, c)  sc_calc((a), (b), SC_OR, (c))
107 #define sc_xor(a, b, c) sc_calc((a), (b), SC_XOR, (c))
108 #define sc_not(a, c)    sc_calc((a), NULL, SC_NOT, (c))
109 #define sc_mul(a, b, c) sc_calc((a), (b), SC_MUL, (c))
110 #define sc_div(a, b, c) sc_calc((a), (b), SC_DIV, (c))
111 #define sc_mod(a, b, c) sc_calc((a), (b), SC_MOD, (c))
112 #define sc_shl(a, b, c, d, e)  sc_bitcalc((a), (b), (c), (d), SC_SHL, (e))
113 #define sc_shr(a, b, c, d, e)  sc_bitcalc((a), (b), (c), (d), SC_SHR, (e))
114 #define sc_shrs(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_SHRS, (e))
115 #define sc_rot(a, b, c, d, e)  sc_bitcalc((a), (b), (c), (d), SC_ROT, (e))
116
117 /*
118  * function declarations
119  */
120 const void *sc_get_buffer(void);
121 int sc_get_buffer_length(void);
122
123 void sign_extend(char *calc_buffer, ir_mode *mode);
124
125 /** create an value form a string representation */
126 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode);
127
128 /** create a value from a long */
129 void sc_val_from_long(long l, void *buffer);
130
131 /** create a value form an unsigned long */
132 void sc_val_from_ulong(unsigned long l, void *buffer);
133
134 /** converts a value to a long */
135 long sc_val_to_long(const void *val);
136 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
137 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
138
139 void sc_calc(const void *val1, const void *val2, unsigned op, void *buffer);
140 void sc_bitcalc(const void *val1, const void *val2, int radius, int sign, unsigned op, void *buffer);
141 int  sc_comp(const void *val1, const void *val2);
142
143 int sc_get_highest_set_bit(const void *value);
144 int sc_get_lowest_set_bit(const void *value);
145 int sc_is_zero(const void *value);
146 int sc_is_negative(const void *value);
147 int sc_had_carry(void);
148 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
149
150 /**
151  * Converts a tarval into a string.
152  *
153  * @param val1      the value pointer
154  * @param bits      number of valid bits in this value
155  * @param base      output base
156  */
157 const char *sc_print(const void *val1, unsigned bits, enum base_t base);
158
159 /** Initialize the strcalc module.
160  * Sets up internal data structures and constants
161  * After the first call subsequent calls have no effect
162  *
163  * @param precision_in_bytes Specifies internal precision to be used
164  *   for calculations. The reason for being multiples of 8 eludes me
165  */
166 void init_strcalc(int precision_in_bytes);
167 void finish_strcalc(void);
168 int sc_get_precision(void);
169
170 #endif /* _STRCALC_H_ */