strcalc.h 4.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  /****h* tools/strcalc
 *
 * NAME
 *   strcalc -- calculations using strings
 *   Provides basic mathematical operations on values represented as strings
 *
 * AUTHORS
 *   Matthias Heil
 *
 * DESCRIPTION
 *    The module uses a string to represent values, and provides operations
 *   to perform calculations with these values.
 *    Results are stored in an internal buffer, so you have to make a copy
 *   of them if you need to store the result.
 *
 ******/

#ifndef _STRCALC_H_
#define _STRCALC_H_

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifdef STRCALC_DEBUG_ALL             /* switch on all debug options */
#  ifndef STRCALC_DEBUG
#    define STRCALC_DEBUG            /* switch on debug output */
#  endif
#  ifndef STRCALC_DEBUG_PRINTCOMP    /* print arguments and result of each computation */
#    define STRCALC_DEBUG_PRINTCOMP
#  endif
#  ifndef STRCALC_DEBUG_FULLPRINT
#    define STRCALC_DEBUG_FULLPRINT  /* print full length of values (e.g. 128 bit instead of 64 bit using default init) */
#  endif
#  ifndef STRCALC_DEBUG_GROUPPRINT
#    define STRCALC_DEBUG_GROUPPRINT /* print spaces after each 8 bits */
#  endif
#endif

/*
 * constants, typedefs, enums
 */

40
#define SC_DEFAULT_PRECISION 64
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

enum {
  SC_0 = 0,
  SC_1,
  SC_2,
  SC_3,
  SC_4,
  SC_5,
  SC_6,
  SC_7,
  SC_8,
  SC_9,
  SC_A,
  SC_B,
  SC_C,
  SC_D,
  SC_E,
Till Riedel's avatar
Till Riedel committed
58
  SC_F
59
60
};

61
62
63
/**
 * Possible operations on integer values.
 */
64
typedef enum {
65
66
67
68
69
70
71
72
73
74
75
76
77
  SC_ADD = 0,		/**< Addition */
  SC_SUB,		/**< Substraction */
  SC_NEG,		/**< Unary Minus */
  SC_MUL,		/**< Multiplication */
  SC_DIV,		/**< Integer Division (with rounding toward zero ?) */
  SC_MOD,		/**< Devision Remainder */
  SC_SHL,		/**< Left Shift */
  SC_SHR,		/**< Logical (unsigned) Right Shift */
  SC_SHRS,		/**< Arithmetic (signed) Right Shift */
  SC_ROT,		/**< Rotation (both directions) */
  SC_AND,		/**< Bitwise And */
  SC_OR,		/**< Bitwise Or */
  SC_NOT,		/**< Bitwise Not */
78
79
  SC_XOR,		/**< Bitwise Exclusive Or */
} sc_op_t;
80

81
82
83
84
/**
 * The output mode for ntger values.
 */
enum base_t {
Michael Beck's avatar
Michael Beck committed
85
86
  SC_hex,	/**< hexadecimal output with small letters */
  SC_HEX,	/**< hexadecimal output with BIG letters */
87
88
  SC_DEC,	/**< decimal output */
  SC_OCT,	/**< octal output */
Till Riedel's avatar
Till Riedel committed
89
  SC_BIN	/**< binary output */
90
91
};

92
/*
93
 * definitions and macros
94
 */
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#define sc_add(a, b, c) sc_calc((a), (b), SC_ADD, (c))
#define sc_sub(a, b, c) sc_calc((a), (b), SC_SUB, (c))
#define sc_neg(a, c) sc_calc((a), NULL, SC_NEG, (c))
#define sc_and(a, b, c) sc_calc((a), (b), SC_AND, (c))
#define sc_or(a, b, c) sc_calc((a), (b), SC_OR, (c))
#define sc_xor(a, b, c) sc_calc((a), (b), SC_XOR, (c))
#define sc_not(a, c) sc_calc((a), NULL, SC_NOT, (c))
#define sc_mul(a, b, c) sc_calc((a), (b), SC_MUL, (c))
#define sc_div(a, b, c) sc_calc((a), (b), SC_DIV, (c))
#define sc_mod(a, b, c) sc_calc((a), (b), SC_MOD, (c))
#define sc_shl(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_SHL, (e))
#define sc_shr(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_SHR, (e))
#define sc_shrs(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_SHRS, (e))
#define sc_rot(a, b, c, d, e) sc_bitcalc((a), (b), (c), (d), SC_ROT, (e))
109

110
/*
111
 * function declarations
112
 */
113
114
115
const void *sc_get_buffer(void);
const int sc_get_buffer_length(void);

116
117
118
void sc_val_from_str(const char *str, unsigned int len, void *buffer);
void sc_val_from_long(long l, void *buffer);
void sc_val_from_ulong(unsigned long l, void *buffer);
119
long sc_val_to_long(const void *val);
120
121
void sc_min_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
void sc_max_from_bits(unsigned int num_bits, unsigned int sign, void *buffer);
122

123
124
void sc_calc(const void *val1, const void *val2, unsigned op, void *buffer);
void sc_bitcalc(const void *val1, const void *val2, int radius, int sign, unsigned op, void *buffer);
125
126
int  sc_comp(const void *val1, const void *val2);

Matthias Heil's avatar
Matthias Heil committed
127
128
int sc_get_highest_set_bit(const void *value);
int sc_get_lowest_set_bit(const void *value);
129
130
131
int sc_is_zero(const void *value);
int sc_is_negative(const void *value);
int sc_had_carry(void);
Matthias Heil's avatar
Matthias Heil committed
132
unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs);
133

134
135
136
137
138
139
140
141
/**
 * Converts a tarval into a string.
 *
 * @param val1		the value pointer
 * @param bits		number of valid bits in this value
 * @param base		output base
 */
const char *sc_print(const void *val1, unsigned bits, enum base_t base);
142

143
144
145
146
147
148
149
/** Init strcalc module
 * Sets up internal data structures and constants
 * After the first call subsequent calls have no effect
 *
 * @param precision_in_bytes Specifies internal precision to be used
 *   for calculations. The reason for being multiples of 8 eludes me
 */
Matthias Heil's avatar
Matthias Heil committed
150
void init_strcalc(int precision_in_bytes);
151
int sc_get_precision(void);
Matthias Heil's avatar
Matthias Heil committed
152

153
#endif /* _STRCALC_H_ */