Page 232 - ARM 64 Bit Assembly Language
P. 232
220 Chapter 7
293 }
294 return sum;
295 }
296
297 /* bigint_mul uses the algorithm from Section 7.2.5 */
298 bigint bigint_mul(bigint l, bigint r) {
299 int i, negative = 0;
300 /* the result may require the sum
301 of the number of chunks in l and r */
302 bigint sum = bigint_from_int(0);
303 bigint tmp1, tmp2;
304 /* make sure the right operand is not negative */
305 if (r->blks[r->size-1] & ((bigchunk)1<<(BITSPERCHUNK-1))) {
306 negative = 1;
307 r = bigint_negate(r); /* make negated copy of r */
308 }
309 for (i = 0; i < r->size; i++) {
310 tmp1 = bigint_mul_uint(l,r->blks[i]);
311 tmp2 = bigint_shift_left_chunk(tmp1,i);
312 bigint_free(tmp1);
313 tmp1 = sum;
314 sum = bigint_adc(sum,tmp2,0);
315 bigint_free(tmp1);
316 bigint_free(tmp2);
317 }
318 if (negative) {
319 tmp1 = sum; /* copy original */
320 sum = bigint_negate(sum); /* create complement */
321 bigint_free(tmp1); /* free original */
322 bigint_free(r);
323 }
324 return sum;
325 }
326
327 /* bigint_div uses the algorithm from Section 7.3.2. */
328 bigint bigint_div(bigint l, bigint r) {
329 bigint lt = bigint_trim(l);
330 bigint rt = bigint_trim(r);
331 bigint tmp,q = bigint_from_int(0);
332 int shift, chunkshift, negative = 0;
333 if (lt->size < rt->size) {
334 bigint_free(lt);
335 bigint_free(rt);
336 return q;
337 }
338 /* make sure the right operand is not negative */
339 if (r->blks[r->size-1] & ((bigchunk)1<<(BITSPERCHUNK-1))) {
340 negative = 1; /* track sign of result */
341 tmp = rt;