Typo fixed.
[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 /*
37  * constants, typedefs, enums
38  */
39
40 #define SC_DEFAULT_PRECISION 64
41
42 enum {
43   SC_0 = 0,
44   SC_1,
45   SC_2,
46   SC_3,
47   SC_4,
48   SC_5,
49   SC_6,
50   SC_7,
51   SC_8,
52   SC_9,
53   SC_A,
54   SC_B,
55   SC_C,
56   SC_D,
57   SC_E,
58   SC_F
59 };
60
61 /**
62  * Possible operations on integer values.
63  */
64 typedef enum {
65   SC_ADD = 0,           /**< Addition */
66   SC_SUB,               /**< Substraction */
67   SC_NEG,               /**< Unary Minus */
68   SC_MUL,               /**< Multiplication */
69   SC_DIV,               /**< Integer Division (with rounding toward zero ?) */
70   SC_MOD,               /**< Devision Remainder */
71   SC_SHL,               /**< Left Shift */
72   SC_SHR,               /**< Logical (unsigned) Right Shift */
73   SC_SHRS,              /**< Arithmetic (signed) Right Shift */
74   SC_ROT,               /**< Rotation (both directions) */
75   SC_AND,               /**< Bitwise And */
76   SC_OR,                /**< Bitwise Or */
77   SC_NOT,               /**< Bitwise Not */
78   SC_XOR,               /**< Bitwise Exclusive Or */
79 } sc_op_t;
80
81 /**
82  * The output mode for ntger values.
83  */
84 enum base_t {
85   SC_hex,       /**< hexadecimal output with small letters */
86   SC_HEX,       /**< hexadecimal output with BIG letters */
87   SC_DEC,       /**< decimal output */
88   SC_OCT,       /**< octal output */
89   SC_BIN        /**< binary output */
90 };
91
92 /*
93  * definitions and macros
94  */
95 #define sc_add(a, b, c) sc_calc((a), (b), SC_ADD, (c))
96 #define sc_sub(a, b, c) sc_calc((a), (b), SC_SUB, (c))
97 #define sc_neg(a, c) sc_calc((a), NULL, SC_NEG, (c))
98 #define sc_and(a, b, c) sc_calc((a), (b), SC_AND, (c))
99 #define sc_or(a, b, c) sc_calc((a), (b), SC_OR, (c))
100 #define sc_xor(a, b, c) sc_calc((a), (b), SC_XOR, (c))
101 #define sc_not(a, c) sc_calc((a), NULL, SC_NOT, (c))
102 #define sc_mul(a, b, c) sc_calc((a), (b), SC_MUL, (c))
103 #define sc_div(a, b, c) sc_calc((a), (b), SC_DIV, (c))
104 #define sc_mod(a, b, c) sc_calc((a), (b), SC_MOD, (c))
105 #define sc_shl(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_SHL, (e))
106 #define sc_shr(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_SHR, (e))
107 #define sc_shrs(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_SHRS, (e))
108 #define sc_rot(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_ROT, (e))
109
110 /*
111  * function declarations
112  */
113 const void *sc_get_buffer(void);
114 const int sc_get_buffer_length(void);
115
116 void sc_val_from_str(const char *str, unsigned int len, void *buffer);
117 void sc_val_from_long(long l, void *buffer);
118 void sc_val_from_ulong(unsigned long l, void *buffer);
119 long sc_val_to_long(const void *val);
120 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
121 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
122
123 void sc_calc(const void *val1, const void *val2, unsigned op, void *buffer);
124 void sc_bitcalc(const void *val1, const void *val2, int radius, int sign, unsigned op, void *buffer);
125 int  sc_comp(const void *val1, const void *val2);
126
127 int sc_get_highest_set_bit(const void *value);
128 int sc_get_lowest_set_bit(const void *value);
129 int sc_is_zero(const void *value);
130 int sc_is_negative(const void *value);
131 int sc_had_carry(void);
132 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
133
134 /**
135  * Converts a tarval into a string.
136  *
137  * @param val1          the value pointer
138  * @param bits          number of valid bits in this value
139  * @param base          output base
140  */
141 const char *sc_print(const void *val1, unsigned bits, enum base_t base);
142
143 /** Init strcalc module
144  * Sets up internal data structures and constants
145  * After the first call subsequent calls have no effect
146  *
147  * @param precision_in_bytes Specifies internal precision to be used
148  *   for calculations. The reason for being multiples of 8 eludes me
149  */
150 void init_strcalc(int precision_in_bytes);
151 int sc_get_precision(void);
152
153 #endif /* _STRCALC_H_ */