summaryrefslogtreecommitdiffstats
path: root/pbn_and.c
blob: ec81ec5df377e056838c1c6df0b6986d6aa89dca (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.
 *
 * ----------------------------------------------------------------------- */

/*
 * pbn_and.c
 */

#include <string.h>
#include "pbnint.h"

struct pbn *pbn_and(struct pbn *s1, struct pbn *s2)
{
    struct pbn *d;
    int i, len;

    if (s1->bits == 0) {
	pbn_free(s2);
	return s1;
    } else if (s2->bits == 0) {
	pbn_free(s1);
	return s2;
    }

    if (s1->ref > s2->ref) {
	struct pbn *t = s1;
	s1 = s2;
	s2 = t;
    }

    d = pbn_cow(s1, s1->len);
    len = d->len;
    if (s2->len < len) {
	len = s2->len;
	memset(&d->num[len], 0, (d->len-len)*sizeof(pbn_limb_t));
    }

    for (i = 0; i < d->len; i++)
	d->num[i] &= s2->num[len];

    d->minus &= s2->minus;

    pbn_free(s2);
    return pbn_adjust_bits(d);
}