summaryrefslogtreecommitdiffstats
path: root/pbn_cmp.c
blob: 72dc5b8a8200ff6d9ae1c7205d4e555a30823723 (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
/* ----------------------------------------------------------------------- *
 *
 *   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_cmp.c
 */

#include "pbnint.h"

int pbn_cmp(const struct pbn *s1, const struct pbn *s2)
{
    int lt = s1->minus ? 1 : -1;
    int gt = -lt;
    int i;

    if (s1->minus != s2->minus)
	return gt;

    if (s1->bits < s2->bits)
	return lt;
    else if (s1->bits > s2->bits)
	return gt;

    i = (s1->bits+PBN_LIMB_BITS-1)/PBN_LIMB_BITS;
    while (i >= 0) {
	if (s1->num[i] != s2->num[i])
	  return (s1->num[i] < s2->num[i]) ? lt : gt;
	i--;
    }

    return 0;
}

/* Compare the absolute values of s1 and s2 */
int pbn_abscmp(const struct pbn *s1, const struct pbn *s2)
{
    int i;

    if (s1->bits < s2->bits)
	return -1;
    else if (s1->bits > s2->bits)
	return 1;

    i = (s1->bits+PBN_LIMB_BITS-1)/PBN_LIMB_BITS;
    while (i >= 0) {
	if (s1->num[i] != s2->num[i])
	  return (s1->num[i] < s2->num[i]) ? -1 : 1;
	i--;
    }

    return 0;
}