Deleted Added
sdiff udiff text old ( 177763 ) new ( 251241 )
full compact
1/*-
2 * Copyright (c) 2007 David Schultz <das@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

24 * SUCH DAMAGE.
25 */
26
27/*
28 * Tests for csqrt{,f}()
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/tools/regression/lib/msun/test-csqrt.c 177763 2008-03-30 20:09:51Z das $");
33
34#include <assert.h>
35#include <complex.h>
36#include <float.h>
37#include <math.h>
38#include <stdio.h>
39
40#define N(i) (sizeof(i) / sizeof((i)[0]))
41
42/*
43 * This is a test hook that can point to csqrtl(), _csqrt(), or to _csqrtf().
44 * The latter two convert to float or double, respectively, and test csqrtf()
45 * and csqrt() with the same arguments.
46 */
47long double complex (*t_csqrt)(long double complex);

--- 10 unchanged lines hidden (view full) ---

58{
59
60 return (csqrt((double complex)d));
61}
62
63#pragma STDC CX_LIMITED_RANGE off
64
65/*
66 * XXX gcc implements complex multiplication incorrectly. In
67 * particular, it implements it as if the CX_LIMITED_RANGE pragma
68 * were ON. Consequently, we need this function to form numbers
69 * such as x + INFINITY * I, since gcc evalutes INFINITY * I as
70 * NaN + INFINITY * I.
71 */
72static inline long double complex
73cpackl(long double x, long double y)
74{
75 long double complex z;
76
77 __real__ z = x;
78 __imag__ z = y;
79 return (z);
80}
81
82/*
83 * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0.
84 * Fail an assertion if they differ.
85 */
86static void
87assert_equal(long double complex d1, long double complex d2)
88{
89
90 if (isnan(creall(d1))) {
91 assert(isnan(creall(d2)));
92 } else {
93 assert(creall(d1) == creall(d2));
94 assert(copysignl(1.0, creall(d1)) ==
95 copysignl(1.0, creall(d2)));
96 }
97 if (isnan(cimagl(d1))) {
98 assert(isnan(cimagl(d2)));
99 } else {
100 assert(cimagl(d1) == cimagl(d2));
101 assert(copysignl(1.0, cimagl(d1)) ==
102 copysignl(1.0, cimagl(d2)));
103 }
104}
105
106/*
107 * Test csqrt for some finite arguments where the answer is exact.
108 * (We do not test if it produces correctly rounded answers when the
109 * result is inexact, nor do we check whether it throws spurious
110 * exceptions.)
111 */

--- 44 unchanged lines hidden (view full) ---

156 int i, j;
157
158 for (i = 0; i < N(tests); i += 4) {
159 for (j = 0; j < N(mults); j++) {
160 a = tests[i] * mults[j] * mults[j];
161 b = tests[i + 1] * mults[j] * mults[j];
162 x = tests[i + 2] * mults[j];
163 y = tests[i + 3] * mults[j];
164 assert(t_csqrt(cpackl(a, b)) == cpackl(x, y));
165 }
166 }
167
168}
169
170/*
171 * Test the handling of +/- 0.
172 */
173static void
174test_zeros()
175{
176
177 assert_equal(t_csqrt(cpackl(0.0, 0.0)), cpackl(0.0, 0.0));
178 assert_equal(t_csqrt(cpackl(-0.0, 0.0)), cpackl(0.0, 0.0));
179 assert_equal(t_csqrt(cpackl(0.0, -0.0)), cpackl(0.0, -0.0));
180 assert_equal(t_csqrt(cpackl(-0.0, -0.0)), cpackl(0.0, -0.0));
181}
182
183/*
184 * Test the handling of infinities when the other argument is not NaN.
185 */
186static void
187test_infinities()
188{

--- 5 unchanged lines hidden (view full) ---

194 INFINITY,
195 -INFINITY,
196 };
197
198 int i;
199
200 for (i = 0; i < N(vals); i++) {
201 if (isfinite(vals[i])) {
202 assert_equal(t_csqrt(cpackl(-INFINITY, vals[i])),
203 cpackl(0.0, copysignl(INFINITY, vals[i])));
204 assert_equal(t_csqrt(cpackl(INFINITY, vals[i])),
205 cpackl(INFINITY, copysignl(0.0, vals[i])));
206 }
207 assert_equal(t_csqrt(cpackl(vals[i], INFINITY)),
208 cpackl(INFINITY, INFINITY));
209 assert_equal(t_csqrt(cpackl(vals[i], -INFINITY)),
210 cpackl(INFINITY, -INFINITY));
211 }
212}
213
214/*
215 * Test the handling of NaNs.
216 */
217static void
218test_nans()
219{
220
221 assert(creall(t_csqrt(cpackl(INFINITY, NAN))) == INFINITY);
222 assert(isnan(cimagl(t_csqrt(cpackl(INFINITY, NAN)))));
223
224 assert(isnan(creall(t_csqrt(cpackl(-INFINITY, NAN)))));
225 assert(isinf(cimagl(t_csqrt(cpackl(-INFINITY, NAN)))));
226
227 assert_equal(t_csqrt(cpackl(NAN, INFINITY)),
228 cpackl(INFINITY, INFINITY));
229 assert_equal(t_csqrt(cpackl(NAN, -INFINITY)),
230 cpackl(INFINITY, -INFINITY));
231
232 assert_equal(t_csqrt(cpackl(0.0, NAN)), cpackl(NAN, NAN));
233 assert_equal(t_csqrt(cpackl(-0.0, NAN)), cpackl(NAN, NAN));
234 assert_equal(t_csqrt(cpackl(42.0, NAN)), cpackl(NAN, NAN));
235 assert_equal(t_csqrt(cpackl(-42.0, NAN)), cpackl(NAN, NAN));
236 assert_equal(t_csqrt(cpackl(NAN, 0.0)), cpackl(NAN, NAN));
237 assert_equal(t_csqrt(cpackl(NAN, -0.0)), cpackl(NAN, NAN));
238 assert_equal(t_csqrt(cpackl(NAN, 42.0)), cpackl(NAN, NAN));
239 assert_equal(t_csqrt(cpackl(NAN, -42.0)), cpackl(NAN, NAN));
240 assert_equal(t_csqrt(cpackl(NAN, NAN)), cpackl(NAN, NAN));
241}
242
243/*
244 * Test whether csqrt(a + bi) works for inputs that are large enough to
245 * cause overflow in hypot(a, b) + a. In this case we are using
246 * csqrt(115 + 252*I) == 14 + 9*I
247 * scaled up to near MAX_EXP.
248 */
249static void
250test_overflow(int maxexp)
251{
252 long double a, b;
253 long double complex result;
254
255 a = ldexpl(115 * 0x1p-8, maxexp);
256 b = ldexpl(252 * 0x1p-8, maxexp);
257 result = t_csqrt(cpackl(a, b));
258 assert(creall(result) == ldexpl(14 * 0x1p-4, maxexp / 2));
259 assert(cimagl(result) == ldexpl(9 * 0x1p-4, maxexp / 2));
260}
261
262int
263main(int argc, char *argv[])
264{
265

--- 58 unchanged lines hidden ---