aboutsummaryrefslogtreecommitdiff
path: root/lib/libmpa/mpa_div.c
blob: 9593ace313665a0bc6d38455fe5b0fc5559b08e7 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 */
#include "mpa.h"

/*************************************************************
 *
 *   HELPER FUNCTIONS
 *
 *************************************************************/

/*------------------------------------------------------------
 *
 *  These functions have ARM assembler implementations
 *
 */
#if !defined(USE_ARM_ASM)

static mpa_dword_t __mpa_soft_div(mpa_dword_t num, mpa_word_t den_in,
				  mpa_word_t *rem)
{
	mpa_dword_t quot = 0, qbit = 1;
	mpa_dword_t den = den_in;

	while ((int64_t) den >= 0) {
		den <<= 1;
		qbit <<= 1;
	}

	while (qbit) {
		if (den <= num) {
			num -= den;
			quot += qbit;
		}
		den >>= 1;
		qbit >>= 1;
	}

	if (rem)
		*rem = (mpa_word_t) num;

	return quot;
}

/*  --------------------------------------------------------------------
 *  Function:   __mpa_div_dword
 *
 *  Calculates quotient and remainder of (n1*base + n0) / d.
 *  It is assumed that the quotient is small enough to fit a single word.
 */
mpa_word_t __mpa_div_dword(mpa_word_t n0,
			   mpa_word_t n1, mpa_word_t d, mpa_word_t *r)
{
#if defined(MPA_SUPPORT_DWORD_T)
	mpa_dword_t n;
	/*    mpa_dword_t tmp_q; */

	n = ((mpa_dword_t) n1 << WORD_SIZE) + n0;
	return __mpa_soft_div(n, d, r);
	/*    tmp_q = n / d; */
	/*    *r = (mpa_word_t)(n % d); */
	/*    return tmp_q; */
#else
#error write non-dword code for __mpa_div_dword
#endif
}

#endif /* USE_ARM_ASM */

/*  --------------------------------------------------------------------
 *  Function:   __mpa_div_q_r_internal
 *
 *  Finds the quotient and remainder when |op1| is divided by |op2|.
 *  q, r, op1 and op2 must all be distinct and not null.
 *  E.i. we get q and r such that |op1| = q*|op2| + r, 0<= r < |op2|.
 *  Assumptions: |op1| >= |op2| and |op2| >= 2^(WORD_SIZE)
 *
 */
