4 * strcalc -- calculations using strings
5 * Provides basic mathematical operations on values represented as strings
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.
21 #ifdef STRCALC_DEBUG_ALL /* switch on all debug options */
22 # ifndef STRCALC_DEBUG
23 # define STRCALC_DEBUG /* switch on debug output */
25 # ifndef STRCALC_DEBUG_PRINTCOMP /* print arguments and result of each computation */
26 # define STRCALC_DEBUG_PRINTCOMP
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) */
31 # ifndef STRCALC_DEBUG_GROUPPRINT
32 # define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
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)
45 * constants, typedefs, enums
48 #define DEFAULT_PRECISION_IN_BYTES 8
70 * Possible operations on integer values.
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 */
90 * The output mode for ntger values.
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 */
101 * definitions and macros
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)
119 * function declarations
121 const void *sc_get_buffer(void);
122 const int sc_get_buffer_length(void);
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);
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);
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);
142 * Converts a tarval into a string.
144 * @param val1 the value pointer
145 * @param bits number of valid bits in this value
146 * @param base output base
148 const char *sc_print(const void *val1, unsigned bits, enum base_t base);
150 void init_strcalc(int precision_in_bytes);
151 int get_precision(void);
153 #endif /* _STRCALC_H_ */