after multiple objections of libtom users [1], we decided to change licensing
[libtomfloat.git] / mpf_cmp.c
blob4a58e3463c307d26a9946d7380ef71192d6babd2
1 /* LibTomFloat, multiple-precision floating-point library
3 * LibTomFloat is a library that provides multiple-precision
4 * floating-point artihmetic as well as trigonometric functionality.
6 * This library requires the public domain LibTomMath to be installed.
7 *
8 * This library is free for all purposes without any express
9 * gurantee it works
11 * Tom St Denis, tomstdenis@iahu.ca, http://float.libtomcrypt.org
13 #include <tomfloat.h>
15 int mpf_cmp(mp_float *a, mp_float *b)
17 int za, zb, sa, sb;
19 /* if one is zero than we early out */
20 za = mp_iszero(&(a->mantissa));
21 sa = a->mantissa.sign;
22 zb = mp_iszero(&(b->mantissa));
23 sb = b->mantissa.sign;
25 if (za == MP_YES && zb == MP_NO) {
26 /* result depends on b */
27 if (sb == MP_NEG) {
28 return MP_GT;
29 } else {
30 return MP_LT;
32 } else if (za == MP_NO && zb == MP_YES) {
33 /* result depends on a */
34 if (sa == MP_NEG) {
35 return MP_LT;
36 } else {
37 return MP_GT;
41 /* compare the signs */
42 if (sa == MP_NEG && sb == MP_ZPOS) {
43 return MP_LT;
44 } else if (sa == MP_ZPOS && sb == MP_NEG) {
45 return MP_GT;
48 /* they're both non-zero, the same sign and normalized, compare the exponents */
49 if (a->exp > b->exp) {
50 return (sa == MP_NEG) ? MP_LT : MP_GT;
51 } else if (a->exp < b->exp) {
52 return (sa == MP_NEG) ? MP_GT : MP_LT;
55 /* same exponent and sign, compare mantissa */
56 return mp_cmp(&(a->mantissa), &(b->mantissa));