summaryrefslogtreecommitdiffstats
path: root/mpn_mul.c
blob: 5d4477b6f4dbffd3da57b5dd60ae5e75ec1fbc21 (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
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2007 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation, Inc.,
 *   59 Temple Place Ste 330, Boston MA 02111-1307, USA; version 2.1,
 *   incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * mpn_mul.c
 */

#include "mpnint.h"

struct mpn *mpn_mul(struct mpn *s1, struct mpn *s2)
{
    size_t bits, len;
    struct mpn *d;
    int i, j;
    mpn_atom_t x1;
    mpn_2atom_t c;
    
    bits = s1->bits + s2->bits;
    len = (bits+MPN_ATOM_BITS-1)/MPN_ATOM_BITS;
    
    d = mpn_new(len);
    d->bits = bits;

    for (i = 0; i < s1->len; i++) {
	x1 = s1->num[i];
	c = 0;
	for (j = 0; j < s2->len; j++) {
	    c += (mpn_2atom_t)x1 * s2->num[j];
	    d->num[i+j] += (mpn_atom_t)c;
	    c >>= MPN_ATOM_BITS;
	}
	if (c)
	    d->num[i+j] += (mpn_atom_t)c;
    }
    d->minus = s1->minus ^ s2->minus;

    mpn_free(s1);
    mpn_free(s2);

    _mpn_adjust_bits(d);

    return d;
}