a69b34f44d6c47c420052575c110089db5a7ae11
[libfirm] / ir / tv / strcalc.h
1   /****h* tools/strcalc
2  *
3  * NAME
4  *   strcalc -- calculations using strings
5  *   Provides basic mathematical operations on values represented as strings
6  *
7  * AUTHORS
8  *   Matthias Heil
9  *
10  * DESCRIPTION
11  *    The module uses a string to represent values, and provides operations
12  *   to perform calculations with these values.
13  *    Results are stored in an internal buffer, so you have to make a copy
14  *   of them if you need to store the result.
15  *
16  ******/
17
18 #ifndef _STRCALC_H_
19 #define _STRCALC_H_
20
21 #define DEFAULT_PRECISION_IN_BYTES 8
22
23 /*****************************************************************************
24  * typedefs, enums and structs
25  *****************************************************************************/
26 enum {
27   SC_0 = 0,
28   SC_1,
29   SC_2,
30   SC_3,
31   SC_4,
32   SC_5,
33   SC_6,
34   SC_7,
35   SC_8,
36   SC_9,
37   SC_A,
38   SC_B,
39   SC_C,
40   SC_D,
41   SC_E,
42   SC_F,
43 };
44
45 /**
46  * Possible operations on integer values.
47  */
48 enum {
49   SC_ADD = 0,           /**< Addition */
50   SC_SUB,               /**< Substraction */
51   SC_NEG,               /**< Unary Minus */
52   SC_MUL,               /**< Multiplication */
53   SC_DIV,               /**< Integer Division (with rounding toward zero ?) */
54   SC_MOD,               /**< Devision Remainder */
55   SC_SHL,               /**< Left Shift */
56   SC_SHR,               /**< Logical (unsigned) Right Shift */
57   SC_SHRS,              /**< Arithmetic (signed) Right Shift */
58   SC_ROT,               /**< Rotation (both directions) */
59   SC_AND,               /**< Bitwise And */
60   SC_OR,                /**< Bitwise Or */
61   SC_NOT,               /**< Bitwise Not */
62   SC_XOR,               /**< Bitwise Exclusive Or */
63 };
64
65 /**
66  * The output mode for ntger values.
67  */
68 enum base_t {
69   SC_HEX,       /**< hexadecimal output */
70   SC_DEC,       /**< decimal output */
71   SC_OCT,       /**< octal output */
72   SC_BIN,       /**< binary output */
73 };
74
75 /*
76  * definitions and macros
77  */
78 #define sc_add(a, b) sc_calc((a), (b), SC_ADD)
79 #define sc_sub(a, b) sc_calc((a), (b), SC_SUB)
80 #define sc_neg(a) sc_calc((a), NULL, SC_NEG)
81 #define sc_and(a, b) sc_calc((a), (b), SC_AND)
82 #define sc_or(a, b) sc_calc((a), (b), SC_OR)
83 #define sc_xor(a, b) sc_calc((a), (b), SC_XOR)
84 #define sc_not(a) sc_calc((a), NULL, SC_NOT)
85 #define sc_mul(a, b) sc_calc((a), (b), SC_MUL)
86 #define sc_div(a, b) sc_calc((a), (b), SC_DIV)
87 #define sc_mod(a, b) sc_calc((a), (b), SC_MOD)
88 #define sc_shl(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_SHL)
89 #define sc_shr(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_SHR)
90 #define sc_shrs(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_SHRS)
91 #define sc_rot(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_ROT)
92
93 /*
94  * function declarations
95  */
96 const void *sc_get_buffer(void);
97 const int sc_get_buffer_length(void);
98
99 void sc_val_from_str(const char *str, unsigned int len);
100 void sc_val_from_long(long l);
101 long sc_val_to_long(const void *val);
102 void sc_min_from_bits(unsigned int num_bits, unsigned int sign);
103 void sc_max_from_bits(unsigned int num_bits, unsigned int sign);
104
105 void sc_calc(const void *val1, const void *val2, unsigned op);
106 void sc_bitcalc(const void *val1, const void *val2, int radius, int sign, unsigned op);
107 int  sc_comp(const void *val1, const void *val2);
108
109 int sc_get_highest_set_bit(const void *value);
110 int sc_get_lowest_set_bit(const void *value);
111 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
112
113 /**
114  * Converts a tarval into a string.
115  *
116  * @param val1          the value pointer
117  * @param bits          number of valid bits in this value
118  * @param base          output base
119  */
120 const char *sc_print(const void *val1, unsigned bits, enum base_t base);
121
122 void init_strcalc(int precision_in_bytes);
123 int get_precision(void);
124
125 #endif /* _STRCALC_H_ */