summaryrefslogtreecommitdiffstats
path: root/pbn_muls.c
blob: 9a4532832c1b10c9fb44b8234124905f2a62c90c (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
/* ----------------------------------------------------------------------- *
 *
 *   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.
 *
 * ----------------------------------------------------------------------- */

/*
 * pbn_muls.c
 *
 * Multiply a PBN with a single limb number.
 */

#include "pbnint.h"

struct pbn *pbn_muls(struct pbn *s1, pbn_slimb_t s2)
{
    size_t i, len;
    struct pbn *d;
    pbn_limb_t s2u, c, *s1p, *dp;
    pbn_2limb_t t;

    if (unlikely(s1->bits == 0)) {
	return s1;		/* s1 == 0 */
    }

    len = (s1->bits+PBN_LIMB_BITS-1) >> PBN_LIMB_SHIFT;

    d = pbn_new(len+1);

    d->minus = s1->minus;
    s2u = s2;
    if (s2 < 0) {
        s2u = -s2;
        d->minus ^= 1;
    }

    c = 0;
    s1p = s1->num;
    dp  = d->num;
    for (i = 0; i < len; i++) {
	t = (pbn_2limb_t)*s1p++ * s2u + c;
        *dp++ = (pbn_limb_t)t;
        c = t >> PBN_LIMB_BITS;
    }
    *dp = c;

    pbn_free(s1);

    return pbn_adjust_bits(d);
}