forked from libtom/libtomfloat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpf_cmp.c
58 lines (51 loc) · 1.49 KB
/
mpf_cmp.c
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
/* LibTomFloat, multiple-precision floating-point library
*
* LibTomFloat is a library that provides multiple-precision
* floating-point artihmetic as well as trigonometric functionality.
*
* This library requires the public domain LibTomMath to be installed.
*
* This library is free for all purposes without any express
* gurantee it works
*
* Tom St Denis, [email protected], http://float.libtomcrypt.org
*/
#include <tomfloat.h>
int mpf_cmp(mp_float *a, mp_float *b)
{
int za, zb, sa, sb;
/* if one is zero than we early out */
za = mp_iszero(&(a->mantissa));
sa = a->mantissa.sign;
zb = mp_iszero(&(b->mantissa));
sb = b->mantissa.sign;
if (za == MP_YES && zb == MP_NO) {
/* result depends on b */
if (sb == MP_NEG) {
return MP_GT;
} else {
return MP_LT;
}
} else if (za == MP_NO && zb == MP_YES) {
/* result depends on a */
if (sa == MP_NEG) {
return MP_LT;
} else {
return MP_GT;
}
}
/* compare the signs */
if (sa == MP_NEG && sb == MP_ZPOS) {
return MP_LT;
} else if (sa == MP_ZPOS && sb == MP_NEG) {
return MP_GT;
}
/* they're both non-zero, the same sign and normalized, compare the exponents */
if (a->exp > b->exp) {
return (sa == MP_NEG) ? MP_LT : MP_GT;
} else if (a->exp < b->exp) {
return (sa == MP_NEG) ? MP_GT : MP_LT;
}
/* same exponent and sign, compare mantissa */
return mp_cmp(&(a->mantissa), &(b->mantissa));
}