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 * constants, typedefs, enums
40 #define SC_DEFAULT_PRECISION 64
62 * Possible operations on integer values.
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 */
82 * The output mode for ntger values.
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 */
93 * definitions and macros
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))
111 * function declarations
113 const void *sc_get_buffer(void);
114 const int sc_get_buffer_length(void);
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);
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);
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);
135 * Converts a tarval into a string.
137 * @param val1 the value pointer
138 * @param bits number of valid bits in this value
139 * @param base output base
141 const char *sc_print(const void *val1, unsigned bits, enum base_t base);
143 /** Init strcalc module
144 * Sets up internal data structures and constants
145 * After the first call subsequent calls have no effect
147 * @param precision_in_bytes Specifies internal precision to be used
148 * for calculations. The reason for being multiples of 8 eludes me
150 void init_strcalc(int precision_in_bytes);
151 int sc_get_precision(void);
153 #endif /* _STRCALC_H_ */