integer floating point values will be printed witha .0 appended
[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 #ifdef STRCALC_DEBUG_ALL             /* switch on all debug options */
22 #  ifndef STRCALC_DEBUG
23 #    define STRCALC_DEBUG            /* switch on debug output */
24 #  endif
25 #  ifndef STRCALC_DEBUG_PRINTCOMP    /* print arguments and result of each computation */
26 #    define STRCALC_DEBUG_PRINTCOMP
27 #  endif
28 #  ifndef STRCALC_DEBUG_FULLPRINT
29 #    define STRCALC_DEBUG_FULLPRINT  /* print full length of values (e.g. 128 bit instead of 64 bit using default init) */
30 #  endif
31 #  ifndef STRCALC_DEBUG_GROUPPRINT
32 #    define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
33 #  endif
34 #endif
35
36 #ifdef STRCALC_DEBUG
37   /* shortcut output for debugging */
38 #  define sc_print_hex(a) sc_print((a), 0, SC_HEX)
39 #  define sc_print_dec(a) sc_print((a), 0, SC_DEC)
40 #  define sc_print_oct(a) sc_print((a), 0, SC_OCT)
41 #  define sc_print_bin(a) sc_print((a), 0, SC_BIN)
42 #endif
43
44 /*
45  * constants, typedefs, enums
46  */
47
48 #define DEFAULT_PRECISION_IN_BYTES 8
49
50 enum {
51   SC_0 = 0,
52   SC_1,
53   SC_2,
54   SC_3,
55   SC_4,
56   SC_5,
57   SC_6,
58   SC_7,
59   SC_8,
60   SC_9,
61   SC_A,
62   SC_B,
63   SC_C,
64   SC_D,
65   SC_E,
66   SC_F
67 };
68
69 /**
70  * Possible operations on integer values.
71  */
72 enum {
73   SC_ADD = 0,           /**< Addition */
74   SC_SUB,               /**< Substraction */
75   SC_NEG,               /**< Unary Minus */
76   SC_MUL,               /**< Multiplication */
77   SC_DIV,               /**< Integer Division (with rounding toward zero ?) */
78   SC_MOD,               /**< Devision Remainder */
79   SC_SHL,               /**< Left Shift */
80   SC_SHR,               /**< Logical (unsigned) Right Shift */
81   SC_SHRS,              /**< Arithmetic (signed) Right Shift */
82   SC_ROT,               /**< Rotation (both directions) */
83   SC_AND,               /**< Bitwise And */
84   SC_OR,                /**< Bitwise Or */
85   SC_NOT,               /**< Bitwise Not */
86   SC_XOR                /**< Bitwise Exclusive Or */
87 };
88
89 /**
90  * The output mode for ntger values.
91  */
92 enum base_t {
93   SC_hex,       /**< hexadecimal output with small letters */
94   SC_HEX,       /**< hexadecimal output with BIG letters */
95   SC_DEC,       /**< decimal output */
96   SC_OCT,       /**< octal output */
97   SC_BIN        /**< binary output */
98 };
99
100 /*
101  * definitions and macros
102  */
103 #define sc_add(a, b) sc_calc((a), (b), SC_ADD)
104 #define sc_sub(a, b) sc_calc((a), (b), SC_SUB)
105 #define sc_neg(a) sc_calc((a), NULL, SC_NEG)
106 #define sc_and(a, b) sc_calc((a), (b), SC_AND)
107 #define sc_or(a, b) sc_calc((a), (b), SC_OR)
108 #define sc_xor(a, b) sc_calc((a), (b), SC_XOR)
109 #define sc_not(a) sc_calc((a), NULL, SC_NOT)
110 #define sc_mul(a, b) sc_calc((a), (b), SC_MUL)
111 #define sc_div(a, b) sc_calc((a), (b), SC_DIV)
112 #define sc_mod(a, b) sc_calc((a), (b), SC_MOD)
113 #define sc_shl(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_SHL)
114 #define sc_shr(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_SHR)
115 #define sc_shrs(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_SHRS)
116 #define sc_rot(a, b, c, d) sc_bitcalc((a), (b), (c), (d), SC_ROT)
117
118 /*
119  * function declarations
120  */
121 const void *sc_get_buffer(void);
122 const int sc_get_buffer_length(void);
123
124 void sc_val_from_str(const char *str, unsigned int len);
125 void sc_val_from_long(long l);
126 long sc_val_to_long(const void *val);
127 void sc_min_from_bits(unsigned int num_bits, unsigned int sign);
128 void sc_max_from_bits(unsigned int num_bits, unsigned int sign);
129
130 void sc_calc(const void *val1, const void *val2, unsigned op);
131 void sc_bitcalc(const void *val1, const void *val2, int radius, int sign, unsigned op);
132 int  sc_comp(const void *val1, const void *val2);
133
134 int sc_get_highest_set_bit(const void *value);
135 int sc_get_lowest_set_bit(const void *value);
136 int sc_is_zero(const void *value);
137 int sc_is_negative(const void *value);
138 int sc_had_carry(void);
139 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
140
141 /**
142  * Converts a tarval into a string.
143  *
144  * @param val1          the value pointer
145  * @param bits          number of valid bits in this value
146  * @param base          output base
147  */
148 const char *sc_print(const void *val1, unsigned bits, enum base_t base);
149
150 void init_strcalc(int precision_in_bytes);
151 int get_precision(void);
152
153 #endif /* _STRCALC_H_ */