1 /* Test file for mpfr_copysign, mpfr_setsign and mpfr_signbit.
2
3 Copyright 2004, 2006-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 #include "mpfr-test.h"
24
25 static void
copysign_variant(mpfr_ptr z,mpfr_srcptr x,mpfr_srcptr y,mpfr_rnd_t rnd_mode,int k)26 copysign_variant (mpfr_ptr z, mpfr_srcptr x, mpfr_srcptr y,
27 mpfr_rnd_t rnd_mode, int k)
28 {
29 mpfr_srcptr p;
30 int a = 0, b = 0, c = 0;
31
32 /* invalid sign, to test that the sign is always correctly set */
33 MPFR_SIGN (z) = 0;
34
35 if (k >= 8)
36 {
37 MPFR_ASSERTN (MPFR_PREC (z) >= MPFR_PREC (x));
38 mpfr_set (z, x, MPFR_RNDN);
39 p = z;
40 k -= 8;
41 }
42 else
43 p = x;
44
45 mpfr_clear_flags ();
46 switch (k)
47 {
48 case 0:
49 mpfr_copysign (z, p, y, rnd_mode);
50 return;
51 case 1:
52 (mpfr_copysign) (z, p, y, rnd_mode);
53 return;
54 case 2:
55 #ifdef IGNORE_CPP_COMPAT
56 #pragma GCC diagnostic push
57 #pragma GCC diagnostic ignored "-Wc++-compat"
58 #endif
59 mpfr_copysign ((a++, VOIDP_CAST(z)),
60 (b++, VOIDP_CAST(p)),
61 (c++, VOIDP_CAST(y)), rnd_mode);
62 #ifdef IGNORE_CPP_COMPAT
63 #pragma GCC diagnostic pop
64 #endif
65 MPFR_ASSERTN (a == 1);
66 MPFR_ASSERTN (b == 1);
67 MPFR_ASSERTN (c == 1);
68 return;
69 case 3:
70 mpfr_setsign (z, p, mpfr_signbit (y), rnd_mode);
71 return;
72 case 4:
73 mpfr_setsign (z, p, (mpfr_signbit) (y), rnd_mode);
74 return;
75 case 5:
76 (mpfr_setsign) (z, p, mpfr_signbit (y), rnd_mode);
77 return;
78 case 6:
79 (mpfr_setsign) (z, p, (mpfr_signbit) (y), rnd_mode);
80 return;
81 case 7:
82 #ifdef IGNORE_CPP_COMPAT
83 #pragma GCC diagnostic push
84 #pragma GCC diagnostic ignored "-Wc++-compat"
85 #endif
86 mpfr_setsign ((a++, VOIDP_CAST(z)),
87 (b++, VOIDP_CAST(p)),
88 mpfr_signbit ((c++, VOIDP_CAST(y))), rnd_mode);
89 #ifdef IGNORE_CPP_COMPAT
90 #pragma GCC diagnostic pop
91 #endif
92 MPFR_ASSERTN (a == 1);
93 MPFR_ASSERTN (b == 1);
94 MPFR_ASSERTN (c == 1);
95 return;
96 }
97 }
98
99 int
main(void)100 main (void)
101 {
102 mpfr_t x, y, z;
103 int i, j, k;
104
105 tests_start_mpfr ();
106
107 mpfr_init (x);
108 mpfr_init (y);
109 mpfr_init (z);
110
111 for (i = 0; i <= 1; i++)
112 for (j = 0; j <= 1; j++)
113 for (k = 0; k < 16; k++)
114 {
115 mpfr_set_nan (x);
116 i ? MPFR_SET_NEG (x) : MPFR_SET_POS (x);
117 mpfr_set_nan (y);
118 j ? MPFR_SET_NEG (y) : MPFR_SET_POS (y);
119 copysign_variant (z, x, y, MPFR_RNDN, k);
120 if (MPFR_SIGN (z) != MPFR_SIGN (y) || !mpfr_nanflag_p ())
121 {
122 printf ("Error in mpfr_copysign (%cNaN, %cNaN)\n",
123 i ? '-' : '+', j ? '-' : '+');
124 exit (1);
125 }
126
127 mpfr_set_si (x, i ? -1250 : 1250, MPFR_RNDN);
128 mpfr_set_nan (y);
129 j ? MPFR_SET_NEG (y) : MPFR_SET_POS (y);
130 copysign_variant (z, x, y, MPFR_RNDN, k);
131 if (i != j)
132 mpfr_neg (x, x, MPFR_RNDN);
133 if (! mpfr_equal_p (z, x) || mpfr_nanflag_p ())
134 {
135 printf ("Error in mpfr_copysign (%c1250, %cNaN)\n",
136 i ? '-' : '+', j ? '-' : '+');
137 exit (1);
138 }
139
140 mpfr_set_si (x, i ? -1250 : 1250, MPFR_RNDN);
141 mpfr_set_si (y, j ? -1717 : 1717, MPFR_RNDN);
142 copysign_variant (z, x, y, MPFR_RNDN, k);
143 if (i != j)
144 mpfr_neg (x, x, MPFR_RNDN);
145 if (! mpfr_equal_p (z, x) || mpfr_nanflag_p ())
146 {
147 printf ("Error in mpfr_copysign (%c1250, %c1717)\n",
148 i ? '-' : '+', j ? '-' : '+');
149 exit (1);
150 }
151 }
152
153 mpfr_clear (x);
154 mpfr_clear (y);
155 mpfr_clear (z);
156
157 tests_end_mpfr ();
158 return 0;
159 }
160