Added decimal/octal/binary output for integer values
[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 BIGGEST_INTEGER_SIZE_IN_BYTES 8
22 #define SCDEBUG
23
24 /*****************************************************************************
25  * typedefs, enums and structs
26  *****************************************************************************/
27 enum {
28   SC_0 = 0,
29   SC_1,
30   SC_2,
31   SC_3,
32   SC_4,
33   SC_5,
34   SC_6,
35   SC_7,
36   SC_8,
37   SC_9,
38   SC_A,
39   SC_B,
40   SC_C,
41   SC_D,
42   SC_E,
43   SC_F,
44 };
45
46 /**
47  * Possible operations on integer values.
48  */
49 enum {
50   SC_ADD = 0,           /**< Addition */
51   SC_SUB,               /**< Substraction */
52   SC_NEG,               /**< Unary Minus */
53   SC_MUL,               /**< Multiplication */
54   SC_DIV,               /**< Integer Division (with rounding toward zero ?) */
55   SC_MOD,               /**< Devision Remainder */
56   SC_SHL,               /**< Left Shift */
57   SC_SHR,               /**< Logical (unsigned) Right Shift */
58   SC_SHRS,              /**< Arithmetic (signed) Right Shift */
59   SC_ROT,               /**< Rotation (both directions) */
60   SC_AND,               /**< Bitwise And */
61   SC_OR,                /**< Bitwise Or */
62   SC_NOT,               /**< Bitwise Not */
63   SC_XOR,               /**< Bitwise Exclusive Or */
64 };
65
66 /**
67  * The output mode for ntger values.
68  */
69 enum base_t {
70   SC_HEX,       /**< hexadecimal output */
71   SC_DEC,       /**< decimal output */
72   SC_OCT,       /**< octal output */
73   SC_BIN,       /**< binary output */
74 };
75
76 /*
77  * definitions and macros
78  */
79 #define sc_add(a, b) sc_calc((a), (b), SC_ADD)
80 #define sc_sub(a, b) sc_calc((a), (b), SC_SUB)
81 #define sc_neg(a) sc_calc((a), NULL, SC_NEG)
82 #define sc_and(a, b) sc_calc((a), (b), SC_AND)
83 #define sc_or(a, b) sc_calc((a), (b), SC_OR)
84 #define sc_xor(a, b) sc_calc((a), (b), SC_XOR)
85 #define sc_not(a) sc_calc((a), NULL, SC_NOT)
86 #define sc_mul(a, b) sc_calc((a), (b), SC_MUL)
87 #define sc_div(a, b) sc_calc((a), (b), SC_DIV)
88 #define sc_mod(a, b) sc_calc((a), (b), SC_MOD)
89 #define sc_shl(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_SHL)
90 #define sc_shr(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_SHR)
91 #define sc_shrs(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_SHRS)
92 #define sc_rot(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_ROT)
93
94 /*
95  * function declarations
96  */
97 const void *sc_get_buffer(void);
98 const int sc_get_buffer_length(void);
99
100 void sc_val_from_str(const char *str, unsigned int len);
101 void sc_val_from_long(long l);
102 long sc_val_to_long(const void *val);
103 void sc_min_from_bits(unsigned int num_bits, unsigned int sign);
104 void sc_max_from_bits(unsigned int num_bits, unsigned int sign);
105
106 void sc_calc(const void *val1, const void *val2, unsigned op);
107 void sc_bitcalc(const void *val1, const void *val2, unsigned radius, unsigned sign, unsigned op);
108 int  sc_comp(const void *val1, const void *val2);
109
110 unsigned char sc_sub_bits(const void *val, int len, unsigned byte_ofs);
111
112 /**
113  * Converts a tarval into a string.
114  *
115  * @param val1          the value pointer
116  * @param bits          number of valid bits in this value
117  * @param base          output base
118  */
119 const char *sc_print(const void *val1, unsigned bits, enum base_t base);
120
121 #endif /* _STRCALC_H_ */