static void __mpa_div_q_r_internal(mpanum q,
				   mpanum r,
				   const mpanum op1,
				   const mpanum op2, mpa_scratch_mem pool)
{
	mpa_word_t normshift;
	int base_diff;
	int i;
	int cmp_same;
	mpa_usize_t n;		/* size of op1 */
	mpa_usize_t t;		/* size of op2 */
	mpa_word_t w1;
	mpa_word_t w2;
	mpa_word_t w3;
	mpa_word_t w4;
	mpa_word_t w5;
	mpanum p;
	mpanum y;
	mpanum x;

	/*
	 *  get temp storage
	 */
	mpa_alloc_static_temp_var(&p, pool);
	mpa_alloc_static_temp_var(&y, pool);
	mpa_copy(y, op2);

	/*
	 * May need a large value for r since op1 may be an "oversized"
	 * value that is reduced. x is used for that internally and it's
	 * initial value is op1.
	 */
	mpa_alloc_static_temp_var(&x, pool);
	mpa_copy(x, op1);
	__mpanum_set_sign(x, MPA_POS_SIGN);
	__mpanum_set_sign(y, MPA_POS_SIGN);

	/*
	 *  Normalization
	 */
	normshift = 0;
	w1 = __mpanum_msw(y);
	while (w1 < ((mpa_word_t)1 << (WORD_SIZE - 1))) {
		normshift++;
		w1 <<= 1;
	}
	if (normshift) {
		mpa_shift_left(x, x, normshift);
		mpa_shift_left(y, y, normshift);
	}

	n = x->size;
	t = y->size;
	base_diff = (int)n - t;

	mpa_wipe(q);

	/*
	 * check if op1 >= op2*base^(base_diff)
	 */
	/* mpa_shift_left(y, y, base_diff * WORD_SIZE); */
	__mpa_shift_words_left(y, base_diff);

	i = __mpanum_size(y);
	cmp_same = 1;
	while (cmp_same != 0 && i > 0) {
		i--;
		cmp_same = (__mpanum_get_word(i, x) == __mpanum_get_word(i, y));
	}
	if (!cmp_same && (__mpanum_get_word(i, x) > __mpanum_get_word(i, y))) {
		q->d[base_diff] = 1;
		mpa_sub(x, x, y, pool);
	}

	/* start main division loop */
	i = x->size - 1;
	while (i >= (int)t) {
		if (__mpanum_get_word(i, x) ==
		    __mpanum_get_word(t + base_diff - 1, y)) {
			q->d[i - t] = WORD_ALL_BITS_ONE;
		} else {
			q->d[i - t] =
				__mpa_div_dword(__mpanum_get_word(i - 1, x),
						__mpanum_get_word(i, x),
						__mpanum_get_word(t + base_diff
								  - 1, y),
						NULL);
		}
		while (1) {
			/* set incoming carry to zero for all three ops */
			w1 = 0;
			w3 = 0;
			w5 = 0;
			__mpa_mul_add_word(q->d[i - t],
					   __mpanum_get_word(t + base_diff - 1,
							   y), &w2, &w1);

			if (w1 > __mpanum_get_word(i, x))
				goto loop_dec;

			__mpa_mul_add_word(q->d[i - t],
					   __mpanum_get_word(t + base_diff - 2,
							   y), &w4, &w3);

			__mpa_full_adder(w2, w3, &w3, &w5);
			w2 = 0;	/* used as carry */
			__mpa_full_adder(w1, w5, &w5, &w2);
			if (w2 || w5 > __mpanum_get_word(i, x))
				goto loop_dec;
			if (w5 < __mpanum_get_word(i, x))
				break;
			if (w3 > __mpanum_get_word(i - 1, x))
				goto loop_dec;
			if (w3 < __mpanum_get_word(i - 1, x))
				break;
			if (w4 > __mpanum_get_word(i - 2, x))
				goto loop_dec;
			break;
loop_dec:
			q->d[i - t]--;
		}

		/* mpa_shift_right(y, y, WORD_SIZE); */
		__mpa_shift_words_right(y, 1);
		base_diff--;
		mpa_mul_word(p, y, q->d[i - t], pool);
		mpa_sub(x, x, p, pool);
		if (__mpanum_sign(x) == MPA_NEG_SIGN) {
			mpa_add(x, x, y, pool);
			q->d[i - t]--;
		}
		i--;
	}
	/*
	 *  Find size of q
	 */
	i = n - t;
	while (i >= 0 && q->d[i] == 0)
		i--;
	q->size = i + 1;
	/*
	 *  Divide r by the normalization value and copy to output
	 */
	mpa_shift_right(x, x, normshift);
	mpa_copy(r, x);

	/*
	 *  release p, y, and x
	 */
	mpa_free_static_temp_var(&p, pool);
	mpa_free_static_temp_var(&y, pool);
	mpa_free_static_temp_var(&x, pool);
}

/*  --------------------------------------------------------------------
 *  Function:   __mpa_div_q_r_internal_word
 *
 *  Finds the quotient and remainder when |op1| is divided by |op2|, where
 *  op2 is a word.
 *  q, r and op1 must all be distinct and not null.
 *  E.i. we get q and r such that |op1| = q*|op2| + r, 0<= r < |op2|.
 *  Assumptions: |op1| >= |op2|.

 */
void __mpa_div_q_r_internal_word(mpanum q,
				 mpanum r,
				 const mpanum op1, const mpa_word_t op2)
{
	int pos1;
	mpa_word_t q_word;
	mpa_word_t r_word;
	mpa_word_t n1;
	mpa_word_t n0;
	int i;

	if (__mpanum_size(op1) == 1) {
		mpa_set_word(q, op1->d[0] / op2);
		mpa_set_word(r, op1->d[0] % op2);
		return;
	}
	mpa_copy(r, op1);
	mpa_set_word(q, 0);

	pos1 = (int)__mpanum_size(r) - 1;
	n1 = 0;
	n0 = r->d[pos1];
	while (pos1 >= 0) {
		q_word = __mpa_div_dword(n0, n1, op2, &r_word);
		q->d[pos1] = q_word;
		r->d[pos1] = r_word;
		n1 = r->d[pos1];
		n0 = r->d[--pos1];
	}
	/* set sizes of r and q */
	r->size = (r->d[0] == 0 ? 0 : 1);
	i = __mpanum_size(op1) - 1;
	while (i >= 0 && q->d[i] == 0)
		i--;
	q->size = i + 1;
}

