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.
26 #ifdef STRCALC_DEBUG_ALL /* switch on all debug options */
27 # ifndef STRCALC_DEBUG
28 # define STRCALC_DEBUG /* switch on debug output */
30 # ifndef STRCALC_DEBUG_PRINTCOMP /* print arguments and result of each computation */
31 # define STRCALC_DEBUG_PRINTCOMP
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) */
36 # ifndef STRCALC_DEBUG_GROUPPRINT
37 # define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
42 * constants, typedefs, enums
45 #define SC_DEFAULT_PRECISION 64
67 * Possible operations on integer values.
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 */
87 * The output mode for ntger values.
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 */
98 * definitions and macros
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))
116 * function declarations
118 const void *sc_get_buffer(void);
119 const int sc_get_buffer_length(void);
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);
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);
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);
140 * Converts a tarval into a string.
142 * @param val1 the value pointer
143 * @param bits number of valid bits in this value
144 * @param base output base
146 const char *sc_print(const void *val1, unsigned bits, enum base_t base);
148 /** Init strcalc module
149 * Sets up internal data structures and constants
150 * After the first call subsequent calls have no effect
152 * @param precision_in_bytes Specifies internal precision to be used
153 * for calculations. The reason for being multiples of 8 eludes me
155 void init_strcalc(int precision_in_bytes);
156 void finish_strcalc(void);
157 int sc_get_precision(void);
159 #endif /* _STRCALC_H_ */