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