/*  --------------------------------------------------------------------
 *  Function:   __mpa_div_q_r
 *
 *  Calculates the quotient and remainder when op1 is divided by op2.
 *  q, r, op1 and op2 must all be distinct and not null, except that op1
 *  may be equal to op2.
 *  There must be enough space allocated in q and r to handle the results.
 *  If op2 is zero a division with zero will be executed and the above layers
 *  can handle that as it pleases.
 *  The sign of q and r are shown in the table:
 *  __________________________________
 *  | Sign(q/r) | op1 >= 0 | op1 < 0 |
 *  |--------------------------------|
 *  | op2 > 0   |    +/+   |  -/-    |
 *  |--------------------------------|
 *  | op2 < 0   |    -/+   |  +/-    |
 *  |________________________________|
 */
void __mpa_div_q_r(mpanum q,
		   mpanum r,
		   const mpanum op1, const mpanum op2, mpa_scratch_mem pool)
{
	int q_sign;
	int cmp;

	if (__mpanum_is_zero(op1)) {
		mpa_set_word(q, 0);
		mpa_set_word(r, 0);
		return;
	}
	if (__mpanum_is_zero(op2)) {
		/* generate a divide by zero error */
		q_sign = 42 / op2->size;
		return;
	}

	q_sign =
	    (__mpanum_sign(op1) !=
	     __mpanum_sign(op2)) ? MPA_NEG_SIGN : MPA_POS_SIGN;
	cmp = __mpa_abs_cmp(op1, op2);
	if (cmp == 0) {
		mpa_set_word(q, 1);
		mpa_set_word(r, 0);
		return;
	}
	if (cmp > 0) {		/* |op1| > |op2| */
		if (__mpanum_size(op2) > 1)
			__mpa_div_q_r_internal(q, r, op1, op2, pool);
		else
			__mpa_div_q_r_internal_word(q, r, op1, op2->d[0]);
	} else {		/* |op1| < |op2| */
		mpa_set_word(q, 0);
		mpa_copy(r, op1);
		return;
	}
	__mpanum_set_sign(q, q_sign);
	__mpanum_set_sign(r, __mpanum_sign(op1));
}

/*************************************************************
 *
 *   LIB FUNCTIONS
 *
 *************************************************************/

/*  --------------------------------------------------------------------
 *  Function:   mpa_div
 *
 *  Returns the quotient and the remainder of op1 divided by op2.
 *   q and r must be distinct variables.
 *  This function returns q and r such that
 *  op1 = q*op2 + r
 *  where 0 <= r < |op2|
 */
void mpa_div(mpanum q,
	     mpanum r, const mpanum op1, const mpanum op2, mpa_scratch_mem pool)
{
	mpanum tmp_q;
	mpanum tmp_r;
	char mem_marker_q;
	char mem_marker_r;

	/* handle the case when q is one of the operands or zero */
	if (q == op1 || q == op2 || q == 0) {
		mpa_alloc_static_temp_var(&tmp_q, pool);
		mem_marker_q = 1;
	} else {
		tmp_q = q;
		mem_marker_q = 0;
	}

	/* handle the case when r is one of the operands or zero */
	if (r == op1 || r == op2 || r == 0) {
		mpa_alloc_static_temp_var(&tmp_r, pool);
		mem_marker_r = 1;
	} else {
		tmp_r = r;
		mem_marker_r = 0;
	}

	__mpa_div_q_r(tmp_q, tmp_r, op1, op2, pool);

	if (q != 0)
		mpa_copy(q, tmp_q);
	if (mem_marker_q)
		mpa_free_static_temp_var(&tmp_q, pool);
	if (r != 0)
		mpa_copy(r, tmp_r);
	if (mem_marker_r)
		mpa_free_static_temp_var(&tmp_r, pool);
}