3 * File name: ir/tv/strcalc.h
4 * Purpose: Provides basic mathematical operations on values represented as strings.
9 * Copyright: (c) 2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
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.
28 #ifdef STRCALC_DEBUG_ALL /* switch on all debug options */
29 # ifndef STRCALC_DEBUG
30 # define STRCALC_DEBUG /* switch on debug output */
32 # ifndef STRCALC_DEBUG_PRINTCOMP /* print arguments and result of each computation */
33 # define STRCALC_DEBUG_PRINTCOMP
35 # ifndef STRCALC_DEBUG_FULLPRINT
36 # define STRCALC_DEBUG_FULLPRINT /* print full length of values (e.g. 128 bit instead of 64 bit using default init) */
38 # ifndef STRCALC_DEBUG_GROUPPRINT
39 # define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
44 * constants, typedefs, enums
47 #define SC_DEFAULT_PRECISION 64
69 * Possible operations on integer values.
72 SC_ADD = 0, /**< Addition */
73 SC_SUB, /**< Substraction */
74 SC_NEG, /**< Unary Minus */
75 SC_MUL, /**< Multiplication */
76 SC_DIV, /**< Integer Division (with rounding toward zero ?) */
77 SC_MOD, /**< Devision Remainder */
78 SC_SHL, /**< Left Shift */
79 SC_SHR, /**< Logical (unsigned) Right Shift */
80 SC_SHRS, /**< Arithmetic (signed) Right Shift */
81 SC_ROT, /**< Rotation (both directions) */
82 SC_AND, /**< Bitwise And */
83 SC_OR, /**< Bitwise Or */
84 SC_NOT, /**< Bitwise Not */
85 SC_XOR /**< Bitwise Exclusive Or */
89 * The output mode for integer values.
92 SC_hex, /**< hexadecimal output with small letters */
93 SC_HEX, /**< hexadecimal output with BIG letters */
94 SC_DEC, /**< decimal output */
95 SC_OCT, /**< octal output */
96 SC_BIN /**< binary output */
100 * definitions and macros
102 #define sc_add(a, b, c) sc_calc((a), (b), SC_ADD, (c))
103 #define sc_sub(a, b, c) sc_calc((a), (b), SC_SUB, (c))
104 #define sc_neg(a, c) sc_calc((a), NULL, SC_NEG, (c))
105 #define sc_and(a, b, c) sc_calc((a), (b), SC_AND, (c))
106 #define sc_or(a, b, c) sc_calc((a), (b), SC_OR, (c))
107 #define sc_xor(a, b, c) sc_calc((a), (b), SC_XOR, (c))
108 #define sc_not(a, c) sc_calc((a), NULL, SC_NOT, (c))
109 #define sc_mul(a, b, c) sc_calc((a), (b), SC_MUL, (c))
110 #define sc_div(a, b, c) sc_calc((a), (b), SC_DIV, (c))
111 #define sc_mod(a, b, c) sc_calc((a), (b), SC_MOD, (c))
112 #define sc_shl(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_SHL, (e))
113 #define sc_shr(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_SHR, (e))
114 #define sc_shrs(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_SHRS, (e))
115 #define sc_rot(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_ROT, (e))
118 * function declarations
120 const void *sc_get_buffer(void);
121 int sc_get_buffer_length(void);
123 void sign_extend(char *calc_buffer, ir_mode *mode);
125 /** create an value form a string representation */
126 void sc_val_from_str(const char *str, unsigned int len, void *buffer, ir_mode *mode);
128 /** create a value from a long */
129 void sc_val_from_long(long l, void *buffer);
131 /** create a value form an unsigned long */
132 void sc_val_from_ulong(unsigned long l, void *buffer);
134 /** converts a value to a long */
135 long sc_val_to_long(const void *val);
136 void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
137 void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
139 void sc_calc(const void *val1, const void *val2, unsigned op, void *buffer);
140 void sc_bitcalc(const void *val1, const void *val2, int radius, int sign, unsigned op, void *buffer);
141 int sc_comp(const void *val1, const void *val2);
143 int sc_get_highest_set_bit(const void *value);
144 int sc_get_lowest_set_bit(const void *value);
145 int sc_is_zero(const void *value);
146 int sc_is_negative(const void *value);
147 int sc_had_carry(void);
148 unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
151 * Converts a tarval into a string.
153 * @param val1 the value pointer
154 * @param bits number of valid bits in this value
155 * @param base output base
156 * @param signed_mode print it signed (only decimal mode supported
158 const char *sc_print(const void *val1, unsigned bits, enum base_t base, int signed_mode);
160 /** Initialize the strcalc module.
161 * Sets up internal data structures and constants
162 * After the first call subsequent calls have no effect
164 * @param precision_in_bytes Specifies internal precision to be used
165 * for calculations. The reason for being multiples of 8 eludes me
167 void init_strcalc(int precision_in_bytes);
168 void finish_strcalc(void);
169 int sc_get_precision(void);
171 #endif /* _STRCALC_H_ */