aboutsummaryrefslogtreecommitdiff
path: root/dfoxtest.c
blob: aefdd5df13703538acdef23d8d5c55396192e1e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*#include <stdint.h>*/
typedef char             int8_t;
typedef unsigned char   uint8_t;
typedef short           int16_t;
typedef unsigned short uint16_t;
typedef long            int32_t;
typedef unsigned long  uint32_t;

/*#include <stdbool.h>*/
typedef uint8_t bool;
#ifndef true
	#define true 1
#endif
#ifndef false
	#define false 0
#endif

/*Defines*/
#define PREFIX_BITS_PER_BYTE 8
	/*Neither can be greater than 128*/
#define PREFIX_intsize     4
#define PREFIX_INTSIZE     8

/*Typedefs*/
typedef uint8_t PREFIX_INT[PREFIX_INTSIZE];
typedef int32_t PREFIX_int;

/*Globals*/
bool PREFIX_math_overflow;

/*==============================================================CODE=============================================================*/

void PREFIX_zero_int(PREFIX_INT *ptr)
{
	uint8_t i;

	for (i = 0; i < PREFIX_INTSIZE; i++)
		*ptr[i] = 0;
}

bool PREFIX_isneg(PREFIX_INT *in)
{
	return (*in[PREFIX_INTSIZE-1] & 0x80) >> 7;
}

void PREFIX_int_to_buf(PREFIX_INT *dest, PREFIX_int *src)
{
	PREFIX_int tmp;
	uint8_t i;

	tmp = *src;
	for (i = 0; i < PREFIX_intsize; i++)
		*dest[i] = (tmp >> (i*PREFIX_BITS_PER_BYTE)) & 0xFF;
}

void PREFIX_buf_to_int(PREFIX_int *dest, PREFIX_INT *src)
{
	PREFIX_int ret;
	uint8_t i;

	ret = 0;
	for (i = 0; i < PREFIX_INTSIZE; i++) {
		ret  += dest[i];
		ret <<= PREFIX_BITS_PER_BYTE;
	}

	*src[0] = ret;
}

void PREFIX_buf_to_buf(PREFIX_INT *dest, PREFIX_INT *src)
{
	uint8_t i;

	for (i = 0; i < PREFIX_INTSIZE; i++)
		*dest[i] = *src[i];
}

/*True to*/
bool PREFIX_equal(PREFIX_INT *left, PREFIX_INT *right)
{
	uint8_t i;

	for (i = 0; i < PREFIX_INTSIZE; i++)
		if (*left[i] != *right[i])
			return false;

	return true;
}

/*greater than*/
bool PREFIX_greater(PREFIX_INT *left, PREFIX_INT *right)
{
	uint8_t i;
	bool left_neg, right_neg;

	left_neg  = PREFIX_isneg(left);
	right_neg = PREFIX_isneg(right);

	if (left_neg || right_neg) {
		if (left_neg  && !right_neg)
			return false;
		else if (!left_neg && right_neg)
			return true;
	}

	for (i = 0; i < PREFIX_INTSIZE; i++) {
		if (*left[i] > *right[i])
			return true;
		else if (*left[i] < *right[i])
			return false;
	}

	return false;
}

/*less than*/
bool PREFIX_less(PREFIX_INT *left, PREFIX_INT *right)
{
	return !PREFIX_greater(left, right);
}

/*greater than or equal to*/
bool PREFIX_greater_or_equal(PREFIX_INT *left, PREFIX_INT *right)
{
	return PREFIX_greater(left, right) || PREFIX_equal(left, right);
}

/*less than or equal to*/
bool PREFIX_less_or_equal(PREFIX_INT *left, PREFIX_INT *right)
{
	return PREFIX_less(left, right) || PREFIX_equal(left, right);
}

/*Negatives not currently supported*/
void PREFIX_add(PREFIX_INT *dest, PREFIX_INT *src)
{
	/*operator*/
	uint16_t op;
	uint8_t i;
	bool dest_neg, src_neg;

	src_neg  = PREFIX_isneg(src);
	dest_neg = PREFIX_isneg(dest);

	if (src_neg || dest_neg) {
		PREFIX_math_overflow = true;
		return;
	}

	for (i = 0; i < PREFIX_INTSIZE; i++) {
		op        = *src[i];
		op       += *dest[i];
		*dest[i]  = op & 0xFF;

		op >>= PREFIX_BITS_PER_BYTE;
	}

	if (i == PREFIX_INTSIZE && op != 0)
		PREFIX_math_overflow = true;

	if (!(src_neg || dest_neg) && PREFIX_isneg(dest))
		PREFIX_math_overflow = true;
}

/*Negatives not currently supported*/
void PREFIX_sub(PREFIX_INT *dest, PREFIX_INT *src)
{
	uint16_t op;
	uint8_t i, j;
	bool dest_neg, src_neg;
			uint16_t tmp_op = 0;
			bool got_borrow = false;
	PREFIX_INT tmp_dest, tmp_src;

	src_neg  = PREFIX_isneg(src);
	dest_neg = PREFIX_isneg(dest);

	if (src_neg || dest_neg)
		PREFIX_math_overflow = true;

	PREFIX_buf_to_buf(&tmp_src, src);
	PREFIX_buf_to_buf(&tmp_dest, dest);
	for (i = 0; i < PREFIX_INTSIZE; i++) {
		op = tmp_dest[i];

		if (tmp_src[i] > tmp_dest[i]) {
			if (i == (PREFIX_INTSIZE-1))
				PREFIX_math_overflow = true;

			for (j = (i+1); j < PREFIX_INTSIZE; j++ )
				if (tmp_dest[j]) {
					tmp_dest[j] -= 1;
					tmp_op = 0xFF;
					got_borrow = true;
					break;
				}

			if (!got_borrow)
				PREFIX_math_overflow = true;

			for (j--; j > i; j--)
				tmp_src[j] = 0xFF;

			op += tmp_op;
		}

		op       -= tmp_src[i];
		*dest[i]  = op & 0xFF;
	}
}