1 /* Test iscanonical and canonicalizel for ldbl-128ibm.
2    Copyright (C) 2016-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <float.h>
20 #include <math.h>
21 #include <math_ldbl.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 
25 struct test
26 {
27   double hi, lo;
28   bool canonical;
29 };
30 
31 static const struct test tests[] =
32   {
33     { __builtin_nan (""), 0.0, true },
34     { __builtin_nan (""), DBL_MAX, true },
35     { __builtin_nan (""), __builtin_inf (), true },
36     { __builtin_nan (""), __builtin_nan (""), true },
37     { __builtin_nan (""), __builtin_nans (""), true },
38     { __builtin_nans (""), 0.0, true },
39     { __builtin_nans (""), DBL_MAX, true },
40     { __builtin_nans (""), __builtin_inf (), true },
41     { __builtin_nans (""), __builtin_nan (""), true },
42     { __builtin_nans (""), __builtin_nans (""), true },
43     { __builtin_inf (), 0.0, true },
44     { __builtin_inf (), -0.0, true },
45     { -__builtin_inf (), 0.0, true },
46     { -__builtin_inf (), -0.0, true },
47     { __builtin_inf (), DBL_TRUE_MIN, false },
48     { __builtin_inf (), -DBL_TRUE_MIN, false },
49     { -__builtin_inf (), DBL_TRUE_MIN, false },
50     { -__builtin_inf (), -DBL_TRUE_MIN, false },
51     { __builtin_inf (), DBL_MIN, false },
52     { __builtin_inf (), -DBL_MIN, false },
53     { -__builtin_inf (), DBL_MIN, false },
54     { -__builtin_inf (), -DBL_MIN, false },
55     { __builtin_inf (), __builtin_inf (), false },
56     { __builtin_inf (), -__builtin_inf (), false },
57     { -__builtin_inf (), __builtin_inf (), false },
58     { -__builtin_inf (), -__builtin_inf (), false },
59     { __builtin_inf (), __builtin_nan (""), false },
60     { __builtin_inf (), -__builtin_nan (""), false },
61     { -__builtin_inf (), __builtin_nan (""), false },
62     { -__builtin_inf (), -__builtin_nan (""), false },
63     { 0.0, 0.0, true },
64     { 0.0, -0.0, true },
65     { -0.0, 0.0, true },
66     { -0.0, -0.0, true },
67     { 0.0, DBL_TRUE_MIN, false },
68     { 0.0, -DBL_TRUE_MIN, false },
69     { -0.0, DBL_TRUE_MIN, false },
70     { -0.0, -DBL_TRUE_MIN, false },
71     { 0.0, DBL_MAX, false },
72     { 0.0, -DBL_MAX, false },
73     { -0.0, DBL_MAX, false },
74     { -0.0, -DBL_MAX, false },
75     { 0.0, __builtin_inf (), false },
76     { 0.0, -__builtin_inf (), false },
77     { -0.0, __builtin_inf (), false },
78     { -0.0, -__builtin_inf (), false },
79     { 0.0, __builtin_nan (""), false },
80     { 0.0, -__builtin_nan (""), false },
81     { -0.0, __builtin_nan (""), false },
82     { -0.0, -__builtin_nan (""), false },
83     { 1.0, 0.0, true },
84     { 1.0, -0.0, true },
85     { -1.0, 0.0, true },
86     { -1.0, -0.0, true },
87     { 1.0, DBL_TRUE_MIN, true },
88     { 1.0, -DBL_TRUE_MIN, true },
89     { -1.0, DBL_TRUE_MIN, true },
90     { -1.0, -DBL_TRUE_MIN, true },
91     { 1.0, DBL_MAX, false },
92     { 1.0, -DBL_MAX, false },
93     { -1.0, DBL_MAX, false },
94     { -1.0, -DBL_MAX, false },
95     { 1.0, __builtin_inf (), false },
96     { 1.0, -__builtin_inf (), false },
97     { -1.0, __builtin_inf (), false },
98     { -1.0, -__builtin_inf (), false },
99     { 1.0, __builtin_nan (""), false },
100     { 1.0, -__builtin_nan (""), false },
101     { -1.0, __builtin_nan (""), false },
102     { -1.0, -__builtin_nan (""), false },
103     { 0x1p1023, 0x1.1p969, true },
104     { 0x1p1023, -0x1.1p969, true },
105     { -0x1p1023, 0x1.1p969, true },
106     { -0x1p1023, -0x1.1p969, true },
107     { 0x1p1023, 0x1.1p970, false },
108     { 0x1p1023, -0x1.1p970, false },
109     { -0x1p1023, 0x1.1p970, false },
110     { -0x1p1023, -0x1.1p970, false },
111     { 0x1p1023, 0x1p970, true },
112     { 0x1p1023, -0x1p970, true },
113     { -0x1p1023, 0x1p970, true },
114     { -0x1p1023, -0x1p970, true },
115     { 0x1.0000000000001p1023, 0x1p970, false },
116     { 0x1.0000000000001p1023, -0x1p970, false },
117     { -0x1.0000000000001p1023, 0x1p970, false },
118     { -0x1.0000000000001p1023, -0x1p970, false },
119     { 0x1p-969, 0x1.1p-1023, true },
120     { 0x1p-969, -0x1.1p-1023, true },
121     { -0x1p-969, 0x1.1p-1023, true },
122     { -0x1p-969, -0x1.1p-1023, true },
123     { 0x1p-969, 0x1.1p-1022, false },
124     { 0x1p-969, -0x1.1p-1022, false },
125     { -0x1p-969, 0x1.1p-1022, false },
126     { -0x1p-969, -0x1.1p-1022, false },
127     { 0x1p-969, 0x1p-1022, true },
128     { 0x1p-969, -0x1p-1022, true },
129     { -0x1p-969, 0x1p-1022, true },
130     { -0x1p-969, -0x1p-1022, true },
131     { 0x1.0000000000001p-969, 0x1p-1022, false },
132     { 0x1.0000000000001p-969, -0x1p-1022, false },
133     { -0x1.0000000000001p-969, 0x1p-1022, false },
134     { -0x1.0000000000001p-969, -0x1p-1022, false },
135     { 0x1p-970, 0x1.1p-1024, true },
136     { 0x1p-970, -0x1.1p-1024, true },
137     { -0x1p-970, 0x1.1p-1024, true },
138     { -0x1p-970, -0x1.1p-1024, true },
139     { 0x1p-970, 0x1.1p-1023, false },
140     { 0x1p-970, -0x1.1p-1023, false },
141     { -0x1p-970, 0x1.1p-1023, false },
142     { -0x1p-970, -0x1.1p-1023, false },
143     { 0x1p-970, 0x1p-1023, true },
144     { 0x1p-970, -0x1p-1023, true },
145     { -0x1p-970, 0x1p-1023, true },
146     { -0x1p-970, -0x1p-1023, true },
147     { 0x1.0000000000001p-970, 0x1p-1023, false },
148     { 0x1.0000000000001p-970, -0x1p-1023, false },
149     { -0x1.0000000000001p-970, 0x1p-1023, false },
150     { -0x1.0000000000001p-970, -0x1p-1023, false },
151     { 0x1p-1000, 0x1.1p-1054, true },
152     { 0x1p-1000, -0x1.1p-1054, true },
153     { -0x1p-1000, 0x1.1p-1054, true },
154     { -0x1p-1000, -0x1.1p-1054, true },
155     { 0x1p-1000, 0x1.1p-1053, false },
156     { 0x1p-1000, -0x1.1p-1053, false },
157     { -0x1p-1000, 0x1.1p-1053, false },
158     { -0x1p-1000, -0x1.1p-1053, false },
159     { 0x1p-1000, 0x1p-1053, true },
160     { 0x1p-1000, -0x1p-1053, true },
161     { -0x1p-1000, 0x1p-1053, true },
162     { -0x1p-1000, -0x1p-1053, true },
163     { 0x1.0000000000001p-1000, 0x1p-1053, false },
164     { 0x1.0000000000001p-1000, -0x1p-1053, false },
165     { -0x1.0000000000001p-1000, 0x1p-1053, false },
166     { -0x1.0000000000001p-1000, -0x1p-1053, false },
167     { 0x1p-1021, 0x1p-1074, true },
168     { 0x1p-1021, -0x1p-1074, true },
169     { -0x1p-1021, 0x1p-1074, true },
170     { -0x1p-1021, -0x1p-1074, true },
171     { 0x1.0000000000001p-1021, 0x1p-1074, false },
172     { 0x1.0000000000001p-1021, -0x1p-1074, false },
173     { -0x1.0000000000001p-1021, 0x1p-1074, false },
174     { -0x1.0000000000001p-1021, -0x1p-1074, false },
175     { 0x1p-1022, 0x1p-1074, false },
176     { 0x1p-1022, -0x1p-1074, false },
177     { -0x1p-1022, 0x1p-1074, false },
178     { -0x1p-1022, -0x1p-1074, false },
179   };
180 
181 static int
do_test(void)182 do_test (void)
183 {
184   int result = 0;
185 
186   for (size_t i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
187     {
188       long double ld = ldbl_pack (tests[i].hi, tests[i].lo);
189       bool canonical = iscanonical (ld);
190       if (canonical == tests[i].canonical)
191 	{
192 	  printf ("PASS: iscanonical test %zu\n", i);
193 	  long double ldc = 12345.0L;
194 	  bool canonicalize_ret = canonicalizel (&ldc, &ld);
195 	  if (canonicalize_ret == !canonical)
196 	    {
197 	      printf ("PASS: canonicalizel test %zu\n", i);
198 	      bool canon_ok;
199 	      if (!canonical)
200 		canon_ok = ldc == 12345.0L;
201 	      else if (isnan (ld))
202 		canon_ok = isnan (ldc) && !issignaling (ldc);
203 	      else
204 		canon_ok = ldc == ld;
205 	      if (canon_ok)
206 		printf ("PASS: canonicalized value test %zu\n", i);
207 	      else
208 		{
209 		  printf ("FAIL: canonicalized value test %zu\n", i);
210 		  result = 1;
211 		}
212 	    }
213 	  else
214 	    {
215 	      printf ("FAIL: canonicalizel test %zu\n", i);
216 	      result = 1;
217 	    }
218 	}
219       else
220 	{
221 	  printf ("FAIL: iscanonical test %zu\n", i);
222 	  result = 1;
223 	}
224     }
225 
226   return result;
227 }
228 
229 #define TEST_FUNCTION do_test ()
230 #include "../test-skeleton.c"
231