1 /* cmp.c -- Default comparison functions.
2
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2010, 2011,
4 Spaces project, Inria Lorraine
5 and Salsa project, INRIA Rocquencourt,
6 and Arenaire project, Inria Rhone-Alpes, France
7 and Lab. ANO, USTL (Univ. of Lille), France
8
9 This file is part of the MPFI Library.
10
11 The MPFI Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or (at your
14 option) any later version.
15
16 The MPFI Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with the MPFI Library; see the file COPYING.LIB. If not, write to
23 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
24 MA 02110-1301, USA. */
25
26 #include "mpfi-impl.h"
27
28 /* Warning: there is no canonical order => */
29 /* interval comparison is not clearly defined */
30
31 /* Customizable comparison functions */
32 /* Since the mpfi_cmp_* are based on mpfi_cmp, only mpfi_cmp needs to be modified */
33
34 int (*mpfi_cmp) (mpfi_srcptr, mpfi_srcptr) = mpfi_cmp_default;
35 int (*mpfi_cmp_d) (mpfi_srcptr, const double) = mpfi_cmp_d_default;
36 int (*mpfi_cmp_ui) (mpfi_srcptr, const unsigned long) = mpfi_cmp_ui_default;
37 int (*mpfi_cmp_si) (mpfi_srcptr, const long) = mpfi_cmp_si_default;
38 int (*mpfi_cmp_z) (mpfi_srcptr, mpz_srcptr) = mpfi_cmp_z_default;
39 int (*mpfi_cmp_q) (mpfi_srcptr, mpq_srcptr) = mpfi_cmp_q_default;
40 int (*mpfi_cmp_fr) (mpfi_srcptr, mpfr_srcptr) = mpfi_cmp_fr_default;
41
42 /* Default comparison functions */
43 /* They return 1 if one (at least) of their operands is invalid (contains NaN) */
44
45 int
mpfi_cmp_default(mpfi_srcptr a,mpfi_srcptr b)46 mpfi_cmp_default (mpfi_srcptr a, mpfi_srcptr b)
47 {
48 if ( MPFI_NAN_P (a) || MPFI_NAN_P (b) )
49 return 1;
50 return ( (mpfr_cmp (&(a->right), &(b->left)) < 0) ? -1
51 :(mpfr_cmp (&(b->right), &(a->left)) < 0) );
52 }
53
54 int
mpfi_cmp_d_default(mpfi_srcptr a,const double b)55 mpfi_cmp_d_default (mpfi_srcptr a, const double b)
56 {
57 int res = 0;
58 mpfi_t tmp;
59
60 mpfi_init2 (tmp, mpfi_get_prec (a));
61 mpfi_set_d (tmp, b);
62 res = mpfi_cmp (a, tmp);
63 MPFI_CLEAR (tmp);
64
65 return (res);
66 }
67
68 int
mpfi_cmp_ui_default(mpfi_srcptr a,const unsigned long b)69 mpfi_cmp_ui_default (mpfi_srcptr a, const unsigned long b)
70 {
71 int res = 0;
72 mpfi_t tmp;
73
74 mpfi_init2 (tmp, mpfi_get_prec (a));
75 mpfi_set_ui (tmp, b);
76 res = mpfi_cmp (a, tmp);
77 MPFI_CLEAR (tmp);
78
79 return (res);
80 }
81
82 int
mpfi_cmp_si_default(mpfi_srcptr a,const long b)83 mpfi_cmp_si_default (mpfi_srcptr a, const long b)
84 {
85 int res = 0;
86 mpfi_t tmp;
87
88 mpfi_init2 (tmp, mpfi_get_prec (a));
89 mpfi_set_si (tmp, b);
90 res = mpfi_cmp (a, tmp);
91 MPFI_CLEAR (tmp);
92
93 return (res);
94 }
95
96 int
mpfi_cmp_z_default(mpfi_srcptr a,mpz_srcptr b)97 mpfi_cmp_z_default (mpfi_srcptr a, mpz_srcptr b)
98 {
99 int res = 0;
100 mpfi_t tmp;
101
102 mpfi_init2 (tmp, mpfi_get_prec (a));
103 mpfi_set_z (tmp, b);
104 res = mpfi_cmp (a, tmp);
105 MPFI_CLEAR (tmp);
106
107 return (res);
108 }
109
110 int
mpfi_cmp_q_default(mpfi_srcptr a,mpq_srcptr b)111 mpfi_cmp_q_default (mpfi_srcptr a, mpq_srcptr b)
112 {
113 int res = 0;
114 mpfi_t tmp;
115
116 mpfi_init2 (tmp, mpfi_get_prec (a));
117 mpfi_set_q (tmp, b);
118 res = mpfi_cmp (a, tmp);
119 MPFI_CLEAR (tmp);
120
121 return (res);
122 }
123
124 int
mpfi_cmp_fr_default(mpfi_srcptr a,mpfr_srcptr b)125 mpfi_cmp_fr_default (mpfi_srcptr a, mpfr_srcptr b)
126 {
127 int res = 0;
128 mpfi_t tmp;
129
130 mpfi_init2 (tmp, mpfi_get_prec (a));
131 mpfi_set_fr (tmp, b);
132 res = mpfi_cmp (a, tmp);
133 MPFI_CLEAR (tmp);
134
135 return (res);
136 }
137